forked from pycaret/pycaret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_time_series_utils_forecasting_pipeline.py
418 lines (345 loc) · 15.1 KB
/
test_time_series_utils_forecasting_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""Module to test time_series forecasting pipeline utils
"""
import numpy as np
import pytest
from sktime.forecasting.naive import NaiveForecaster
from pycaret.time_series import TSForecastingExperiment
from pycaret.utils.time_series.forecasting.models import DummyForecaster
from pycaret.utils.time_series.forecasting.pipeline import (
_add_model_to_pipeline,
_are_pipeline_tansformations_empty,
_get_imputed_data,
_transformations_present_X,
_transformations_present_y,
)
pytestmark = pytest.mark.filterwarnings("ignore::UserWarning")
##############################
# Functions Start Here ####
##############################
# NOTE: Fixtures can not be used to parameterize tests
# https://stackoverflow.com/questions/52764279/pytest-how-to-parametrize-a-test-with-a-list-that-is-returned-from-a-fixture
# Hence, we have to create functions and create the parameterized list first
# (must happen during collect phase) before passing it to mark.parameterize.
############################
# Functions End Here ####
############################
##########################
# Tests Start Here ####
##########################
def test_get_imputed_data_noexo(load_pos_data_missing):
"""Tests _get_imputed_data WITHOUT exogenous variables"""
y = load_pos_data_missing
exp = TSForecastingExperiment()
FH = 12
###################################
# 1: Missing Values Present ####
###################################
# Due to imputation, the imputed values will not be same as original
# 1A: Missing Values Present: Only Imputation Steps in Pipeline ----
exp.setup(data=y, fh=FH, numeric_imputation_target="drift")
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y)
assert not np.array_equal(y_imputed, y)
assert X_imputed is None
# 1B: Missing Values Present: Imputation + Other Steps in Pipeline ----
y_imputed_expected = y_imputed.copy()
exp.setup(
data=y,
fh=FH,
numeric_imputation_target="drift",
transform_target="exp",
)
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y)
assert not np.array_equal(y_imputed, y)
assert np.array_equal(y_imputed, y_imputed_expected)
assert X_imputed is None
#######################################
# 2: Missing Values not Present ####
#######################################
# There are no missing values, so imputation should return original values
y_no_miss = y.copy()
y_no_miss.fillna(10, inplace=True)
# 2A: Missing Values not Present: No imputation step in Pipeline ----
exp.setup(data=y_no_miss, fh=FH)
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y_no_miss)
assert np.array_equal(y_imputed, y_no_miss)
assert X_imputed is None
# 2B: Missing Values not Present: Only Imputation Steps in Pipeline ----
exp.setup(data=y_no_miss, fh=FH, numeric_imputation_target="drift")
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y_no_miss)
assert np.array_equal(y_imputed, y_no_miss)
assert X_imputed is None
# 2C: Missing Values not Present: Imputation + Other Steps in Pipeline ----
exp.setup(
data=y_no_miss,
fh=FH,
numeric_imputation_target="drift",
transform_target="exp",
)
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y_no_miss)
assert np.array_equal(y_imputed, y_no_miss)
assert X_imputed is None
def test_get_imputed_data_exo(load_uni_exo_data_target_missing):
"""Tests _get_imputed_data WITH exogenous variables"""
data, target = load_uni_exo_data_target_missing
y = data[target]
X = data.drop(columns=target)
exp = TSForecastingExperiment()
FH = 12
###################################
# 1: Missing Values Present ####
###################################
# Due to imputation, the imputed values will not be same as original
# 1A: Missing Values Present: Only Imputation Steps in Pipeline ----
exp.setup(
data=data,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
numeric_imputation_exogenous="drift",
)
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y, X=X)
assert not np.array_equal(y_imputed, y)
assert not X_imputed.equals(X)
# 1B: Missing Values Present: Imputation + Other Steps in Pipeline ----
y_imputed_expected = y_imputed.copy()
exp.setup(
data=data,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
numeric_imputation_exogenous="drift",
transform_target="exp",
transform_exogenous="exp",
)
y_imputed, X_imputed = _get_imputed_data(pipeline=exp.pipeline, y=y, X=X)
assert not np.array_equal(y_imputed, y)
assert np.array_equal(y_imputed, y_imputed_expected)
assert not X_imputed.equals(X)
#######################################
# 2: Missing Values not Present ####
#######################################
# There are no missing values, so imputation should return original values
data_no_miss = data.copy()
data_no_miss.fillna(10, inplace=True)
y_no_miss = data_no_miss[target]
X_no_miss = data_no_miss.drop(columns=target)
# 2A: Missing Values not Present: No imputation step in Pipeline ----
exp.setup(data=data_no_miss, target=target, fh=FH, seasonal_period=4)
y_imputed, X_imputed = _get_imputed_data(
pipeline=exp.pipeline, y=y_no_miss, X=X_no_miss
)
assert np.array_equal(y_imputed, y_no_miss)
assert X_imputed.equals(X_no_miss)
# 2B: Missing Values not Present: Only Imputation Steps in Pipeline ----
exp.setup(
data=data_no_miss,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
numeric_imputation_exogenous="drift",
)
y_imputed, X_imputed = _get_imputed_data(
pipeline=exp.pipeline, y=y_no_miss, X=X_no_miss
)
assert np.array_equal(y_imputed, y_no_miss)
assert X_imputed.equals(X_no_miss)
# 2C: Missing Values not Present: Imputation + Other Steps in Pipeline ----
exp.setup(
data=data_no_miss,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
numeric_imputation_exogenous="drift",
transform_target="exp",
transform_exogenous="exp",
)
y_imputed, X_imputed = _get_imputed_data(
pipeline=exp.pipeline, y=y_no_miss, X=X_no_miss
)
assert np.array_equal(y_imputed, y_no_miss)
assert X_imputed.equals(X_no_miss)
def test_are_pipeline_tansformations_empty_noexo(load_pos_data_missing):
"""Tests _are_pipeline_tansformations_empty, _transformations_present_X, and
_transformations_present_y WITHOUT exogenous variables"""
y = load_pos_data_missing
y_no_miss = y.copy()
y_no_miss.fillna(10, inplace=True)
exp = TSForecastingExperiment()
FH = 12
###############################
# 1: Not Empty Pipeline ####
###############################
# 1A: Data has missing values ----
exp.setup(data=y, fh=FH, numeric_imputation_target="drift")
assert not _transformations_present_X(pipeline=exp.pipeline)
assert _transformations_present_y(pipeline=exp.pipeline)
assert not _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
# 1B: Data has no missing values, but y impute step added ----
# Even though data has no missing values, imputation step is added as user has requested
exp.setup(data=y_no_miss, fh=FH, numeric_imputation_target="drift")
assert not _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
###########################
# 2: Empty Pipeline ####
###########################
# 2A: No Imputation in Pipeline ----
exp.setup(data=y_no_miss, fh=FH)
assert not _transformations_present_X(pipeline=exp.pipeline)
assert not _transformations_present_y(pipeline=exp.pipeline)
assert _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
def test_are_pipeline_tansformations_empty_exo(load_uni_exo_data_target_missing):
"""Tests _are_pipeline_tansformations_empty, _transformations_present_X, and
_transformations_present_y WITH exogenous variables"""
data, target = load_uni_exo_data_target_missing
data_no_miss = data.copy()
data_no_miss.fillna(10, inplace=True)
exp = TSForecastingExperiment()
FH = 12
###############################
# 1: Not Empty Pipeline ####
###############################
# 1A: Both y and X have missing values ----
exp.setup(
data=data,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
numeric_imputation_exogenous="drift",
)
assert _transformations_present_X(pipeline=exp.pipeline)
assert _transformations_present_y(pipeline=exp.pipeline)
assert not _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
# 1B: Data has no missing values, but y impute step added ----
# Even though data has no missing values, imputation step y is added as user has requested
exp.setup(
data=data_no_miss,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
)
assert not _transformations_present_X(pipeline=exp.pipeline)
assert _transformations_present_y(pipeline=exp.pipeline)
assert not _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
# 1C: Data has no missing values, but X impute step added ----
# Even though data has no missing values, imputation step X is added as user has requested
exp.setup(
data=data_no_miss,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_exogenous="drift",
)
assert _transformations_present_X(pipeline=exp.pipeline)
assert not _transformations_present_y(pipeline=exp.pipeline)
assert not _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
###########################
# 2: Empty Pipeline ####
###########################
# 2A: No Imputation in Pipeline ----
exp.setup(data=data_no_miss, target=target, fh=FH, seasonal_period=4)
assert not _transformations_present_X(pipeline=exp.pipeline)
assert not _transformations_present_y(pipeline=exp.pipeline)
assert _are_pipeline_tansformations_empty(pipeline=exp.pipeline)
def test_add_model_to_pipeline_noexo(load_pos_and_neg_data):
"""Tests _add_model_to_pipeline WITHOUT exogenous variables"""
y = load_pos_and_neg_data
exp = TSForecastingExperiment()
FH = 12
model = NaiveForecaster()
###########################
# 1: Empty Pipeline ####
###########################
exp.setup(data=y, fh=FH)
# Check that the final model has changed ----
assert isinstance(exp.pipeline.steps[-1][1].steps[-1][1], DummyForecaster)
pipeline = _add_model_to_pipeline(pipeline=exp.pipeline, model=model)
assert isinstance(pipeline.steps[-1][1].steps[-1][1], NaiveForecaster)
# Check that the steps for X in the Forecasting Pipeline have not changed ----
for i in np.arange(len(exp.pipeline.steps_)):
assert exp.pipeline.steps_[i][1].__class__ is pipeline.steps_[i][1].__class__
# Check that the steps for y in the Forecasting Pipeline have not changed ----
tgt_fcst_org = exp.pipeline.steps_[-1][1]
tgt_fcst_new = pipeline.steps_[-1][1]
# Check except last step which has been checked above (Dummy vs Naive)
for i in np.arange(len(tgt_fcst_org.steps_) - 1):
assert (
tgt_fcst_org.steps_[i][1].__class__ is tgt_fcst_new.steps_[i][1].__class__
)
###############################
# 2: Not Empty Pipeline ####
###############################
exp.setup(data=y, fh=FH, numeric_imputation_target="drift")
# Check that the final model has changed ----
assert isinstance(exp.pipeline.steps[-1][1].steps[-1][1], DummyForecaster)
pipeline = _add_model_to_pipeline(pipeline=exp.pipeline, model=model)
assert isinstance(pipeline.steps[-1][1].steps[-1][1], NaiveForecaster)
# Check that the steps for X in the Forecasting Pipeline have not changed ----
for i in np.arange(len(exp.pipeline.steps_)):
assert exp.pipeline.steps_[i][1].__class__ is pipeline.steps_[i][1].__class__
# Check that the steps for y in the Forecasting Pipeline have not changed ----
tgt_fcst_org = exp.pipeline.steps_[-1][1]
tgt_fcst_new = pipeline.steps_[-1][1]
# Check except last step which has been checked above (Dummy vs Naive)
for i in np.arange(len(tgt_fcst_org.steps_) - 1):
assert (
tgt_fcst_org.steps_[i][1].__class__ is tgt_fcst_new.steps_[i][1].__class__
)
def test_add_model_to_pipeline_exo(load_uni_exo_data_target):
"""Tests _add_model_to_pipeline WITH exogenous variables"""
data, target = load_uni_exo_data_target
exp = TSForecastingExperiment()
FH = 12
model = NaiveForecaster()
###########################
# 1: Empty Pipeline ####
###########################
exp.setup(
data=data,
target=target,
fh=FH,
seasonal_period=4,
)
# Check that the final model has changed ----
assert isinstance(exp.pipeline.steps[-1][1].steps[-1][1], DummyForecaster)
pipeline = _add_model_to_pipeline(pipeline=exp.pipeline, model=model)
assert isinstance(pipeline.steps[-1][1].steps[-1][1], NaiveForecaster)
# Check that the steps for X in the Forecasting Pipeline have not changed ----
for i in np.arange(len(exp.pipeline.steps_)):
assert exp.pipeline.steps_[i][1].__class__ is pipeline.steps_[i][1].__class__
# Check that the steps for y in the Forecasting Pipeline have not changed ----
tgt_fcst_org = exp.pipeline.steps_[-1][1]
tgt_fcst_new = pipeline.steps_[-1][1]
# Check except last step which has been checked above (Dummy vs Naive)
for i in np.arange(len(tgt_fcst_org.steps_) - 1):
assert isinstance(tgt_fcst_org.steps_[i][1], tgt_fcst_new.steps_[i][1])
###############################
# 2: Not Empty Pipeline ####
###############################
exp.setup(
data=data,
target=target,
fh=FH,
seasonal_period=4,
numeric_imputation_target="drift",
numeric_imputation_exogenous="drift",
)
# Check that the final model has changed ----
assert isinstance(exp.pipeline.steps[-1][1].steps[-1][1], DummyForecaster)
pipeline = _add_model_to_pipeline(pipeline=exp.pipeline, model=model)
assert isinstance(pipeline.steps[-1][1].steps[-1][1], NaiveForecaster)
# Check that the steps for X in the Forecasting Pipeline have not changed ----
for i in np.arange(len(exp.pipeline.steps_)):
assert exp.pipeline.steps_[i][1].__class__ is pipeline.steps_[i][1].__class__
# Check that the steps for y in the Forecasting Pipeline have not changed ----
tgt_fcst_org = exp.pipeline.steps_[-1][1]
tgt_fcst_new = pipeline.steps_[-1][1]
# Check except last step which has been checked above (Dummy vs Naive)
for i in np.arange(len(tgt_fcst_org.steps_) - 1):
assert (
tgt_fcst_org.steps_[i][1].__class__ is tgt_fcst_new.steps_[i][1].__class__
)