Skip to content

Commit

Permalink
317 cannot make prediction of dummy component set without tzone attr (#…
Browse files Browse the repository at this point in the history
…335)

* added None default tzone to prediction

* added testcase

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Sep 3, 2024
1 parent 9a8c5b9 commit 7465b2a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Feat
- flexible timezone support in astrog in [#333](https://github.com/Deltares/hatyan/pull/333)
- support for missing `comp` timezone attribute in `hatyan.prediction()` in [#335](https://github.com/Deltares/hatyan/pull/335)

### Fix
- repaired support for equidistant multiblock diafiles with varying timesteps in [#314](https://github.com/Deltares/hatyan/pull/314)
Expand Down
2 changes: 1 addition & 1 deletion hatyan/analysis_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def split_components(comp, dood_date_mid, hatyan_settings):
def prediction_singleperiod(comp:pd.DataFrame, times:pd.DatetimeIndex, hatyan_settings) -> pd.DataFrame:

metadata_comp = metadata_from_obj(comp)
tzone_comp = metadata_comp.pop('tzone')
tzone_comp = metadata_comp.pop('tzone', None)

if not isinstance(times, pd.DatetimeIndex):
raise TypeError(f'times argument can be of type pd.DatetimeIndex or slice, not {type(times)}')
Expand Down
56 changes: 40 additions & 16 deletions tests/test_analysis_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,22 @@ def test_analysis_component_splitting_invalid_components():


@pytest.mark.unittest
def test_predictionsettings():
def test_analysis_deprecatedsettings():
times_pred = slice(dt.datetime(2009,12,31,14),dt.datetime(2010,1,2,12), "1min")
file_data_comp0 = os.path.join(dir_testdata,'DENHDR_ana.txt')
comp = hatyan.read_components(filename=file_data_comp0)

with pytest.raises(DeprecationWarning) as e:
_ = hatyan.analysis(comp=comp, times=times_pred, CS_comps=1)
assert str(e.value) == "Argument 'CS_comps' has been deprecated for hatyan.analysis(), use 'cs_comps' instead"

with pytest.raises(DeprecationWarning) as e:
_ = hatyan.analysis(comp=comp, times=times_pred, xTxmat_condition_max=1)
assert str(e.value) == "Argument 'xTxmat_condition_max' has been deprecated for hatyan.analysis(), use 'max_matrix_condition' instead"


@pytest.mark.unittest
def test_prediction_settings():
times_pred = slice(dt.datetime(2009,12,31,14),dt.datetime(2010,1,2,12), "1min")
current_station = 'DENHDR'

Expand Down Expand Up @@ -344,21 +359,6 @@ def test_predictionsettings():
assert np.allclose(ts_pred["values"].values[:10], expected)


@pytest.mark.unittest
def test_analysis_deprecatedsettings():
times_pred = slice(dt.datetime(2009,12,31,14),dt.datetime(2010,1,2,12), "1min")
file_data_comp0 = os.path.join(dir_testdata,'DENHDR_ana.txt')
comp = hatyan.read_components(filename=file_data_comp0)

with pytest.raises(DeprecationWarning) as e:
_ = hatyan.analysis(comp=comp, times=times_pred, CS_comps=1)
assert str(e.value) == "Argument 'CS_comps' has been deprecated for hatyan.analysis(), use 'cs_comps' instead"

with pytest.raises(DeprecationWarning) as e:
_ = hatyan.analysis(comp=comp, times=times_pred, xTxmat_condition_max=1)
assert str(e.value) == "Argument 'xTxmat_condition_max' has been deprecated for hatyan.analysis(), use 'max_matrix_condition' instead"


@pytest.mark.unittest
def test_prediction_settings_invalid_values():
current_station = 'VLISSGN'
Expand Down Expand Up @@ -500,6 +500,7 @@ def test_prediction_1018():
assert (np.abs(ts_prediction.index-ts_prediction_times_pd) < dt.timedelta(days=10E-9)).all()


@pytest.mark.unittest
def test_prediction_comp_and_times_different_timezones():
"""
it is possible to supply a timezone via times, but the comp dataframe also contains a timezone already.
Expand Down Expand Up @@ -531,6 +532,26 @@ def test_prediction_comp_and_times_different_timezones():
assert ((ts_prediction1-ts_prediction2).dropna()["values"] < 1e-9).all()


@pytest.mark.unittest
def test_prediction_comp_no_timezone():
"""
https://github.com/Deltares/hatyan/issues/317
"""
comp = pd.DataFrame({"A": [1, 0.5, 0.2],
"phi_deg": [10,15,20]},
index=["M2","M4","S2"])
comp.attrs["nodalfactors"] = True
comp.attrs["fu_alltimes"] = True
comp.attrs["xfac"] = False
comp.attrs["source"] = "schureman"
dtindex = pd.date_range("2020-01-01","2020-01-02", freq="10min")
pred = hatyan.prediction(comp, times=dtindex)
assert pred.index.tz is None
assert pred.index[0] == pd.Timestamp('2020-01-01 00:00:00')
assert pred.index[-1] == pd.Timestamp('2020-01-02 00:00:00')


@pytest.mark.unittest
def test_prediction_perperiod_month():
file_data_comp0 = os.path.join(dir_testdata,'VLISSGN_obs1.txt')
ts_measurements_group0 = hatyan.read_dia(filename=file_data_comp0)
Expand All @@ -552,6 +573,7 @@ def test_prediction_perperiod_month():
assert len(ts_pred_atonce) == len(ts_pred_allmonths)


@pytest.mark.unittest
def test_prediction_perperiod_year():
# TODO: when the prediction function does not treat Y and M differently, this testcase can be dropped
file_data_comp0 = os.path.join(dir_testdata,'VLISSGN_obs1.txt')
Expand All @@ -574,6 +596,7 @@ def test_prediction_perperiod_year():
assert len(ts_pred_atonce) == len(ts_pred_allyears)


@pytest.mark.unittest
def test_prediction_hasrequiredargs():
file_data_comp0 = os.path.join(dir_testdata,'VLISSGN_obs1.txt')
ts_measurements_group0 = hatyan.read_dia(filename=file_data_comp0)
Expand All @@ -590,6 +613,7 @@ def test_prediction_hasrequiredargs():
assert str(e.value) == "prediction() per period, so 'timestep' argument should not be None"


@pytest.mark.unittest
def test_prediction_deprecatedsettings():
file_data_comp0 = os.path.join(dir_testdata,'VLISSGN_obs1.txt')
ts_measurements_group0 = hatyan.read_dia(filename=file_data_comp0)
Expand Down

0 comments on commit 7465b2a

Please sign in to comment.