Skip to content

Commit

Permalink
[ENG-2082] fix bugs when dumping model to json and reloading into mod…
Browse files Browse the repository at this point in the history
…el (#151)

* fix bugs when dumping model to json and reloading into model

* Simplify and tidy
  • Loading branch information
edwardxtg authored Oct 23, 2024
1 parent cdb55d6 commit 62446be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
6 changes: 3 additions & 3 deletions tz/osemosys/schemas/compat/time_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _to_dataframe(self, stem: str) -> pd.DataFrame:
"VALUE": self.days_in_day_type[daytype],
}
for season, daytype, year in product(
self.seasons, list(self.days_in_day_type.keys()), self.years
self.seasons, self.days_in_day_type, self.years
)
]
)
Expand All @@ -237,7 +237,7 @@ def _to_dataframe(self, stem: str) -> pd.DataFrame:
return pd.DataFrame.from_records(
[
{"TIMESLICE": ts, "YEAR": year, "VALUE": self.year_split[ts]}
for ts, year in product(list(self.year_split.keys()), self.years)
for ts, year in product(self.year_split, self.years)
]
)
elif stem == "DaySplit":
Expand All @@ -249,7 +249,7 @@ def _to_dataframe(self, stem: str) -> pd.DataFrame:
"YEAR": year,
"VALUE": self.day_split[dtb],
}
for dtb, year in product(self.daily_time_brackets, self.years)
for dtb, year in product(self.day_split, self.years)
]
)
if self.daily_time_brackets is not None
Expand Down
57 changes: 31 additions & 26 deletions tz/osemosys/schemas/validation/timedefinition_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _maybe_check_keys_or_equal(
if split is not None:
if part_list is not None:
# validate keys match
if {str(item) for item in set(split.keys())} != set(part_list):
if {str(item) for item in set(split.keys())} != {str(item) for item in part_list}:
raise ValueError(f"provided '{name}_split' keys do not match '{name}'")
return split
else:
Expand Down Expand Up @@ -158,43 +158,48 @@ def validate_adjacency_keys(
if {int(yr) for yr in list(adj["years"].keys()) + list(adj["years"].values())} != set(years):
raise ValueError("provided 'years' do not match keys or values of 'adj.years'")
if seasons is not None and "seasons" in adj.keys():
if len(seasons) > 1:
if set(list(adj["seasons"].keys()) + list(adj["seasons"].values())) != set(seasons):
raise ValueError("provided 'seasons' do not match keys or values of 'adj.seasons'")
else:
if adj["seasons"] != {}:
if adj["seasons"] != {}:
if len(seasons) > 1:
if set(list(adj["seasons"].keys()) + list(adj["seasons"].values())) != set(seasons):
raise ValueError(
"provided 'seasons' do not match keys or values of 'adj.seasons'"
)
else:
raise ValueError(
f"Adjacency provided for seasons, but only one season {seasons} is defined."
)
elif seasons is not None or adj.get("seasons"):
raise ValueError("seasons provided without adjacency.")
if day_types is not None and "day_types" in adj.keys():
if len(day_types) > 1:
if set(list(adj["day_types"].keys()) + list(adj["day_types"].values())) != set(
day_types
):
raise ValueError(
"provided 'day_types' do not match keys or values of 'adj.day_types'"
)
else:
if adj["day_types"] != {}:
if adj["day_types"] != {}:
if len(day_types) > 1:
if set(list(adj["day_types"].keys()) + list(adj["day_types"].values())) != set(
day_types
):
raise ValueError(
"provided 'day_types' do not match keys or values of 'adj.day_types'"
)
else:
raise ValueError(
f"Adjacency provided for day_types, but only one day_type {day_types} is defined." # NOQA E501
f"Adjacency provided for day_types, but only one day_type {day_types} "
f"is defined."
)
elif day_types is not None or adj.get("day_types"):
raise ValueError("day_types provided without adjacency")
if time_brackets is not None and "time_brackets" in adj.keys():
if len(time_brackets) > 1:
if set(list(adj["time_brackets"].keys()) + list(adj["time_brackets"].values())) != set(
time_brackets
):
raise ValueError(
"provided 'time_brackets' do not match keys or values of 'adj.time_brackets'"
)
else:
if adj["time_brackets"] != {}:
if adj["time_brackets"] != {}:
if len(time_brackets) > 1:
if set(
list(adj["time_brackets"].keys()) + list(adj["time_brackets"].values())
) != set(time_brackets):
raise ValueError(
"provided 'time_brackets' do not match keys or values of "
"'adj.time_brackets'"
)
else:
raise ValueError(
f"Adjacency provided for time_brackets, but only one time_bracket {time_brackets} is defined." # NOQA E501
f"Adjacency provided for time_brackets, but only one time_bracket "
f"{time_brackets} is defined."
)
elif time_brackets is not None or adj.get("time_brackets"):
raise ValueError("time_brackets provided without adjacency")
Expand Down

0 comments on commit 62446be

Please sign in to comment.