Skip to content

Commit

Permalink
Merge pull request #414 from DHI/legacy-test
Browse files Browse the repository at this point in the history
Test compatability with older versions
  • Loading branch information
jsmariegaard committed Feb 9, 2024
2 parents 085c836 + 3c24fc1 commit 3cba82a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 32 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/legacy_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Legacy test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Install older dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements_min.txt
pip install pytest
- name: Install modelskill
run: |
pip install .[test]
- name: Test with pytest
run: |
pytest --ignore tests/notebooks/
11 changes: 4 additions & 7 deletions modelskill/comparison/_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@


class Scoreable(Protocol):
def score(self, metric: str | Callable, **kwargs: Any) -> Dict[str, float]:
...
def score(self, metric: str | Callable, **kwargs: Any) -> Dict[str, float]: ...

def skill(
self,
by: str | Iterable[str] | None = None,
metrics: Iterable[str] | Iterable[Callable] | str | Callable | None = None,
**kwargs: Any,
) -> SkillTable:
...
) -> SkillTable: ...

def gridded_skill(
self,
Expand All @@ -69,8 +67,7 @@ def gridded_skill(
metrics: Iterable[str] | Iterable[Callable] | str | Callable | None = None,
n_min: int | None = None,
**kwargs: Any,
) -> SkillGrid:
...
) -> SkillGrid: ...


def _parse_dataset(data: xr.Dataset) -> xr.Dataset:
Expand Down Expand Up @@ -922,7 +919,7 @@ def _to_long_dataframe(

df = (
data.to_dataframe()
.reset_index(names="time")
.reset_index()
.melt(
value_vars=self.mod_names,
var_name="model",
Expand Down
9 changes: 5 additions & 4 deletions modelskill/timeseries/_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from dataclasses import dataclass
from typing import ClassVar, Optional, TypeVar, Any
import numpy as np
from numpy.typing import NDArray
import pandas as pd
import xarray as xr

Expand Down Expand Up @@ -121,7 +120,9 @@ class TimeSeries:
"""Time series data"""

data: xr.Dataset
plotter: ClassVar = MatplotlibTimeSeriesPlotter # TODO is this the best option to choose a plotter? Can we use the settings module?
plotter: ClassVar = (
MatplotlibTimeSeriesPlotter # TODO is this the best option to choose a plotter? Can we use the settings module?
)

def __init__(self, data: xr.Dataset) -> None:
self.data = data if self._is_input_validated(data) else _validate_dataset(data)
Expand Down Expand Up @@ -213,7 +214,7 @@ def y(self) -> Any:
def y(self, value: Any) -> None:
self.data["y"] = value

def _coordinate_values(self, coord: str) -> float | NDArray[Any]:
def _coordinate_values(self, coord: str) -> float | np.ndarray:
vals = self.data[coord].values
return np.atleast_1d(vals)[0] if vals.ndim == 0 else vals

Expand All @@ -222,7 +223,7 @@ def _is_modelresult(self) -> bool:
return bool(self.data[self.name].attrs["kind"] == "model")

@property
def values(self) -> NDArray[Any]:
def values(self) -> np.ndarray:
"""Values as numpy array"""
return self.data[self.name].values

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exclude = ["notebooks", "tests"]
name="modelskill"
version="1.0.dev24"
dependencies = [
"numpy",
"pandas",
"numpy >= 1.20.0",
"pandas >= 1.4",
"mikeio >= 1.2",
"matplotlib",
"xarray",
Expand Down
3 changes: 3 additions & 0 deletions requirements_min.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy==1.20.0
pandas==1.4.0
xarray==2022.3.0
19 changes: 0 additions & 19 deletions tests/model/test_dfsu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy as np
import pytest

import mikeio
Expand Down Expand Up @@ -208,15 +207,6 @@ def test_dfsu_extract_point(sw_dutch_coast, Hm0_EPL):
mr_extr_2 = mr2.extract(Hm0_EPL.copy())

assert list(mr_extr_1.data.data_vars) == list(mr_extr_2.data.data_vars)
assert np.all(mr_extr_1.data == mr_extr_2.data)

c1 = mr1.extract(Hm0_EPL.copy())
c2 = mr2.extract(Hm0_EPL.copy())
assert isinstance(c1, ms.PointModelResult)
assert isinstance(c2, ms.PointModelResult)
assert np.all(c1.data == c2.data)
# c1.observation.itemInfo == Hm0_EPL.itemInfo
# assert len(c1.observation.data.index.difference(Hm0_EPL.data.index)) == 0


def test_dfsu_extract_point_aux(sw_dutch_coast, Hm0_EPL):
Expand All @@ -243,15 +233,6 @@ def test_dfsu_extract_track(sw_dutch_coast, Hm0_C2):
ds2 = mr_track2.data

assert list(ds1.data_vars) == list(ds2.data_vars)
assert np.all(ds1 == ds2)

c1 = mr1.extract(Hm0_C2.copy())
c2 = mr2.extract(Hm0_C2.copy())
assert isinstance(c1, ms.TrackModelResult)
assert isinstance(c2, ms.TrackModelResult)
assert np.all(c1.data == c2.data)
# c1.observation.itemInfo == Hm0_C2.itemInfo
# assert len(c1.observation.data.index.difference(Hm0_C2.data.index)) == 0


def test_dfsu_extract_track_aux(sw_dutch_coast, Hm0_C2):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_aggregated_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def test_skill_mm_swaplevel(cc2):
assert np.all(sk2.index.levels[0] == sk.index.levels[1])


@pytest.mark.skipif(pd.__version__ < "2.0.0", reason="requires newer pandas")
def test_skill_mm_sort_index(cc2):
sk = cc2.skill(metrics=["rmse", "bias"])
assert list(sk.index.get_level_values(1)) == [
Expand Down Expand Up @@ -321,6 +322,7 @@ def test_skill_plot_grid(cc2):
assert "only possible for MultiIndex" in str(wn[0].message)


@pytest.mark.skipif(pd.__version__ < "1.5.0", reason="requires Pandas 1.5.0 or higher")
def test_skill_style(cc2):
sk = cc2.skill(metrics=["bias", "rmse", "lin_slope", "si"])
sk.style()
Expand All @@ -331,6 +333,7 @@ def test_skill_style(cc2):
sk.style(cmap="viridis_r", show_best=False)


@pytest.mark.skipif(pd.__version__ < "1.5.0", reason="requires Pandas 1.5.0 or higher")
def test_styled_skill_can_be_rendered(cc2):
sk = cc2.skill()
# _repr_html_ is called by Jupyter
Expand Down
1 change: 1 addition & 0 deletions tests/test_comparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ def test_skill_dt(pc):
assert list(sk.data.index.levels[1]) == [1, 2, 3, 4, 5] # Tuesday to Saturday


@pytest.mark.skipif(pd.__version__ < "2.0.0", reason="requires newer pandas")
def test_skill_freq(pc):
assert pc.time.freq == "D"

Expand Down
1 change: 1 addition & 0 deletions tests/test_grid_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_gridded_skill_sel_model(cc2) -> None:
gs.sel(model="bad_model")


@pytest.mark.skipif(pd.__version__ < "2.0.0", reason="requires newer pandas")
def test_gridded_skill_is_subsettable(cc2) -> None:
gs = cc2.gridded_skill(bins=3, metrics=["rmse", "bias"])
gs.data.rmse.sel(x=2, y=53.5, method="nearest").values == pytest.approx(0.10411702)
Expand Down

0 comments on commit 3cba82a

Please sign in to comment.