Commit f6f040bb authored by Eric Duminil's avatar Eric Duminil
Browse files

Better tests for load_profile

parent d7ec71b7
......@@ -21,18 +21,37 @@ class LoadProfileResults(SimStadtResults):
def _parse_results(self) -> pd.DataFrame:
csv_decimal = detect_decimal(self.csv_path, 6, "Area")
header = pd.read_csv(
self.csv_path, nrows=9, sep=";", header=None, index_col=0, decimal=csv_decimal
)
header.index = header.index.str.lstrip("# ")
def _header_sum(label: str) -> float:
return pd.to_numeric(header.loc[label]).fillna(0).sum()
df = pd.read_csv(self.csv_path, skiprows=range(1, 12), sep=";", decimal=csv_decimal)
# First column is the timestamp; remaining columns are per-building loads
return df.drop(columns=[df.columns[0]])
df = df.drop(columns=[df.columns[0]])
df.attrs["number_of_households"] = int(_header_sum("Number of Households"))
df.attrs["number_of_people"] = int(_header_sum("Number of Occupants"))
df.attrs["total_heated_area"] = float(_header_sum("Heated Area"))
return df
@property
def kpis(self) -> list[KPI]:
# TODO: Add sum, average. Add timestep too?
# TODO: Add people? Add total heated area?
df = self.dataframe
total = df.sum().sum()
return [
total_heated_area = df.attrs["total_heated_area"]
result = [
KPI("Number of buildings", df.shape[1], precision=0),
KPI("Number of households", df.attrs["number_of_households"], precision=0),
KPI("Number of inhabitants", df.attrs["number_of_people"], precision=0),
KPI("Total heated area", total_heated_area, "m²", precision=0),
KPI("Total load", total, "kWh / a", precision=1),
KPI("Average load", total / 8760, "kWh / h", precision=1),
]
if total_heated_area > 0:
result.append(KPI("Specific load", total / total_heated_area, "kWh / (m² · a)", precision=1))
return result
......@@ -485,10 +485,18 @@ def test_loadprofile_kpis(loadprofile_results):
kpis = kpis_by_name(loadprofile_results)
assert kpis["Number of buildings"].value == 3
assert kpis["Number of buildings"].unit is None
assert kpis["Number of households"].value == 8
assert kpis["Number of households"].unit is None
assert kpis["Number of inhabitants"].value == 13
assert kpis["Number of inhabitants"].unit is None
assert kpis["Total heated area"].value == pytest.approx(1198.1, abs=0.1)
assert kpis["Total heated area"].unit == "m²"
assert kpis["Total load"].value == pytest.approx(17004.0, abs=1.0)
assert kpis["Total load"].unit == "kWh / a"
assert kpis["Average load"].value == pytest.approx(1.9, abs=0.1)
assert kpis["Average load"].unit == "kWh / h"
assert kpis["Specific load"].value == pytest.approx(14.2, abs=0.2)
assert kpis["Specific load"].unit == "kWh / (m² · a)"
def test_loadprofile_no_diagrams(loadprofile_results):
......@@ -514,7 +522,7 @@ def test_loadprofile_serialization(loadprofile_results):
assert isinstance(reconstructed, LoadProfileResults)
assert reconstructed.description == loadprofile_results.description
assert len(reconstructed.output_files) == len(loadprofile_results.output_files)
assert len(reconstructed.kpis) == 3
assert len(reconstructed.kpis) == 7
pd.testing.assert_frame_equal(
reconstructed.dataframe, loadprofile_results.dataframe
)
......
......@@ -42,7 +42,7 @@ def test_citygml_with_explicit_project_path(mock_simstadt, tmp_path):
assert results.workflow_path.parent == proj
assert len(results.dataframe) > 0
assert len(results.kpis) == 3
assert len(results.kpis) == 7
assert results.kpi("Average load").unit == "kWh / h"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment