FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stumpy/stumpy/aampi.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
66
Pull requests
11
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
/
aampi.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
429 lines (349 loc) · 13.1 KB
Breadcrumbs
stumpy
/
stumpy
/
aampi.py
Copy path
File metadata and controls
429 lines (349 loc) · 13.1 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
# STUMPY
# Copyright 2019 TD Ameritrade. Released under the terms of the 3-Clause BSD license.
# STUMPY is a trademark of TD Ameritrade IP Company, Inc. All rights reserved.
import
numpy
as
np
from
.
import
config
,
core
from
.
aamp
import
aamp
class
aampi
:
# needs to be enhanced to support top-k matrix profile
"""
Compute an incremental non-normalized (i.e., without z-normalization) matrix profile
for streaming data
Parameters
----------
T : numpy.ndarray
The time series or sequence for which the non-normalized matrix profile and
matrix profile indices will be returned
m : int
Window size
egress : bool, default True
If set to `True`, the oldest data point in the time series is removed and
the time series length remains constant rather than forever increasing
p : float, default 2.0
The p-norm to apply for computing the Minkowski distance.
k : int, default 1
The number of top `k` smallest distances used to construct the matrix profile.
Note that this will increase the total computational time and memory usage
when k > 1.
mp : numpy.ndarray, default None
A pre-computed matrix profile (and corresponding matrix profile indices).
This is a 2D array of shape `(len(T) - m + 1, 2 * k + 2)`, where the first `k`
columns are top-k matrix profile, and the next `k` columns are their
corresponding indices. The last two columns correspond to the top-1 left and
top-1 right matrix profile indices. When None (default), this array is computed
internally using `stumpy.aamp`.
Attributes
----------
P_ : numpy.ndarray
The updated matrix profile for `T`
I_ : numpy.ndarray
The updated matrix profile indices for `T`
left_P_ : numpy.ndarray
The updated left matrix profile for `T`
left_I_ : numpy.ndarray
The updated left matrix profile indices for `T`
T_ : numpy.ndarray
The updated time series or sequence for which the matrix profile and matrix
profile indices are computed
Methods
-------
update(t)
Append a single new data point, `t`, to the time series, `T`, and update the
matrix profile
Notes
-----
`arXiv:1901.05708
\
<https://arxiv.org/pdf/1901.05708.pdf>`__
See Algorithm 1
Note that we have extended this algorithm for AB-joins as well.
"""
def
__init__
(
self
,
T
,
m
,
egress
=
True
,
p
=
2.0
,
k
=
1
,
mp
=
None
):
"""
Initialize the `aampi` object
Parameters
----------
T : numpy.ndarray
The time series or sequence for which the unnormalized matrix profile and
matrix profile indices will be returned
m : int
Window size
egress : bool, default True
If set to `True`, the oldest data point in the time series is removed and
the time series length remains constant rather than forever increasing
p : float, default 2.0
The p-norm to apply for computing the Minkowski distance.
k : int, default 1
The number of top `k` smallest distances used to construct the matrix
profile. Note that this will increase the total computational time and
memory usage when k > 1.
mp : numpy.ndarray, default None
A pre-computed matrix profile (and corresponding matrix profile indices).
This is a 2D array of shape `(len(T) - m + 1, 2 * k + 2)`, where the first
`k` columns are top-k matrix profile, and the next `k` columns are their
corresponding indices. The last two columns correspond to the top-1 left
and top-1 right matrix profile indices. When None (default), this array is
computed internally using `stumpy.aamp`.
Returns
-------
None
"""
self
.
_T
=
core
.
_preprocess
(
T
)
core
.
check_window_size
(
m
,
max_size
=
self
.
_T
.
shape
[
0
])
self
.
_m
=
m
self
.
_n
=
self
.
_T
.
shape
[
0
]
self
.
_excl_zone
=
int
(
np
.
ceil
(
self
.
_m
/
config
.
STUMPY_EXCL_ZONE_DENOM
))
self
.
_egress
=
egress
self
.
_p
=
p
self
.
_k
=
k
if
mp
is
None
:
mp
=
aamp
(
self
.
_T
,
self
.
_m
,
p
=
self
.
_p
,
k
=
self
.
_k
)
else
:
mp
=
mp
.
copy
()
if
mp
.
shape
!=
(
len
(
self
.
_T
)
-
self
.
_m
+
1
,
2
*
self
.
_k
+
2
,
):
# pragma: no cover
msg
=
(
f"The shape of `mp` must match (
{
len
(
T
)
-
m
+
1
}
,
{
2
*
k
+
2
}
) but "
+
f"found
{
mp
.
shape
}
instead."
)
raise
ValueError
(
msg
)
self
.
_P
=
mp
[:, :
self
.
_k
].
astype
(
np
.
float64
)
self
.
_I
=
mp
[:,
self
.
_k
:
2
*
self
.
_k
].
astype
(
np
.
int64
)
self
.
_left_I
=
mp
[:,
2
*
self
.
_k
].
astype
(
np
.
int64
)
self
.
_left_P
=
np
.
full_like
(
self
.
_left_I
,
np
.
inf
,
dtype
=
np
.
float64
)
self
.
_left_P
[:]
=
np
.
inf
self
.
_T_isfinite
=
np
.
isfinite
(
self
.
_T
)
self
.
_T
,
self
.
_T_subseq_isfinite
=
core
.
preprocess_non_normalized
(
self
.
_T
,
self
.
_m
)
# Retrieve the left matrix profile values
# Since each matrix profile value is the minimum between the left and right
# matrix profile values, we can save time by re-computing only the left matrix
# profile value when the matrix profile index is equal to the right matrix
# profile index.
mask
=
self
.
_left_I
==
self
.
_I
[:,
0
]
self
.
_left_P
[
mask
]
=
self
.
_P
[
mask
,
0
]
# Only re-compute the `i`-th left matrix profile value, `self._left_P[i]`,
# when `self._I[i] != self._left_I[i]`
for
i
in
np
.
flatnonzero
(
self
.
_left_I
>=
0
&
~
mask
):
j
=
self
.
_left_I
[
i
]
if
j
>=
0
:
self
.
_left_P
[
i
]
=
np
.
linalg
.
norm
(
self
.
_T
[
i
:
i
+
self
.
_m
]
-
self
.
_T
[
j
:
j
+
self
.
_m
],
ord
=
self
.
_p
)
Q
=
self
.
_T
[
-
self
.
_m
:]
self
.
_p_norm
=
core
.
mass_absolute
(
Q
,
self
.
_T
,
p
=
self
.
_p
)
**
self
.
_p
if
self
.
_egress
:
self
.
_p_norm_new
=
np
.
empty
(
self
.
_p_norm
.
shape
[
0
],
dtype
=
np
.
float64
)
self
.
_n_appended
=
0
def
update
(
self
,
t
):
"""
Append a single new data point, `t`, to the existing time series `T` and update
the non-normalized (i.e., without z-normalization) matrix profile and matrix
profile indices.
Parameters
----------
t : float
A single new data point to be appended to `T`
Returns
-------
None
Notes
-----
`arXiv:1901.05708
\
<https://arxiv.org/pdf/1901.05708.pdf>`__
See Algorithm 1
Note that we have extended this algorithm for AB-joins as well.
"""
if
self
.
_egress
:
self
.
_update_egress
(
t
)
else
:
self
.
_update
(
t
)
def
_update_egress
(
self
,
t
):
"""
Ingress a new data point, egress the oldest data point, and update the matrix
profile and matrix profile indices
Parameters
----------
t : float
A single new data point to be appended to `T`
Returns
-------
None
"""
self
.
_n
=
self
.
_T
.
shape
[
0
]
l
=
self
.
_n
-
self
.
_m
+
1
-
1
# Subtract 1 due to egress
self
.
_T
[:
-
1
]
=
self
.
_T
[
1
:]
self
.
_T
[
-
1
]
=
t
self
.
_n_appended
+=
1
self
.
_p_norm
[:
-
1
]
=
self
.
_p_norm
[
1
:]
S
=
self
.
_T
[
l
:]
t_drop
=
self
.
_T
[
l
-
1
]
self
.
_T_isfinite
[:
-
1
]
=
self
.
_T_isfinite
[
1
:]
self
.
_T_subseq_isfinite
[:
-
1
]
=
self
.
_T_subseq_isfinite
[
1
:]
self
.
_I
[:
-
1
]
=
self
.
_I
[
1
:]
self
.
_P
[:
-
1
]
=
self
.
_P
[
1
:]
self
.
_left_I
[:
-
1
]
=
self
.
_left_I
[
1
:]
self
.
_left_P
[:
-
1
]
=
self
.
_left_P
[
1
:]
if
np
.
isfinite
(
t
):
self
.
_T_isfinite
[
-
1
]
=
True
else
:
self
.
_T_isfinite
[
-
1
]
=
False
t
=
0
self
.
_T
[
-
1
]
=
0
S
[
-
1
]
=
0
self
.
_T_subseq_isfinite
[
-
1
]
=
np
.
all
(
self
.
_T_isfinite
[
-
self
.
_m
:])
self
.
_p_norm_new
[
1
:]
=
(
self
.
_p_norm
[:
l
]
-
np
.
power
(
abs
(
self
.
_T
[:
l
]
-
t_drop
),
self
.
_p
)
+
np
.
power
(
abs
(
self
.
_T
[
self
.
_m
:]
-
t
),
self
.
_p
)
)
self
.
_p_norm_new
[
0
]
=
(
np
.
linalg
.
norm
(
self
.
_T
[:
self
.
_m
]
-
S
[:
self
.
_m
],
ord
=
self
.
_p
)
**
self
.
_p
)
mask
=
self
.
_p_norm_new
<
config
.
STUMPY_P_NORM_THRESHOLD
self
.
_p_norm_new
[
mask
]
=
0
D
=
np
.
power
(
self
.
_p_norm_new
,
1.0
/
self
.
_p
)
D
[
~
self
.
_T_subseq_isfinite
]
=
np
.
inf
if
np
.
any
(
~
self
.
_T_isfinite
[
-
self
.
_m
:]):
D
[:]
=
np
.
inf
core
.
_update_incremental_PI
(
D
,
self
.
_P
,
self
.
_I
,
self
.
_excl_zone
,
n_appended
=
self
.
_n_appended
)
# All neighbors of the last subsequence are on its left. So, its (top-1)
# matrix profile value/index and its left matrix profile value/index must
# be equal.
self
.
_left_P
[
-
1
]
=
self
.
_P
[
-
1
,
0
]
self
.
_left_I
[
-
1
]
=
self
.
_I
[
-
1
,
0
]
self
.
_p_norm
[:]
=
self
.
_p_norm_new
def
_update
(
self
,
t
):
"""
Ingress a new data point and update the (top-k) matrix profile and matrix
profile indices without egressing the oldest data point
Parameters
----------
t : float
A single new data point to be appended to `T`
Returns
-------
None
"""
self
.
_n
=
self
.
_T
.
shape
[
0
]
l
=
self
.
_n
-
self
.
_m
+
1
T_new
=
np
.
append
(
self
.
_T
,
t
)
p_norm_new
=
np
.
empty
(
self
.
_p_norm
.
shape
[
0
]
+
1
,
dtype
=
np
.
float64
)
S
=
T_new
[
l
:]
t_drop
=
T_new
[
l
-
1
]
if
np
.
isfinite
(
t
):
self
.
_T_isfinite
=
np
.
append
(
self
.
_T_isfinite
,
True
)
else
:
self
.
_T_isfinite
=
np
.
append
(
self
.
_T_isfinite
,
False
)
t
=
0
T_new
[
-
1
]
=
0
S
[
-
1
]
=
0
self
.
_T_subseq_isfinite
=
np
.
append
(
self
.
_T_subseq_isfinite
,
np
.
all
(
self
.
_T_isfinite
[
-
self
.
_m
:])
)
p_norm_new
[
1
:]
=
(
self
.
_p_norm
[:
l
]
-
np
.
power
(
abs
(
T_new
[:
l
]
-
t_drop
),
self
.
_p
)
+
np
.
power
(
abs
(
T_new
[
self
.
_m
:]
-
t
),
self
.
_p
)
)
p_norm_new
[
0
]
=
(
np
.
linalg
.
norm
(
T_new
[:
self
.
_m
]
-
S
[:
self
.
_m
],
ord
=
self
.
_p
)
**
self
.
_p
)
mask
=
p_norm_new
<
config
.
STUMPY_P_NORM_THRESHOLD
p_norm_new
[
mask
]
=
0
D
=
np
.
power
(
p_norm_new
,
1.0
/
self
.
_p
)
D
[
~
self
.
_T_subseq_isfinite
]
=
np
.
inf
if
np
.
any
(
~
self
.
_T_isfinite
[
-
self
.
_m
:]):
D
[:]
=
np
.
inf
P_new
=
np
.
full
(
self
.
_k
,
np
.
inf
,
dtype
=
np
.
float64
)
I_new
=
np
.
full
(
self
.
_k
,
-
1
,
dtype
=
np
.
int64
)
self
.
_P
=
np
.
append
(
self
.
_P
,
P_new
.
reshape
(
1
,
-
1
),
axis
=
0
)
self
.
_I
=
np
.
append
(
self
.
_I
,
I_new
.
reshape
(
1
,
-
1
),
axis
=
0
)
core
.
_update_incremental_PI
(
D
,
self
.
_P
,
self
.
_I
,
self
.
_excl_zone
,
n_appended
=
0
)
left_I_new
=
self
.
_I
[
-
1
,
0
]
left_P_new
=
self
.
_P
[
-
1
,
0
]
self
.
_T
=
T_new
self
.
_left_P
=
np
.
append
(
self
.
_left_P
,
left_P_new
)
self
.
_left_I
=
np
.
append
(
self
.
_left_I
,
left_I_new
)
self
.
_p_norm
=
p_norm_new
@
property
def
P_
(
self
):
"""
Get the (top-k) matrix profile. When `k=1` (default), the output is
a 1D array consisting of the matrix profile. When `k > 1`, the
output is a 2D array that has exactly `k` columns and it consists of the
top-k matrix profile.
Parameters
----------
None
Returns
-------
out : numpy.ndarray
The (top-k) matrix profile
"""
if
self
.
_k
==
1
:
return
self
.
_P
.
flatten
().
astype
(
np
.
float64
)
else
:
return
self
.
_P
.
astype
(
np
.
float64
)
@
property
def
I_
(
self
):
"""
Get the (top-k) matrix profile indices. When `k=1` (default), the output is
a 1D array consisting of the matrix profile indices. When `k > 1`, the
output is a 2D array that has exactly `k` columns and it consists of the
top-k matrix profile indices.
Parameters
----------
None
Returns
-------
out : numpy.ndarray
The (top-k) matrix profile indices
"""
if
self
.
_k
==
1
:
return
self
.
_I
.
flatten
().
astype
(
np
.
int64
)
else
:
return
self
.
_I
.
astype
(
np
.
int64
)
@
property
def
left_P_
(
self
):
"""
Get the (top-1) left matrix profile
Parameters
----------
None
Returns
-------
out : numpy.ndarray
The (top-1) left matrix profile
"""
return
self
.
_left_P
.
astype
(
np
.
float64
)
@
property
def
left_I_
(
self
):
"""
Get the (top-1) left matrix profile indices
Parameters
----------
None
Returns
-------
out : numpy.ndarray
The (top-1) left matrix profile indices
"""
return
self
.
_left_I
.
astype
(
np
.
int64
)
@
property
def
T_
(
self
):
"""
Get the time series
Parameters
----------
None
Returns
-------
out : numpy.ndarray
The time series
"""
return
self
.
_T
Back
|
FazBrowse Home
|
New Git URL