-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_dtw.pyx
328 lines (245 loc) · 10.1 KB
/
fast_dtw.pyx
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
# python fast_dtw_setup.py build_ext --inplace
import numpy as np
cimport numpy as np
np.import_array()
from libc.math cimport sqrt, acos, INFINITY, abs
# Calculate distance between a vector X and a list of vectors Y
def dist_VecToMat(np.ndarray[float, ndim=1] X,
np.ndarray[float, ndim=2] Y,
):
cdef int i
cdef int j
cdef int Y_x = Y.shape[0]
cdef int Y_y = Y.shape[1]
cdef float num
cdef float normY
cdef float normX = 0
cdef float normDist = 0
cdef np.ndarray[double, ndim=1] dist = np.empty(Y_y, dtype=np.float64)
for j in range(Y_x):
normX += X[j] * X[j]
for i in range(Y_y):
num = 0
normY = 0
for j in range(Y_x):
num += Y[j, i] * X[j]
normY += Y[j, i] * Y[j, i]
dist[i] = acos(num / sqrt(normX * normY))
normDist += dist[i] * dist[i]
for i in range(Y_y):
dist[i] /= sqrt(normDist)
return dist
def costVec(int middle_inf,
int middle_sup,
np.ndarray[double, ndim=2] gamma,
np.ndarray[double, ndim=1] dist,
int i,
):
cdef int idx
cdef int j
cdef float norm_cost
cdef float cost_pos = INFINITY
cdef int gamma_x = gamma.shape[0]
cdef int gamma_y = gamma.shape[1]
cdef np.ndarray[double, ndim=1] cost_vec = np.ones(gamma_y, dtype=np.float64) * INFINITY
for idx, j in enumerate(range(middle_inf, middle_sup)):
gamma[1,j+1] = dist[idx] + min(gamma[0, j], gamma[0, j+1], gamma[1, j])
cost_vec[j] = gamma[1, j+1] / (i+j+1)
if cost_vec[j] < cost_pos:
cost_pos = cost_vec[j]
gamma[0, :] = gamma[1, :]
gamma[1, :] = (gamma[1, :] + 1e-10) * INFINITY
return gamma, cost_vec
def dist_VecToMat_nonorm(np.ndarray[float, ndim=1] X,
np.ndarray[float, ndim=2] Y,
):
cdef int i
cdef int j
cdef int Y_x = Y.shape[0]
cdef int Y_y = Y.shape[1]
cdef float num
cdef float normY
cdef float normX = 0
cdef float normDist = 0
cdef np.ndarray[double, ndim=1] dist = np.empty(Y_y, dtype=np.float64)
for j in range(Y_x):
normX += X[j] * X[j]
for i in range(Y_y):
num = 0
normY = 0
for j in range(Y_x):
num += Y[j, i] * X[j]
normY += Y[j, i] * Y[j, i]
dist[i] = acos(num / sqrt(normX * normY))
normDist += dist[i] * dist[i]
# for i in range(Y_y):
# dist[i] /= sqrt(normDist)
return dist
def costVec_jump2(int middle_inf,
int middle_sup,
np.ndarray[double, ndim=2] gamma,
np.ndarray[double, ndim=1] dist_state,
int i,
bint jump, # check if jump
np.ndarray[double, ndim=2] dist_jump_list, # vector of dists
np.ndarray[int, ndim=1] s_list, # vector of start indexes
np.ndarray[int, ndim=1] t_list, # vector of end indexes
int state_idx, # current state
int prev_norm, # cumulative norm
int size_vec, # size_vec
float penalty
):
cdef int idx
cdef int ids
cdef int idj
cdef int j
cdef int s
cdef int gamma_x = gamma.shape[0]
cdef int gamma_y = gamma.shape[1]
cdef np.ndarray[double, ndim=1] cost_vec = np.ones(gamma_y, dtype=np.float64) * INFINITY
for idx, j in enumerate(range(middle_inf, middle_sup)):
gamma[1,j+1] = dist_state[idx] + min(gamma[0, j], gamma[0, j+1], gamma[1, j])
if i+j+1 - s_list[state_idx] + prev_norm == 0:
cost_vec[j+1] = INFINITY
else:
cost_vec[j+1] = gamma[1, j+1] / (i+j+1 - s_list[state_idx] + prev_norm)
if jump:
# for all beginnings
for ids, s in enumerate(s_list):
# if the beginning is not in [middle_inf, middle_sup[
if not (middle_inf <= s < middle_sup):
for idj, j in enumerate(range(s, s + size_vec)):
if idj == 0:
# gamma[1,j+1] = dist_jump_list[ids][idj] + gamma[0, t_list[state_idx]] + 0.5 * t_list[state_idx] / 100
gamma[1,j+1] = dist_jump_list[ids][idj] + gamma[0, t_list[state_idx]] + 0.5 * (prev_norm+t_list[state_idx]-s_list[state_idx]) * penalty
else:
gamma[1,j+1] = dist_jump_list[ids][idj] + min(gamma[0, j], gamma[0, j+1], gamma[1, j])
if i+j+1 - s + (t_list[state_idx] - s_list[state_idx]) + prev_norm == 0:
cost_vec[j+1] = INFINITY
else:
cost_vec[j+1] = gamma[1, j+1] / (i+j+1 - s + (t_list[state_idx] - s_list[state_idx]) + prev_norm)
gamma[0, :] = gamma[1, :]
gamma[1, :] = (gamma[1, :] + 1e-10) * INFINITY
return gamma, cost_vec
def costVec_jump2test(int middle_inf,
int middle_sup,
np.ndarray[double, ndim=2] gamma,
np.ndarray[double, ndim=1] dist_state,
int checking_parts,
int i,
bint jump, # check if jump
np.ndarray[double, ndim=2] dist_jump_list, # vector of dists
np.ndarray[int, ndim=1] s_list, # vector of start indexes
np.ndarray[int, ndim=1] t_list, # vector of end indexes
int state_idx, # current state
int prev_norm, # cumulative norm
int size_vec, # size_vec
float penalty
):
cdef int idx
cdef int ids
cdef int idj
cdef int j
cdef int s
cdef int gamma_x = gamma.shape[0]
cdef int gamma_y = gamma.shape[1]
cdef np.ndarray[double, ndim=1] cost_vec = np.ones(gamma_y, dtype=np.float64) * INFINITY
for idx, j in enumerate(range(middle_inf, middle_sup)):
gamma[1,j+1] = dist_state[idx] + min(gamma[0, j], gamma[0, j+1], gamma[1, j])
if i+j+1 - s_list[state_idx] + prev_norm == 0:
cost_vec[j+1] = INFINITY
else:
cost_vec[j+1] = gamma[1, j+1] / (i+j+1 - s_list[state_idx] + prev_norm)
if jump:
# for all beginnings
for ids in range(state_idx, min(len(s_list)-1, state_idx+checking_parts)):
# if the beginning is not in [middle_inf, middle_sup[
if not (middle_inf <= s_list[ids] < middle_sup):
for idj, j in enumerate(range(s_list[ids], s_list[ids] + size_vec)):
if idj == 0:
# gamma[1,j+1] = dist_jump_list[ids][idj] + gamma[0, t_list[state_idx]] + 0.5 * t_list[state_idx] / 100
gamma[1,j+1] = dist_jump_list[ids][idj] + gamma[0, t_list[state_idx]] + 0.5 * (prev_norm+t_list[state_idx]-s_list[state_idx]) * penalty
else:
gamma[1,j+1] = dist_jump_list[ids][idj] + min(gamma[0, j], gamma[0, j+1], gamma[1, j])
if i+j+1 - s_list[ids] + (t_list[state_idx] - s_list[state_idx]) + prev_norm == 0:
cost_vec[j+1] = INFINITY
else:
cost_vec[j+1] = gamma[1, j+1] / (i+j+1 - s_list[ids] + (t_list[state_idx] - s_list[state_idx]) + prev_norm)
gamma[0, :] = gamma[1, :]
gamma[1, :] = (gamma[1, :] + 1e-10) * INFINITY
return gamma, cost_vec
def diagonal_matching(int middle_inf,
int middle_sup,
np.ndarray[double, ndim=2] gamma,
np.ndarray[double, ndim=1] dist,
):
cdef int j
for j in range(middle_inf, middle_sup):
gamma[1,j] = dist[j] + gamma[0, j-1]
gamma[0, :] = gamma[1, :]
gamma[1, :] = (gamma[1, :] + 1e-10) * INFINITY
return gamma
def diagonal_matching_jump(int middle_inf,
int middle_sup,
np.ndarray[double, ndim=2] gamma,
np.ndarray[double, ndim=1] dist,
np.ndarray[int, ndim=1] s_list, # vector of start indexes
np.ndarray[int, ndim=1] t_list, # vector of end indexes
):
cdef int start = middle_inf
cdef int j
cdef int e
cdef int t
cdef int len_t_list = t_list.shape[0]
cdef float gamma_end_min = INFINITY
# Cut into segments in between the t_list
# Way faster than: if j not in t_list
for e in range(len_t_list):
# Normal DTW
for j in range(start, t_list[e]+1):
gamma[1,j] = dist[j] + gamma[0, j-1]
# Jump DTW
j = t_list[e]+1
if j < middle_sup:
# Jump DTW
gamma_end_min = INFINITY
for t in range(len_t_list):
if gamma[0, t_list[t]] < gamma_end_min:
gamma_end_min = gamma[0, t_list[t]]
gamma[1,j] = dist[j] + min(gamma[0, j-1], gamma_end_min)
start = j + 1
gamma[0, :] = gamma[1, :]
gamma[1, :] = (gamma[1, :] + 1e-10) * INFINITY
return gamma
def cumulative_matrix_jump(int middle_inf,
int middle_sup,
np.ndarray[double, ndim=2] gamma,
np.ndarray[double, ndim=1] dist,
np.ndarray[int, ndim=1] s_list, # vector of start indexes
np.ndarray[int, ndim=1] t_list, # vector of end indexes
):
cdef int start = middle_inf
cdef int j
cdef int e
cdef int t
cdef int len_t_list = t_list.shape[0]
cdef float gamma_end_min = INFINITY
# Cut into segments in between the t_list
# Way faster than: if j not in t_list
for e in range(len_t_list):
# Normal DTW
for j in range(start, t_list[e]+1):
gamma[1,j] = dist[j] + min(gamma[0, j-1], gamma[0, j], gamma[1, j-1])
# Jump DTW
j = t_list[e]+1
if j < middle_sup:
# Jump DTW
gamma_end_min = INFINITY
for t in range(len_t_list):
if gamma[0, t_list[t]] < gamma_end_min:
gamma_end_min = gamma[0, t_list[t]]
gamma[1,j] = dist[j] + min(gamma[0, j-1], gamma[0, j], gamma[1, j-1], gamma_end_min)
start = j + 1
gamma[0, :] = gamma[1, :]
gamma[1, :] = (gamma[1, :] + 1e-10) * INFINITY
return gamma