Skip to content

Commit

Permalink
adding monthly seasonality
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosler committed Feb 13, 2025
1 parent e58e2cd commit 1e0061e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
28 changes: 19 additions & 9 deletions ads/opctl/operator/lowcode/forecast/model/prophet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

# Copyright (c) 2024 Oracle and/or its affiliates.
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

import logging
Expand Down Expand Up @@ -43,7 +43,11 @@ def _add_unit(num, unit):
def _fit_model(data, params, additional_regressors):
from prophet import Prophet

monthly_seasonality = params.pop("monthly_seasonality", False)
model = Prophet(**params)
if monthly_seasonality:
model.add_seasonality(name="monthly", period=30.5, fourier_order=5)
params["monthly_seasonality"] = monthly_seasonality
for add_reg in additional_regressors:
model.add_regressor(add_reg)
model.fit(data)
Expand Down Expand Up @@ -256,7 +260,7 @@ def _generate_report(self):
self.outputs[s_id], include_legend=True
),
series_ids=series_ids,
target_category_column=self.target_cat_col
target_category_column=self.target_cat_col,
)
section_1 = rc.Block(
rc.Heading("Forecast Overview", level=2),
Expand All @@ -269,7 +273,7 @@ def _generate_report(self):
sec2 = _select_plot_list(
lambda s_id: self.models[s_id].plot_components(self.outputs[s_id]),
series_ids=series_ids,
target_category_column=self.target_cat_col
target_category_column=self.target_cat_col,
)
section_2 = rc.Block(
rc.Heading("Forecast Broken Down by Trend Component", level=2), sec2
Expand All @@ -285,7 +289,7 @@ def _generate_report(self):
sec3 = _select_plot_list(
lambda s_id: sec3_figs[s_id],
series_ids=series_ids,
target_category_column=self.target_cat_col
target_category_column=self.target_cat_col,
)
section_3 = rc.Block(rc.Heading("Forecast Changepoints", level=2), sec3)

Expand All @@ -299,7 +303,9 @@ def _generate_report(self):
pd.Series(
m.seasonalities,
index=pd.Index(m.seasonalities.keys(), dtype="object"),
name=s_id if self.target_cat_col else self.original_target_column,
name=s_id
if self.target_cat_col
else self.original_target_column,
dtype="object",
)
)
Expand Down Expand Up @@ -330,11 +336,15 @@ def _generate_report(self):
self.formatted_local_explanation = aggregate_local_explanations

if not self.target_cat_col:
self.formatted_global_explanation = self.formatted_global_explanation.rename(
{"Series 1": self.original_target_column},
axis=1,
self.formatted_global_explanation = (
self.formatted_global_explanation.rename(
{"Series 1": self.original_target_column},
axis=1,
)
)
self.formatted_local_explanation.drop(
"Series", axis=1, inplace=True
)
self.formatted_local_explanation.drop("Series", axis=1, inplace=True)

# Create a markdown section for the global explainability
global_explanation_section = rc.Block(
Expand Down
27 changes: 27 additions & 0 deletions tests/operators/forecast/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,33 @@ def test_pandas_to_historical_test(model):
print(test_metrics)


# CostAD
@pytest.mark.parametrize("model", ["prophet", "neuralprophet"])
def test_pandas_to_historical_test(model):
df = pd.read_csv(f"{DATASET_PREFIX}dataset5.csv")
df_train = df[:-1]
df_test = df[-1:]

with tempfile.TemporaryDirectory() as tmpdirname:
output_data_path = f"{tmpdirname}/results"
yaml_i = deepcopy(TEMPLATE_YAML)
yaml_i["spec"]["model"] = model
yaml_i["spec"]["historical_data"].pop("url")
yaml_i["spec"]["historical_data"]["data"] = df_train
yaml_i["spec"]["test_data"] = {"data": df_test}
yaml_i["spec"]["target_column"] = "Y"
yaml_i["spec"]["datetime_column"]["name"] = DATETIME_COL
yaml_i["spec"]["horizon"] = 1
yaml_i["spec"]["output_directory"]["url"] = output_data_path
if model == "automlx":
yaml_i["spec"]["model_kwargs"] = {"time_budget": 2}
operator_config = ForecastOperatorConfig.from_dict(yaml_i)
forecast_operate(operator_config)
check_output_for_errors(output_data_path)
test_metrics = pd.read_csv(f"{output_data_path}/metrics.csv")
print(test_metrics)


def check_output_for_errors(output_data_path):
# try:
# List files in the directory
Expand Down

0 comments on commit 1e0061e

Please sign in to comment.