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

Fix issues related to new Pandas offset names #3117

Merged
merged 12 commits into from
Feb 2, 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
2 changes: 1 addition & 1 deletion src/gluonts/dataset/artificial/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(
self,
num_timeseries: int = 10,
num_steps: int = 30,
freq: str = "1H",
freq: str = "1h",
start: str = "2000-01-01 00:00:00",
# Generates constant dataset of 0s with explicit NaN missing values
is_nan: bool = False,
Expand Down
3 changes: 3 additions & 0 deletions src/gluonts/mx/model/deepstate/_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@
# series in the dataset.
FREQ_LONGEST_PERIOD_DICT = {
"M": 12, # yearly seasonality
"ME": 12, # yearly seasonality
"W": 52, # yearly seasonality
"D": 31, # monthly seasonality
"B": 22, # monthly seasonality
"H": 168, # weekly seasonality
"h": 168, # weekly seasonality
"T": 1440, # daily seasonality
"min": 1440, # daily seasonality
}


Expand Down
6 changes: 3 additions & 3 deletions src/gluonts/mx/model/deepstate/issm.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,20 +305,20 @@ def get_from_freq(cls, freq: str, add_trend: bool = DEFAULT_ADD_TREND):

seasonal_issms: List[SeasonalityISSM] = []

if offset.name == "M":
if offset.name in ["M", "ME"]:
seasonal_issms = [MonthOfYearSeasonalISSM()]
elif norm_freq_str(offset.name) == "W":
seasonal_issms = [WeekOfYearSeasonalISSM()]
elif offset.name == "D":
seasonal_issms = [DayOfWeekSeasonalISSM()]
elif offset.name == "B": # TODO: check this case
seasonal_issms = [DayOfWeekSeasonalISSM()]
elif offset.name == "H":
elif offset.name in ["H", "h"]:
seasonal_issms = [
HourOfDaySeasonalISSM(),
DayOfWeekSeasonalISSM(),
]
elif offset.name == "T":
elif offset.name in ["T", "min"]:
seasonal_issms = [
MinuteOfHourSeasonalISSM(),
HourOfDaySeasonalISSM(),
Expand Down
6 changes: 4 additions & 2 deletions src/gluonts/mx/model/deepvar/_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ def __call__(self, index: pd.PeriodIndex) -> np.ndarray:
def time_features_from_frequency_str(freq_str: str) -> List[TimeFeature]:
features = {
"M": ["weekofyear"],
"ME": ["weekofyear"],
"W": ["daysinmonth", "weekofyear"],
"D": ["dayofweek"],
"B": ["dayofweek", "dayofyear"],
"H": ["hour", "dayofweek"],
"h": ["hour", "dayofweek"],
"min": ["minute", "hour", "dayofweek"],
"T": ["minute", "hour", "dayofweek"],
}
Expand All @@ -114,13 +116,13 @@ def get_lags_for_frequency(
) -> List[int]:
offset = to_offset(freq_str)

if offset.name == "M":
if offset.name in ["M", "ME"]:
lags = [[1, 12]]
elif offset.name == "D":
lags = [[1, 7, 14]]
elif offset.name == "B":
lags = [[1, 2]]
elif offset.name == "H":
elif offset.name in ["H", "h"]:
lags = [[1, 24, 168]]
elif offset.name in ("min", "T"):
lags = [[1, 4, 12, 24, 48]]
Expand Down
2 changes: 2 additions & 0 deletions src/gluonts/mx/model/wavenet/_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ def __init__(
self.freq,
{
"H": 7 * 24,
"h": 7 * 24,
"D": 7,
"W": 52,
"M": 12,
"ME": 12,
"B": 7 * 5,
"min": 24 * 60,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def fourier_time_features_from_frequency(freq_str: str) -> List[TimeFeature]:

features = {
"M": ["weekofyear"],
"ME": ["weekofyear"],
"W": ["daysinmonth", "weekofyear"],
"D": ["dayofweek"],
"B": ["dayofweek", "dayofyear"],
"H": ["hour", "dayofweek"],
"h": ["hour", "dayofweek"],
"min": ["minute", "hour", "dayofweek"],
"T": ["minute", "hour", "dayofweek"],
}
Expand Down
4 changes: 2 additions & 2 deletions src/gluonts/nursery/robust-mts-attack/pts/feature/lags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def lags_for_fourier_time_features_from_frequency(
offset = to_offset(freq_str)
multiple, granularity = offset.n, offset.name

if granularity == "M":
if granularity in ("M", "ME"):
lags = [[1, 12]]
elif granularity == "D":
lags = [[1, 7, 14]]
elif granularity == "B":
lags = [[1, 2]]
elif granularity == "H":
elif granularity in ("H", "h"):
lags = [[1, 24, 168]]
elif granularity in ("T", "min"):
lags = [[1, 4, 12, 24, 48]]
Expand Down
16 changes: 7 additions & 9 deletions src/gluonts/time_feature/lag.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ def _make_lags_for_month(multiple, num_cycles=3):
# normalize offset name, so that both `W` and `W-SUN` refer to `W`
offset_name = norm_freq_str(offset.name)

if offset_name == "A":
if offset_name in ["A", "Y", "YE"]:
lags = []
elif offset_name == "Q":
elif offset_name in ["Q", "QE"]:
assert (
offset.n == 1
), "Only multiple 1 is supported for quarterly. Use x month instead."
lags = _make_lags_for_month(offset.n * 3.0)
elif offset_name == "M":
elif offset_name in ["M", "ME"]:
lags = _make_lags_for_month(offset.n)
elif offset_name == "W":
lags = _make_lags_for_week(offset.n)
Expand All @@ -124,31 +124,29 @@ def _make_lags_for_month(multiple, num_cycles=3):
lags = _make_lags_for_day(
offset.n, days_in_week=5, days_in_month=22
) + _make_lags_for_week(offset.n / 5.0)
elif offset_name == "H":
elif offset_name in ["H", "h"]:
lags = (
_make_lags_for_hour(offset.n)
+ _make_lags_for_day(offset.n / 24)
+ _make_lags_for_week(offset.n / (24 * 7))
)
# minutes
elif offset_name == "T":
elif offset_name in ["T", "min"]:
lags = (
_make_lags_for_minute(offset.n)
+ _make_lags_for_hour(offset.n / 60)
+ _make_lags_for_day(offset.n / (60 * 24))
+ _make_lags_for_week(offset.n / (60 * 24 * 7))
)
# second
elif offset_name == "S":
elif offset_name in ["S", "s"]:
lags = (
_make_lags_for_second(offset.n)
+ _make_lags_for_minute(offset.n / 60)
+ _make_lags_for_hour(offset.n / (60 * 60))
)
else:
raise ValueError(
f"invalid frequency | `freq_str={freq_str}` -> `offset_name={offset_name}`"
)
raise ValueError(f"invalid frequency: {freq_str=}, {offset_name=}")

# flatten lags list and filter
lags = [
Expand Down
6 changes: 5 additions & 1 deletion src/gluonts/time_feature/seasonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

DEFAULT_SEASONALITIES = {
"S": 3600, # 1 hour
"s": 3600, # 1 hour
"T": 1440, # 1 day
"min": 1440, # 1 day
"H": 24, # 1 day
"h": 24, # 1 day
"D": 1, # 1 day
"W": 1, # 1 week
"M": 12,
"ME": 12,
"B": 5,
"Q": 4,
}
Expand All @@ -36,7 +40,7 @@ def get_seasonality(freq: str, seasonalities=DEFAULT_SEASONALITIES) -> int:
"""
Return the seasonality of a given frequency:

>>> get_seasonality("2H")
>>> get_seasonality("2h")
12
"""
offset = pd.tseries.frequencies.to_offset(freq)
Expand Down
16 changes: 8 additions & 8 deletions test/time_feature/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@


def test_norm_freq_str():
assert norm_freq_str(to_offset("Y").name) == "A"
assert norm_freq_str(to_offset("YS").name) == "A"
assert norm_freq_str(to_offset("A").name) == "A"
assert norm_freq_str(to_offset("AS").name) == "A"
assert norm_freq_str(to_offset("Y").name) in ["A", "YE"]
assert norm_freq_str(to_offset("YS").name) in ["A", "Y"]
assert norm_freq_str(to_offset("A").name) in ["A", "YE"]
assert norm_freq_str(to_offset("AS").name) in ["A", "Y"]

assert norm_freq_str(to_offset("Q").name) == "Q"
assert norm_freq_str(to_offset("Q").name) in ["Q", "QE"]
assert norm_freq_str(to_offset("QS").name) == "Q"

assert norm_freq_str(to_offset("M").name) == "M"
assert norm_freq_str(to_offset("MS").name) == "M"
assert norm_freq_str(to_offset("M").name) in ["M", "ME"]
assert norm_freq_str(to_offset("MS").name) in ["M", "ME"]

assert norm_freq_str(to_offset("S").name) == "S"
assert norm_freq_str(to_offset("S").name) in ["S", "s"]
Loading