Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved error message including test #89

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading