Skip to content

Commit

Permalink
Refactor parsers to depend on a uniform ABC
Browse files Browse the repository at this point in the history
  • Loading branch information
brynpickering committed Oct 26, 2023
1 parent fd57332 commit bec7bb8
Show file tree
Hide file tree
Showing 14 changed files with 731 additions and 600 deletions.
20 changes: 14 additions & 6 deletions src/calliope/backend/backend_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def __init__(self, inputs: xr.Dataset, **config_overrides):
self.inputs.attrs["config"].build.union(
AttrDict(config_overrides), allow_override=True
)
self.valid_math_element_names: set = set()
self._solve_logger = logging.getLogger(__name__ + ".<solve>")

@abstractmethod
Expand Down Expand Up @@ -197,10 +196,6 @@ def _build(self) -> None:
"objectives",
]:
component = components.removesuffix("s")
if components in ["variables", "global_expressions"]:
self.valid_math_element_names.update(
self.inputs.math[components].keys()
)
for name in self.inputs.math[components]:
getattr(self, f"add_{component}")(name)
LOGGER.info(
Expand Down Expand Up @@ -284,7 +279,7 @@ def _add_component(

self._create_obj_list(name, component_type)

equations = parsed_component.parse_equations(self.valid_math_element_names)
equations = parsed_component.parse_equations(self.valid_component_names)
if not equations:
component_da = component_setter(
parsed_component.drop_dims_not_in_foreach(top_level_where)
Expand Down Expand Up @@ -517,6 +512,19 @@ def objectives(self):
"Slice of backend dataset to show only built objectives"
return self._dataset.filter_by_attrs(obj_type="objectives")

@property
def valid_component_names(self):
def _filter(val):
return val in ["variables", "parameters", "global_expressions"]

in_data = set(self._dataset.filter_by_attrs(obj_type=_filter).data_vars.keys())
in_math = set(
name
for component in ["variables", "global_expressions"]
for name in self.inputs.math[component].keys()
)
return in_data.union(in_math)


class BackendModel(BackendModelGenerator, Generic[T]):
def __init__(self, inputs: xr.Dataset, instance: T, **config_overrides) -> None:
Expand Down
Loading

0 comments on commit bec7bb8

Please sign in to comment.