Skip to content

Commit

Permalink
Merge pull request #447 from DHI/xy-in-comparer
Browse files Browse the repository at this point in the history
Observation xyz is not included in Comparer for point-to-point comparisons
  • Loading branch information
daniel-caichac-DHI authored Sep 25, 2024
2 parents 11d50e3 + 8ae039e commit db2f7c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
33 changes: 16 additions & 17 deletions modelskill/matching.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
from __future__ import annotations

import warnings
from datetime import timedelta
from pathlib import Path
import warnings

from typing import (
Iterable,
Any,
Collection,
Iterable,
List,
Literal,
Mapping,
Optional,
Union,
Sequence,
get_args,
TypeVar,
Any,
Union,
get_args,
overload,
)

import mikeio
import numpy as np
import pandas as pd
import xarray as xr

import mikeio


from . import model_result, Quantity
from .timeseries import TimeSeries
from .types import Period
from . import Quantity, __version__, model_result
from .comparison import Comparer, ComparerCollection
from .model._base import Alignable
from .model.grid import GridModelResult
from .model.dfsu import DfsuModelResult
from .model.track import TrackModelResult
from .model.dummy import DummyModelResult
from .model.grid import GridModelResult
from .model.track import TrackModelResult
from .obs import Observation, observation
from .comparison import Comparer, ComparerCollection
from . import __version__
from .timeseries import TimeSeries
from .types import Period

TimeDeltaTypes = Union[float, int, np.timedelta64, pd.Timedelta, timedelta]
IdxOrNameTypes = Optional[Union[int, str]]
Expand Down Expand Up @@ -403,7 +401,8 @@ def match_space_time(
)
aligned = aligned.rename({v: f"{v}_mod" for v in overlapping_names})

data.update(aligned)
for dv in aligned:
data[dv] = aligned[dv]

# drop NaNs in model and observation columns (but allow NaNs in aux columns)
def mo_kind(k: str) -> bool:
Expand Down
42 changes: 38 additions & 4 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ def mr3():
return ms.model_result(fn, item=0, name="SW_3")


def test_properties_after_match(o1, mr1):
cmp = ms.match(o1, mr1)
assert cmp.n_models == 1
assert cmp.n_points == 386
assert cmp.x == 4.242
assert cmp.y == 52.6887
assert cmp.z is None
assert cmp.name == "HKNA"
assert cmp.gtype == "point"
assert cmp.mod_names == ["SW_1"]


def test_properties_after_match_ts(o1):
fn = "tests/testdata/SW/HKNA_Hm0.dfs0"
mr = ms.PointModelResult(fn, item=0, name="SW_1")
cmp = ms.match(o1, mr)
assert cmp.n_models == 1
assert cmp.n_points == 564
assert cmp.x == 4.242
assert cmp.y == 52.6887
assert cmp.z is None
assert cmp.name == "HKNA"
assert cmp.gtype == "point"
assert cmp.mod_names == ["SW_1"]


# TODO remove in v1.1
def test_compare_multi_obs_multi_model_is_deprecated(o1, o2, o3, mr1, mr2):
with pytest.warns(FutureWarning, match="match"):
Expand Down Expand Up @@ -543,12 +569,20 @@ def test_compare_model_vs_dummy_for_track(mr1, o3):
# better than dummy 🙂
assert cmp2.score()["SW_1"] == pytest.approx(0.3524703)

def test_match_obs_model_pos_args_wrong_order_helpful_error_message():

def test_match_obs_model_pos_args_wrong_order_helpful_error_message():
# match is pretty helpful in converting strings or dataset
# so we need to use a ModelResult to trigger the error
mr = ms.PointModelResult(data=pd.Series([0.0,0.0], index=pd.date_range("1970", periods=2, freq='d')), name="Zero")
obs = ms.PointObservation(data=pd.Series([1.0, 2.0, 3.0], index=pd.date_range("1970", periods=3, freq='h')), name="MyStation")
mr = ms.PointModelResult(
data=pd.Series([0.0, 0.0], index=pd.date_range("1970", periods=2, freq="d")),
name="Zero",
)
obs = ms.PointObservation(
data=pd.Series(
[1.0, 2.0, 3.0], index=pd.date_range("1970", periods=3, freq="h")
),
name="MyStation",
)

with pytest.raises(TypeError, match="order"):
ms.match(mr, obs)
ms.match(mr, obs)

0 comments on commit db2f7c2

Please sign in to comment.