forked from pycaret/pycaret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
152 lines (117 loc) · 4.65 KB
/
conftest.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
import numpy as np
import pytest
import pycaret.anomaly.functional
import pycaret.classification.functional
import pycaret.clustering.functional
import pycaret.regression.functional
import pycaret.time_series.forecasting.functional
from pycaret.containers.models.time_series import get_all_model_containers
from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
#############################
# Fixtures Start Here ####
#############################
@pytest.fixture(name="change_test_dir", autouse=True)
def change_test_dir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
@pytest.fixture(autouse=True)
def reset_experiments():
yield
pycaret.classification.functional._CURRENT_EXPERIMENT = None
pycaret.regression.functional._CURRENT_EXPERIMENT = None
pycaret.anomaly.functional._CURRENT_EXPERIMENT = None
pycaret.clustering.functional._CURRENT_EXPERIMENT = None
pycaret.time_series.forecasting.functional._CURRENT_EXPERIMENT = None
@pytest.fixture(scope="session", name="load_pos_data")
def load_pos_data():
"""Load Pycaret Airline dataset."""
data = get_data("airline")
return data
@pytest.fixture(scope="session", name="load_pos_and_neg_data")
def load_pos_and_neg_data():
"""Load Pycaret Airline dataset (with some negative values)."""
data = get_data("airline")
data = data - 400 # simulate negative values
return data
@pytest.fixture(scope="session", name="load_uni_exo_data_target")
def load_uni_exo_data_target():
"""Load Pycaret Univariate data with exogenous variables."""
data = get_data("uschange")
target = "Consumption"
return data, target
@pytest.fixture(scope="session", name="load_uni_exo_data_target_positive")
def load_uni_exo_data_target_positive():
"""Load Pycaret Univariate data with exogenous variables (strictly positive)."""
data = get_data("uschange")
data = data.clip(lower=0.1)
target = "Consumption"
return data, target
@pytest.fixture(scope="session", name="load_pos_data_missing")
def load_pos_data_missing():
"""Load Pycaret Airline dataset (with missing values)."""
data = get_data("airline")
remove_n = int(0.4 * len(data))
np.random.seed(42)
na_indices = np.random.choice(data.index, remove_n, replace=False)
data[na_indices] = np.nan
return data
@pytest.fixture(scope="session", name="load_pos_and_neg_data_missing")
def load_pos_and_neg_data_missing():
"""Load Pycaret Airline dataset (with some negative & missing values)."""
data = get_data("airline")
data = data - 400 # simulate negative values
data[10:20] = np.nan # In train with FH = 12
data[-5:-2] = np.nan # In test with FH = 12
return data
@pytest.fixture(scope="session", name="load_uni_exo_data_target_missing")
def load_uni_exo_data_target_missing():
"""Load Pycaret Univariate data with exogenous variables & missing values."""
data = get_data("uschange")
data[10:20] = np.nan # In train with FH = 12
data[-5:-2] = np.nan # In test with FH = 12
target = "Consumption"
return data, target
@pytest.fixture(scope="session", name="load_models_uni_exo")
def load_models_uni_exo():
"""Load models that support univariate date with exogenous variables."""
# TODO: Later, get this dynamically from sktime
models = ["arima", "lr_cds_dt"]
return models
@pytest.fixture(scope="session", name="load_models_uni_mix_exo_noexo")
def load_models_uni_mix_exo_noexo():
"""Load a sample mix of models that support univariate date with
exogenous variables and those that do not."""
# TODO: Later, get this dynamically from sktime
models = ["naive", "ets", "arima", "lr_cds_dt"]
return models
@pytest.fixture(scope="session", name="load_setup")
def load_setup(load_pos_and_neg_data):
"""Create a TSForecastingExperiment to test module functionalities"""
exp = TSForecastingExperiment()
fh = np.arange(1, 13)
fold = 2
return exp.setup(
data=load_pos_and_neg_data,
fh=fh,
fold=fold,
fold_strategy="sliding",
verbose=False,
session_id=42,
)
@pytest.fixture(scope="session", name="load_models")
def load_ts_models(load_setup):
"""Load all time series module models"""
exp = load_setup
model_containers = get_all_model_containers(exp)
from time_series_test_utils import ( # TODO Put it back once preprocessing supports series as X
_BLEND_TEST_MODELS,
)
ts_estimators = [
exp.create_model(key)
for key in model_containers.keys()
if key in _BLEND_TEST_MODELS
]
return ts_estimators
###########################
# Fixtures End Here ####
###########################