Skip to content

Commit

Permalink
Remove redundant AttrDict call in AttrDict.union(). Fill CHANGELOG.
Browse files Browse the repository at this point in the history
  • Loading branch information
irm-codebase committed Nov 29, 2024
1 parent 2a1330d commit 5526a65
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### User-facing changes

|changed| `template:` can now be used anywhere within YAML definition files, not just in the `nodes`, `techs` and `data_tables` sections.

|changed| Single data entries defined in YAML indexed parameters will not be automatically broadcast along indexed dimensions.
To achieve the same functionality as in `<v0.7.dev4`, the user must set the new `init` configuration option `broadcast_param_data` to True (#615).

Expand All @@ -24,6 +26,10 @@ This change has occurred to avoid confusion between data "sources" and model ene

### Internal changes

|changed| Model definition reading is now defined in a single place (preprocess/model_definition.py).

|changed| Moved YAML reading/importing functionality out of `AttrDict`. It is now part of our `io` functionality.

|fixed| Avoided gurobi 12.0 incompatibility with pyomo by setting the lower bound to v6.8.2.

## 0.7.0.dev4 (2024-09-10)
Expand Down
2 changes: 1 addition & 1 deletion src/calliope/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _init_from_model_definition(
(model_def_full, applied_overrides) = preprocess.prepare_model_definition(
model_definition, scenario, override_dict
)
model_def_full.union(AttrDict({"config.init": kwargs}), allow_override=True)
model_def_full.union({"config.init": kwargs}, allow_override=True)
# First pass to check top-level keys are all good
validate_dict(model_def_full, CONFIG_SCHEMA, "Model definition")

Expand Down
18 changes: 8 additions & 10 deletions src/calliope/preprocess/data_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def tech_dict(self) -> tuple[AttrDict, AttrDict]:
for param in self.PARAMS_TO_INITIALISE_YAML:
if param in self.dataset:
base_tech_dict = self.dataset[param].to_dataframe().dropna().T.to_dict()
base_tech_data.union(AttrDict(base_tech_dict))
base_tech_data.union(base_tech_dict)

return tech_dict, base_tech_data

Expand Down Expand Up @@ -229,15 +229,13 @@ def __extract_data(grouped_series):
)
else:
lookup_dict.union(
AttrDict(
self.dataset[param]
.to_series()
.reset_index(lookup_dim)
.groupby("techs")
.apply(__extract_data)
.dropna()
.to_dict()
)
self.dataset[param]
.to_series()
.reset_index(lookup_dim)
.groupby("techs")
.apply(__extract_data)
.dropna()
.to_dict()
)

return lookup_dict
Expand Down
10 changes: 4 additions & 6 deletions src/calliope/preprocess/model_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,10 @@ def _links_to_node_format(self, active_node_dict: AttrDict) -> AttrDict:
self._update_one_way_links(node_from_data, node_to_data)

link_tech_dict.union(
AttrDict(
{
node_from: {link_name: node_from_data},
node_to: {link_name: node_to_data},
}
)
{
node_from: {link_name: node_from_data},
node_to: {link_name: node_to_data},
}
)

return link_tech_dict
Expand Down
10 changes: 5 additions & 5 deletions src/calliope/preprocess/model_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ def prepare_model_definition(
scenario: str | None = None,
override_dict: dict | None = None,
) -> tuple[AttrDict, str]:
"""Arrenge model definition data folloging our standardised order of priority.
"""Arrange model definition data folloging our standardised order of priority.
Should always be called when defining calliope models from configuration files.
The order of priority is:
- scenarios/overrides > templates > regular data sections
- override_dict > scenarios > data section > template
Args:
data (str | Path | dict): _description_
scenario (str | None, optional): _description_. Defaults to None.
override_dict (dict | None, optional): _description_. Defaults to None.
data (str | Path | dict): model data file or dictionary.
scenario (str | None, optional): scenario to run. Defaults to None.
override_dict (dict | None, optional): additional overrides. Defaults to None.
Returns:
tuple[AttrDict, str]: _description_
Expand Down
4 changes: 2 additions & 2 deletions src/calliope/util/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def update_then_validate_config(
) -> AttrDict:
"""Return an updated version of the configuration schema."""
to_validate = deepcopy(config_dict[config_key])
to_validate.union(AttrDict(update_kwargs), allow_override=True)
to_validate.union(update_kwargs, allow_override=True)
validate_dict(
{"config": {config_key: to_validate}},
CONFIG_SCHEMA,
Expand Down Expand Up @@ -70,7 +70,7 @@ def update_model_schema(
"^[^_^\\d][\\w]*$"
]["properties"]

to_update.union(AttrDict(new_entries), allow_override=allow_override)
to_update.union(new_entries, allow_override=allow_override)

validator = jsonschema.Draft202012Validator
validator.META_SCHEMA["unevaluatedProperties"] = False
Expand Down
4 changes: 2 additions & 2 deletions tests/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def build_lp(
if isinstance(math_data, dict):
for component_group, component_math in math_data.items():
if isinstance(component_math, dict):
math_to_add.union(calliope.AttrDict({component_group: component_math}))
math_to_add.union({component_group: component_math})
elif isinstance(component_math, list):
for name in component_math:
math_to_add.set_key(
Expand All @@ -113,7 +113,7 @@ def build_lp(
obj = {
"dummy_obj": {"equations": [{"expression": "1 + 1"}], "sense": "minimize"}
}
math_to_add.union(calliope.AttrDict({"objectives": obj}))
math_to_add.union({"objectives": obj})
obj_to_activate = "dummy_obj"
else:
obj_to_activate = list(math_to_add["objectives"].keys())[0]
Expand Down

0 comments on commit 5526a65

Please sign in to comment.