-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathaamp.py
More file actions
442 lines (358 loc) · 14.2 KB
/
Copy pathaamp.py
File metadata and controls
442 lines (358 loc) · 14.2 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
# 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 numba
import numpy as np
from numba import njit, prange
from . import config, core
from .mparray import mparray
@njit(
# "(f8[:], f8[:], i8, b1[:], b1[:], f8, i8[:], i8, i8, i8, f8[:, :, :],"
# "f8[:, :], f8[:, :], i8[:, :, :], i8[:, :], i8[:, :], b1)",
fastmath=config.STUMPY_FASTMATH_FLAGS,
)
def _compute_diagonal(
T_A,
T_B,
m,
T_A_subseq_isfinite,
T_B_subseq_isfinite,
p,
diags,
diags_start_idx,
diags_stop_idx,
thread_idx,
P,
PL,
PR,
I,
IL,
IR,
ignore_trivial,
):
"""
Compute (Numba JIT-compiled) and update the (top-k) matrix profile P,
PL, PR, I, IL, and IR sequentially along individual diagonals using a single
thread and avoiding race conditions.
Parameters
----------
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 diag of diagonals to process and compute
diags_start_idx : int
The start index for a range of diagonal diag to process and compute
diags_stop_idx : int
The (exclusive) stop index for a range of diagonal diag to process and compute
thread_idx : int
The thread index
P : numpy.ndarray
The (top-k) matrix profile, sorted in ascending order per row
PL : numpy.ndarray
The top-1 left marix profile
PR : numpy.ndarray
The top-1 right marix profile
I : numpy.ndarray
The (top-k) matrix profile indices
IL : numpy.ndarray
The top-1 left matrix profile indices
IR : numpy.ndarray
The top-1 right matrix profile indices
ignore_trivial : bool
Set to `True` if this is a self-join. Otherwise, for AB-join, set this to
`False`. Default is `True`.
Returns
-------
None
"""
n_A = T_A.shape[0]
n_B = T_B.shape[0]
uint64_m = np.uint64(m)
uint64_1 = np.uint64(1)
for diag_idx in range(diags_start_idx, diags_stop_idx):
g = diags[diag_idx]
if g >= 0:
iter_range = range(0, min(n_A - m + 1, n_B - m + 1 - g))
else:
iter_range = range(-g, min(n_A - m + 1, n_B - m + 1 - g))
for i in iter_range:
uint64_i = np.uint64(i)
uint64_j = np.uint64(i + g)
if uint64_i == 0 or uint64_j == 0:
p_norm = (
np.linalg.norm(
T_B[uint64_j : uint64_j + uint64_m]
- T_A[uint64_i : uint64_i + uint64_m],
ord=p,
)
** p
)
else:
p_norm = np.abs(
p_norm
- np.absolute(T_B[uint64_j - uint64_1] - T_A[uint64_i - uint64_1])
** p
+ np.absolute(
T_B[uint64_j + uint64_m - uint64_1]
- T_A[uint64_i + uint64_m - uint64_1]
)
** p
)
if p_norm < config.STUMPY_P_NORM_THRESHOLD:
p_norm = 0.0
if T_A_subseq_isfinite[uint64_i] and T_B_subseq_isfinite[uint64_j]:
# Neither subsequence contains NaNs
# `P[thread_idx, i, :]` is sorted in ascending order and MUST be updated
# when the newly-calculated `p_norm` value becomes smaller than the
# last (i.e. greatest) element in this array. Note that the goal
# is to have top-k smallest distances for each subsequence.
if p_norm < P[thread_idx, uint64_i, -1]:
idx = np.searchsorted(P[thread_idx, uint64_i], p_norm)
core._shift_insert_at_index(
P[thread_idx, uint64_i], idx, p_norm, shift="right"
)
core._shift_insert_at_index(
I[thread_idx, uint64_i], idx, uint64_j, shift="right"
)
if ignore_trivial: # self-joins only
if p_norm < P[thread_idx, uint64_j, -1]:
idx = np.searchsorted(P[thread_idx, uint64_j], p_norm)
core._shift_insert_at_index(
P[thread_idx, uint64_j], idx, p_norm, shift="right"
)
core._shift_insert_at_index(
I[thread_idx, uint64_j], idx, uint64_i, shift="right"
)
if uint64_i < uint64_j:
# left matrix profile and left matrix profile index
if p_norm < PL[thread_idx, uint64_j]:
PL[thread_idx, uint64_j] = p_norm
IL[thread_idx, uint64_j] = uint64_i
# right matrix profile and right matrix profile index
if p_norm < PR[thread_idx, uint64_i]:
PR[thread_idx, uint64_i] = p_norm
IR[thread_idx, uint64_i] = uint64_j
return
@njit(
# "(f8[:], f8[:], i8, b1[:], b1[:], i8[:], b1, i8)",
parallel=True,
fastmath=config.STUMPY_FASTMATH_FLAGS,
)
def _aamp(
T_A,
T_B,
m,
T_A_subseq_isfinite,
T_B_subseq_isfinite,
p,
diags,
ignore_trivial,
k,
):
"""
A Numba JIT-compiled version of AAMP for parallel computation of the matrix
profile and matrix profile indices.
Parameters
----------
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 diag of diagonals to process and compute
ignore_trivial : bool
Set to `True` if this is a self-join. Otherwise, for AB-join, set this to
`False`. Default is `True`.
k : int
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
-------
out1 : numpy.ndarray
The (top-k) matrix profile
out2 : numpy.ndarray
The (top-1) left matrix profile
out3 : numpy.ndarray
The (top-1) right matrix profile
out4 : numpy.ndarray
The (top-k) matrix profile indices
out5 : numpy.ndarray
The (top-1) left matrix profile indices
out6 : numpy.ndarray
The (top-1) right matrix profile indices
Notes
-----
`DOI: 10.1109/ICDM.2018.00099 \
<https://www.cs.ucr.edu/~eamonn/SCRIMP_ICDM_camera_ready_updated.pdf>`__
See Algorithm 1
"""
n_A = T_A.shape[0]
n_B = T_B.shape[0]
l = n_A - m + 1
n_threads = numba.config.NUMBA_NUM_THREADS
P = np.full((n_threads, l, k), np.inf, dtype=np.float64)
I = np.full((n_threads, l, k), -1, dtype=np.int64)
PL = np.full((n_threads, l), np.inf, dtype=np.float64)
IL = np.full((n_threads, l), -1, dtype=np.int64)
PR = np.full((n_threads, l), np.inf, dtype=np.float64)
IR = np.full((n_threads, l), -1, dtype=np.int64)
ndist_counts = core._count_diagonal_ndist(diags, m, n_A, n_B)
diags_ranges = core._get_array_ranges(ndist_counts, n_threads, False)
for thread_idx in prange(n_threads):
# Compute and update P, I within a single thread while avoiding race conditions
_compute_diagonal(
T_A,
T_B,
m,
T_A_subseq_isfinite,
T_B_subseq_isfinite,
p,
diags,
diags_ranges[thread_idx, 0],
diags_ranges[thread_idx, 1],
thread_idx,
P,
PL,
PR,
I,
IL,
IR,
ignore_trivial,
)
# Reduction of results from all threads
for thread_idx in range(1, n_threads):
# update top-k arrays
core._merge_topk_PI(P[0], P[thread_idx], I[0], I[thread_idx])
# update left matrix profile and matrix profile indices
mask = PL[0] > PL[thread_idx]
PL[0][mask] = PL[thread_idx][mask]
IL[0][mask] = IL[thread_idx][mask]
# update right matrix profile and matrix profile indices
mask = PR[0] > PR[thread_idx]
PR[0][mask] = PR[thread_idx][mask]
IR[0][mask] = IR[thread_idx][mask]
return (
np.power(P[0], 1.0 / p),
np.power(PL[0], 1.0 / p),
np.power(PR[0], 1.0 / p),
I[0],
IL[0],
IR[0],
)
def aamp(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
This is a convenience wrapper around the Numba JIT-compiled parallelized
`_aamp` function which computes the matrix profile according to AAMP.
Parameters
----------
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]
l = n_A - m + 1
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: # self-join
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: # AB-join
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)
P, PL, PR, I, IL, IR = _aamp(
T_A,
T_B,
m,
T_A_subseq_isfinite,
T_B_subseq_isfinite,
p,
diags,
ignore_trivial,
k,
)
out = np.empty((l, 2 * k + 2), dtype=object)
out[:, :k] = P
out[:, k:] = np.column_stack((I, IL, IR))
core._check_P(out[:, 0])
return mparray(out, m, k, config.STUMPY_EXCL_ZONE_DENOM)