Skip to content

Commit

Permalink
Make type of eWaterCycleModel.parameters property an ItemsView
Browse files Browse the repository at this point in the history
To be more inline with pymt

See #365 (comment)
  • Loading branch information
sverhoeven committed Sep 18, 2023
1 parent 38e33bc commit 8c44672
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Formatted as described on [https://keepachangelog.com](https://keepachangelog.co
- Functions of a model inside a container that return the same result each call are cached with [MemoizedBmi](https://grpc4bmi.readthedocs.io/en/latest/api/grpc4bmi.bmi_memoized.html#grpc4bmi.bmi_memoized.MemoizedBmi) ([#339](https://github.com/eWaterCycle/ewatercycle/pull/339))
- Moved CaseConfig to src/utils.py
- forcing.load_foreign has been superceded by using sources.model(...)
- eWaterCycleModel.parameters property type is ItemsView instead of dict.

### Deprecated

Expand Down
11 changes: 6 additions & 5 deletions src/ewatercycle/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from datetime import timezone
from pathlib import Path
from typing import Annotated, Any, Iterable, Optional, Type, cast
from typing import Annotated, Any, ItemsView, Iterable, Optional, Type, cast

import bmipy
import numpy as np
Expand Down Expand Up @@ -83,9 +83,9 @@ def _make_bmi_instance(self) -> OptionalDestBmi:
"""Attach a BMI instance to self._bmi."""

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
"""Display the model's parameters and their values."""
return {}
return {}.items()

def setup(self, *, cfg_dir: str | None = None, **kwargs) -> tuple[str, str]:
"""Perform model setup.
Expand Down Expand Up @@ -131,9 +131,10 @@ def _make_cfg_dir(self, cfg_dir: Optional[str] = None, **kwargs) -> Path:
def _make_cfg_file(self, **kwargs):
"""Create new config file and return its path."""
cfg_file = self._cfg_dir / "config.yaml"
self.parameters.update(**kwargs)
myparameters = dict(list(self.parameters))
myparameters.update(**kwargs)
with cfg_file.open(mode="w") as file:
yaml.dump({k: v for k, v in self.parameters}, file)
yaml.dump({k: v for k, v in self.myparameters}, file)

return cfg_file

Expand Down
6 changes: 3 additions & 3 deletions src/ewatercycle/plugins/hype/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import types
from pathlib import Path
from typing import Any, Iterable, Optional
from typing import Any, ItemsView, Iterable, Optional

import bmipy
import xarray as xr
Expand Down Expand Up @@ -133,7 +133,7 @@ def get_latlon_grid(self, name: str) -> tuple[Any, Any, Any]:
raise NotImplementedError("Hype coordinates cannot be mapped to grid")

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
"""List the parameters for this model.
Exposed Lisflood parameters:
Expand All @@ -157,7 +157,7 @@ def parameters(self) -> dict[str, Any]:
"crit_time": _get_hype_time(
_get_code_in_cfg(self._config, "cdate")
).strftime(ISO_TIMEFMT),
}
}.items()

def _coords_to_indices(
self, name: str, lat: Iterable[float], lon: Iterable[float]
Expand Down
6 changes: 3 additions & 3 deletions src/ewatercycle/plugins/lisflood/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from pathlib import Path
from typing import Any
from typing import Any, ItemsView

from pydantic import PrivateAttr, model_validator

Expand Down Expand Up @@ -137,7 +137,7 @@ def _make_cfg_file(self, **kwargs) -> Path:
return lisflood_file

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
"""List the parameters for this model.
Exposed Lisflood parameters:
Expand All @@ -158,7 +158,7 @@ def parameters(self) -> dict[str, Any]:
"MaskMap": self._get_textvar_value("MaskMap"),
"start_time": get_time(self.forcing.start_time).strftime(ISO_TIMEFMT),
"end_time": get_time(self.forcing.end_time).strftime(ISO_TIMEFMT),
}
}.items()

def finalize(self) -> None:
"""Perform tear-down tasks for the model."""
Expand Down
10 changes: 5 additions & 5 deletions src/ewatercycle/plugins/marrmot/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Literal
from typing import Any, ItemsView, Literal

import scipy.io as sio
from pydantic import PrivateAttr, model_validator
Expand Down Expand Up @@ -136,7 +136,7 @@ def _make_cfg_file(self, **kwargs) -> Path:
return config_file

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
"""List MarrmotM01's parameters and their values.
Model parameters:
Expand All @@ -157,7 +157,7 @@ def parameters(self) -> dict[str, Any]:
"solver": get_solver(self._config),
"start time": get_marrmot_time(self._config, "start"),
"end time": get_marrmot_time(self._config, "end"),
}
}.items()


