forked from stumpy-dev/stumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_core.py
More file actions
1973 lines (1498 loc) · 58.7 KB
/
test_core.py
File metadata and controls
1973 lines (1498 loc) · 58.7 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import math
import os
from unittest.mock import patch
import naive
import numpy as np
import numpy.testing as npt
import pandas as pd
import pytest
from numba import cuda
from scipy.spatial.distance import cdist
from stumpy import config, core
from stumpy.stump import stump
if cuda.is_available():
@cuda.jit("(f8[:, :], f8[:], i8[:], i8, b1, i8[:])")
def _gpu_searchsorted_kernel(a, v, bfs, nlevel, is_left, idx):
# A wrapper kernel for calling device function _gpu_searchsorted_left/right.
i = cuda.grid(1)
if i < a.shape[0]:
if is_left:
idx[i] = core._gpu_searchsorted_left(a[i], v[i], bfs, nlevel)
else:
idx[i] = core._gpu_searchsorted_right(a[i], v[i], bfs, nlevel)
try:
from numba.errors import NumbaPerformanceWarning
except ModuleNotFoundError:
from numba.core.errors import NumbaPerformanceWarning
TEST_THREADS_PER_BLOCK = 10
def naive_rolling_window_dot_product(Q, T):
window = len(Q)
result = np.zeros(len(T) - window + 1)
for i in range(len(result)):
result[i] = np.dot(T[i : i + window], Q)
return result
def naive_compute_mean_std_multidimensional(T, m):
n = T.shape[1]
nrows, ncols = T.shape
cumsum_T = np.empty((nrows, ncols + 1))
np.cumsum(T, axis=1, out=cumsum_T[:, 1:]) # store output in cumsum_T[1:]
cumsum_T[:, 0] = 0
cumsum_T_squared = np.empty((nrows, ncols + 1))
np.cumsum(np.square(T), axis=1, out=cumsum_T_squared[:, 1:])
cumsum_T_squared[:, 0] = 0
subseq_sum_T = cumsum_T[:, m:] - cumsum_T[:, : n - m + 1]
subseq_sum_T_squared = cumsum_T_squared[:, m:] - cumsum_T_squared[:, : n - m + 1]
M_T = subseq_sum_T / m
Σ_T = np.abs((subseq_sum_T_squared / m) - np.square(M_T))
Σ_T = np.sqrt(Σ_T)
return M_T, Σ_T
def naive_idx_to_mp(I, T, m, normalize=True, p=2.0, T_subseq_isconstant=None):
I = I.astype(np.int64)
T = T.copy()
if normalize:
if T_subseq_isconstant is None:
T_subseq_isconstant = naive.rolling_isconstant(T, m)
T_isfinite = np.isfinite(T)
T_subseq_isfinite = np.all(core.rolling_window(T_isfinite, m), axis=1)
T[~T_isfinite] = 0.0
T_subseqs = core.rolling_window(T, m)
nn_subseqs = T_subseqs[I]
if normalize:
P = naive.distance(
naive.z_norm(T_subseqs, axis=1), naive.z_norm(nn_subseqs, axis=1), axis=1
)
for i, nn_i in enumerate(I):
if T_subseq_isconstant[i] and T_subseq_isconstant[nn_i]:
P[i] = 0
elif T_subseq_isconstant[i] or T_subseq_isconstant[nn_i]:
P[i] = np.sqrt(m)
else: # pragma: no cover
pass
else:
P = naive.distance(T_subseqs, nn_subseqs, axis=1, p=p)
P[~T_subseq_isfinite] = np.inf
P[I < 0] = np.inf
return P
def split(node, out):
mid = len(node) // 2
out.append(node[mid])
return node[:mid], node[mid + 1 :]
def naive_bfs_indices(n, fill_value=None):
a = np.arange(n)
nodes = [a.tolist()]
out = []
while nodes:
tmp = []
for node in nodes:
for n in split(node, out):
if n:
tmp.append(n)
nodes = tmp
out = np.array(out)
if fill_value is not None:
remainder = out.shape[0]
level = 0
count = np.power(2, level)
while remainder >= count:
remainder -= count
level += 1
count = np.power(2, level)
if remainder > 0:
out = out[:-remainder]
last_level = np.empty(np.power(2, level), dtype=np.int64)
last_level[0::2] = out[-np.power(2, level - 1) :] - 1
last_level[1::2] = out[-np.power(2, level - 1) :] + 1
mask = np.isin(last_level, out)
last_level[mask] = fill_value
n = len(a)
last_level[last_level >= n] = fill_value
out = np.concatenate([out, last_level])
return out
test_data = [
(np.array([-1, 1, 2], dtype=np.float64), np.array(range(5), dtype=np.float64)),
(
np.array([9, 8100, -60], dtype=np.float64),
np.array([584, -11, 23, 79, 1001], dtype=np.float64),
),
(np.random.uniform(-1000, 1000, [8]), np.random.uniform(-1000, 1000, [64])),
]
n = list(range(1, 50))
def test_check_bad_dtype():
for dtype in [np.int32, np.int64, np.float32]:
with pytest.raises(TypeError):
core.check_dtype(np.random.rand(10).astype(dtype))
def test_check_dtype_float64():
assert core.check_dtype(np.random.rand(10))
def test_get_max_window_size():
for n in range(3, 10):
ref_max_m = (
int(
n
- math.floor(
(n + (config.STUMPY_EXCL_ZONE_DENOM - 1))
// (config.STUMPY_EXCL_ZONE_DENOM + 1)
)
)
- 1
)
cmp_max_m = core.get_max_window_size(n)
assert ref_max_m == cmp_max_m
def test_check_window_size():
for m in range(-1, 3):
with pytest.raises(ValueError):
core.check_window_size(m)
def test_check_max_window_size():
for m in range(4, 7):
with pytest.raises(ValueError):
core.check_window_size(m, max_size=3)
def test_check_window_size_excl_zone():
# To ensure warning is raised if there is at least one subsequence
# that has no non-trivial neighbor
T = np.random.rand(10)
m = 7
# For `len(T) == 10` and `m == 7`, the `excl_zone` is ceil(m / 4) = 2.
# In this case, there are `10 - 7 + 1 = 4` subsequences of length 7,
# starting at indices 0, 1, 2, and 3. For a subsequence that starts at
# index 1, there are no non-trivial neighbors. So, a warning should be
# raised.
with pytest.warns(UserWarning):
core.check_window_size(m, max_size=len(T), n=len(T))
@pytest.mark.parametrize("Q, T", test_data)
def test_sliding_dot_product(Q, T):
ref_mp = naive_rolling_window_dot_product(Q, T)
comp_mp = core.sliding_dot_product(Q, T)
npt.assert_almost_equal(ref_mp, comp_mp)
def test_welford_nanvar():
T = np.random.rand(64)
m = 10
ref_var = np.nanvar(T)
comp_var = core.welford_nanvar(T)
npt.assert_almost_equal(ref_var, comp_var)
ref_var = np.nanvar(core.rolling_window(T, m), axis=1)
comp_var = core.welford_nanvar(T, m)
npt.assert_almost_equal(ref_var, comp_var)
def test_welford_nanvar_catastrophic_cancellation():
T = np.array([4.0, 7.0, 13.0, 16.0, 10.0]) + 10**8
m = 4
ref_var = np.nanvar(core.rolling_window(T, m), axis=1)
comp_var = core.welford_nanvar(T, m)
npt.assert_almost_equal(ref_var, comp_var)
def test_welford_nanvar_nan():
T = np.random.rand(64)
m = 10
T[1] = np.nan
T[10] = np.nan
T[13:18] = np.nan
ref_var = np.nanvar(T)
comp_var = core.welford_nanvar(T)
npt.assert_almost_equal(ref_var, comp_var)
ref_var = np.nanvar(core.rolling_window(T, m), axis=1)
comp_var = core.welford_nanvar(T, m)
npt.assert_almost_equal(ref_var, comp_var)
def test_welford_nanstd():
T = np.random.rand(64)
m = 10
ref_var = np.nanstd(T)
comp_var = core.welford_nanstd(T)
npt.assert_almost_equal(ref_var, comp_var)
ref_var = np.nanstd(core.rolling_window(T, m), axis=1)
comp_var = core.welford_nanstd(T, m)
npt.assert_almost_equal(ref_var, comp_var)
def test_rolling_std_1d():
a = np.random.rand(64)
for w in range(3, 6):
ref_std = naive.rolling_nanstd(a, w)
# welford = False (default)
comp_std = core.rolling_nanstd(a, w)
npt.assert_almost_equal(ref_std, comp_std)
# welford = True
comp_std = core.rolling_nanstd(a, w, welford=True)
npt.assert_almost_equal(ref_std, comp_std)
def test_rolling_std_2d():
w = 5
for n_rows in range(1, 4):
a = np.random.rand(n_rows * 64).reshape(n_rows, 64)
ref_std = naive.rolling_nanstd(a, w)
# welford = False (default)
comp_std = core.rolling_nanstd(a, w)
npt.assert_almost_equal(ref_std, comp_std)
# welford = True
comp_std = core.rolling_nanstd(a, w, welford=True)
npt.assert_almost_equal(ref_std, comp_std)
def test_rolling_nanmin_1d():
T = np.random.rand(64)
for m in range(1, 12):
ref_min = np.nanmin(T)
comp_min = core._rolling_nanmin_1d(T)
npt.assert_almost_equal(ref_min, comp_min)
ref_min = np.nanmin(T)
comp_min = core._rolling_nanmin_1d(T)
npt.assert_almost_equal(ref_min, comp_min)
def test_rolling_nanmin():
T = np.random.rand(64)
for m in range(1, 12):
ref_min = np.nanmin(core.rolling_window(T, m), axis=1)
comp_min = core.rolling_nanmin(T, m)
npt.assert_almost_equal(ref_min, comp_min)
ref_min = np.nanmin(core.rolling_window(T, m), axis=1)
comp_min = core.rolling_nanmin(T, m)
npt.assert_almost_equal(ref_min, comp_min)
def test_rolling_nanmax_1d():
T = np.random.rand(64)
for m in range(1, 12):
ref_max = np.nanmax(T)
comp_max = core._rolling_nanmax_1d(T)
npt.assert_almost_equal(ref_max, comp_max)
ref_max = np.nanmax(T)
comp_max = core._rolling_nanmax_1d(T)
npt.assert_almost_equal(ref_max, comp_max)
def test_rolling_nanmax():
T = np.random.rand(64)
for m in range(1, 12):
ref_max = np.nanmax(core.rolling_window(T, m), axis=1)
comp_max = core.rolling_nanmax(T, m)
npt.assert_almost_equal(ref_max, comp_max)
ref_max = np.nanmax(core.rolling_window(T, m), axis=1)
comp_max = core.rolling_nanmax(T, m)
npt.assert_almost_equal(ref_max, comp_max)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std(Q, T):
m = Q.shape[0]
ref_μ_Q, ref_σ_Q = naive.compute_mean_std(Q, m)
ref_M_T, ref_Σ_T = naive.compute_mean_std(T, m)
comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m)
comp_M_T, comp_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(ref_μ_Q, comp_μ_Q)
npt.assert_almost_equal(ref_σ_Q, comp_σ_Q)
npt.assert_almost_equal(ref_M_T, comp_M_T)
npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std_chunked(Q, T):
m = Q.shape[0]
with patch("stumpy.config.STUMPY_MEAN_STD_NUM_CHUNKS", 2):
ref_μ_Q, ref_σ_Q = naive.compute_mean_std(Q, m)
ref_M_T, ref_Σ_T = naive.compute_mean_std(T, m)
comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m)
comp_M_T, comp_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(ref_μ_Q, comp_μ_Q)
npt.assert_almost_equal(ref_σ_Q, comp_σ_Q)
npt.assert_almost_equal(ref_M_T, comp_M_T)
npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std_chunked_many(Q, T):
m = Q.shape[0]
with patch("stumpy.config.STUMPY_MEAN_STD_NUM_CHUNKS", 128):
ref_μ_Q, ref_σ_Q = naive.compute_mean_std(Q, m)
ref_M_T, ref_Σ_T = naive.compute_mean_std(T, m)
comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m)
comp_M_T, comp_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(ref_μ_Q, comp_μ_Q)
npt.assert_almost_equal(ref_σ_Q, comp_σ_Q)
npt.assert_almost_equal(ref_M_T, comp_M_T)
npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std_multidimensional(Q, T):
m = Q.shape[0]
Q = np.array([Q, np.random.uniform(-1000, 1000, [Q.shape[0]])])
T = np.array([T, T, np.random.uniform(-1000, 1000, [T.shape[0]])])
ref_μ_Q, ref_σ_Q = naive_compute_mean_std_multidimensional(Q, m)
ref_M_T, ref_Σ_T = naive_compute_mean_std_multidimensional(T, m)
comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m)
comp_M_T, comp_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(ref_μ_Q, comp_μ_Q)
npt.assert_almost_equal(ref_σ_Q, comp_σ_Q)
npt.assert_almost_equal(ref_M_T, comp_M_T)
npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std_multidimensional_chunked(Q, T):
m = Q.shape[0]
Q = np.array([Q, np.random.uniform(-1000, 1000, [Q.shape[0]])])
T = np.array([T, T, np.random.uniform(-1000, 1000, [T.shape[0]])])
with patch("stumpy.config.STUMPY_MEAN_STD_NUM_CHUNKS", 2):
ref_μ_Q, ref_σ_Q = naive_compute_mean_std_multidimensional(Q, m)
ref_M_T, ref_Σ_T = naive_compute_mean_std_multidimensional(T, m)
comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m)
comp_M_T, comp_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(ref_μ_Q, comp_μ_Q)
npt.assert_almost_equal(ref_σ_Q, comp_σ_Q)
npt.assert_almost_equal(ref_M_T, comp_M_T)
npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_compute_mean_std_multidimensional_chunked_many(Q, T):
m = Q.shape[0]
Q = np.array([Q, np.random.uniform(-1000, 1000, [Q.shape[0]])])
T = np.array([T, T, np.random.uniform(-1000, 1000, [T.shape[0]])])
with patch("stumpy.config.STUMPY_MEAN_STD_NUM_CHUNKS", 128):
ref_μ_Q, ref_σ_Q = naive_compute_mean_std_multidimensional(Q, m)
ref_M_T, ref_Σ_T = naive_compute_mean_std_multidimensional(T, m)
comp_μ_Q, comp_σ_Q = core.compute_mean_std(Q, m)
comp_M_T, comp_Σ_T = core.compute_mean_std(T, m)
npt.assert_almost_equal(ref_μ_Q, comp_μ_Q)
npt.assert_almost_equal(ref_σ_Q, comp_σ_Q)
npt.assert_almost_equal(ref_M_T, comp_M_T)
npt.assert_almost_equal(ref_Σ_T, comp_Σ_T)
@pytest.mark.parametrize("Q, T", test_data)
def test_calculate_squared_distance_profile(Q, T):
m = Q.shape[0]
ref = (
np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
** 2
)
QT = core.sliding_dot_product(Q, T)
Q_subseq_isconstant = core.rolling_isconstant(Q, m)[0]
μ_Q, σ_Q = [arr[0] for arr in core.compute_mean_std(Q, m)]
T_subseq_isconstant = core.rolling_isconstant(T, m)
M_T, Σ_T = core.compute_mean_std(T, m)
comp = core._calculate_squared_distance_profile(
m,
QT,
μ_Q,
σ_Q,
M_T,
Σ_T,
Q_subseq_isconstant,
T_subseq_isconstant,
)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_calculate_distance_profile(Q, T):
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
QT = core.sliding_dot_product(Q, T)
Q_subseq_isconstant = core.rolling_isconstant(Q, m)[0]
μ_Q, σ_Q = [arr[0] for arr in core.compute_mean_std(Q, m)]
T_subseq_isconstant = core.rolling_isconstant(T, m)
M_T, Σ_T = core.compute_mean_std(T, m)
comp = core.calculate_distance_profile(
m,
QT,
μ_Q,
σ_Q,
M_T,
Σ_T,
Q_subseq_isconstant,
T_subseq_isconstant,
)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mueen_calculate_distance_profile(Q, T):
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
comp = core.mueen_calculate_distance_profile(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass(Q, T):
Q = Q.copy()
T = T.copy()
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
comp = core.mass(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_Q_nan(Q, T):
Q = Q.copy()
Q[1] = np.nan
T = T.copy()
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
ref[np.isnan(ref)] = np.inf
comp = core.mass(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_Q_inf(Q, T):
Q = Q.copy()
Q[1] = np.inf
T = T.copy()
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
ref[np.isnan(ref)] = np.inf
comp = core.mass(Q, T)
npt.assert_almost_equal(ref, comp)
T[1] = 1e10
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_T_nan(Q, T):
Q = Q.copy()
T = T.copy()
T[1] = np.nan
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
ref[np.isnan(ref)] = np.inf
comp = core.mass(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_T_inf(Q, T):
Q = Q.copy()
T = T.copy()
T[1] = np.inf
m = Q.shape[0]
ref = np.linalg.norm(
core.z_norm(core.rolling_window(T, m), 1) - core.z_norm(Q), axis=1
)
ref[np.isnan(ref)] = np.inf
comp = core.mass(Q, T)
npt.assert_almost_equal(ref, comp)
T[1] = 1e10
@pytest.mark.parametrize("Q, T", test_data)
def test_p_norm_distance_profile(Q, T):
Q = Q.copy()
T = T.copy()
m = Q.shape[0]
for p in [1.0, 1.5, 2.0]:
ref = cdist(
core.rolling_window(Q, m),
core.rolling_window(T, m),
metric="minkowski",
p=p,
).flatten()
ref = np.power(ref, p)
cmp = core._p_norm_distance_profile(Q, T, p)
npt.assert_almost_equal(ref, cmp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_absolute(Q, T):
Q = Q.copy()
T = T.copy()
m = Q.shape[0]
for p in [1.0, 2.0, 3.0]:
ref = np.linalg.norm(core.rolling_window(T, m) - Q, axis=1, ord=p)
comp = core.mass_absolute(Q, T, p=p)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_absolute_Q_nan(Q, T):
Q = Q.copy()
Q[1] = np.nan
T = T.copy()
m = Q.shape[0]
ref = np.linalg.norm(core.rolling_window(T, m) - Q, axis=1)
ref[np.isnan(ref)] = np.inf
comp = core.mass_absolute(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_absolute_Q_inf(Q, T):
Q = Q.copy()
Q[1] = np.inf
T = T.copy()
m = Q.shape[0]
ref = np.linalg.norm(core.rolling_window(T, m) - Q, axis=1)
ref[np.isnan(ref)] = np.inf
comp = core.mass_absolute(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_absolute_T_nan(Q, T):
Q = Q.copy()
T = T.copy()
T[1] = np.nan
m = Q.shape[0]
ref = np.linalg.norm(core.rolling_window(T, m) - Q, axis=1)
ref[np.isnan(ref)] = np.inf
comp = core.mass_absolute(Q, T)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("Q, T", test_data)
def test_mass_absolute_T_inf(Q, T):
Q = Q.copy()
T = T.copy()
T[1] = np.inf
m = Q.shape[0]
ref = np.linalg.norm(core.rolling_window(T, m) - Q, axis=1)
ref[np.isnan(ref)] = np.inf
comp = core.mass_absolute(Q, T)
npt.assert_almost_equal(ref, comp)
def test_mass_absolute_sqrt_input_negative():
Q = np.array(
[
-13.09,
-14.1,
-15.08,
-16.31,
-17.13,
-17.5,
-18.07,
-18.07,
-17.48,
-16.24,
-14.88,
-13.56,
-12.65,
-11.93,
-11.48,
-11.06,
-10.83,
-10.67,
-10.59,
-10.81,
-10.92,
-11.15,
-11.37,
-11.53,
-11.19,
-11.08,
-10.48,
-10.14,
-9.92,
-9.99,
-10.11,
-9.92,
-9.7,
-9.47,
-9.06,
-9.01,
-8.79,
-8.67,
-8.33,
-8.0,
-8.26,
-8.0,
-7.54,
-7.32,
-7.13,
-7.24,
-7.43,
-7.93,
-8.8,
-9.71,
]
)
ref = 0.0
comp = core.mass_absolute(Q, Q)
npt.assert_almost_equal(ref, comp)
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mass_distance_matrix(T_A, T_B):
m = 3
ref_distance_matrix = naive.distance_matrix(T_A, T_B, m)
k = T_A.shape[0] - m + 1
l = T_B.shape[0] - m + 1
comp_distance_matrix = np.full((k, l), np.inf)
core.mass_distance_matrix(T_A, T_B, m, comp_distance_matrix)
npt.assert_almost_equal(ref_distance_matrix, comp_distance_matrix)
@pytest.mark.parametrize("T_A, T_B", test_data)
def test_mass_absolute_distance_matrix(T_A, T_B):
m = 3
ref_distance_matrix = cdist(
core.rolling_window(T_A, m), core.rolling_window(T_B, m)
)
k = T_A.shape[0] - m + 1
l = T_B.shape[0] - m + 1
comp_distance_matrix = np.full((k, l), np.inf)
core._mass_absolute_distance_matrix(T_A, T_B, m, comp_distance_matrix)
npt.assert_almost_equal(ref_distance_matrix, comp_distance_matrix)
def test_apply_exclusion_zone():
T = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float64)
ref = np.empty(T.shape, dtype=np.float64)
comp = np.empty(T.shape, dtype=np.float64)
exclusion_zone = 2
for i in range(T.shape[0]):
ref[:] = T[:]
naive.apply_exclusion_zone(ref, i, exclusion_zone, np.inf)
comp[:] = T[:]
core.apply_exclusion_zone(comp, i, exclusion_zone, np.inf)
naive.replace_inf(ref)
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
def test_apply_exclusion_zone_int():
T = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)
ref = np.empty(T.shape, dtype=np.int64)
comp = np.empty(T.shape, dtype=np.int64)
exclusion_zone = 2
for i in range(T.shape[0]):
ref[:] = T[:]
naive.apply_exclusion_zone(ref, i, exclusion_zone, -1)
comp[:] = T[:]
core.apply_exclusion_zone(comp, i, exclusion_zone, -1)
naive.replace_inf(ref)
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
def test_apply_exclusion_zone_bool():
T = np.ones(10, dtype=bool)
ref = np.empty(T.shape, dtype=bool)
comp = np.empty(T.shape, dtype=bool)
exclusion_zone = 2
for i in range(T.shape[0]):
ref[:] = T[:]
naive.apply_exclusion_zone(ref, i, exclusion_zone, False)
comp[:] = T[:]
core.apply_exclusion_zone(comp, i, exclusion_zone, False)
naive.replace_inf(ref)
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
def test_apply_exclusion_zone_multidimensional():
T = np.array(
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
dtype=np.float64,
)
ref = np.empty(T.shape, dtype=np.float64)
comp = np.empty(T.shape, dtype=np.float64)
exclusion_zone = 2
for i in range(T.shape[1]):
ref[:, :] = T[:, :]
naive.apply_exclusion_zone(ref, i, exclusion_zone, np.inf)
comp[:, :] = T[:, :]
core.apply_exclusion_zone(comp, i, exclusion_zone, np.inf)
naive.replace_inf(ref)
naive.replace_inf(comp)
npt.assert_array_equal(ref, comp)
def test_preprocess():
T = np.array([0, np.nan, 2, 3, 4, 5, 6, 7, np.inf, 9])
m = 3
ref_T = np.array([0, 0, 2, 3, 4, 5, 6, 7, 0, 9], dtype=float)
ref_subseq_isconstant = naive.rolling_isconstant(T, m)
ref_M, ref_Σ = naive.compute_mean_std(T, m)
comp_T, comp_M, comp_Σ, comp_subseq_isconstant = core.preprocess(T, m)
npt.assert_almost_equal(ref_T, comp_T)
npt.assert_almost_equal(ref_M, comp_M)
npt.assert_almost_equal(ref_Σ, comp_Σ)
npt.assert_almost_equal(ref_subseq_isconstant, comp_subseq_isconstant)
T = pd.Series(T)
comp_T, comp_M, comp_Σ, comp_subseq_isconstant = core.preprocess(T, m)
npt.assert_almost_equal(ref_T, comp_T)
npt.assert_almost_equal(ref_M, comp_M)
npt.assert_almost_equal(ref_Σ, comp_Σ)
npt.assert_almost_equal(ref_subseq_isconstant, comp_subseq_isconstant)
def test_preprocess_non_normalized():
T = np.array([0, np.nan, 2, 3, 4, 5, 6, 7, np.inf, 9])
m = 3
ref_T_subseq_isfinite = np.full(T.shape[0] - m + 1, False, dtype=bool)
for i in range(T.shape[0] - m + 1):
if np.all(np.isfinite(T[i : i + m])):
ref_T_subseq_isfinite[i] = True
ref_T = np.array([0, 0, 2, 3, 4, 5, 6, 7, 0, 9], dtype=float)
comp_T, comp_T_subseq_isfinite = core.preprocess_non_normalized(T, m)
npt.assert_almost_equal(ref_T, comp_T)
npt.assert_almost_equal(ref_T_subseq_isfinite, comp_T_subseq_isfinite)
T = pd.Series(T)
comp_T, comp_T_subseq_isfinite = core.preprocess_non_normalized(T, m)
npt.assert_almost_equal(ref_T, comp_T)
npt.assert_almost_equal(ref_T_subseq_isfinite, comp_T_subseq_isfinite)
def test_preprocess_diagonal():
T = np.array([0, np.nan, 2, 3, 4, 5, 6, 7, np.inf, 9])
m = 3
ref_T = np.array([0, 0, 2, 3, 4, 5, 6, 7, 0, 9], dtype=float)
ref_M, ref_Σ = naive.compute_mean_std(ref_T, m)
ref_Σ_inverse = 1.0 / ref_Σ
ref_M_m_1, _ = naive.compute_mean_std(ref_T, m - 1)
(
comp_T,
comp_M,
comp_Σ_inverse,
comp_M_m_1,
comp_T_subseq_isfinite,
comp_T_subseq_isconstant,
) = core.preprocess_diagonal(T, m)
npt.assert_almost_equal(ref_T, comp_T)
npt.assert_almost_equal(ref_M, comp_M)
npt.assert_almost_equal(ref_Σ_inverse, comp_Σ_inverse)
npt.assert_almost_equal(ref_M_m_1, comp_M_m_1)
T = pd.Series(T)
(
comp_T,
comp_M,
comp_Σ_inverse,
comp_M_m_1,
comp_T_subseq_isfinite,
comp_T_subseq_isconstant,
) = core.preprocess_diagonal(T, m)
npt.assert_almost_equal(ref_T, comp_T)
npt.assert_almost_equal(ref_M, comp_M)
npt.assert_almost_equal(ref_Σ_inverse, comp_Σ_inverse)
npt.assert_almost_equal(ref_M_m_1, comp_M_m_1)
def test_replace_distance():
right = np.random.rand(30).reshape(5, 6)
left = right.copy()
np.fill_diagonal(right, config.STUMPY_MAX_DISTANCE - 1e-9)
np.fill_diagonal(left, np.inf)
core.replace_distance(right, config.STUMPY_MAX_DISTANCE, np.inf, 1e-6)
def test_array_to_temp_file():
left = np.random.rand()
fname = core.array_to_temp_file(left)
right = np.load(fname, allow_pickle=False)
os.remove(fname)
npt.assert_almost_equal(left, right)
def test_count_diagonal_ndist():
for n_A in range(10, 15):
for n_B in range(10, 15):
for m in range(3, 6):
diags = np.random.permutation(
range(-(n_A - m + 1) + 1, n_B - m + 1)
).astype(np.int64)
ones_matrix = np.ones((n_A - m + 1, n_B - m + 1), dtype=np.int64)
ref_ndist_counts = np.empty(len(diags))
for i, diag in enumerate(diags):
ref_ndist_counts[i] = ones_matrix.diagonal(offset=diag).sum()
comp_ndist_counts = core._count_diagonal_ndist(diags, m, n_A, n_B)
npt.assert_almost_equal(ref_ndist_counts, comp_ndist_counts)
def test_get_array_ranges():
x = np.array([3, 9, 2, 1, 5, 4, 7, 7, 8, 6], dtype=np.int64)
for n_chunks in range(2, 5):
ref = naive.get_array_ranges(x, n_chunks, False)
cmp = core._get_array_ranges(x, n_chunks, False)
npt.assert_almost_equal(ref, cmp)
def test_get_array_ranges_exhausted():
x = np.array([3, 3, 3, 11, 11, 11], dtype=np.int64)
n_chunks = 6
ref = naive.get_array_ranges(x, n_chunks, False)
cmp = core._get_array_ranges(x, n_chunks, False)
npt.assert_almost_equal(ref, cmp)
def test_get_array_ranges_exhausted_truncated():
x = np.array([3, 3, 3, 11, 11, 11], dtype=np.int64)
n_chunks = 6
ref = naive.get_array_ranges(x, n_chunks, True)
cmp = core._get_array_ranges(x, n_chunks, True)
npt.assert_almost_equal(ref, cmp)
def test_get_array_ranges_empty_array():
x = np.array([], dtype=np.int64)
n_chunks = 6
ref = naive.get_array_ranges(x, n_chunks, False)
cmp = core._get_array_ranges(x, n_chunks, False)
npt.assert_almost_equal(ref, cmp)