forked from pycaret/pycaret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_time_series_plots.py
384 lines (306 loc) · 11.5 KB
/
test_time_series_plots.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
"""Module to test time_series plotting functionality
"""
import os
import sys
import numpy as np # type: ignore
import pytest
from time_series_test_utils import (
_ALL_PLOTS_DATA,
_ALL_PLOTS_ESTIMATOR,
_ALL_PLOTS_ESTIMATOR_NOT_DATA,
_return_all_plots_estimator_ts_results,
_return_data_with_without_period_index,
_return_model_names_for_plots_stats,
)
from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
pytestmark = pytest.mark.filterwarnings("ignore::UserWarning")
os.environ["PYCARET_TESTING"] = "1"
if sys.platform == "win32":
pytest.skip("Skipping test module on Windows", allow_module_level=True)
##############################
# 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.
_data_with_without_period_index = _return_data_with_without_period_index()
_model_names_for_plots = _return_model_names_for_plots_stats()
_all_plots_estimator_ts_results = _return_all_plots_estimator_ts_results()
############################
# Functions End Here ####
############################
##########################
# Tests Start Here ####
##########################
@pytest.mark.parametrize("data", _data_with_without_period_index)
@pytest.mark.parametrize("plot", _ALL_PLOTS_DATA)
def test_plot_model_data(data, plot):
"""Tests the plot_model functionality on original dataset
NOTE: Want to show multiplicative plot here so can not take data with negative values
"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
######################
# OOP Approach ####
######################
exp.setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
exp.plot_model(plot=plot)
########################
# Functional API ####
########################
from pycaret.time_series import plot_model, setup
_ = setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="expanding",
session_id=42,
n_jobs=-1,
)
plot_model(plot=plot)
@pytest.mark.parametrize("model_name", _model_names_for_plots)
@pytest.mark.parametrize("data", _data_with_without_period_index)
@pytest.mark.parametrize("plot", _ALL_PLOTS_ESTIMATOR)
def test_plot_model_estimator(model_name, data, plot):
"""Tests the plot_model functionality on estimators
NOTE: Want to show multiplicative plot here so can not take data with negative values
"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
######################
# OOP Approach ####
######################
exp.setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
model = exp.create_model(model_name)
exp.plot_model(estimator=model, plot=plot)
########################
# Functional API ####
########################
from pycaret.time_series import create_model, plot_model, setup
_ = setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="expanding",
session_id=42,
n_jobs=-1,
)
model = create_model(model_name)
plot_model(estimator=model, plot=plot)
@pytest.mark.parametrize("plot", _ALL_PLOTS_ESTIMATOR_NOT_DATA)
def test_plot_model_data_raises(load_pos_and_neg_data, plot):
"""Tests the plot_model functionality when it raises an exception
on data plots (i.e. estimator is not passed)
"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
######################
# OOP Approach ####
######################
exp.setup(
data=load_pos_and_neg_data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
with pytest.raises(ValueError) as errmsg:
# Some code that produces a value error
exp.plot_model(plot=plot)
# Capture Error message
exceptionmsg = errmsg.value.args[0]
# Check exact error received
assert (
f"Plot type '{plot}' is not supported when estimator is not provided"
in exceptionmsg
)
@pytest.mark.parametrize("data", _data_with_without_period_index)
def test_plot_model_customization(data):
"""Tests the customization of plot_model
NOTE: Want to show multiplicative plot here so can not take data with negative values
"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
exp.setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
model = exp.create_model("naive")
#######################
# Customization ####
#######################
print("\n\n==== Testing Customization ON DATA ====")
exp.plot_model(
plot="pacf",
data_kwargs={
"nlags": 36,
},
fig_kwargs={"fig_size": [800, 500], "fig_template": "simple_white"},
)
exp.plot_model(plot="decomp_classical", data_kwargs={"type": "multiplicative"})
print("\n\n==== Testing Customization ON ESTIMATOR ====")
exp.plot_model(estimator=model, plot="forecast", data_kwargs={"fh": 24})
@pytest.mark.parametrize("data", _data_with_without_period_index)
@pytest.mark.parametrize("plot", _ALL_PLOTS_DATA)
def test_plot_model_return_data_original_data(data, plot):
"""Tests whether the return_data parameter of the plot_model function works
properly or not for the original data
"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
exp.setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
plot_data = exp.plot_model(plot=plot, return_data=True)
# If plot is successful, it will return a dictionary
# If plot is not possible (e.g. decomposition without index), then it will return None
assert isinstance(plot_data, dict) or plot_data is None
@pytest.mark.parametrize("data", _data_with_without_period_index)
@pytest.mark.parametrize("model_name", _model_names_for_plots)
@pytest.mark.parametrize("plot", _ALL_PLOTS_ESTIMATOR)
def test_plot_model_return_data_estimator(data, model_name, plot):
"""Tests whether the return_data parameter of the plot_model function works
properly or not for the estimator
"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
exp.setup(
data=data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
model = exp.create_model(model_name)
plot_data = exp.plot_model(estimator=model, plot=plot, return_data=True)
# If plot is successful, it will return a dictionary
# If plot is not possible (e.g. decomposition without index), then it will return None
assert isinstance(plot_data, dict) or plot_data is None
@pytest.mark.parametrize("plot, all_models_supported", _all_plots_estimator_ts_results)
def test_plot_multiple_model_overlays(
load_pos_and_neg_data, plot, all_models_supported
):
"""Tests the plot_model functionality on estimators where the results from
multiple models get overlaid (time series plots)
Checks:
(1) Plots are correct even when the multiple models are of the same type
(2) Plot labels are correct when user provides custom labels
(3) When some models do not support certain plots, they are dropped appropriately
(4) When some models do not support certain plots, they are dropped appropriately
even when user provides custom labels
(5) When user provides custom labels, the number of labels must match number of models
"""
data = load_pos_and_neg_data
exp = TSForecastingExperiment()
fh = 12
fold = 2
exp.setup(data=data, fh=fh, fold=fold, fold_strategy="sliding")
# Model that produces insample predictions
m1 = exp.create_model("exp_smooth")
# Check 1: Even if same model type is passed, the plot should make overlays ----
models = [m1, m1]
fig_data = exp.plot_model(models, plot=plot, return_data=True)
assert fig_data.get("overlay_data").shape[1] == len(models)
# Check 2: User specified labels are used in plots
labels = ["Model 1", "Model 2"]
fig_data = exp.plot_model(
models,
plot=plot,
data_kwargs={"labels": labels},
return_data=True,
)
assert fig_data.get("overlay_data").shape[1] == len(models)
assert np.all(fig_data.get("overlay_data").columns.to_list() == labels)
if not all_models_supported:
# Model that does not produce insample predictions
m2 = exp.create_model("lr_cds_dt")
# Check 3: If Model does not produce insample predictions, it should be excluded
models = [m1, m2, m1]
fig_data = exp.plot_model(models, plot=plot, return_data=True)
assert fig_data.get("overlay_data").shape[1] == len(models) - 1
# Check 4: If Model does not produce insample predictions, custom labels should exclude it.
labels = ["Model 1", "Model 2", "Model 3"]
fig_data = exp.plot_model(
models,
plot=plot,
data_kwargs={"labels": labels},
return_data=True,
)
assert fig_data.get("overlay_data").shape[1] == len(models) - 1
labels.remove("Model 2")
assert np.all(fig_data.get("overlay_data").columns.to_list() == labels)
# Check 5: When user provides custom labels, the number of labels must match
# number of models
models = [m1, m1]
# (A) Less labels than models ----
labels = ["Model 1"]
with pytest.raises(ValueError) as errmsg:
fig_data = exp.plot_model(models, plot=plot, data_kwargs={"labels": labels})
# Capture Error message
exceptionmsg = errmsg.value.args[0]
# Check exact error received
assert (
"Please provide a label corresponding to each model to proceed." in exceptionmsg
)
# (B) More labels than models ----
labels = ["Model 1", "Model 2", "Model 3"]
with pytest.raises(ValueError) as errmsg:
fig_data = exp.plot_model(models, plot=plot, data_kwargs={"labels": labels})
# Capture Error message
exceptionmsg = errmsg.value.args[0]
# Check exact error received
assert (
"Please provide a label corresponding to each model to proceed." in exceptionmsg
)
def test_plot_final_model_exo():
"""Tests running plot model after running finalize_model when exogenous
variables are present. Fix for https://github.com/pycaret/pycaret/issues/3565
"""
data = get_data("uschange")
target = "Consumption"
FH = 3
train = data.iloc[: int(len(data) - FH)]
test = data.iloc[int(len(data)) - FH :]
test = test.drop(columns=[target], axis=1)
exp = TSForecastingExperiment()
exp.setup(data=train, target=target, fh=FH, session_id=42)
model = exp.create_model("arima")
final_model = exp.finalize_model(model)
# Previous issue coming from renderer resolution due to X
# This should not give an error (passing X explicitly)
exp.plot_model(final_model, data_kwargs={"X": test})
# Also, plotting without explicit passing X should also pass
exp.plot_model()