Skip to content

Commit f1a8eb2

Browse files
committed
add tests
1 parent 8dbc9aa commit f1a8eb2

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

runtime/tests/automl_runtime/forecast/pmdarima/training_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ def test_fit_success_with_exogenous(self):
7272
results_pd = arima_estimator.fit(self.df_with_exogenous)
7373
self.assertIn("smape", results_pd)
7474
self.assertIn("pickled_model", results_pd)
75+
76+
def test_fit_success_with_split_cutoff(self):
77+
arima_estimator = ArimaEstimator(horizon=1,
78+
frequency_unit="d",
79+
metric="smape",
80+
seasonal_periods=[1, 7],
81+
num_folds=2,
82+
split_cutoff=pd.Timestamp('2020-07-17 00:00:00'))
83+
results_pd = arima_estimator.fit(self.df)
84+
self.assertIn("smape", results_pd)
85+
self.assertIn("pickled_model", results_pd)
7586

7687
def test_fit_skip_too_long_seasonality(self):
7788
arima_estimator = ArimaEstimator(horizon=1,

runtime/tests/automl_runtime/forecast/prophet/forecast_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,34 @@ def test_training_with_extra_regressors(self):
140140
model_json = json.loads(results["model_json"][0])
141141
self.assertListEqual(model_json["extra_regressors"][0], ["f1", "f2"])
142142

143+
def test_training_with_split_cutoff(self):
144+
hyperopt_estim = ProphetHyperoptEstimator(horizon=1,
145+
frequency_unit="d",
146+
metric="smape",
147+
interval_width=0.8,
148+
country_holidays="US",
149+
search_space=self.search_space,
150+
num_folds=2,
151+
trial_timeout=1000,
152+
random_state=0,
153+
is_parallel=False,
154+
regressors=["f1", "f2"],
155+
split_cutoff=pd.Timestamp('2020-07-10 00:00:00'))
156+
157+
for df in [self.df, self.df_datetime_date, self.df_string_time]:
158+
results = hyperopt_estim.fit(df)
159+
self.assertAlmostEqual(results["mse"][0], 0)
160+
self.assertAlmostEqual(results["rmse"][0], 0, delta=1e-6)
161+
self.assertAlmostEqual(results["mae"][0], 0, delta=1e-6)
162+
self.assertAlmostEqual(results["mape"][0], 0)
163+
self.assertAlmostEqual(results["mdape"][0], 0)
164+
self.assertAlmostEqual(results["smape"][0], 0)
165+
self.assertAlmostEqual(results["coverage"][0], 1)
166+
# check the best result parameter is inside the search space
167+
model_json = json.loads(results["model_json"][0])
168+
self.assertGreaterEqual(model_json["changepoint_prior_scale"], 0.1)
169+
self.assertLessEqual(model_json["changepoint_prior_scale"], 0.5)
170+
143171
@patch("databricks.automl_runtime.forecast.prophet.forecast.fmin")
144172
@patch("databricks.automl_runtime.forecast.prophet.forecast.Trials")
145173
@patch("databricks.automl_runtime.forecast.prophet.forecast.partial")

runtime/tests/automl_runtime/forecast/utils_test.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from databricks.automl_runtime.forecast import DATE_OFFSET_KEYWORD_MAP
2323
from databricks.automl_runtime.forecast.utils import \
2424
generate_cutoffs, get_validation_horizon, calculate_period_differences, \
25-
is_frequency_consistency, make_future_dataframe, make_single_future_dataframe
25+
is_frequency_consistency, make_future_dataframe, make_single_future_dataframe, \
26+
generate_custom_cutoffs
2627

2728

2829
class TestGetValidationHorizon(unittest.TestCase):
@@ -177,6 +178,71 @@ def test_generate_cutoffs_success_annualy(self):
177178
self.assertEqual([pd.Timestamp('2018-07-14 00:00:00'), pd.Timestamp('2019-07-14 00:00:00'), pd.Timestamp('2020-07-14 00:00:00')], cutoffs)
178179

179180

181+
class TestTestGenerateCustomCutoffs(unittest.TestCase):
182+
def test_generate_custom_cutoffs_success_hourly(self):
183+
df = pd.DataFrame(
184+
pd.date_range(start="2020-07-01", periods=168, freq='h'), columns=["ds"]
185+
).rename_axis("y").reset_index()
186+
expected_cutoffs = [pd.Timestamp('2020-07-07 13:00:00'),
187+
pd.Timestamp('2020-07-07 14:00:00'),
188+
pd.Timestamp('2020-07-07 15:00:00'),
189+
pd.Timestamp('2020-07-07 16:00:00')]
190+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="H", split_cutoff=pd.Timestamp('2020-07-07 13:00:00'))
191+
self.assertEqual(expected_cutoffs, cutoffs)
192+
193+
def test_generate_custom_cutoffs_success_daily(self):
194+
df = pd.DataFrame(
195+
pd.date_range(start="2020-07-01", end="2020-08-30", freq='d'), columns=["ds"]
196+
).rename_axis("y").reset_index()
197+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="D", split_cutoff=pd.Timestamp('2020-08-21 00:00:00'))
198+
self.assertEqual([pd.Timestamp('2020-08-21 12:00:00'), pd.Timestamp('2020-08-22 00:00:00'), pd.Timestamp('2020-08-23 00:00:00')], cutoffs)
199+
200+
def test_generate_custom_cutoffs_success_weekly(self):
201+
df = pd.DataFrame(
202+
pd.date_range(start="2020-07-01", periods=52, freq='W'), columns=["ds"]
203+
).rename_axis("y").reset_index()
204+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="W", split_cutoff=pd.Timestamp('2021-04-25 00:00:00'))
205+
self.assertEqual([pd.Timestamp('2021-04-25 00:00:00'), pd.Timestamp('2021-05-02 00:00:00'), pd.Timestamp('2021-05-09 00:00:00')], cutoffs)
206+
207+
def test_generate_custom_cutoffs_success_monthly(self):
208+
df = pd.DataFrame(
209+
pd.date_range(start="2020-01-12", periods=24, freq=pd.DateOffset(months=1)), columns=["ds"]
210+
).rename_axis("y").reset_index()
211+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="MS", split_cutoff=pd.Timestamp('2021-03-12 00:00:00'))
212+
self.assertEqual([pd.Timestamp('2021-03-12 00:00:00'), pd.Timestamp('2021-04-12 00:00:00'), pd.Timestamp('2021-05-12 00:00:00')], cutoffs)
213+
214+
def test_generate_custom_cutoffs_success_quaterly(self):
215+
df = pd.DataFrame(
216+
pd.date_range(start="2020-07-12", periods=9, freq=pd.DateOffset(months=3)), columns=["ds"]
217+
).rename_axis("y").reset_index()
218+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="QS", split_cutoff=pd.Timestamp('2021-07-12 00:00:00'))
219+
self.assertEqual([pd.Timestamp('2021-07-12 00:00:00'), pd.Timestamp('2022-10-12 00:00:00')], cutoffs)
220+
221+
def test_generate_custom_cutoffs_success_annualy(self):
222+
df = pd.DataFrame(
223+
pd.date_range(start="2012-07-14", periods=10, freq=pd.DateOffset(years=1)), columns=["ds"]
224+
).rename_axis("y").reset_index()
225+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="YS", split_cutoff=pd.Timestamp('2012-07-14 00:00:00'))
226+
self.assertEqual([pd.Timestamp('2012-07-14 00:00:00'), pd.Timestamp('2013-07-14 00:00:00'), pd.Timestamp('2014-07-14 00:00:00')], cutoffs)
227+
228+
def test_generate_custom_cutoffs_success_with_small_gaps(self):
229+
df = pd.DataFrame(
230+
pd.date_range(start="2020-07-01", periods=30, freq='3d'), columns=["ds"]
231+
).rename_axis("y").reset_index()
232+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="D", split_cutoff=pd.Timestamp('2020-09-17 00:00:00'))
233+
self.assertEqual([pd.Timestamp('2020-09-17 00:00:00'),
234+
pd.Timestamp('2020-09-18 00:00:00'),
235+
pd.Timestamp('2020-09-19 00:00:00')], cutoffs)
236+
237+
def test_generate_custom_cutoffs_success_with_large_gaps(self):
238+
df = pd.DataFrame(
239+
pd.date_range(start="2020-07-01", periods=30, freq='9d'), columns=["ds"]
240+
).rename_axis("y").reset_index()
241+
cutoffs = generate_custom_cutoffs(df, horizon=7, unit="D", split_cutoff=pd.Timestamp('2021-03-10 00:00:00'))
242+
self.assertEqual([pd.Timestamp('2021-03-10 00:00:00'),
243+
pd.Timestamp('2021-03-12 00:00:00')], cutoffs)
244+
245+
180246
class TestCalculatePeriodsAndFrequency(unittest.TestCase):
181247
def setUp(self) -> None:
182248
return super().setUp()

0 commit comments

Comments
 (0)