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

Backports for v0.14.5 #3236

Draft
wants to merge 4 commits into
base: v0.14.x
Choose a base branch
from
Draft
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
14 changes: 13 additions & 1 deletion src/gluonts/core/serde/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ def encode_partial(v: partial) -> Any:
}


decode_disallow = [
eval,
exec,
compile,
open,
input,
]


def decode(r: Any) -> Any:
"""
Decodes a value from an intermediate representation `r`.
Expand All @@ -312,7 +321,10 @@ def decode(r: Any) -> Any:
kind = r["__kind__"]
cls = cast(Any, locate(r["class"]))

assert cls is not None, f"Can not locate {r['class']}."
if cls is None:
raise ValueError(f"Cannot locate {r['class']}.")
if cls in decode_disallow:
raise ValueError(f"{r['class']} cannot be run.")

if kind == Kind.Type:
return cls
Expand Down
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
26 changes: 13 additions & 13 deletions src/gluonts/time_feature/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

from packaging.version import Version
from typing import Any, Callable, Dict, List

import numpy as np
Expand Down Expand Up @@ -194,7 +195,10 @@ def norm_freq_str(freq_str: str) -> str:
# Note: Secondly ("S") frequency exists, where we don't want to remove the
# "S"!
if len(base_freq) >= 2 and base_freq.endswith("S"):
return base_freq[:-1]
base_freq = base_freq[:-1]
# In pandas >= 2.2, period end frequencies have been renamed, e.g. "M" -> "ME"
if Version(pd.__version__) >= Version("2.2.0"):
base_freq += "E"

return base_freq

Expand Down Expand Up @@ -250,17 +254,13 @@ def time_features_from_frequency_str(freq_str: str) -> List[TimeFeature]:
Unsupported frequency {freq_str}

The following frequencies are supported:

Y - yearly
alias: A
Q - quarterly
M - monthly
W - weekly
D - daily
B - business days
H - hourly
T - minutely
alias: min
S - secondly

"""

for offset_cls in features_by_offsets:
offset = offset_cls()
supported_freq_msg += (
f"\t{offset.freqstr.split('-')[0]} - {offset_cls.__name__}"
)

raise RuntimeError(supported_freq_msg)
12 changes: 6 additions & 6 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,22 +124,22 @@ 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)
Expand Down
7 changes: 6 additions & 1 deletion src/gluonts/time_feature/seasonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,26 @@

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,
"QE": 4,
}


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
23 changes: 23 additions & 0 deletions test/core/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,26 @@ def test_serde_method():
def test_np_str_dtype():
a = np.array(["foo"])
serde.decode(serde.encode(a.dtype)) == a.dtype


@pytest.mark.parametrize(
"obj",
[
{"__kind__": 42, "class": cls_str}
for cls_str in [
"builtins.eval",
"builtins.exec",
"builtins.compile",
"builtins.open",
"builtins.input",
"eval",
"exec",
"compile",
"open",
"input",
]
],
)
def test_decode_disallow(obj):
with pytest.raises(ValueError):
serde.decode(obj)
12 changes: 12 additions & 0 deletions test/time_feature/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
28 changes: 28 additions & 0 deletions test/time_feature/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

import pandas as pd
from packaging.version import Version

if Version(pd.__version__) <= Version("2.2.0"):
S = "S"
H = "H"
M = "M"
Q = "Q"
Y = "A"
else:
S = "s"
H = "h"
M = "ME"
Q = "QE"
Y = "YE"
1 change: 0 additions & 1 deletion test/time_feature/test_agg_lags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import pytest

from gluonts.dataset.common import ListDataset

from gluonts.dataset.field_names import FieldName
from gluonts.transform import AddAggregateLags

Expand Down
26 changes: 14 additions & 12 deletions test/time_feature/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.

import pytest
from pandas.tseries.frequencies import to_offset

from gluonts.time_feature import norm_freq_str

from .common import M, Q, S, Y

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("Q").name) == "Q"
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("S").name) == "S"
@pytest.mark.parametrize(
" aliases, normalized_freq_str",
[
(["Y", "YS", "A", "AS"], Y),
(["Q", "QS"], Q),
(["M", "MS"], M),
(["S"], S),
],
)
def test_norm_freq_str(aliases, normalized_freq_str):
for alias in aliases:
assert norm_freq_str(to_offset(alias).name) == normalized_freq_str
1 change: 0 additions & 1 deletion test/time_feature/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import pytest

from gluonts import zebras as zb

from gluonts.time_feature import (
Constant,
TimeFeature,
Expand Down
Loading
Loading