forked from target/matrixprofile-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixProfile.py
276 lines (205 loc) · 11 KB
/
matrixProfile.py
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
range = getattr(__builtins__, 'xrange', range)
# end of py2 compatability boilerplate
from . import distanceProfile
from . import order
from .utils import mass, movmeanstd
import numpy as np
import multiprocessing
from functools import partial
import math
def _self_join_or_not_preprocess(tsA, tsB, m):
"""
Core method for determining if a self join is occuring and returns appropriate
profile and index numpy arrays with correct dimensions as all np.nan values.
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
tsB: Time series to compare the query against. Note that, if no value is provided, ts_b = ts_a by default.
m: Length of subsequence to compare.
"""
n = len(tsA)
if tsB is not None:
n = len(tsB)
shape = n - m + 1
return (np.full(shape, np.inf), np.full(shape, np.inf))
def _matrixProfile(tsA,m,orderClass,distanceProfileFunction,tsB=None):
"""
Core method for calculating the Matrix Profile
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
m: Length of subsequence to compare.
orderClass: Method defining the order in which distance profiles are calculated.
distanceProfileFunction: Method for calculating individual distance profiles.
sampling: The percentage of all possible distance profiles to sample for the final Matrix Profile.
"""
order = orderClass(len(tsA)-m+1)
mp, mpIndex = _self_join_or_not_preprocess(tsA, tsB, m)
idx=order.next()
while idx != None:
(distanceProfile,querySegmentsID) = distanceProfileFunction(tsA,idx,m,tsB)
#Check which of the indices have found a new minimum
idsToUpdate = distanceProfile < mp
#Update the Matrix Profile Index to indicate that the current index is the minimum location for the aforementioned indices
mpIndex[idsToUpdate] = querySegmentsID[idsToUpdate]
#Update the matrix profile to include the new minimum values (where appropriate)
mp = np.minimum(mp,distanceProfile)
idx = order.next()
return (mp,mpIndex)
def _stamp_parallel(tsA, m, tsB=None, sampling=0.2, n_threads=-1, random_state=None):
"""
Computes distance profiles in parallel using all CPU cores by default.
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
m: Length of subsequence to compare.
tsB: Time series to compare the query against. Note that, if no value is provided, tsB = tsA by default.
sampling: The percentage of all possible distance profiles to sample for the final Matrix Profile. 0 to 1
n_threads: Number of threads to use in parallel mode. Defaults to using all CPU cores.
random_state: Set the random seed generator for reproducible results.
"""
if n_threads is -1:
n_threads = multiprocessing.cpu_count()
n = len(tsA)
mp, mpIndex = _self_join_or_not_preprocess(tsA, tsB, m)
# determine sampling size
sample_size = math.ceil((n - m + 1) * sampling)
# generate indices to sample and split based on n_threads
if random_state is not None:
np.random.seed(random_state)
indices = np.arange(n - m + 1)
indices = np.random.choice(indices, size=sample_size, replace=False)
indices = np.array_split(indices, n_threads)
# create pool of workers and compute
with multiprocessing.Pool(processes=n_threads) as pool:
func = partial(distanceProfile.mass_distance_profile_parallel, tsA=tsA, tsB=tsB, m=m)
results = pool.map(func, indices)
# The overall matrix profile is the element-wise minimum of each sub-profile, and each element of the overall
# matrix profile index is the time series position of the corresponding sub-profile.
for result in results:
for dp, querySegmentsID in result:
#Check which of the indices have found a new minimum
idsToUpdate = dp < mp
#Update the Matrix Profile Index to indicate that the current index is the minimum location for the aforementioned indices
mpIndex[idsToUpdate] = querySegmentsID[idsToUpdate]
#Update the matrix profile to include the new minimum values (where appropriate)
mp = np.minimum(mp, dp)
return (mp, mpIndex)
def _matrixProfile_sampling(tsA,m,orderClass,distanceProfileFunction,tsB=None,sampling=0.2,random_state=None):
order = orderClass(len(tsA)-m+1, random_state=random_state)
mp, mpIndex = _self_join_or_not_preprocess(tsA, tsB, m)
idx=order.next()
#Define max numbers of iterations to sample
iters = (len(tsA)-m+1)*sampling
iter_val = 0
while iter_val < iters:
(distanceProfile,querySegmentsID) = distanceProfileFunction(tsA,idx,m,tsB)
#Check which of the indices have found a new minimum
idsToUpdate = distanceProfile < mp
#Update the Matrix Profile Index to indicate that the current index is the minimum location for the aforementioned indices
mpIndex[idsToUpdate] = querySegmentsID[idsToUpdate]
#Update the matrix profile to include the new minimum values (where appropriate)
mp = np.minimum(mp,distanceProfile)
idx = order.next()
iter_val += 1
return (mp,mpIndex)
#Write matrix profile function for STOMP and then consolidate later! (aka link to the previous distance profile)
def _matrixProfile_stomp(tsA,m,orderClass,distanceProfileFunction,tsB=None):
order = orderClass(len(tsA)-m+1)
mp, mpIndex = _self_join_or_not_preprocess(tsA, tsB, m)
idx=order.next()
#Get moving mean and standard deviation
mean, std = movmeanstd(tsA,m)
#Initialize code to set dot_prev to None for the first pass
dp = None
#Initialize dot_first to None for the first pass
dot_first = None
while idx != None:
#Need to pass in the previous sliding dot product for subsequent distance profile calculations
(distanceProfile,querySegmentsID),dot_prev = distanceProfileFunction(tsA,idx,m,tsB,dot_first,dp,mean,std)
if idx == 0:
dot_first = dot_prev
#Check which of the indices have found a new minimum
idsToUpdate = distanceProfile < mp
#Update the Matrix Profile Index to indicate that the current index is the minimum location for the aforementioned indices
mpIndex[idsToUpdate] = querySegmentsID[idsToUpdate]
#Update the matrix profile to include the new minimum values (where appropriate)
mp = np.minimum(mp,distanceProfile)
idx = order.next()
dp = dot_prev
return (mp,mpIndex)
def stampi_update(tsA,m,mp,mpIndex,newval,tsB=None,distanceProfileFunction=distanceProfile.massDistanceProfile):
'''Updates the self-matched matrix profile for a time series TsA with the arrival of a new data point newval. Note that comparison of two separate time-series with new data arriving will be built later -> currently, tsB should be set to tsA'''
#Update time-series array with recent value
tsA_new = np.append(np.copy(tsA),newval)
#Expand matrix profile and matrix profile index to include space for latest point
mp_new= np.append(np.copy(mp),np.inf)
mpIndex_new = np.append(np.copy(mpIndex),np.inf)
#Determine new index value
idx = len(tsA_new)-m
(distanceProfile,querySegmentsID) = distanceProfileFunction(tsA_new,idx,m,tsB)
#Check which of the indices have found a new minimum
idsToUpdate = distanceProfile < mp_new
#Update the Matrix Profile Index to indicate that the current index is the minimum location for the aforementioned indices
mpIndex_new[idsToUpdate] = querySegmentsID[idsToUpdate]
#Update the matrix profile to include the new minimum values (where appropriate)
mp_final = np.minimum(np.copy(mp_new),distanceProfile)
#Finally, set the last value in the matrix profile to the minimum of the distance profile (with corresponding index)
mp_final[-1] = np.min(distanceProfile)
mpIndex_new[-1] = np.argmin(distanceProfile)
return (mp_final,mpIndex_new)
def naiveMP(tsA,m,tsB=None):
"""
Calculate the Matrix Profile using the naive all-pairs calculation.
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
m: Length of subsequence to compare.
tsB: Time series to compare the query against. Note that, if no value is provided, tsB = tsA by default.
"""
return _matrixProfile(tsA,m,order.linearOrder,distanceProfile.naiveDistanceProfile,tsB)
def stmp(tsA,m,tsB=None):
"""
Calculate the Matrix Profile using the more efficient MASS calculation. Distance profiles are computed linearly across every time series index.
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
m: Length of subsequence to compare.
tsB: Time series to compare the query against. Note that, if no value is provided, tsB = tsA by default.
"""
return _matrixProfile(tsA,m,order.linearOrder,distanceProfile.massDistanceProfile,tsB)
def stamp(tsA,m,tsB=None,sampling=0.2, n_threads=None, random_state=None):
"""
Calculate the Matrix Profile using the more efficient MASS calculation. Distance profiles are computed in a random order.
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
m: Length of subsequence to compare.
tsB: Time series to compare the query against. Note that, if no value is provided, tsB = tsA by default.
sampling: The percentage of all possible distance profiles to sample for the final Matrix Profile. 0 to 1
n_threads: Number of threads to use in parallel mode. Defaults to single threaded mode. Set to -1 to use all threads.
random_state: Set the random seed generator for reproducible results.
"""
if sampling > 1 or sampling < 0:
raise ValueError('Sampling value must be a percentage in decimal format from 0 to 1.')
if n_threads is None:
return _matrixProfile_sampling(tsA,m,order.randomOrder,distanceProfile.massDistanceProfile,tsB,sampling=sampling,random_state=random_state)
return _stamp_parallel(tsA, m, tsB=tsB, sampling=sampling, n_threads=n_threads, random_state=random_state)
def stomp(tsA,m,tsB=None):
"""
Calculate the Matrix Profile using the more efficient MASS calculation. Distance profiles are computed according to the directed STOMP procedure.
Parameters
----------
tsA: Time series containing the queries for which to calculate the Matrix Profile.
m: Length of subsequence to compare.
tsB: Time series to compare the query against. Note that, if no value is provided, tsB = tsA by default.
"""
return _matrixProfile_stomp(tsA,m,order.linearOrder,distanceProfile.STOMPDistanceProfile,tsB)
if __name__ == "__main__":
import doctest
doctest.method()