-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_distances.py
416 lines (335 loc) · 10.2 KB
/
utils_distances.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
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
import numpy as np
from aeon.distances import dtw_distance
# from tslearn.metrics import dtw, lcss, soft_dtw
from tslearn.metrics import dtw as dtw_d_tslearn
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
def baseline_distance(multivariate_signal_1, multivariate_signal_2):
"""Baseline: distance between the difference in lengths (which is the number of samples)."""
return np.abs(len(multivariate_signal_2) - len(multivariate_signal_1))
def dtw_i_tslearn(
X1: np.ndarray,
X2: np.ndarray,
):
"""
Multivariate distance with independent strategy, built on the univariate
(dependent) distance.
Simply sums over the results of applying the distance separately to each
dimension.
The inputs must be multivariate.
X1 and X2 are of shape (n_timepoints, n_channels), as in tslearn
"""
if len(X1.shape) <= 1 or len(X2.shape) <= 1:
raise TypeError("The inputs must be multivariate.")
if X1.shape[1] != X2.shape[1]:
raise ValueError("The inputs must have the same dimension.")
total_d = X1.shape[1] # dimension
dtw_cost = 0
for d in range(total_d):
dtw_cost += dtw_d_tslearn(
np.array(X1[:, d]),
np.array(X2[:, d]),
)
return dtw_cost
def multivariate_euclidean(X1, X2):
return np.linalg.norm(X1 - X2)
def derivate_ts(X):
"""
For DDTW.
TODO: look into sktime.transformations.series.difference
"""
X_deriv = list()
n = len(X)
for i in range(1, n - 1):
elem = X[i] - X[i - 1] + (X[i + 1] - X[i - 1]) / 2
X_deriv.append(elem)
return np.array(X_deriv)
def set_weight(i, j, g, L):
"""
For WDTW: modified logistic weight function (MLWF).
"""
weight_max = 1
return weight_max / (1 + np.exp(-g * (abs(i - j) - L) / 2))
def build_weight_matrix(i, j, g, L):
"""
For WDTW.
"""
M = np.zeros((L, L))
for i in range(L):
for j in range(L):
M[i, j] = set_weight(i, j, g, L)
return M
def multivariate_dtw_dependent(
X1: np.ndarray,
X2: np.ndarray,
is_weighted: bool = False,
g: float = None,
is_derivate: bool = False,
):
"""
Multivariate DTW with dependent strategy. Included variants:
- DTW
- WDTW
- DDTW
- WDDTW (first derivated, then weighted)
Can also be considered as univariate DTW if the inputs are univariate.
The inputs can be univariate or multivariate.
TODO:
- include a window size
- for WDTW, choose `g` using cross-validation (requires supervised setting)
Parameters
----------
X1 : array-like of shape (n_samples_1, d)
First multivariate signal
X2 : array-like of shape (n_samples_2, d)
Second multivariate signal
is_weighted : bool, default=False
True when the WDTW (Weighted DTW) is called.
g : float or None, default=None
For WDTW.
is_derivate : bool, default=False
True when the DDTW (Derivate DTW) is called.
"""
if type(X1) != type(X2):
raise TypeError("The inputs must have the same type.")
if is_weighted:
if g is None:
raise ValueError("When using WDTW, set `g`.")
if len(X1) != len(X2):
raise ValueError("When using WDTW, the inputs must have the same length.")
if is_derivate:
X1 = derivate_ts(X1)
X2 = derivate_ts(X2)
n_samples_1 = len(X1)
n_samples_2 = len(X2)
D = np.zeros((n_samples_1, n_samples_2))
C = np.zeros((n_samples_1, n_samples_2))
if is_weighted:
weight_matrix = np.zeros((n_samples_1, n_samples_1))
for i in range(n_samples_1):
for j in range(n_samples_1):
weight_matrix[i, j] = set_weight(i, j, g, n_samples_1)
for i in range(n_samples_1):
for j in range(n_samples_2):
D[i, j] = np.sum((X1[i] - X2[j]) ** 2)
C[0, :] = D[0, :]
C[:, 0] = D[:, 0]
if not (is_weighted):
for i in range(1, n_samples_1):
for j in range(1, n_samples_2):
C[i, j] = D[i, j] + min(C[i - 1, j - 1], C[i - 1, j], C[i, j - 1])
else:
for i in range(1, n_samples_1):
for j in range(1, n_samples_2):
C[i, j] = weight_matrix[i, j] * D[i, j] + min(
C[i - 1, j - 1], C[i - 1, j], C[i, j - 1]
)
dtw_cost = np.sqrt(C[n_samples_1 - 1, n_samples_2 - 1])
return dtw_cost
def dtw_d(X1, X2):
"""
Multivariate DTW with dependent strategy.
"""
dtw_cost = multivariate_dtw_dependent(
X1=X1, X2=X2, is_weighted=False, g=None, is_derivate=False
)
return dtw_cost
def dtwsc_d(X1, X2):
"""
Multivariate DTW with Sakoe-Chiba constraints with dependent strategy.
"""
dtw_cost = dtw(
X1,
X2,
global_constraint="sakoe_chiba",
sakoe_chiba_radius=3,
)
return dtw_cost
def ddtw_d(X1, X2):
"""
Multivariate DDTW with dependent strategy.
"""
dtw_cost = multivariate_dtw_dependent(
X1=X1, X2=X2, is_weighted=False, g=None, is_derivate=True
)
return dtw_cost
def wdtw_d(X1, X2):
"""
Multivariate WDTW with dependent strategy.
"""
dtw_cost = multivariate_dtw_dependent(
X1=X1, X2=X2, is_weighted=True, g=0.1, is_derivate=False
)
return dtw_cost
def wddtw_d(X1, X2):
"""
Multivariate WDDTW with dependent strategy.
"""
dtw_cost = multivariate_dtw_dependent(
X1=X1, X2=X2, is_weighted=True, g=0.1, is_derivate=True
)
return dtw_cost
def soft_dtw_d(X1, X2):
"""
Multivariate Soft-DTW with dependent strategy.
"""
dtw_cost = soft_dtw(
X1,
X2,
)
return dtw_cost
def lcss_d(X1, X2):
"""
LCSS with dependent strategy.
"""
lcss_cost = lcss(
X1,
X2,
)
return lcss_cost
def multivariate_distance_independent(
X1: np.ndarray,
X2: np.ndarray,
distance_str: str,
):
"""
Multivariate distance with independent strategy, built on the univariate
(dependent) distance.
Simply sums over the results of applying the distance separately to each
dimension.
The inputs must be multivariate.
Included multivariate dependent distances:
- DTW
- DTW with Sakoe-Chiba constraints
- WDTW
- DDTW
- WDDTW (first derivated, then weighted)
- Soft-DTW
- LCSS
Parameters
----------
X1 : array-like of shape (n_samples_1, d)
First multivariate signal
X2 : array-like of shape (n_samples_2, d)
Second multivariate signal
distance_str : {'DTW', 'DTWSC', 'DDTW', 'WDTW', 'WDDTW', 'Soft-DTW', 'LCSS'}, default='DTW'
Multivariate DTW variant.
"""
d_func = {
"DTW": dtw_d,
"DTWSC": dtwsc_d,
"DDTW": ddtw_d,
"WDTW": wdtw_d,
"WDDTW": wddtw_d,
"Soft-DTW": soft_dtw_d,
"LCSS": lcss_d,
}
if len(X1.shape) <= 1 or len(X2.shape) <= 1:
raise TypeError("The inputs must be multivariate.")
if X1.shape[1] != X2.shape[1]:
raise ValueError("The inputs must have the same dimension.")
total_d = X1.shape[1] # dimension
dtw_cost = 0
for d in range(total_d):
dtw_cost += d_func[distance_str](
X1=X1[:, d],
X2=X2[:, d],
)
return dtw_cost
def dtw_i(X1, X2):
"""
Multivariate DTW with independent strategy.
"""
dtw_cost = multivariate_distance_independent(
X1=X1,
X2=X2,
distance_str="DTW",
)
return dtw_cost
def dtwsc_i(X1, X2):
"""
Multivariate DTW with Sakoe-Chiba constraints with independent strategy.
"""
dtw_cost = multivariate_distance_independent(
X1=X1,
X2=X2,
distance_str="DTWSC",
)
return dtw_cost
def ddtw_i(X1, X2):
"""
Multivariate DDTW with independent strategy.
"""
dtw_cost = multivariate_distance_independent(
X1=X1,
X2=X2,
distance_str="DDTW",
)
return dtw_cost
def wdtw_i(X1, X2):
"""
Multivariate WDTW with independent strategy.
"""
dtw_cost = multivariate_distance_independent(
X1=X1,
X2=X2,
distance_str="WDTW",
)
return dtw_cost
def wddtw_i(X1, X2):
"""
Multivariate WDDTW with independent strategy.
"""
dtw_cost = multivariate_distance_independent(
X1=X1,
X2=X2,
distance_str="WDDTW",
)
return dtw_cost
def soft_dtw_i(X1, X2):
dtw_cost = multivariate_distance_independent(
X1=X2,
X2=X2,
distance_str="Soft-DTW",
)
return dtw_cost
def lcss_i(X1, X2):
dtw_cost = multivariate_distance_independent(
X1=X2,
X2=X2,
distance_str="LCSS",
)
return dtw_cost
# NEW
def numpy3d_to_listoflists(X):
"""
Transfrom a 3D numpay array of shape (n_ts, dimension, length) into a list of mulvariate signals.
A data set of `N` multivariate signals of dimension `d` is:
a list of `N` multivariate signals
each multivariate signal being a list of `d` univariate signals
They are not stored as numpy arrays because the signals are not required to be equal-sized.
"""
if X.shape[1] > X.shape[2]:
raise ValueError("The shape of `X` does not seem right!")
list_of_multivariate_signals = list()
for i in range(X.shape[0]):
multivariate_signal = X[i]
list_of_univariate_signals = list()
for d in range(X.shape[1]):
univariate_signal = multivariate_signal[d]
list_of_univariate_signals.append(univariate_signal)
list_of_multivariate_signals.append(list_of_univariate_signals)
return list_of_multivariate_signals
def scale_dataset_of_multivariate_signals(X):
"""
Scale each dimension of each multivariate signal.
"""
list_of_scaled_univariate_signals = list()
for d in range(X.shape[1]):
# data set of univariate signals (for one dimension)
univariate_signals = X[:, d, :]
scaled_univariate_signals = TimeSeriesScalerMeanVariance().fit_transform(
univariate_signals
)
list_of_scaled_univariate_signals.append(scaled_univariate_signals)
# list of numpy arrays for each dimension
return list_of_scaled_univariate_signals