Skip to content

Commit

Permalink
Merge pull request #405 from DHI/peak-ratio-comparercollection
Browse files Browse the repository at this point in the history
Integration test peak ratio
  • Loading branch information
ecomodeller authored Jan 30, 2024
2 parents b37a539 + de06647 commit 5a29b5d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
10 changes: 7 additions & 3 deletions modelskill/comparison/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ def _groupby_df(
metrics: List[Callable],
n_min: Optional[int] = None,
) -> pd.DataFrame:
def calc_metrics(group):
row = {}
row["n"] = len(group)
def calc_metrics(group: pd.DataFrame) -> pd.Series:
# set index to time column (in most cases a DatetimeIndex, but not always)
group = group.set_index("time")

# TODO is n a metric or not?
row = {"n": len(group)}

for metric in metrics:
row[metric.__name__] = metric(group.obs_val, group.mod_val)
return pd.Series(row)
Expand Down
8 changes: 5 additions & 3 deletions modelskill/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def explained_variance(obs: np.ndarray, model: np.ndarray) -> float:


def pr(
obs: np.ndarray,
obs: pd.Series,
model: np.ndarray,
inter_event_level: float = 0.7,
AAP: int = 2,
Expand All @@ -544,7 +544,7 @@ def pr(

def peak_ratio(
obs: pd.Series,
model: pd.Series,
model: np.ndarray,
inter_event_level: float = 0.7,
AAP: int = 2,
inter_event_time="36h",
Expand Down Expand Up @@ -576,6 +576,7 @@ def peak_ratio(
return np.nan
assert isinstance(obs.index, pd.DatetimeIndex)
time = obs.index

# Calculate number of years
dt_int = time[1:].values - time[0:-1].values
dt_int_mode = float(stats.mode(dt_int, keepdims=False)[0]) / 1e9 # in seconds
Expand Down Expand Up @@ -1206,4 +1207,5 @@ def _parse_metric(
return [get_metric(m) for m in metrics]


__all__ = list(defined_metrics)
# TODO add non-metric functions to __all__
__all__ = [str(m) for m in defined_metrics]
26 changes: 26 additions & 0 deletions tests/test_comparercollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def _set_attrs(data: xr.Dataset) -> xr.Dataset:
return data


@pytest.fixture
def cc_pr() -> modelskill.comparison.Comparer:
"""Real data to test Peak Ratio from top-to-bottom"""
obs = modelskill.PointObservation(
"tests/testdata/PR_test_data.dfs0", item="Hs_measured"
)
mod = modelskill.PointModelResult(
"tests/testdata/PR_test_data.dfs0", item="Hs_model"
)
return modelskill.match(obs, mod)


@pytest.fixture
def pc() -> modelskill.comparison.Comparer:
"""A comparer with fake point data and 2 models"""
Expand Down Expand Up @@ -543,3 +555,17 @@ def test_plot_accepts_figsize(cc_plot_function):
ax = cc_plot_function(figsize=figsize)
a, b = ax.get_figure().get_size_inches()
assert a, b == figsize


def test_peak_ratio(cc):
"""Non existent peak ratio"""
cc = cc.sel(model="m1")
sk = cc.skill(metrics=["peak_ratio"])

assert sk.loc["fake point obs", "peak_ratio"] == pytest.approx(1.119999999)


def test_peak_ratio_2(cc_pr):
sk = cc_pr.skill(metrics=["peak_ratio"])
assert "peak_ratio" in sk.data.columns
assert sk.to_dataframe()["peak_ratio"].values == pytest.approx(1.0799999095653732)
2 changes: 1 addition & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_ev():
assert ev == 1.0


def test_pr(obs_series, mod_series):
def test_pr(obs_series: pd.Series, mod_series: pd.Series) -> None:
# Obs needs to be a series as the mode of the time index is used.
# Will use the same data for a real test of ev
obs = obs_series
Expand Down

0 comments on commit 5a29b5d

Please sign in to comment.