Skip to content

Commit

Permalink
extra assertions and fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
veenstrajelmer committed Jun 12, 2024
1 parent 6b694b9 commit fa925c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions kenmerkendewaarden/slotgemiddelden.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ def fit_models(mean_array_todate: pd.Series, with_nodal=True) -> pd.DataFrame:
logger.info(f"fit linear model for {start} to {end}")

# We'll just use the years. This assumes that annual waterlevels are used that are stored left-padded, the mean waterlevel for 2020 is stored as 2020-1-1. This is not logical, but common practice.
allyears_DTI = pd.date_range(start=start, end=end, freq='YS')
mean_array_allyears = pd.Series(mean_array_todate, index=allyears_DTI)
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?

fit, names, X = linear_model(df, with_wind=False, with_nodal=with_nodal)
fit, _, X = linear_model(df, with_wind=False, with_nodal=with_nodal)
pred_linear = fit.predict(X)

linear_fit = pd.Series(pred_linear, index=allyears_DTI)
linear_fit = pd.Series(pred_linear, index=allyears_dt)
return linear_fit


Expand Down
16 changes: 8 additions & 8 deletions tests/test_slotgemiddelden.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
def test_fit_models(df_meas_2010_2014):
dict_wltidalindicators_valid = kw.calc_wltidalindicators(df_meas_2010_2014, min_count=2900) #24*365=8760 (hourly interval), 24/3*365=2920 (3-hourly interval)
wl_mean_peryear_valid = dict_wltidalindicators_valid['wl_mean_peryear']
pred_pd_wl = kw.slotgemiddelden.fit_models(wl_mean_peryear_valid)

# TODO: maybe add nonodal
# nonodal_expected = np.array([0.07851414, 0.0813139 , 0.08411366, 0.08691342, 0.08971318,
# 0.09251294, 0.0953127 , 0.09811246, 0.10091222])
winodal_expected = np.array([0.0141927 , 0.08612119, 0.0853051 , 0.07010864, 0.10051922, 0.23137634])
# assert np.allclose(pred_pd_wl['pred_linear_nonodal'].values, nonodal_expected)
assert np.allclose(pred_pd_wl.values, winodal_expected)
wl_model_fit_nodal = kw.slotgemiddelden.fit_models(wl_mean_peryear_valid, with_nodal=True)
nodal_expected = np.array([0.0141927 , 0.08612119, 0.0853051 , 0.07010864, 0.10051922, 0.23137634])
assert np.allclose(wl_model_fit_nodal.values, nodal_expected)

wl_model_fit_linear = kw.slotgemiddelden.fit_models(wl_mean_peryear_valid, with_nodal=False)
linear_expected = np.array([0.07851414, 0.0813139 , 0.08411366, 0.08691342, 0.08971318, 0.09251294])
assert np.allclose(wl_model_fit_linear.values, linear_expected)


@pytest.mark.unittest
Expand All @@ -35,7 +35,7 @@ def test_calc_slotgemiddelden(df_meas_2010_2014, df_ext_12_2010_2014):
for key in expected_keys_inclext:
assert isinstance(slotgemiddelden_dict_inclext[key], pd.Series)

# assert values
# assertion of values
wl_mean_peryear_expected = np.array([0.07960731, 0.08612119, 0.0853051 , 0.07010864, 0.10051922])
hw_mean_peryear_expected = np.array([1.13968839, 1.12875177, 1.13988685, 1.1415461 , 1.18998584])
lw_mean_peryear_expected = np.array([-0.60561702, -0.59089362, -0.59342291, -0.61334278, -0.58024113])
Expand Down

0 comments on commit fa925c7

Please sign in to comment.