FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stumpy/stumpy/core.py at main · stumpy-dev/stumpy · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stumpy-dev
/
stumpy
Public
Notifications
You must be signed in to change notification settings
Fork
367
Star
4.1k
Code
Issues
67
Pull requests
12
Discussions
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
stumpy
/
stumpy
/
core.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
4507 lines (3595 loc) · 134 KB
Breadcrumbs
stumpy
/
stumpy
/
core.py
Copy path
File metadata and controls
4507 lines (3595 loc) · 134 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# STUMPY
# Copyright 2019 TD Ameritrade. Released under the terms of the 3-Clause BSD license. # noqa: E501
# STUMPY is a trademark of TD Ameritrade IP Company, Inc. All rights reserved.
import
functools
import
inspect
import
math
import
tempfile
import
warnings
import
numpy
as
np
from
numba
import
cuda
,
njit
,
prange
from
scipy
import
linalg
from
scipy
.
ndimage
import
maximum_filter1d
,
minimum_filter1d
from
scipy
.
spatial
.
distance
import
cdist
from
.
import
config
,
sdp
try
:
from
numba
.
cuda
.
cudadrv
.
driver
import
_raise_driver_not_found
except
ImportError
:
pass
def
_compare_parameters
(
norm
,
non_norm
,
exclude
=
None
):
"""
Compare if the parameters in `norm` and `non_norm` are the same
Parameters
----------
norm : function
The normalized function (or class) that is complementary to the
non-normalized function (or class)
non_norm : function
The non-normalized function (or class) that is complementary to the
z-normalized function (or class)
exclude : list
A list of parameters to exclude for the comparison
Returns
-------
is_same_params : bool
`True` if parameters from both `norm` and `non-norm` are the same. `False`
otherwise.
"""
norm_params
=
list
(
inspect
.
signature
(
norm
).
parameters
.
keys
())
non_norm_params
=
list
(
inspect
.
signature
(
non_norm
).
parameters
.
keys
())
if
exclude
is
not
None
:
for
param
in
exclude
:
if
param
in
norm_params
:
norm_params
.
remove
(
param
)
if
param
in
non_norm_params
:
non_norm_params
.
remove
(
param
)
is_same_params
=
set
(
norm_params
)
==
set
(
non_norm_params
)
if
not
is_same_params
:
msg
=
""
if
exclude
is
not
None
or
(
isinstance
(
exclude
,
list
)
and
len
(
exclude
)):
msg
+=
f"Excluding `
{
exclude
}
` parameters, "
msg
+=
f"function `
{
norm
.
__name__
}
(
{
norm_params
}
) and "
msg
+=
f"function `
{
non_norm
.
__name__
}
(
{
non_norm_params
}
) "
msg
+=
"have different arguments/parameters."
warnings
.
warn
(
msg
)
return
is_same_params
def
non_normalized
(
non_norm
,
exclude
=
None
,
replace
=
None
):
"""
Decorator for swapping a z-normalized function (or class) for its complementary
non-normalized function (or class) as defined by `non_norm`. This requires that
the z-normalized function (or class) has a `normalize` parameter.
With the exception of `normalize` parameter, the `non_norm` function (or class)
must have the same siganture as the `norm` function (or class) signature in order
to be compatible. Please use a combination of the `exclude` and/or `replace`
parameters when necessary.
```
def non_norm_func(Q, T, A_non_norm):
...
return
@non_normalized(
non_norm_func,
exclude=["normalize", "p", "A_norm", "A_non_norm"],
replace={"A_norm": "A_non_norm", "other_norm": None},
)
def norm_func(Q, T, A_norm=None, other_norm=None, normalize=True, p=2.0):
...
return
```
Parameters
----------
non_norm : function
The non-normalized function (or class) that is complementary to the
z-normalized function (or class)
exclude : list, default None
A list of function (or class) parameter names to exclude when comparing the
function (or class) signatures. When `exlcude is None`, this parameter is
automatically set to `exclude = ["normalize", "p", "T_A_subseq_isconstant",
T_B_subseq_isconstant]` by default.
replace : dict, default None
A dictionary of function (or class) parameter key-value pairs. Each key that
is found as a parameter name in the `norm` function (or class) will be replaced
by its corresponding or complementary parameter name in the `non_norm` function
(or class) (e.g., {"norm_param": "non_norm_param"}). To remove any parameter in
the `norm` function (or class) that does not exist in the `non_norm` function,
simply set the value to `None` (i.e., {"norm_param": None}).
Returns
-------
outer_wrapper : function
The desired z-normalized/non-normalized function (or class)
"""
if
exclude
is
None
:
exclude
=
[
"normalize"
,
"p"
,
"T_A_subseq_isconstant"
,
"T_B_subseq_isconstant"
,
]
@
functools
.
wraps
(
non_norm
)
def
outer_wrapper
(
norm
):
@
functools
.
wraps
(
norm
)
def
inner_wrapper
(
*
args
,
**
kwargs
):
is_same_params
=
_compare_parameters
(
norm
,
non_norm
,
exclude
=
exclude
)
if
not
is_same_params
or
kwargs
.
get
(
"normalize"
,
True
):
return
norm
(
*
args
,
**
kwargs
)
else
:
kwargs
=
{
k
:
v
for
k
,
v
in
kwargs
.
items
()
if
k
!=
"normalize"
}
if
replace
is
not
None
:
for
k
,
v
in
replace
.
items
():
if
k
in
kwargs
.
keys
():
if
v
is
None
:
# pragma: no cover
_
=
kwargs
.
pop
(
k
)
else
:
kwargs
[
v
]
=
kwargs
.
pop
(
k
)
return
non_norm
(
*
args
,
**
kwargs
)
return
inner_wrapper
return
outer_wrapper
def
driver_not_found
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Helper function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
_raise_driver_not_found
()
def
_gpu_stump_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_aamp_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_ostinato_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_aamp_ostinato_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_mpdist_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_aampdist_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_stimp_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_aamp_stimp_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_searchsorted_left_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
_gpu_searchsorted_right_dnf
(
*
args
,
**
kwargs
):
# pragma: no cover
"""
Dummy function to raise CudaSupportError driver not found error.
Parameters
----------
None
Returns
-------
None
"""
driver_not_found
()
def
get_pkg_name
():
# pragma: no cover
"""
Return package name.
Parameters
----------
None
Returns
-------
None
"""
return
__name__
.
split
(
"."
)[
0
]
def
rolling_window
(
a
,
window
):
"""
Use strides to generate rolling/sliding windows for a numpy array.
Parameters
----------
a : numpy.ndarray
numpy array
window : int
Size of the rolling window
Returns
-------
output : numpy.ndarray
This will be a new view of the original input array.
"""
a
=
np
.
asarray
(
a
)
shape
=
a
.
shape
[:
-
1
]
+
(
a
.
shape
[
-
1
]
-
window
+
1
,
window
)
strides
=
a
.
strides
+
(
a
.
strides
[
-
1
],)
return
np
.
lib
.
stride_tricks
.
as_strided
(
a
,
shape
=
shape
,
strides
=
strides
)
def
z_norm
(
a
,
axis
=
0
,
threshold
=
config
.
STUMPY_STDDEV_THRESHOLD
):
"""
Calculate the z-normalized input array `a` by subtracting the mean and
dividing by the standard deviation along a given axis.
Parameters
----------
a : numpy.ndarray
NumPy array
axis : int, default 0
NumPy array axis
threshold : float, default to config.STUMPY_STDDEV_THRESHOLD
A non-nan std value being less than `threshold` will be replaced with 1.0
Returns
-------
output : numpy.ndarray
An array with z-normalized values computed along a specified axis.
"""
std
=
np
.
std
(
a
,
axis
,
keepdims
=
True
)
std
[
np
.
less
(
std
,
threshold
,
where
=
~
np
.
isnan
(
std
))]
=
1.0
return
(
a
-
np
.
mean
(
a
,
axis
,
keepdims
=
True
))
/
std
def
check_nan
(
a
):
# pragma: no cover
"""
Check if the array contains NaNs.
Parameters
----------
a : numpy.ndarray
NumPy array
Returns
-------
None
Raises
------
ValueError
If the array contains a NaN
"""
if
np
.
any
(
np
.
isnan
(
a
)):
msg
=
"Input array contains one or more NaNs"
raise
ValueError
(
msg
)
return
def
check_dtype
(
a
,
dtype
=
np
.
float64
):
# pragma: no cover
"""
Check if the array type of `a` is of type specified by `dtype` parameter.
Parameters
----------
a : numpy.ndarray
NumPy array
dtype : dtype, default np.float64
NumPy `dtype`
Returns
-------
None
Raises
------
TypeError
If the array type does not match `dtype`
"""
if
dtype
is
int
:
dtype
=
np
.
int64
if
dtype
is
float
:
dtype
=
np
.
float64
if
dtype
is
bool
:
dtype
=
np
.
bool_
if
not
np
.
issubdtype
(
a
.
dtype
,
dtype
):
msg
=
f"
{
dtype
}
dtype expected but found
{
a
.
dtype
}
in input array
\n
"
msg
+=
"Please change your input `dtype` with `.astype(dtype)`"
raise
TypeError
(
msg
)
return
True
def
transpose_dataframe
(
df
):
# pragma: no cover
"""
Check if the input is a column-wise pandas/polars `DataFrame`. If `True`, return a
transpose dataframe since stumpy assumes that each row represents data from a
different dimension while each column represents data from the same dimension.
If `False`, return `a` unchanged. Pandas/polars `Series` do not need to be
transposed.
Note that this function has zero dependency on Pandas (not even a soft dependency).
Parameters
----------
df : DataFrame
pandas/polars dataframe
Returns
-------
output : df
If `df` is a Pandas `DataFrame` then return `df.T`. Otherwise, return `df`
"""
if
type
(
df
).
__name__
==
"DataFrame"
:
return
df
.
transpose
()
return
df
def
are_arrays_equal
(
a
,
b
):
# pragma: no cover
"""
Check if two arrays are equal; first by comparing memory addresses,
and secondly by their values.
Parameters
----------
a : numpy.ndarray
NumPy array
b : numpy.ndarray
NumPy array
Returns
-------
output : bool
This is `True` if the arrays are equal and `False` otherwise.
"""
if
id
(
a
)
==
id
(
b
):
return
True
# For numpy >= 1.19
# return np.array_equal(a, b, equal_nan=True)
if
a
.
shape
!=
b
.
shape
:
return
False
return
bool
(((
a
==
b
)
|
(
np
.
isnan
(
a
)
&
np
.
isnan
(
b
))).
all
())
def
are_distances_too_small
(
a
,
threshold
=
10e-6
):
# pragma: no cover
"""
Check the distance values from a matrix profile.
If the values are smaller than the threshold (i.e., less than 10e-6) then
it could suggest that this is a self-join.
Parameters
----------
a : numpy.ndarray
NumPy array
threshold : float, default 10e-6
Minimum value in which to compare the matrix profile to
Returns
-------
output : bool
This is `True` if the matrix profile distances are all below the
threshold and `False` if they are all above the threshold.
"""
if
a
.
mean
()
<
threshold
or
np
.
all
(
a
<
threshold
):
return
True
return
False
def
get_max_window_size
(
n
):
"""
Get the maximum window size for a self-join
Parameters
----------
n : int
The length of the time series
Returns
-------
max_m : int
The maximum window size allowed given `config.STUMPY_EXCL_ZONE_DENOM`
"""
max_m
=
(
int
(
n
-
np
.
floor
(
(
n
+
(
config
.
STUMPY_EXCL_ZONE_DENOM
-
1
))
//
(
config
.
STUMPY_EXCL_ZONE_DENOM
+
1
)
)
)
-
1
)
return
max_m
def
check_window_size
(
m
,
max_size
=
None
,
n
=
None
):
"""
Check the window size and ensure that it is greater than or equal to 3 and, if
``max_size`` is provided, ensure that the window size is less than or equal to
the ``max_size``. Furthermore, if ``n`` is provided, then a self-join is assumed
and it checks whether all subsequences have at least one non-trivial neighbor.
Parameters
----------
m : int
Window size
max_size : int, default None
The maximum window size allowed
n : int, default None
The length of the time series in the case of a self-join.
``n`` should not be supplied (or set to ``None``) in the case of an AB-join.
Returns
-------
None
"""
if
m
<=
2
:
raise
ValueError
(
"All window sizes must be greater than or equal to three"
,
"""A window size that is less than or equal to two is meaningless when
it comes to computing the z-normalized Euclidean distance. In the case of
`m=1` produces a standard deviation of zero. In the case of `m=2`, both
the mean and standard deviation for any given subsequence are identical
and so the z-normalization for any sequence will either be [-1., 1.] or
[1., -1.]. Thus, the z-normalized Euclidean distance will be (very likely)
zero between any subsequence and its nearest neighbor (assuming that the
time series is large enough to contain both scenarios).
"""
,
)
if
max_size
is
not
None
and
m
>
max_size
:
raise
ValueError
(
f"The window size must be less than or equal to
{
max_size
}
"
)
if
n
is
not
None
:
# Raise warning if there is at least one subsequence with no eligible
# (non-trivial) neighbor in the case of a self-join.
# For any time series `T`, an "eligible nearest neighbor" subsequence for
# the central-most subsequence must be located outside the `excl_zone`,
# and the central-most subsequence will ALWAYS have the smallest relative
# (index-wise) distance to its farthest neighbor amongst all other subsequences.
# Therefore, we only need to check whether the `excl_zone` eliminates all
# "neighbors" for the central-most subsequence in `T`. In fact, we just need to
# verify whether the `excl_zone` eliminates the "neighbor" that is farthest
# away (index-wise) from the central-most subsequence. If it does not, this
# implies that all subsequences in `T` will have at least one "eligible nearest
# neighbor" that is located outside of their respective excl_zone.
excl_zone
=
int
(
math
.
ceil
(
m
/
config
.
STUMPY_EXCL_ZONE_DENOM
))
l
=
n
-
m
+
1
# The start index of subsequences are: 0, 1, ..., l-1
# If `l` is odd
# Suppose `l == 5`. So, the start index of the subsequences
# are: 0, 1, 2, 3, 4
# The central subsequence is located at index position c=2, with two
# farthest neighbors, one located at index 0, and the other is located
# at index 4. In both cases, the relative (index-wise) distance is 2,
# which is simply `5 // 2`. In general, it can be shown that the
# (index-wise) distance from the central subsequence to its farthest
# neighbor is `l // 2`.
# If `l` is even
# Suppose `l == 6`. So, the start index of the subsequences
# are: 0, 1, 2, 3, 4, 5
# There are two central-most subsequences, located at the index
# positions c=2 and c=3. For the central-most subsequence at index
# position c=2, its farthest neighbor will be located at index 5 (to the
# right of c=2) and, for the central-most subsequence at index position
# c=3, its farthest neighbor will be located at index 0 (to the left of
# c=3). In both cases, the relative (index-wise) distance is 3,
# which is simply `6 // 2`. In general, it can be shown that the
# (index-wise) distance from the central-most subsequence to its
# farthest neighbor is `l // 2`.
# Therefore, regardless if `l` is even or odd, for the central
# subsequence for any time series, the index location of its
# farthest neighbor will always be `l // 2` index positions away.
diff_to_farthest_idx
=
l
//
2
if
diff_to_farthest_idx
<=
excl_zone
:
msg
=
(
f"The window size, 'm =
{
m
}
', may be too large and could lead to "
+
"meaningless results. Consider reducing 'm' where necessary"
)
warnings
.
warn
(
msg
)
def
sliding_dot_product
(
Q
,
T
):
"""
Calculate the sliding window dot product.
Parameters
----------
Q : numpy.ndarray
Query array or subsequence
T : numpy.ndarray
Time series or sequence
Returns
-------
output : numpy.ndarray
Sliding dot product between `Q` and `T`.
"""
return
sdp
.
_sliding_dot_product
(
Q
,
T
)
@
njit
(
# "f8[:](f8[:], i8, b1[:])",
fastmath
=
config
.
STUMPY_FASTMATH_FLAGS
)
def
_welford_nanvar
(
a
,
w
,
a_subseq_isfinite
):
"""
Compute the rolling variance for a 1-D array while ignoring NaNs using a modified
version of Welford's algorithm but is much faster than using `np.nanstd` with stride
tricks.
Parameters
----------
a : numpy.ndarray
The input array
w : int
The rolling window size
a_subseq_isfinite : numpy.ndarray
A boolean array that describes whether each subequence of length `w` within `a`
is finite.
Returns
-------
all_variances : numpy.ndarray
Rolling window nanvar
"""
all_variances
=
np
.
empty
(
a
.
shape
[
0
]
-
w
+
1
,
dtype
=
np
.
float64
)
prev_mean
=
0.0
prev_var
=
0.0
for
start_idx
in
range
(
a
.
shape
[
0
]
-
w
+
1
):
prev_start_idx
=
start_idx
-
1
stop_idx
=
start_idx
+
w
# Exclusive index value
last_idx
=
start_idx
+
w
-
1
# Last inclusive index value
if
(
start_idx
==
0
or
not
a_subseq_isfinite
[
prev_start_idx
]
or
not
a_subseq_isfinite
[
start_idx
]
):
curr_mean
=
np
.
nanmean
(
a
[
start_idx
:
stop_idx
])
curr_var
=
np
.
nanvar
(
a
[
start_idx
:
stop_idx
])
else
:
curr_mean
=
prev_mean
+
(
a
[
last_idx
]
-
a
[
prev_start_idx
])
/
w
curr_var
=
(
prev_var
+
(
a
[
last_idx
]
-
a
[
prev_start_idx
])
*
(
a
[
last_idx
]
-
curr_mean
+
a
[
prev_start_idx
]
-
prev_mean
)
/
w
)
all_variances
[
start_idx
]
=
curr_var
prev_mean
=
curr_mean
prev_var
=
curr_var
return
all_variances
def
welford_nanvar
(
a
,
w
=
None
):
"""
Compute the rolling variance for a 1-D array while ignoring NaNs using a modified
version of Welford's algorithm but is much faster than using `np.nanstd` with stride
tricks.
This is a convenience wrapper around the `_welford_nanvar` function.
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray, default None
The rolling window size
Returns
-------
output : numpy.ndarray
Rolling window nanvar.
"""
if
w
is
None
:
w
=
a
.
shape
[
0
]
a_subseq_isfinite
=
rolling_isfinite
(
a
,
w
)
return
_welford_nanvar
(
a
,
w
,
a_subseq_isfinite
)
def
welford_nanstd
(
a
,
w
=
None
):
"""
Compute the rolling standard deviation for a 1-D array while ignoring NaNs using
a modified version of Welford's algorithm but is much faster than using `np.nanstd`
with stride tricks.
This a convenience wrapper around `welford_nanvar`.
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray, default None
The rolling window size
Returns
-------
output : numpy.ndarray
Rolling window nanstd.
"""
if
w
is
None
:
w
=
a
.
shape
[
0
]
return
np
.
sqrt
(
np
.
clip
(
welford_nanvar
(
a
,
w
),
a_min
=
0
,
a_max
=
None
))
@
njit
(
parallel
=
True
,
fastmath
=
config
.
STUMPY_FASTMATH_FLAGS
)
def
_rolling_nanstd_1d
(
a
,
w
):
"""
A Numba JIT-compiled and parallelized function for computing the rolling standard
deviation for 1-D array while ignoring NaN.
Parameters
----------
a : numpy.ndarray
The input array
w : int
The rolling window size
Returns
-------
out : numpy.ndarray
This 1D array has the length of `a.shape[0]-w+1`. `out[i]`
contains the stddev value of `a[i : i + w]`
"""
n
=
a
.
shape
[
0
]
-
w
+
1
out
=
np
.
empty
(
n
,
dtype
=
np
.
float64
)
for
i
in
prange
(
n
):
out
[
i
]
=
np
.
nanstd
(
a
[
i
:
i
+
w
])
return
out
def
rolling_nanstd
(
a
,
w
,
welford
=
False
):
"""
Compute the rolling standard deviation over the last axis of `a` while ignoring
NaNs.
This essentially replaces:
`np.nanstd(rolling_window(a[..., start:stop], w), axis=a.ndim)`
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray
The rolling window size
welford : bool, default False
When False (default), the computation is parallelized and the stddev of
each subsequence is calculated on its own. When `welford==True`, the
welford method is used to reduce the computing time at the cost of slightly
reduced precision.
Returns
-------
out : numpy.ndarray
Rolling window nanstd
"""
axis
=
a
.
ndim
-
1
# Account for rolling
if
welford
:
return
np
.
apply_along_axis
(
lambda
a_row
,
w
:
welford_nanstd
(
a_row
,
w
),
axis
=
axis
,
arr
=
a
,
w
=
w
)
else
:
return
np
.
apply_along_axis
(
lambda
a_row
,
w
:
_rolling_nanstd_1d
(
a_row
,
w
),
axis
=
axis
,
arr
=
a
,
w
=
w
)
def
_rolling_nanmin_1d
(
a
,
w
=
None
):
"""
Compute the rolling min for 1-D while ignoring NaNs.
This essentially replaces:
`np.nanmin(rolling_window(a[..., start:stop], w), axis=a.ndim)`
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray, default None
The rolling window size
Returns
-------
output : numpy.ndarray
Rolling window nanmin.
"""
if
w
is
None
:
w
=
a
.
shape
[
0
]
half_window_size
=
int
(
math
.
ceil
((
w
-
1
)
/
2
))
return
minimum_filter1d
(
a
,
size
=
w
)[
half_window_size
:
half_window_size
+
a
.
shape
[
0
]
-
w
+
1
]
def
_rolling_nanmax_1d
(
a
,
w
=
None
):
"""
Compute the rolling max for 1-D while ignoring NaNs.
This essentially replaces:
`np.nanmax(rolling_window(a[..., start:stop], w), axis=a.ndim)`
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray, default None
The rolling window size
Returns
-------
output : numpy.ndarray
Rolling window nanmax.
"""
if
w
is
None
:
w
=
a
.
shape
[
0
]
half_window_size
=
int
(
math
.
ceil
((
w
-
1
)
/
2
))
return
maximum_filter1d
(
a
,
size
=
w
)[
half_window_size
:
half_window_size
+
a
.
shape
[
0
]
-
w
+
1
]
def
rolling_nanmin
(
a
,
w
):
"""
Compute the rolling min for 1-D and 2-D arrays while ignoring NaNs.
This a convenience wrapper around `_rolling_nanmin_1d`.
This essentially replaces:
`np.nanmin(rolling_window(a[..., start:stop], w), axis=a.ndim)`
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray
The rolling window size
Returns
-------
output : numpy.ndarray
Rolling window nanmin.
"""
axis
=
a
.
ndim
-
1
# Account for rolling
return
np
.
apply_along_axis
(
lambda
a_row
,
w
:
_rolling_nanmin_1d
(
a_row
,
w
),
axis
=
axis
,
arr
=
a
,
w
=
w
)
def
rolling_nanmax
(
a
,
w
):
"""
Compute the rolling max for 1-D and 2-D arrays while ignoring NaNs.
This a convenience wrapper around `_rolling_nanmax_1d`.
This essentially replaces:
`np.nanmax(rolling_window(a[..., start:stop], w), axis=a.ndim)`
Parameters
----------
a : numpy.ndarray
The input array
w : numpy.ndarray
The rolling window size
Returns
-------
output : numpy.ndarray
Rolling window nanmax.
"""
axis
=
a
.
ndim
-
1
# Account for rolling
return
np
.
apply_along_axis
(
lambda
a_row
,
w
:
_rolling_nanmax_1d
(
a_row
,
w
),
axis
=
axis
,
arr
=
a
,
w
=
w
)
def
compute_mean_std
(
T
,
m
):
"""
Compute the sliding mean and standard deviation for the array `T` with
a window size of `m`
Parameters
----------
T : numpy.ndarray
Time series or sequence
m : int
Window size
Returns
-------
M_T : numpy.ndarray
Sliding mean. All nan values are replaced with np.inf
Σ_T : numpy.ndarray
Sliding standard deviation
Notes
-----
`DOI: 10.1109/ICDM.2016.0179
\
<https://www.cs.ucr.edu/~eamonn/PID4481997_extend_Matrix%20Profile_I.pdf>`__
See Table II
DOI: 10.1145/2020408.2020587
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL