Skip to content

Commit

Permalink
Automatically propagate parameter unit metadata except to scales (#178)
Browse files Browse the repository at this point in the history
Fixes #175
  • Loading branch information
nikhilwoodruff authored Apr 12, 2024
1 parent 4bdd444 commit b827cfa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
6 changes: 3 additions & 3 deletions policyengine_core/charts/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def format_fig(fig: go.Figure) -> go.Figure:
source="https://raw.githubusercontent.com/PolicyEngine/policyengine-app/master/src/images/logos/policyengine/blue.png",
xref="paper",
yref="paper",
x=1,
x=1.1,
y=-0.15,
sizex=0.2,
sizey=0.2,
sizex=0.15,
sizey=0.15,
xanchor="right",
yanchor="bottom",
)
Expand Down
2 changes: 1 addition & 1 deletion policyengine_core/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def save_dataset(self, data, file_path: str = None) -> None:
for table_name, dataframe in data.items():
self.save(table_name, dataframe)
elif self.data_format == Dataset.TIME_PERIOD_ARRAYS:
with h5py.File(file, "a" if file.exists() else "w") as f:
with h5py.File(file, "w") as f:
for variable, values in data.items():
for time_period, value in values.items():
key = f"{variable}/{time_period}"
Expand Down
9 changes: 9 additions & 0 deletions policyengine_core/parameters/parameter_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def __init__(self, name: str, data: dict, file_path: str):
)
brackets.append(bracket)
self.brackets: typing.List[parameters.ParameterScaleBracket] = brackets
self.propagate_uprating()
self.propagate_units()

def __getitem__(self, key: str) -> Any:
if isinstance(key, int) and key < len(self.brackets):
Expand Down Expand Up @@ -87,6 +89,13 @@ def propagate_units(self) -> None:
self.metadata[unit_key]
)

def propagate_uprating(self) -> None:
for bracket in self.brackets:
bracket.propagate_uprating(
self.metadata.get("uprating"),
threshold=self.metadata.get("uprate_thresholds", False),
)

def get_descendants(self) -> Iterable:
for bracket in self.brackets:
yield bracket
Expand Down
11 changes: 11 additions & 0 deletions policyengine_core/parameters/parameter_scale_bracket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ def get_descendants(self) -> Iterable[Parameter]:
for key in self._allowed_keys:
if key in self.children:
yield self.children[key]

def propagate_uprating(
self, uprating: str, threshold: bool = False
) -> None:
for key in self._allowed_keys:
if key in self.children:
if key == "threshold" and not threshold:
continue
self.children[key].metadata["uprating"] = (
uprating or self.children[key].metadata.get("uprating")
)

0 comments on commit b827cfa

Please sign in to comment.