Skip to content

Commit

Permalink
fix: splitting aligned/shifted unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madtoinou committed Nov 2, 2023
1 parent 1a216c2 commit b5ac093
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions darts/tests/dataprocessing/transformers/test_midas.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def test_error_when_from_low_to_high(self):
"""
Tests if the transformer raises an error when the user asks for a transform in the wrong direction.
"""
# wrong direction / low to high freq
# wrong direction : low to high freq
midas_1 = MIDAS(low_freq="M")
with pytest.raises(ValueError):
midas_1.fit_transform(self.quarterly_ts)
Expand Down Expand Up @@ -414,11 +414,10 @@ def test_ts_with_static_covariates(self):
monthly_multivar_with_static_covs.static_covariates
)

# @pytest.mark.parametrize()
def test_int_anchored_low_freq(self):
def test_aligned_low_freq_anchor(self):
"""
Check that values are properly extracted when the beginning of the series is not aligned with the low frequency
offset anchor value (day of the week, week of the month).
Check that values are properly extracted when the beginning of the series is aligned with the low frequency
anchor value (day of the week).
"""
ts = linear_timeseries(
start=pd.Timestamp("2000-01-01"),
Expand All @@ -427,9 +426,8 @@ def test_int_anchored_low_freq(self):
end_value=22,
length=22,
)

# resampling with anchor aligned with the first date
assert ts.time_index[0].day_name() == "Saturday"

midas_sat = MIDAS(low_freq="W-SAT")
ts_saturday_anchor = midas_sat.fit_transform(ts)
# the resampled series start at the same timestamp
Expand All @@ -439,32 +437,44 @@ def test_int_anchored_low_freq(self):
ts_saturday_anchor[0].values().flatten() == ts[:7].values().flatten()
).all()

# reverse transform with anchor aligned
# inverse transform
ts_inv_saturday_anchor = midas_sat.inverse_transform(ts_saturday_anchor)
np.testing.assert_array_almost_equal(
ts.values(), ts_inv_saturday_anchor.values()
)
assert (ts.time_index == ts_inv_saturday_anchor.time_index).all()
assert ts.time_index.equals(ts_inv_saturday_anchor.time_index)

# resampling with shifted anchor
def test_shifted_low_freq_anchor(self):
"""
Check that values are properly extracted when the beginning of the series is not aligned with the low
frequency anchor value (day of the week).
"""
ts = linear_timeseries(
start=pd.Timestamp("2000-01-01"),
freq="D",
start_value=1,
end_value=22,
length=22,
)
assert ts.time_index[2].day_name() == "Monday"

midas_mon = MIDAS(low_freq="W-MON")
ts_monday_anchor = midas_mon.fit_transform(ts)
# the resampled series start at the previous anchor point (incomplete)
assert ts_monday_anchor.time_index[0] == ts.time_index[2] - pd.Timedelta(
weeks=1
)
assert ts_monday_anchor.time_index[1] == ts.time_index[2]
# the incomplete low frequency period
# incomplete low frequency period
assert (
ts_monday_anchor[0].values().flatten()[-2:] == ts[:2].values().flatten()
).all()
# the complete low frequency period
# complete low frequency period
assert (
ts_monday_anchor[1].values().flatten() == ts[2:9].values().flatten()
).all()

# reverse transform with shifted anchor
# reverse transform
ts_inv_monday_anchor = midas_mon.inverse_transform(ts_monday_anchor)
np.testing.assert_array_almost_equal(ts.values(), ts_inv_monday_anchor.values())
assert (ts.time_index == ts_inv_monday_anchor.time_index).all()
assert ts.time_index.equals(ts_inv_monday_anchor.time_index)

0 comments on commit b5ac093

Please sign in to comment.