Skip to content

Commit

Permalink
implement suggestions
Browse files Browse the repository at this point in the history
- remove double filter from costs.cumulate
- remove transpose argument from costs.to_csv_lists
- make test_mode_report more readable
  • Loading branch information
stefan.schirmeister committed Dec 11, 2024
1 parent c61d0f7 commit 163264b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
13 changes: 2 additions & 11 deletions simba/costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def cumulate(self):
# Vehicle costs cannot be cumulated since they might be double counted for vehicles
# with multiple depots. Instead, the vehicle costs were previously calculated in
# set_vehicle_costs_per_gc()
if key in ["c_vehicles", "c_vehicles_annual", "station_type"]:
if key in ["c_vehicles", "c_vehicles_annual"]:
continue
self.costs_per_gc[self.CUMULATED][key] = 0
for gc in self.costs_per_gc.keys() - [self.CUMULATED]:
Expand Down Expand Up @@ -696,11 +696,9 @@ def get_total_annual_km(self):
1)
return self.DAYS_PER_YEAR / simulated_days * total_km

def to_csv_lists(self, transpose=False):
def to_csv_lists(self):
""" Convert costs to a list of lists easily convertible to a CSV.
:param transpose: swap rows and columns
:type transpose: bool
:return: List of lists of parameters, units and costs per gc
:rtype: list
"""
Expand All @@ -727,11 +725,4 @@ def to_csv_lists(self, transpose=False):
except TypeError:
row.append(num)
output.append(row)

if transpose:
transposed_output = []
for column, _ in enumerate(output[0]):
transposed_output.append([row[column] for row in output])
output = transposed_output

return output
23 changes: 14 additions & 9 deletions tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,23 @@ def test_mode_report(self, tmp_path):
with (tmp_path / "report_1/summary_vehicles_costs.csv").open() as csvfile:
reader = DictReader(csvfile)
# save procurement costs and annual energy for each Station
station_data = {s: [None, None] for s in reader.fieldnames if s.startswith("Station")}
station_data = {s: {"energy_costs": None, "energy_amount": None}
for s in reader.fieldnames if s.startswith("Station")}
for row in reader:
for i, param in enumerate(["c_el_procurement_annual", "annual_kWh_from_grid"]):
if row["parameter"] == param:
for k, v in station_data.items():
v[i] = float(row[k])
if row["parameter"] == "c_el_procurement_annual":
for name in station_data:
station_data[name]["energy_costs"] = float(row[name])
if row["parameter"] == "annual_kWh_from_grid":
for name in station_data:
station_data[name]["energy_amount"] = float(row[name])
# check quotient: no energy must mean no cost, otherwise procurement price
for k, v in station_data.items():
if v[1] == 0:
assert v[0] == 0, f"{k} has costs without energy"
for name in station_data:
energy = station_data[name]["energy_amount"]
cost = station_data[name]["energy_costs"]
if energy == 0:
assert cost == 0, f"{name} has costs without energy"
else:
assert pytest.approx(v[0]/v[1]) == procurement_price, f"{k}: procurement price"
assert pytest.approx(cost/energy) == procurement_price, f"{name}: procurement price"

def test_empty_report(self, tmp_path):
# report with no rotations
Expand Down

0 comments on commit 163264b

Please sign in to comment.