M14_PARAMS = (
Expand Down Expand Up @@ -272,7 +272,7 @@ def _make_cfg_file(self, **kwargs) -> Path:
return config_file

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
"""List the parameters for this model.
Exposed Marrmot M14 parameters:
Expand Down Expand Up @@ -304,4 +304,4 @@ def parameters(self) -> dict[str, Any]:
"end time": get_marrmot_time(self._config, "end"),
}
)
return pars
return pars.items()
6 changes: 3 additions & 3 deletions src/ewatercycle/plugins/pcrglobwb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from os import PathLike
from typing import Any, Optional
from typing import Any, ItemsView, Optional

import bmipy
import numpy as np
Expand Down Expand Up @@ -97,7 +97,7 @@ def _initialize_config(self: "PCRGlobWB") -> "PCRGlobWB":
return self

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
"""List the parameters for this model."""
return {
"start_time": f"{self._config.get('globalOptions', 'startTime')}T00:00:00Z",
Expand All @@ -106,7 +106,7 @@ def parameters(self) -> dict[str, Any]:
"max_spinups_in_years": self._config.get(
"globalOptions", "maxSpinUpsInYears"
),
}
}.items()

def _make_cfg_file(self, **kwargs):
self._update_config(**kwargs)
Expand Down
6 changes: 3 additions & 3 deletions src/ewatercycle/plugins/wflow/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import shutil
from pathlib import Path
from typing import Any, Optional
from typing import Any, ItemsView, Optional

import bmipy
import numpy as np
Expand Down Expand Up @@ -142,11 +142,11 @@ def _make_cfg_dir(self, cfg_dir: Optional[str] = None, **kwargs) -> Path:
return self._work_dir

@property
def parameters(self) -> dict[str, Any]:
def parameters(self) -> ItemsView[str, Any]:
return {
"start_time": _wflow_to_iso(self._config.get("run", "starttime")),
"end_time": _wflow_to_iso(self._config.get("run", "endtime")),
}
}.items()


def _wflow_to_iso(time):
Expand Down
6 changes: 3 additions & 3 deletions tests/plugins/hype/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_parameters(self, model):
"start_time": "1961-01-01T00:00:00Z",
"end_time": "1963-12-31T00:00:00Z",
"crit_time": "1962-01-01T00:00:00Z",
}
}.items()
assert model.parameters == expected

def test_get_value_as_xarray(self, model):
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_parameters(self, model_with_setup, model):
"start_time": "2000-01-01T00:00:00Z",
"end_time": "2010-12-31T00:00:00Z",
"crit_time": "2002-01-01T00:00:00Z",
}
}.items()
assert model.parameters == expected


Expand Down Expand Up @@ -393,5 +393,5 @@ def test_parameters(self, model):
"start_time": "1986-01-02T00:00:00Z",
"end_time": "2018-01-02T00:00:00Z",
"crit_time": "1986-01-02T00:00:00Z",
}
}.items()
assert model.parameters == expected
4 changes: 2 additions & 2 deletions tests/plugins/lisflood/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_default_parameters(self, model: Lisflood):
"MaskMap": "$(PathMaps)/masksmall.map",
"start_time": "1986-01-02T00:00:00Z",
"end_time": "2018-01-02T00:00:00Z",
}
}.items()
assert model.parameters == expected_parameters

@pytest.fixture
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_parameters_after_setup(self, model_with_setup, tmp_path):
"MaskMap": f"{tmp_path}/custommask/mask",
"start_time": "1986-01-02T00:00:00Z",
"end_time": "2018-01-02T00:00:00Z",
}
}.items()
assert model_with_setup[3].parameters == expected_parameters


Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/marrmot/test_model_m01.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_parameters(self, model):
"solver": Solver(),
"start time": "1989-01-01T00:00:00Z",
"end time": "1992-12-31T00:00:00Z",
}
}.items()
assert model.parameters == expected

def test_setup(self, model_with_setup, forcing_file):
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_parameters_after_setup(self, model_with_setup):
"solver": Solver(),
"start time": "1989-01-01T00:00:00Z",
"end time": "1992-12-31T00:00:00Z",
}
}.items()
assert model.parameters == expected

def test_get_value_as_xarray(self, model_with_setup):
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/marrmot/test_model_m14.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_parameters(self, model):
"solver": Solver(),
"start time": "1989-01-01T00:00:00Z",
"end time": "1992-12-31T00:00:00Z",
}
}.items()
assert model.parameters == expected

def test_setup(self, model_with_setup):
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_parameters_after_setup(self, model_with_setup):
"solver": Solver(),
"start time": "1989-01-01T00:00:00Z",
"end time": "1992-12-31T00:00:00Z",
}
}.items()
assert model.parameters == expected

def test_get_value_as_xarray(self, model_with_setup):
Expand Down
2 changes: 1 addition & 1 deletion tests/src/base/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_version(mocked_model: eWaterCycleModel):


def test_parameters(mocked_model: eWaterCycleModel):
assert mocked_model.parameters == {}
assert mocked_model.parameters == {}.items()


class TestWithSetup:
Expand Down

0 comments on commit 8c44672

Please sign in to comment.