Skip to content

Commit

Permalink
condense code
Browse files Browse the repository at this point in the history
  • Loading branch information
remyogasawara committed Aug 22, 2023
1 parent 70e34c8 commit 09e31f2
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,34 @@ def _detrend_on_fly(X, y):
relative_maxima = _get_rel_max_from_acf(y_detrended)
return relative_maxima

# def set_period(
# self,
# X: pd.DataFrame,
# y: pd.Series,
# acf_threshold: float = 0.01,
# rel_max_order: int = 5,
# ):
# """Function to set the component's seasonal period based on the target's seasonality.

# Args:
# X (pandas.DataFrame): The feature data of the time series problem.
# y (pandas.Series): The target data of a time series problem.
# acf_threshold (float) : The threshold for the autocorrelation function to determine the period. Any values below
# the threshold are considered to be 0 and will not be considered for the period. Defaults to 0.01.
# rel_max_order (int) : The order of the relative maximum to determine the period. Defaults to 5.

# """
# self.periods = {}
# if len(y.columns) == 1:
# self.period = self.determine_periodicity(X, y, acf_threshold, rel_max_order)
# self.update_parameters({"period": self.period})
# self.periods[id] = self.period
# return
# else:
# for id in y.columns:
# self.periods[id] = self.determine_periodicity(X, y[id], acf_threshold, rel_max_order)
# self.update_parameters({"periods": self.periods})

def set_period(
self,
X: pd.DataFrame,
Expand Down Expand Up @@ -356,9 +384,6 @@ def plot_decomposition(
fig, axs = plt.subplots(4)
fig.set_size_inches(18.5, 14.5)

for ax in axs:
ax.cla()

if len(y.columns) > 1:
results = decomposition_results[id]
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ def __init__(
series_id: str = None,
degree: int = 1, # Currently unused.
period: int = None,
periods: dict = None,
seasonal_smoother: int = 7,
random_seed: int = 0,
**kwargs,
):
self.logger = logging.getLogger(__name__)
self.series_id = series_id
self.periods = periods
# Programmatically adjust seasonal_smoother to fit underlying STL requirements,
# that seasonal_smoother must be odd.
if seasonal_smoother % 2 == 0:
Expand All @@ -64,6 +66,7 @@ def __init__(
parameters = {
"degree": degree,
"period": period,
"periods": periods,
"seasonal_smoother": seasonal_smoother,
"time_index": time_index,
"series_id": series_id,
Expand Down Expand Up @@ -189,10 +192,22 @@ def fit(
self.frequency = y.index.freqstr or pd.infer_freq(y.index)
# Iterate through each id group
self.seasonals = {}
self.periods = {}
self.seasonalities = {}
self.trends = {}
self.residuals = {}
self.periods = {}

# # Determine the period of the seasonal component
# # Set the period if it is single series and period is given
# if self.period is not None and len(y.columns) == 1:
# self.periods = {0: self.period}
# # Set periods if it is single series and period is
# if self.periods is None or self.period is None:
# self.set_period(X, y)

# if self.period is None:
# self.set_period(X, y)

for id in y.columns:
series_y = y[id]

Expand Down Expand Up @@ -347,14 +362,13 @@ def inverse_transform(
if isinstance(y_t, pd.Series):
y_t = y_t.to_frame()

index = self._choose_proper_index(y_t)
y = []
for id in y_t.columns:
y_in_sample = pd.Series([])
y_out_of_sample = pd.Series([])
series_y = y_t[id]

index = self._choose_proper_index(series_y)

if len(y_t.columns) > 1:
old_trend = self.trends[id]
old_seasonal = self.seasonals[id]
Expand Down Expand Up @@ -454,13 +468,9 @@ def get_trend_dataframe(self, X, y):

def _decompose_target(X, y, fh, trend, seasonal, residual, period, id):
"""Function to generate a single DataFrame with trend, seasonality and residual components."""
if len(y.index) == len(trend.index) and all(
if len(y.index) != len(trend.index) or not all(
y.index == trend.index,
):
trend = trend
seasonal = seasonal
residual = residual
else:
# TODO: Do a better job cloning.
decomposer = STLDecomposer(
seasonal_smoother=self.seasonal_smoother,
Expand Down
Loading

0 comments on commit 09e31f2

Please sign in to comment.