Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Feb 6, 2025
1 parent 64069e2 commit 4221a58
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ class PreproArea(FolderNode):
@override
def build(self) -> TREE:
children: TREE = {
"conversion": InputSeriesMatrix(self.context, self.config.next_file("conversion.txt"), default_conversion),
"data": InputSeriesMatrix(self.context, self.config.next_file("data.txt"), default_data),
"k": InputSeriesMatrix(self.context, self.config.next_file("k.txt"), default_k),
"conversion": InputSeriesMatrix(
self.context, self.config.next_file("conversion.txt"), default_empty=default_conversion
),
"data": InputSeriesMatrix(self.context, self.config.next_file("data.txt"), default_empty=default_data),
"k": InputSeriesMatrix(self.context, self.config.next_file("k.txt"), default_empty=default_k),
"translation": InputSeriesMatrix(
self.context, self.config.next_file("translation.txt"), default_scenario_hourly
self.context, self.config.next_file("translation.txt"), default_empty=default_scenario_hourly
),
"settings": PreproAreaSettings(self.context, self.config.next_file("settings.ini")),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ def __init__(
self,
context: ContextServer,
config: FileStudyTreeConfig,
default_empty: npt.NDArray[np.float64],
freq: MatrixFrequency = MatrixFrequency.HOURLY,
nb_columns: t.Optional[int] = None,
default_empty: t.Optional[npt.NDArray[np.float64]] = None,
):
super().__init__(context=context, config=config, freq=freq)
self.nb_columns = nb_columns
self.default_empty = np.copy(default_empty)
self.default_empty.flags.writeable = True
if default_empty is None:
self.default_empty = None
else:
# Clone the template value and make it writable
self.default_empty = np.copy(default_empty)
self.default_empty.flags.writeable = True

def parse_as_dataframe(self, file_path: t.Optional[Path] = None) -> pd.DataFrame:
file_path = file_path or self.config.path
Expand Down Expand Up @@ -81,7 +85,10 @@ def parse_as_dataframe(self, file_path: t.Optional[Path] = None) -> pd.DataFrame
return final_matrix
except EmptyDataError:
logger.warning(f"Empty file found when parsing {file_path}")
return pd.DataFrame(self.default_empty)
final_matrix = pd.DataFrame()
if self.default_empty is not None:
final_matrix = pd.DataFrame(self.default_empty)
return final_matrix

@override
def parse_as_json(self, file_path: t.Optional[Path] = None) -> JSON:
Expand Down Expand Up @@ -145,5 +152,5 @@ def get_file_content(self) -> OriginalFile:
return OriginalFile(content=content, suffix=suffix, filename=filename)

@override
def get_default_empty_matrix(self) -> npt.NDArray[np.float64]:
def get_default_empty_matrix(self) -> t.Optional[npt.NDArray[np.float64]]:
return self.default_empty
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
InputHydroPreproAreaPrepro,
)

default_energy = np.zeros((5, 12), dtype=np.float64)
default_energy = np.zeros((12, 5), dtype=np.float64)
default_energy.flags.writeable = False


class InputHydroPreproArea(FolderNode):
@override
def build(self) -> TREE:
children: TREE = {
"energy": InputSeriesMatrix(self.context, self.config.next_file("energy.txt"), default_energy),
"energy": InputSeriesMatrix(
self.context, self.config.next_file("energy.txt"), default_empty=default_energy
),
"prepro": InputHydroPreproAreaPrepro(self.context, self.config.next_file("prepro.ini")),
}
return children
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@ def test_download_matrices(self, client: TestClient, user_access_token: str, int
assert list(dataframe.index) == list(dataframe.columns) == ["de", "es", "fr", "it"]
assert all(np.isclose(dataframe.iloc[i, i], 1.0) for i in range(len(dataframe)))

# test for empty matrix
# checks default value for an empty water_values matrix
res = client.get(
f"/v1/studies/{internal_study_id}/raw/download",
params={"path": "input/hydro/common/capacity/waterValues_de", "format": "tsv"},
)
assert res.status_code == 200
content = io.BytesIO(res.content)
dataframe = pd.read_csv(content, index_col=0, sep="\t")
assert dataframe.empty
assert dataframe.to_numpy().tolist() == 365 * [101 * [0.0]]

# modulation matrix
res = client.get(
Expand Down Expand Up @@ -328,15 +328,15 @@ def test_download_matrices(self, client: TestClient, user_access_token: str, int
"('HURDLE COST', 'Euro', '')",
]

# test energy matrix to test the regex
# checks default value for an empty energy matrix
res = client.get(
f"/v1/studies/{internal_study_id}/raw/download",
params={"path": "input/hydro/prepro/de/energy", "format": "tsv"},
)
assert res.status_code == 200
content = io.BytesIO(res.content)
dataframe = pd.read_csv(content, index_col=0, sep="\t")
assert dataframe.empty
assert dataframe.to_numpy().tolist() == 12 * [5 * [0.0]]

# test the Min Gen of the 8.6 study
for export_format in ["tsv", "xlsx"]:
Expand Down

0 comments on commit 4221a58

Please sign in to comment.