-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathtest_ostinato.py
More file actions
272 lines (219 loc) · 9.17 KB
/
test_ostinato.py
File metadata and controls
272 lines (219 loc) · 9.17 KB
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
import functools
import naive
import numpy as np
import numpy.testing as npt
import pytest
from dask.distributed import Client, LocalCluster
import stumpy
@pytest.fixture(scope="module")
def dask_cluster():
cluster = LocalCluster(
n_workers=2,
threads_per_worker=2,
dashboard_address=None,
worker_dashboard_address=None,
)
yield cluster
cluster.close()
@pytest.mark.parametrize(
"seed", np.random.choice(np.arange(10000), size=25, replace=False)
)
def test_random_ostinato(seed):
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = stumpy.ostinato(Ts, m)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
@pytest.mark.parametrize("seed", [79, 109, 112, 133, 151, 161, 251, 275, 309, 355])
def test_deterministic_ostinato(seed):
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = stumpy.ostinato(Ts, m)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
@pytest.mark.parametrize(
"seed", np.random.choice(np.arange(10000), size=25, replace=False)
)
def test_random_ostinatoed(seed, dask_cluster):
with Client(dask_cluster) as dask_client:
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = stumpy.ostinatoed(
dask_client, Ts, m
)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
@pytest.mark.parametrize("seed", [79, 109, 112, 133, 151, 161, 251, 275, 309, 355])
def test_deterministic_ostinatoed(seed, dask_cluster):
with Client(dask_cluster) as dask_client:
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(Ts, m)
comp_radius, comp_Ts_idx, comp_subseq_idx = stumpy.ostinatoed(
dask_client, Ts, m
)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
@pytest.mark.parametrize(
"seed", np.random.choice(np.arange(10000), size=25, replace=False)
)
def test_random_ostinato_with_isconstant(seed):
isconstant_custom_func = functools.partial(
naive.isconstant_func_stddev_threshold, quantile_threshold=0.05
)
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
Ts_subseq_isconstant = [isconstant_custom_func for _ in range(len(Ts))]
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(
Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant
)
comp_radius, comp_Ts_idx, comp_subseq_idx = stumpy.ostinato(
Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant
)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
@pytest.mark.parametrize("seed", [79, 109, 112, 133, 151, 161, 251, 275, 309, 355])
def test_deterministic_ostinatoed_with_isconstant(seed, dask_cluster):
isconstant_custom_func = functools.partial(
naive.isconstant_func_stddev_threshold, quantile_threshold=0.05
)
with Client(dask_cluster) as dask_client:
m = 50
np.random.seed(seed)
Ts = [np.random.rand(n) for n in [64, 128, 256]]
l = 64 - m + 1
subseq_isconsant = np.full(l, 0, dtype=bool)
subseq_isconsant[np.random.randint(0, l)] = True
Ts_subseq_isconstant = [
subseq_isconsant,
None,
isconstant_custom_func,
]
ref_radius, ref_Ts_idx, ref_subseq_idx = naive.ostinato(
Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant
)
comp_radius, comp_Ts_idx, comp_subseq_idx = stumpy.ostinatoed(
dask_client, Ts, m, Ts_subseq_isconstant=Ts_subseq_isconstant
)
npt.assert_almost_equal(ref_radius, comp_radius)
npt.assert_almost_equal(ref_Ts_idx, comp_Ts_idx)
npt.assert_almost_equal(ref_subseq_idx, comp_subseq_idx)
def test_input_not_overwritten_ostinato():
# ostinato preprocesses its input, a list of time series,
# by replacing nan value with 0 in each time series.
# This test ensures that the original input is not overwritten
m = 50
Ts = [np.random.rand(n) for n in [64, 128, 256]]
for T in Ts:
T[0] = np.nan
# raise error if ostinato overwrite its input
Ts_input = [T.copy() for T in Ts]
stumpy.ostinato(Ts_input, m)
for i in range(len(Ts)):
T_ref = Ts[i]
T_comp = Ts_input[i]
npt.assert_almost_equal(T_ref[np.isfinite(T_ref)], T_comp[np.isfinite(T_comp)])
def test_extract_several_consensus_ostinato():
# This test is to further ensure that the function `ostinato`
# does not tamper with the original data.
Ts = [np.random.rand(n) for n in [256, 512, 1024]]
Ts_ref = [T.copy() for T in Ts]
Ts_comp = [T.copy() for T in Ts]
m = 20
k = 5 # Get the first `k` consensus motifs
for _ in range(k):
# Find consensus motif and its NN in each time series in Ts_comp
# Remove them from Ts_comp as well as Ts_ref, and assert that the
# two time series are the same
radius, Ts_idx, subseq_idx = stumpy.ostinato(Ts_comp, m)
consensus_motif = Ts_comp[Ts_idx][subseq_idx : subseq_idx + m].copy()
for i in range(len(Ts_comp)):
if i == Ts_idx:
query_idx = subseq_idx
else:
query_idx = None
idx = np.argmin(
stumpy.core.mass(consensus_motif, Ts_comp[i], query_idx=query_idx)
)
Ts_comp[i][idx : idx + m] = np.nan
Ts_ref[i][idx : idx + m] = np.nan
npt.assert_almost_equal(
Ts_ref[i][np.isfinite(Ts_ref[i])], Ts_comp[i][np.isfinite(Ts_comp[i])]
)
def test_input_not_overwritten_ostinatoed(dask_cluster):
# ostinatoed preprocesses its input, a list of time series,
# by replacing nan value with 0 in each time series.
# This test ensures that the original input is not overwritten
with Client(dask_cluster) as dask_client:
m = 50
Ts = [np.random.rand(n) for n in [64, 128, 256]]
for T in Ts:
T[0] = np.nan
# raise error if ostinato overwrite its input
Ts_input = [T.copy() for T in Ts]
stumpy.ostinatoed(dask_client, Ts_input, m)
for i in range(len(Ts)):
T_ref = Ts[i]
T_comp = Ts_input[i]
npt.assert_almost_equal(
T_ref[np.isfinite(T_ref)], T_comp[np.isfinite(T_comp)]
)
def test_extract_several_consensus_ostinatoed(dask_cluster):
# This test is to further ensure that the function `ostinatoed`
# does not tamper with the original data.
Ts = [np.random.rand(n) for n in [256, 512, 1024]]
Ts_ref = [T.copy() for T in Ts]
Ts_comp = [T.copy() for T in Ts]
m = 20
with Client(dask_cluster) as dask_client:
k = 5 # Get the first `k` consensus motifs
for _ in range(k):
# Find consensus motif and its NN in each time series in Ts_comp
# Remove them from Ts_comp as well as Ts_ref, and assert that the
# two time series are the same
radius, Ts_idx, subseq_idx = stumpy.ostinatoed(dask_client, Ts_comp, m)
consensus_motif = Ts_comp[Ts_idx][subseq_idx : subseq_idx + m].copy()
for i in range(len(Ts_comp)):
if i == Ts_idx:
query_idx = subseq_idx
else:
query_idx = None
idx = np.argmin(
stumpy.core.mass(consensus_motif, Ts_comp[i], query_idx=query_idx)
)
Ts_comp[i][idx : idx + m] = np.nan
Ts_ref[i][idx : idx + m] = np.nan
npt.assert_almost_equal(
Ts_ref[i][np.isfinite(Ts_ref[i])],
Ts_comp[i][np.isfinite(Ts_comp[i])],
)
@pytest.mark.filterwarnings(
"error:divide by zero encountered in divide", category=RuntimeWarning
)
def test_divide_by_zero_ostinato():
Ts = [np.random.rand(n) for n in [64, 128, 256]]
m = 5
Ts[0][:m] = np.nan
stumpy.ostinato(Ts, m)
@pytest.mark.filterwarnings(
"error:divide by zero encountered in divide", category=RuntimeWarning
)
def test_divide_by_zero_ostinatoed(dask_cluster):
Ts = [np.random.rand(n) for n in [64, 128, 256]]
m = 5
Ts[0][:m] = np.nan
with Client(dask_cluster) as dask_client:
stumpy.ostinatoed(dask_client, Ts, m)