FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stumpy/stumpy/aamped.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
/
aamped.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
420 lines (342 loc) · 14.6 KB
Breadcrumbs
stumpy
/
stumpy
/
aamped.py
Copy path
File metadata and controls
420 lines (342 loc) · 14.6 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
# 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 inspect
import
numpy
as
np
from
.
import
config
,
core
from
.
aamp
import
_aamp
from
.
mparray
import
mparray
def
_dask_aamped
(
dask_client
,
T_A
,
T_B
,
m
,
T_A_subseq_isfinite
,
T_B_subseq_isfinite
,
p
,
diags
,
ignore_trivial
,
k
,
):
"""
Compute the non-normalized (i.e., without z-normalization) matrix profile with a
`dask` cluster
This is a highly distributed implementation around the Numba JIT-compiled
parallelized `_aamp` function which computes the non-normalized matrix profile
according to AAMP.
Parameters
----------
dask_client : client
A `dask` client. Setting up a cluster is beyond the scope of this library.
Please refer to the `dask` documentation.
T_A : numpy.ndarray
The time series or sequence for which to compute the matrix profile
T_B : numpy.ndarray
The time series or sequence that will be used to annotate T_A. For every
subsequence in T_A, its nearest neighbor in T_B will be recorded.
m : int
Window size
T_A_subseq_isfinite : numpy.ndarray
A boolean array that indicates whether a subsequence in `T_A` contains a
`np.nan`/`np.inf` value (False)
T_B_subseq_isfinite : numpy.ndarray
A boolean array that indicates whether a subsequence in `T_B` contains a
`np.nan`/`np.inf` value (False)
p : float
The p-norm to apply for computing the Minkowski distance. Minkowski distance is
typically used with `p` being 1 or 2, which correspond to the Manhattan distance
and the Euclidean distance, respectively.
diags : numpy.ndarray
The diagonal indices
ignore_trivial : bool, default True
Set to `True` if this is a self-join. Otherwise, for AB-join, set this
to `False`. Default is `True`.
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. If you have access to a GPU device, then you may be able to
leverage `gpu_stump` for better performance and scalability.
Returns
-------
out : numpy.ndarray
When k = 1 (default), the first column consists of the matrix profile,
the second column consists of the matrix profile indices, the third column
consists of the left matrix profile indices, and the fourth column consists
of the right matrix profile indices. However, when k > 1, the output array
will contain exactly 2 * k + 2 columns. The first k columns (i.e., out[:, :k])
consists of the top-k matrix profile, the next set of k columns
(i.e., out[:, k:2k]) consists of the corresponding top-k matrix profile
indices, and the last two columns (i.e., out[:, 2k] and out[:, 2k+1] or,
equivalently, out[:, -2] and out[:, -1]) correspond to the top-1 left
matrix profile indices and the top-1 right matrix profile indices, respectively.
"""
n_A
=
T_A
.
shape
[
0
]
n_B
=
T_B
.
shape
[
0
]
l
=
n_A
-
m
+
1
hosts
=
list
(
dask_client
.
ncores
().
keys
())
nworkers
=
len
(
hosts
)
ndist_counts
=
core
.
_count_diagonal_ndist
(
diags
,
m
,
n_A
,
n_B
)
diags_ranges
=
core
.
_get_array_ranges
(
ndist_counts
,
nworkers
,
False
)
diags_ranges
+=
diags
[
0
]
# Scatter data to Dask cluster
T_A_future
=
dask_client
.
scatter
(
T_A
,
broadcast
=
True
,
hash
=
False
)
T_B_future
=
dask_client
.
scatter
(
T_B
,
broadcast
=
True
,
hash
=
False
)
T_A_subseq_isfinite_future
=
dask_client
.
scatter
(
T_A_subseq_isfinite
,
broadcast
=
True
,
hash
=
False
)
T_B_subseq_isfinite_future
=
dask_client
.
scatter
(
T_B_subseq_isfinite
,
broadcast
=
True
,
hash
=
False
)
diags_futures
=
[]
for
i
,
host
in
enumerate
(
hosts
):
diags_future
=
dask_client
.
scatter
(
np
.
arange
(
diags_ranges
[
i
,
0
],
diags_ranges
[
i
,
1
],
dtype
=
np
.
int64
),
workers
=
[
host
],
hash
=
False
,
)
diags_futures
.
append
(
diags_future
)
futures
=
[]
for
i
in
range
(
len
(
hosts
)):
futures
.
append
(
dask_client
.
submit
(
_aamp
,
T_A_future
,
T_B_future
,
m
,
T_A_subseq_isfinite_future
,
T_B_subseq_isfinite_future
,
p
,
diags_futures
[
i
],
ignore_trivial
,
k
,
)
)
results
=
dask_client
.
gather
(
futures
)
profile
,
profile_L
,
profile_R
,
indices
,
indices_L
,
indices_R
=
results
[
0
]
for
i
in
range
(
1
,
len
(
hosts
)):
P
,
PL
,
PR
,
I
,
IL
,
IR
=
results
[
i
]
# Update top-k matrix profile and matrix profile indices
core
.
_merge_topk_PI
(
profile
,
P
,
indices
,
I
)
# Update top-1 left matrix profile and matrix profile index
mask
=
PL
<
profile_L
profile_L
[
mask
]
=
PL
[
mask
]
indices_L
[
mask
]
=
IL
[
mask
]
# Update top-1 right matrix profile and matrix profile index
mask
=
PR
<
profile_R
profile_R
[
mask
]
=
PR
[
mask
]
indices_R
[
mask
]
=
IR
[
mask
]
out
=
np
.
empty
((
l
,
2
*
k
+
2
),
dtype
=
object
)
out
[:, :
k
]
=
profile
out
[:,
k
:
2
*
k
+
2
]
=
np
.
column_stack
((
indices
,
indices_L
,
indices_R
))
return
out
def
_ray_aamped
(
ray_client
,
T_A
,
T_B
,
m
,
T_A_subseq_isfinite
,
T_B_subseq_isfinite
,
p
,
diags
,
ignore_trivial
,
k
,
):
"""
Compute the non-normalized (i.e., without z-normalization) matrix profile with a
`ray` cluster
This is a highly distributed implementation around the Numba JIT-compiled
parallelized `_aamp` function which computes the non-normalized matrix profile
according to AAMP.
Parameters
----------
ray_client : client
A `ray` client. Setting up a cluster is beyond the scope of this library.
Please refer to the `ray` documentation.
T_A : numpy.ndarray
The time series or sequence for which to compute the matrix profile
T_B : numpy.ndarray
The time series or sequence that will be used to annotate T_A. For every
subsequence in T_A, its nearest neighbor in T_B will be recorded.
m : int
Window size
T_A_subseq_isfinite : numpy.ndarray
A boolean array that indicates whether a subsequence in `T_A` contains a
`np.nan`/`np.inf` value (False)
T_B_subseq_isfinite : numpy.ndarray
A boolean array that indicates whether a subsequence in `T_B` contains a
`np.nan`/`np.inf` value (False)
p : float
The p-norm to apply for computing the Minkowski distance. Minkowski distance is
typically used with `p` being 1 or 2, which correspond to the Manhattan distance
and the Euclidean distance, respectively.
diags : numpy.ndarray
The diagonal indices
ignore_trivial : bool, default True
Set to `True` if this is a self-join. Otherwise, for AB-join, set this
to `False`. Default is `True`.
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. If you have access to a GPU device, then you may be able to
leverage `gpu_stump` for better performance and scalability.
Returns
-------
out : numpy.ndarray
When k = 1 (default), the first column consists of the matrix profile,
the second column consists of the matrix profile indices, the third column
consists of the left matrix profile indices, and the fourth column consists
of the right matrix profile indices. However, when k > 1, the output array
will contain exactly 2 * k + 2 columns. The first k columns (i.e., out[:, :k])
consists of the top-k matrix profile, the next set of k columns
(i.e., out[:, k:2k]) consists of the corresponding top-k matrix profile
indices, and the last two columns (i.e., out[:, 2k] and out[:, 2k+1] or,
equivalently, out[:, -2] and out[:, -1]) correspond to the top-1 left
matrix profile indices and the top-1 right matrix profile indices, respectively.
"""
core
.
check_ray
(
ray_client
)
n_A
=
T_A
.
shape
[
0
]
n_B
=
T_B
.
shape
[
0
]
l
=
n_A
-
m
+
1
nworkers
=
core
.
get_ray_nworkers
(
ray_client
)
ndist_counts
=
core
.
_count_diagonal_ndist
(
diags
,
m
,
n_A
,
n_B
)
diags_ranges
=
core
.
_get_array_ranges
(
ndist_counts
,
nworkers
,
False
)
diags_ranges
+=
diags
[
0
]
# Scatter data to Ray cluster
T_A_ref
=
ray_client
.
put
(
T_A
)
T_B_ref
=
ray_client
.
put
(
T_B
)
T_A_subseq_isfinite_ref
=
ray_client
.
put
(
T_A_subseq_isfinite
)
T_B_subseq_isfinite_ref
=
ray_client
.
put
(
T_B_subseq_isfinite
)
diags_refs
=
[]
for
i
in
range
(
nworkers
):
diags_ref
=
ray_client
.
put
(
np
.
arange
(
diags_ranges
[
i
,
0
],
diags_ranges
[
i
,
1
],
dtype
=
np
.
int64
)
)
diags_refs
.
append
(
diags_ref
)
ray_aamp_func
=
ray_client
.
remote
(
core
.
deco_ray_tor
(
_aamp
))
refs
=
[]
for
i
in
range
(
nworkers
):
refs
.
append
(
ray_aamp_func
.
remote
(
T_A_ref
,
T_B_ref
,
m
,
T_A_subseq_isfinite_ref
,
T_B_subseq_isfinite_ref
,
p
,
diags_refs
[
i
],
ignore_trivial
,
k
,
)
)
results
=
ray_client
.
get
(
refs
)
# Must make a mutable copy from Ray's object store (ndarrays are immutable)
profile
,
profile_L
,
profile_R
,
indices
,
indices_L
,
indices_R
=
[
arr
.
copy
()
for
arr
in
results
[
0
]
]
for
i
in
range
(
1
,
nworkers
):
P
,
PL
,
PR
,
I
,
IL
,
IR
=
results
[
i
]
# Read-only variables
# Update top-k matrix profile and matrix profile indices
core
.
_merge_topk_PI
(
profile
,
P
,
indices
,
I
)
# Update top-1 left matrix profile and matrix profile index
mask
=
PL
<
profile_L
profile_L
[
mask
]
=
PL
[
mask
]
indices_L
[
mask
]
=
IL
[
mask
]
# Update top-1 right matrix profile and matrix profile index
mask
=
PR
<
profile_R
profile_R
[
mask
]
=
PR
[
mask
]
indices_R
[
mask
]
=
IR
[
mask
]
out
=
np
.
empty
((
l
,
2
*
k
+
2
),
dtype
=
object
)
out
[:, :
k
]
=
profile
out
[:,
k
:
2
*
k
+
2
]
=
np
.
column_stack
((
indices
,
indices_L
,
indices_R
))
return
out
def
aamped
(
client
,
T_A
,
m
,
T_B
=
None
,
ignore_trivial
=
True
,
p
=
2.0
,
k
=
1
):
"""
Compute the non-normalized (i.e., without z-normalization) matrix profile
with a `dask`/`ray` cluster
This is a highly distributed implementation around the Numba JIT-compiled
parallelized `_aamp` function which computes the non-normalized matrix profile
according to AAMP.
Parameters
----------
client : client
A `dask`/`ray` client. Setting up a cluster is beyond the scope of this library.
Please refer to the `dask`/`ray` documentation.
T_A : numpy.ndarray
The time series or sequence for which to compute the matrix profile
m : int
Window size
T_B : numpy.ndarray, default None
The time series or sequence that will be used to annotate T_A. For every
subsequence in T_A, its nearest neighbor in T_B will be recorded. Default is
`None` which corresponds to a self-join.
ignore_trivial : bool, default True
Set to `True` if this is a self-join. Otherwise, for AB-join, set this
to `False`. Default is `True`.
p : float, default 2.0
The p-norm to apply for computing the Minkowski distance. Minkowski distance is
typically used with `p` being 1 or 2, which correspond to the Manhattan distance
and the Euclidean distance, respectively.
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.
Returns
-------
out : numpy.ndarray
When k = 1 (default), the first column consists of the matrix profile,
the second column consists of the matrix profile indices, the third column
consists of the left matrix profile indices, and the fourth column consists
of the right matrix profile indices. However, when k > 1, the output array
will contain exactly 2 * k + 2 columns. The first k columns (i.e., out[:, :k])
consists of the top-k matrix profile, the next set of k columns
(i.e., out[:, k:2k]) consists of the corresponding top-k matrix profile
indices, and the last two columns (i.e., out[:, 2k] and out[:, 2k+1] or,
equivalently, out[:, -2] and out[:, -1]) correspond to the top-1 left
matrix profile indices and the top-1 right matrix profile indices, respectively.
For convenience, the matrix profile (distances) and matrix profile indices can
also be accessed via their corresponding named array attributes, `.P_` and
`.I_`,respectively. Similarly, the corresponding left matrix profile indices
and right matrix profile indices may also be accessed via the `.left_I_` and
`.right_I_` array attributes.
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
T_B
is
None
:
T_B
=
T_A
.
copy
()
core
.
check_self_join
(
ignore_trivial
)
ignore_trivial
=
True
T_A
,
T_A_subseq_isfinite
=
core
.
preprocess_non_normalized
(
T_A
,
m
)
T_B
,
T_B_subseq_isfinite
=
core
.
preprocess_non_normalized
(
T_B
,
m
)
if
T_A
.
ndim
!=
1
:
# pragma: no cover
raise
ValueError
(
f"T_A is
{
T_A
.
ndim
}
-dimensional and must be 1-dimensional. "
)
if
T_B
.
ndim
!=
1
:
# pragma: no cover
raise
ValueError
(
f"T_B is
{
T_B
.
ndim
}
-dimensional and must be 1-dimensional. "
)
n_A
=
T_A
.
shape
[
0
]
n_B
=
T_B
.
shape
[
0
]
ignore_trivial
=
core
.
check_ignore_trivial
(
T_A
,
T_B
,
ignore_trivial
)
excl_zone
=
int
(
np
.
ceil
(
m
/
config
.
STUMPY_EXCL_ZONE_DENOM
))
if
ignore_trivial
:
core
.
check_window_size
(
m
,
max_size
=
min
(
n_A
,
n_B
),
n
=
n_A
)
diags
=
np
.
arange
(
excl_zone
+
1
,
n_A
-
m
+
1
,
dtype
=
np
.
int64
)
else
:
core
.
check_window_size
(
m
,
max_size
=
min
(
n_A
,
n_B
))
diags
=
np
.
arange
(
-
(
n_A
-
m
+
1
)
+
1
,
n_B
-
m
+
1
,
dtype
=
np
.
int64
)
_aamped
=
core
.
_client_to_func
(
client
)
out
=
_aamped
(
client
,
T_A
,
T_B
,
m
,
T_A_subseq_isfinite
,
T_B_subseq_isfinite
,
p
,
diags
,
ignore_trivial
,
k
,
)
core
.
_check_P
(
out
[:,
0
])
return
mparray
(
out
,
m
,
k
,
config
.
STUMPY_EXCL_ZONE_DENOM
)
Back
|
FazBrowse Home
|
New Git URL