Skip to content

Commit

Permalink
Add record of single element attribute lists in IO
Browse files Browse the repository at this point in the history
  • Loading branch information
brynpickering committed Jun 30, 2024
1 parent 3a63bc1 commit 5281200
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### User-facing changes

|fixed| Single element lists/sets in the model Dataset attribute dictionary are restored to lists/sets on loading from NetCDF (#614).

|new| Decision variables and global expressions can have a `title` defined, which will be available in the model results as attributes of those components and can be used for e.g. visualisation (#582).
Parameter titles from the model definition schema will also propagate to the model inputs.

Expand Down
26 changes: 20 additions & 6 deletions src/calliope/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from calliope import exceptions
from calliope.attrdict import AttrDict
from calliope.util.tools import listify

CONFIG_DIR = importlib.resources.files("calliope") / "config"

Expand All @@ -39,26 +40,33 @@ def read_netcdf(path):


def _pop_serialised_list(
attrs: dict, serialised_items: Union[str, list, np.ndarray]
attrs: dict, serialised_items: Union[str, list]
) -> Union[list, np.ndarray]:
"""Pop a list of serialised attributes from the attribute dictionary."""
serialised_ = attrs.pop(serialised_items, [])
if not isinstance(serialised_, (list, np.ndarray)):
return [serialised_]
else:
return serialised_
return listify(serialised_)


def _serialise(attrs: dict) -> None:
"""Convert troublesome datatypes to nicer ones in xarray attribute dictionaries.
This will tackle dictionaries (to string), booleans (to int), None (to string), and sets (to list).
We also make note of any single element lists as they will be stored as simple strings in netcdf format,
but we want them to be returned as lists on loading the data from file.
Args:
attrs (dict):
Attribute dictionary from an xarray Dataset/DataArray.
Changes will be made in-place, so be sure to supply a copy of your dictionary if you want access to its original state.
"""
# Convert dicts attrs to yaml strings
# Keep track of single element lists, to listify them again when loading from file.

attrs["serialised_single_element_list"] = [
k for k, v in attrs.items() if isinstance(v, list) and len(v) == 1
]

# Convert dict attrs to yaml strings
dict_attrs = [k for k, v in attrs.items() if isinstance(v, dict)]
attrs["serialised_dicts"] = dict_attrs
for attr in dict_attrs:
Expand Down Expand Up @@ -89,6 +97,10 @@ def _serialise(attrs: dict) -> None:
)
else:
attrs["serialised_sets"] = set_attrs
# Also keep track of single element sets
attrs["serialised_single_element_list"].extend(
[k for k in set_attrs if len(attrs[k]) == 1]
)


def _deserialise(attrs: dict) -> None:
Expand All @@ -107,6 +119,8 @@ def _deserialise(attrs: dict) -> None:
attrs[attr] = bool(attrs[attr])
for attr in _pop_serialised_list(attrs, "serialised_nones"):
attrs[attr] = None
for attr in _pop_serialised_list(attrs, "serialised_single_element_list"):
attrs[attr] = listify(attrs[attr])
for attr in _pop_serialised_list(attrs, "serialised_sets"):
attrs[attr] = set(attrs[attr])

Expand Down
9 changes: 8 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def model(self, vars_to_add_attrs):
"foo_dict": {"foo": {"a": 1}},
"foo_attrdict": calliope.AttrDict({"foo": {"a": 1}}),
"foo_set": set(["foo", "bar"]),
"foo_set_1_item": set(["foo"]),
"foo_list": ["foo", "bar"],
"foo_list_1_item": ["foo"],
}
model._model_data = model._model_data.assign_attrs(**attrs)
model.build()
Expand Down Expand Up @@ -67,6 +70,9 @@ def test_save_netcdf(self, model_file):
("foo_dict", dict, {"foo": {"a": 1}}),
("foo_attrdict", calliope.AttrDict, calliope.AttrDict({"foo": {"a": 1}})),
("foo_set", set, set(["foo", "bar"])),
("foo_set_1_item", set, set(["foo"])),
("foo_list", list, ["foo", "bar"]),
("foo_list_1_item", list, ["foo"]),
],
)
@pytest.mark.parametrize("model_name", ["model", "model_from_file"])
Expand Down Expand Up @@ -100,7 +106,8 @@ def test_serialised_list_popped(self, request, serialised_list, model_name):
"serialised_dicts",
["foo_dict", "foo_attrdict", "defaults", "config", "math"],
),
("serialised_sets", ["foo_set"]),
("serialised_sets", ["foo_set", "foo_set_1_item"]),
("serialised_single_element_list", ["foo_list_1_item", "foo_set_1_item"]),
],
)
def test_serialisation_lists(
Expand Down

0 comments on commit 5281200

Please sign in to comment.