-
Notifications
You must be signed in to change notification settings - Fork 319
/
test_mstumped.py
215 lines (156 loc) · 7.47 KB
/
test_mstumped.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
import numpy as np
import numpy.testing as npt
import pandas as pd
from stumpy import mstumped, config
import pytest
from dask.distributed import Client, LocalCluster
import naive
@pytest.fixture(scope="module")
def dask_cluster():
cluster = LocalCluster(n_workers=2, threads_per_worker=2)
yield cluster
cluster.close()
test_data = [
(np.array([[584, -11, 23, 79, 1001, 0, -19]], dtype=np.float64), 3),
(np.random.uniform(-1000, 1000, [5, 20]).astype(np.float64), 5),
]
substitution_locations = [slice(0, 0), 0, -1, slice(1, 3), [0, 3]]
def test_mstumped_int_input(dask_cluster):
with pytest.raises(TypeError):
with Client(dask_cluster) as dask_client:
mstumped(dask_client, np.arange(20).reshape(2, 10), 5)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
def test_mstumped(T, m, dask_cluster):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T, m)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
def test_mstumped_include(T, m, dask_cluster):
with Client(dask_cluster) as dask_client:
for width in range(T.shape[0]):
for i in range(T.shape[0] - width):
include = np.asarray(range(i, i + width + 1))
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone, include)
comp_P, comp_I = mstumped(dask_client, T, m, include)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
def test_mstumped_discords(T, m, dask_cluster):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone, discords=True)
comp_P, comp_I = mstumped(dask_client, T, m, discords=True)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
def test_mstumped_include_discords(T, m, dask_cluster):
with Client(dask_cluster) as dask_client:
for width in range(T.shape[0]):
for i in range(T.shape[0] - width):
include = np.asarray(range(i, i + width + 1))
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone, include, discords=True)
comp_P, comp_I = mstumped(dask_client, T, m, include, discords=True)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
def test_mstumped_df(T, m, dask_cluster):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone)
df = pd.DataFrame(T.T)
comp_P, comp_I = mstumped(dask_client, df, m)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
def test_mstumped_constant_subsequence_self_join(dask_cluster):
with Client(dask_cluster) as dask_client:
T_A = np.concatenate(
(np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64))
)
T = np.array([T_A, T_A, np.random.rand(T_A.shape[0])])
m = 3
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T, m)
npt.assert_almost_equal(ref_P, comp_P) # ignore indices
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
def test_mstumped_identical_subsequence_self_join(dask_cluster):
with Client(dask_cluster) as dask_client:
identical = np.random.rand(8)
T_A = np.random.rand(20)
T_A[1 : 1 + identical.shape[0]] = identical
T_A[11 : 11 + identical.shape[0]] = identical
T = np.array([T_A, T_A, np.random.rand(T_A.shape[0])])
m = 3
excl_zone = int(np.ceil(m / 4))
ref_P, ref_I = naive.mstump(T, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T, m)
npt.assert_almost_equal(
ref_P, comp_P, decimal=config.STUMPY_TEST_PRECISION
) # ignore indices
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
@pytest.mark.parametrize("substitution_location", substitution_locations)
def test_mstumped_one_subsequence_inf_self_join_first_dimension(
T, m, substitution_location, dask_cluster
):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
T_sub = T.copy()
T_sub[0, substitution_location] = np.inf
ref_P, ref_I = naive.mstump(T_sub, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T_sub, m)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
@pytest.mark.parametrize("substitution_location", substitution_locations)
def test_mstumped_one_subsequence_inf_self_join_all_dimensions(
T, m, substitution_location, dask_cluster
):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
T_sub = T.copy()
T_sub[:, substitution_location] = np.inf
ref_P, ref_I = naive.mstump(T_sub, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T_sub, m)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
@pytest.mark.parametrize("substitution_location", substitution_locations)
def test_mstumped_one_subsequence_nan_self_join_first_dimension(
T, m, substitution_location, dask_cluster
):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
T_sub = T.copy()
T_sub[0, substitution_location] = np.nan
ref_P, ref_I = naive.mstump(T_sub, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T_sub, m)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T, m", test_data)
@pytest.mark.parametrize("substitution_location", substitution_locations)
def test_mstumped_one_subsequence_nan_self_join_all_dimensions(
T, m, substitution_location, dask_cluster
):
with Client(dask_cluster) as dask_client:
excl_zone = int(np.ceil(m / 4))
T_sub = T.copy()
T_sub[:, substitution_location] = np.nan
ref_P, ref_I = naive.mstump(T_sub, m, excl_zone)
comp_P, comp_I = mstumped(dask_client, T_sub, m)
npt.assert_almost_equal(ref_P, comp_P)
npt.assert_almost_equal(ref_I, comp_I)