-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathtest_mpdist.py
More file actions
227 lines (184 loc) · 6.88 KB
/
test_mpdist.py
File metadata and controls
227 lines (184 loc) · 6.88 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
import functools
import naive
import numpy as np
import numpy.testing as npt
import pytest
from dask.distributed import Client, LocalCluster
from stumpy.mpdist import _mpdist_vect, mpdist, mpdisted
@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.scheduler_address
cluster.close(timeout=60)
test_data = [
(
np.array([9, 8100, -60, 7], dtype=np.float64),
np.array([584, -11, 23, 79, 1001, 0, -19], dtype=np.float64),
),
(
np.random.uniform(-1000, 1000, [8]).astype(np.float64),
np.random.uniform(-1000, 1000, [64]).astype(np.float64),
),
]
percentage = [0.25, 0.5, 0.75]
k = [0, 1, 2, 3, 4]
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mpdist_vect(T_A, T_B):
m = 3
ref_mpdist_vect = naive.mpdist_vect(T_A, T_B, m)
Q_subseq_isconstant = naive.rolling_isconstant(T_A, m)
T_subseq_isconstant = naive.rolling_isconstant(T_B, m)
μ_Q, σ_Q = naive.compute_mean_std(T_A, m)
M_T, Σ_T = naive.compute_mean_std(T_B, m)
comp_mpdist_vect = _mpdist_vect(
T_A, T_B, m, μ_Q, σ_Q, M_T, Σ_T, Q_subseq_isconstant, T_subseq_isconstant
)
npt.assert_almost_equal(ref_mpdist_vect, comp_mpdist_vect)
@pytest.mark.parametrize("T_A, T_B", test_data)
@pytest.mark.parametrize("percentage", percentage)
def test_mpdist_vect_percentage(T_A, T_B, percentage):
m = 3
ref_mpdist_vect = naive.mpdist_vect(T_A, T_B, m, percentage=percentage)
Q_subseq_isconstant = naive.rolling_isconstant(T_A, m)
T_subseq_isconstant = naive.rolling_isconstant(T_B, m)
μ_Q, σ_Q = naive.compute_mean_std(T_A, m)
M_T, Σ_T = naive.compute_mean_std(T_B, m)
comp_mpdist_vect = _mpdist_vect(
T_A,
T_B,
m,
μ_Q,
σ_Q,
M_T,
Σ_T,
Q_subseq_isconstant,
T_subseq_isconstant,
percentage=percentage,
)
npt.assert_almost_equal(ref_mpdist_vect, comp_mpdist_vect)
@pytest.mark.parametrize("T_A, T_B", test_data)
@pytest.mark.parametrize("k", k)
def test_mpdist_vect_k(T_A, T_B, k):
m = 3
ref_mpdist_vect = naive.mpdist_vect(T_A, T_B, m, k=k)
Q_subseq_isconstant = naive.rolling_isconstant(T_A, m)
T_subseq_isconstant = naive.rolling_isconstant(T_B, m)
μ_Q, σ_Q = naive.compute_mean_std(T_A, m)
M_T, Σ_T = naive.compute_mean_std(T_B, m)
comp_mpdist_vect = _mpdist_vect(
T_A, T_B, m, μ_Q, σ_Q, M_T, Σ_T, Q_subseq_isconstant, T_subseq_isconstant, k=k
)
npt.assert_almost_equal(ref_mpdist_vect, comp_mpdist_vect)
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mpdist(T_A, T_B):
m = 3
ref_mpdist = naive.mpdist(T_A, T_B, m)
comp_mpdist = mpdist(T_A, T_B, m)
npt.assert_almost_equal(ref_mpdist, comp_mpdist)
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mpdist_with_isconstant(T_A, T_B):
m = 3
T_A_subseq_isconstant = np.random.choice(
[True, False], size=len(T_A) - m + 1, replace=True
)
T_B_subseq_isconstant = np.random.choice(
[True, False], size=len(T_B) - m + 1, replace=True
)
ref_mpdist = naive.mpdist(
T_A,
T_B,
m,
T_A_subseq_isconstant=T_A_subseq_isconstant,
T_B_subseq_isconstant=T_B_subseq_isconstant,
)
comp_mpdist = mpdist(
T_A,
T_B,
m,
T_A_subseq_isconstant=T_A_subseq_isconstant,
T_B_subseq_isconstant=T_B_subseq_isconstant,
)
npt.assert_almost_equal(ref_mpdist, comp_mpdist)
@pytest.mark.parametrize("T_A, T_B", test_data)
@pytest.mark.parametrize("percentage", percentage)
def test_mpdist_percentage(T_A, T_B, percentage):
m = 3
ref_mpdist = naive.mpdist(T_A, T_B, m, percentage=percentage)
comp_mpdist = mpdist(T_A, T_B, m, percentage=percentage)
npt.assert_almost_equal(ref_mpdist, comp_mpdist)
@pytest.mark.parametrize("T_A, T_B", test_data)
@pytest.mark.parametrize("k", k)
def test_mpdist_k(T_A, T_B, k):
m = 3
ref_mpdist = naive.mpdist(T_A, T_B, m, k=k)
comp_mpdist = mpdist(T_A, T_B, m, k=k)
npt.assert_almost_equal(ref_mpdist, comp_mpdist)
@pytest.mark.filterwarnings("ignore:numpy.dtype size changed")
@pytest.mark.filterwarnings("ignore:numpy.ufunc size changed")
@pytest.mark.filterwarnings("ignore:numpy.ndarray size changed")
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mpdisted(T_A, T_B, dask_cluster):
with Client(dask_cluster) as dask_client:
m = 3
ref_mpdist = naive.mpdist(T_A, T_B, m)
comp_mpdist = mpdisted(dask_client, T_A, T_B, m)
npt.assert_almost_equal(ref_mpdist, comp_mpdist)
@pytest.mark.filterwarnings("ignore:numpy.dtype size changed")
@pytest.mark.filterwarnings("ignore:numpy.ufunc size changed")
@pytest.mark.filterwarnings("ignore:numpy.ndarray size changed")
@pytest.mark.filterwarnings("ignore:\\s+Port 8787 is already in use:UserWarning")
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mpdisted_with_isconstant(T_A, T_B, dask_cluster):
with Client(dask_cluster) as dask_client:
m = 3
T_A_subseq_isconstant = np.random.choice(
[True, False], size=len(T_A) - m + 1, replace=True
)
T_B_subseq_isconstant = np.random.choice(
[True, False], size=len(T_B) - m + 1, replace=True
)
ref_mpdist = naive.mpdist(
T_A,
T_B,
m,
T_A_subseq_isconstant=T_A_subseq_isconstant,
T_B_subseq_isconstant=T_B_subseq_isconstant,
)
comp_mpdist = mpdisted(
dask_client,
T_A,
T_B,
m,
T_A_subseq_isconstant=T_A_subseq_isconstant,
T_B_subseq_isconstant=T_B_subseq_isconstant,
)
npt.assert_almost_equal(ref_mpdist, comp_mpdist)
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mpdist_vect_with_isconstant(T_A, T_B):
m = 3
isconstant_custom_func = functools.partial(
naive.isconstant_func_stddev_threshold, quantile_threshold=0.05
)
T_A_subseq_isconstant = naive.rolling_isconstant(T_A, m, isconstant_custom_func)
T_B_subseq_isconstant = naive.rolling_isconstant(T_B, m, isconstant_custom_func)
ref_mpdist_vect = naive.mpdist_vect(
T_A,
T_B,
m,
T_A_subseq_isconstant=T_A_subseq_isconstant,
T_B_subseq_isconstant=T_B_subseq_isconstant,
)
Q_subseq_isconstant = T_A_subseq_isconstant
T_subseq_isconstant = T_B_subseq_isconstant
μ_Q, σ_Q = naive.compute_mean_std(T_A, m)
M_T, Σ_T = naive.compute_mean_std(T_B, m)
comp_mpdist_vect = _mpdist_vect(
T_A, T_B, m, μ_Q, σ_Q, M_T, Σ_T, Q_subseq_isconstant, T_subseq_isconstant
)
npt.assert_almost_equal(ref_mpdist_vect, comp_mpdist_vect)