-
Notifications
You must be signed in to change notification settings - Fork 141
/
test_series_name.py
244 lines (218 loc) · 7.86 KB
/
test_series_name.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
"Check if the series name or column name is correctly kept."
import sys
import numpy as np
import pandas as pd
import pytest
from sklearn.cluster import KMeans
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import LocalOutlierFactor
import adtk.detector as detector
import adtk.transformer as transformer
from adtk._base import _TrainableModel
from adtk._detector_base import ( # _NonTrainableMultivariateDetector,
_NonTrainableUnivariateDetector,
_TrainableMultivariateDetector,
_TrainableUnivariateDetector,
)
_Detector = (
_NonTrainableUnivariateDetector,
# _NonTrainableMultivariateDetector,
_TrainableUnivariateDetector,
_TrainableMultivariateDetector,
)
# We have 4 types of models
# - one-to-one: input a univariate series, output a univariate series
# - one-to-many: input a univariate series, output a multivariate series
# - many-to-one: input a multivariate series, output a univariate series
# - many-to-many: input a multivariate series, output a multivariate series
one2one_models = [
detector.ThresholdAD(),
detector.QuantileAD(),
detector.InterQuartileRangeAD(),
detector.GeneralizedESDTestAD(),
detector.PersistAD(window=10),
detector.LevelShiftAD(window=10),
detector.VolatilityShiftAD(window=10),
detector.AutoregressionAD(),
detector.SeasonalAD(freq=2),
transformer.RollingAggregate(window=10, agg="median"),
transformer.RollingAggregate(
window=10, agg="quantile", agg_params={"q": 0.5}
),
transformer.DoubleRollingAggregate(window=10, agg="median"),
transformer.DoubleRollingAggregate(
window=10, agg="quantile", agg_params={"q": [0.1, 0.5, 0.9]}
),
transformer.DoubleRollingAggregate(
window=10, agg="hist", agg_params={"bins": [30, 50, 70]}
),
transformer.StandardScale(),
transformer.ClassicSeasonalDecomposition(freq=2),
]
one2many_models = [
transformer.RollingAggregate(
window=10, agg="quantile", agg_params={"q": [0.1, 0.5, 0.9]}
),
transformer.RollingAggregate(
window=10, agg="hist", agg_params={"bins": [20, 50, 80]}
),
transformer.Retrospect(n_steps=3),
]
many2one_models = [
detector.MinClusterDetector(KMeans(n_clusters=2)),
detector.OutlierDetector(
LocalOutlierFactor(n_neighbors=20, contamination=0.1)
),
detector.RegressionAD(target="A", regressor=LinearRegression()),
detector.PcaAD(),
transformer.SumAll(),
transformer.RegressionResidual(target="A", regressor=LinearRegression()),
transformer.PcaReconstructionError(),
]
@pytest.mark.parametrize("model", one2one_models)
def test_one2one_s2s_w_name(model):
"""
if a one-to-one model is applied to a Series, it should keep the Series
name unchanged
"""
s_name = pd.Series(
np.arange(100),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
name="A",
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(s_name)
else:
result = model.predict(s_name)
assert result.name == "A"
@pytest.mark.parametrize("model", one2one_models)
def test_one2one_s2s_wo_name(model):
"""
if a one-to-one model is applied to a Series, it should keep the Series
name unchanged
"""
s_no_name = pd.Series(
np.arange(100),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(s_no_name)
else:
result = model.predict(s_no_name)
assert result.name is None
@pytest.mark.parametrize("model", one2one_models)
def test_one2one_df2df(model):
"""
if a one-to-one model is applied to a DataFrame, it should keep the column
names unchanged
"""
df = pd.DataFrame(
np.arange(300).reshape(100, 3),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
columns=["A", "B", "C"],
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(df)
else:
result = model.predict(df)
assert list(result.columns) == ["A", "B", "C"]
@pytest.mark.parametrize("model", one2one_models)
def test_one2one_df2list(model):
"""
if a one-to-one model (detector) is applied to a DataFrame and returns a
dict, the output dict keys should match the input column names
"""
if isinstance(model, _Detector):
df = pd.DataFrame(
np.arange(300).reshape(100, 3),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
columns=["A", "B", "C"],
)
if isinstance(model, _TrainableModel):
result = model.fit_detect(df, return_list=True)
else:
result = model.detect(df, return_list=True)
if sys.version_info[1] >= 6:
assert list(result.keys()) == ["A", "B", "C"]
else:
assert set(result.keys()) == {"A", "B", "C"}
@pytest.mark.parametrize("model", one2many_models)
def test_one2many_s2df_w_name(model):
"""
if a one-to-many model is applied to a Series, the output should not have
prefix in column names, no matter whether the input Series has a name.
"""
s_name = pd.Series(
np.arange(100),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
name="A",
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(s_name)
else:
result = model.predict(s_name)
assert all([col[:2] != "A_" for col in result.columns])
@pytest.mark.parametrize("model", one2many_models)
def test_one2many_s2df_wo_name(model):
"""
if a one-to-many model is applied to a Series, the output should not have
prefix in column names, no matter whether the input Series has a name.
"""
s_no_name = pd.Series(
np.arange(100),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(s_no_name)
else:
result = model.predict(s_no_name)
assert all([col[:2] != "A_" for col in result.columns])
@pytest.mark.parametrize("model", one2many_models)
def test_one2many_df2df(model):
"""
if a one-to-many model is applied to a DataFrame, the output should have
prefix in column names to indicate the input columns they correspond.
"""
df = pd.DataFrame(
np.arange(300).reshape(100, 3),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
columns=["A", "B", "C"],
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(df)
else:
result = model.predict(df)
n_cols = round(len(result.columns) / 3)
assert all([col[:2] == "A_" for col in result.columns[:n_cols]])
assert all([col[2:4] != "A_" for col in result.columns[:n_cols]])
assert all(
[col[:2] == "B_" for col in result.columns[n_cols : 2 * n_cols]]
)
assert all(
[col[2:4] != "B_" for col in result.columns[n_cols : 2 * n_cols]]
)
assert all([col[:2] == "C_" for col in result.columns[2 * n_cols :]])
assert all([col[2:4] != "C_" for col in result.columns[2 * n_cols :]])
@pytest.mark.parametrize("model", many2one_models)
def test_many2one(model):
"""
The output Series from a many-to-one model should NOT have name
"""
df = pd.DataFrame(
np.arange(300).reshape(100, 3),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
columns=["A", "B", "C"],
)
if isinstance(model, _TrainableModel):
result = model.fit_predict(df)
else:
result = model.predict(df)
assert result.name is None
def test_pca_reconstruction():
df = pd.DataFrame(
np.arange(300).reshape(100, 3),
index=pd.date_range(start="2017-1-1", periods=100, freq="D"),
columns=["A", "B", "C"],
)
result = transformer.PcaReconstruction(k=2).fit_predict(df)
assert list(result.columns) == ["A", "B", "C"]