Skip to content

Commit

Permalink
improved error message including test (#89)
Browse files Browse the repository at this point in the history
* improved error message including test

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Jun 20, 2024
1 parent 8bb04fa commit 5de2ea0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
### Fix
- implemented workaround for pandas 2.2.0 with different rounding behaviour in [#69](https://github.com/Deltares-research/kenmerkendewaarden/pull/69)
- fixed different lengths of `compute_expected_counts()` and `compute_actual_counts()` in case of all-nan periods in [#87](https://github.com/Deltares-research/kenmerkendewaarden/pull/87)
- clearer error message in case of too many nans in timeseries slotgemiddelde model fit in [#89](https://github.com/Deltares-research/kenmerkendewaarden/pull/89)


## 0.1.0 (2024-03-11)
Expand Down
12 changes: 9 additions & 3 deletions kenmerkendewaarden/slotgemiddelden.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,16 @@ def fit_models(mean_array_todate: pd.Series, with_nodal=True) -> pd.DataFrame:
allyears_dt = pd.date_range(start=start, end=end, freq='YS')
mean_array_allyears = pd.Series(mean_array_todate, index=allyears_dt)

df = pd.DataFrame({'year':mean_array_allyears.index.year, 'height':mean_array_allyears.values}) #TODO: make functions accept mean_array instead of df as argument?

# below methods are copied from https://github.com/openearth/sealevel/blob/master/slr/slr/models.py #TODO: install slr package as dependency or keep separate?
mean_array_allyears_nonans = mean_array_allyears.loc[~mean_array_allyears.isnull()]
if len(mean_array_allyears_nonans) < 2:
raise ValueError(f'nan-filtered timeseries has only one timestep, cannot perform model fit:\n{mean_array_allyears_nonans}')

# convert to dataframe of expected format
# TODO: make functions accept mean_array instead of df as argument?
df = pd.DataFrame({'year':mean_array_allyears.index.year, 'height':mean_array_allyears.values})

# below methods are copied from https://github.com/openearth/sealevel/blob/master/slr/slr/models.py
# TODO: install slr package as dependency or keep separate?
fit, _, X = linear_model(df, with_wind=False, with_nodal=with_nodal)
pred_linear = fit.predict(X)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_slotgemiddelden.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def test_calc_slotgemiddelden(df_meas_2010_2014, df_ext_12_2010_2014):
assert np.allclose(slotgemiddelden_dict_inclext['LW_model_fit'].values, lw_model_fit_expected)


@pytest.mark.unittest
def test_calc_slotgemiddelden_oneyear_fails(df_meas_2010_2014):
df_meas_onevalidyear = df_meas_2010_2014.copy()
df_meas_onevalidyear.loc["2011":] = np.nan
with pytest.raises(ValueError) as e:
kw.calc_slotgemiddelden(df_meas=df_meas_onevalidyear, df_ext=None)
assert "nan-filtered timeseries has only one timestep" in str(e.value)


@pytest.mark.unittest
def test_plot_slotgemiddelden(df_meas_2010_2014, df_ext_12_2010_2014):
slotgemiddelden_dict_inclext = kw.calc_slotgemiddelden(df_meas=df_meas_2010_2014, df_ext=df_ext_12_2010_2014)
Expand Down

0 comments on commit 5de2ea0

Please sign in to comment.