-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathDBA_multivariate.py
216 lines (178 loc) · 7.16 KB
/
DBA_multivariate.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
'''
/*******************************************************************************
* Copyright (C) 2018 Francois Petitjean
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
'''
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from functools import reduce
__author__ ="Francois Petitjean"
def performDBA(series, n_iterations=10):
n_series = len(series)
max_length = 0
for s in series:
max_length = max(max_length,s.shape[1])
cost_mat = np.zeros((max_length, max_length))
delta_mat = np.zeros((max_length, max_length))
tmp_delta_mat = np.zeros((max_length, max_length))
path_mat = np.zeros((max_length, max_length), dtype=np.int8)
medoid_ind = approximate_medoid_index(series,cost_mat,delta_mat,tmp_delta_mat)
center = series[medoid_ind]
for i in range(0,n_iterations):
center = DBA_update(center, series, cost_mat, path_mat, delta_mat,tmp_delta_mat)
return center
def approximate_medoid_index(series,cost_mat,delta_mat,tmp_delta_mat):
if len(series)<=50:
indices = range(0,len(series))
else:
indices = np.random.choice(range(0,len(series)),50,replace=False)
medoid_ind = -1
best_ss = 1e20
for index_candidate in indices:
candidate = series[index_candidate]
ss = sum_of_squares(candidate,series,cost_mat,delta_mat,tmp_delta_mat)
if(medoid_ind==-1 or ss<best_ss):
best_ss = ss
medoid_ind = index_candidate
return medoid_ind
def sum_of_squares(s,series,cost_mat,delta_mat,tmp_delta_mat):
return sum(map(lambda t:squared_DTW(s,t,cost_mat,delta_mat,tmp_delta_mat),series))
def DTW(s,t,cost_mat,delta_mat):
return np.sqrt(squared_DTW(s,t,cost_mat,delta_mat))
def squared_DTW(s,t,cost_mat,delta_mat,tmp_delta_mat):
s_len = s.shape[1]
t_len = t.shape[1]
fill_delta_mat_dtw(s, t, delta_mat,tmp_delta_mat)
cost_mat[0, 0] = delta_mat[0, 0]
for i in range(1, s_len):
cost_mat[i, 0] = cost_mat[i-1, 0]+delta_mat[i, 0]
for j in range(1, t_len):
cost_mat[0, j] = cost_mat[0, j-1]+delta_mat[0, j]
for i in range(1, s_len):
for j in range(1, t_len):
diag,left,top =cost_mat[i-1, j-1], cost_mat[i, j-1], cost_mat[i-1, j]
if(diag <=left):
if(diag<=top):
res = diag
else:
res = top
else:
if(left<=top):
res = left
else:
res = top
cost_mat[i, j] = res+delta_mat[i, j]
return cost_mat[s_len-1,t_len-1]
def fill_delta_mat_dtw(center, s, delta_mat, tmp_delta_mat):
n_dims = center.shape[0]
len_center = center.shape[1]
len_s= s.shape[1]
slim = delta_mat[:len_center,:len_s]
slim_tmp = tmp_delta_mat[:len_center,:len_s]
#first dimension - not in the loop to avoid initialisation of delta_mat
np.subtract.outer(center[0], s[0],out = slim)
np.square(slim, out=slim)
for d in range(1,center.shape[0]):
np.subtract.outer(center[d], s[d],out = slim_tmp)
np.square(slim_tmp, out=slim_tmp)
np.add(slim,slim_tmp,out=slim)
assert(np.abs(np.sum(np.square(center[:,0]-s[:,0]))-delta_mat[0,0])<=1e-6)
def DBA_update(center, series, cost_mat, path_mat, delta_mat, tmp_delta_mat):
options_argmin = [(-1, -1), (0, -1), (-1, 0)]
updated_center = np.zeros(center.shape)
center_length = center.shape[1]
n_elements = np.zeros(center_length, dtype=int)
for s in series:
s_len = s.shape[1]
fill_delta_mat_dtw(center, s, delta_mat, tmp_delta_mat)
cost_mat[0, 0] = delta_mat[0, 0]
path_mat[0, 0] = -1
for i in range(1, center_length):
cost_mat[i, 0] = cost_mat[i-1, 0]+delta_mat[i, 0]
path_mat[i, 0] = 2
for j in range(1, s_len):
cost_mat[0, j] = cost_mat[0, j-1]+delta_mat[0, j]
path_mat[0, j] = 1
for i in range(1, center_length):
for j in range(1, s_len):
diag,left,top =cost_mat[i-1, j-1], cost_mat[i, j-1], cost_mat[i-1, j]
if(diag <=left):
if(diag<=top):
res = diag
path_mat[i,j] = 0
else:
res = top
path_mat[i,j] = 2
else:
if(left<=top):
res = left
path_mat[i,j] = 1
else:
res = top
path_mat[i,j] = 2
cost_mat[i, j] = res+delta_mat[i, j]
i = center_length-1
j = s_len-1
while(path_mat[i, j] != -1):
updated_center[:,i] += s[:,j]
n_elements[i] += 1
move = options_argmin[path_mat[i, j]]
i += move[0]
j += move[1]
assert(i == 0 and j == 0)
updated_center[:,i] += s[:,j]
n_elements[i] += 1
return np.divide(updated_center, n_elements)
def main():
#generating synthetic data
n_series = 20
length = 200
n_dims = 201
print('Important note: the data should be structure "channels-first", ie the series should have shape (n_channels,length)')
series = list()
padding_length=30
indices = range(0, length-padding_length)
main_profile_gen = np.array([np.sin(2.0*np.pi*j/len(indices)) for j in indices])
randomizer = lambda j:np.random.normal(j,0.02)
randomizer_fun = np.vectorize(randomizer)
for i in range(0,n_series):
n_pad_left = np.random.randint(0,padding_length)
#adding zero at the start or at the end to shif the profile
b = n_pad_left
a = padding_length-n_pad_left
padded_pattern = np.pad(main_profile_gen,(a,b),mode='constant',constant_values=0)
#chop some of the end to prove it can work with multiple lengths
l = np.random.randint(length-20,length+1)
padded_pattern = padded_pattern[:l]
padded_pattern = randomizer_fun(padded_pattern)
series_i = np.zeros((n_dims,l))
for d in range(0,n_dims):
series_i[d]=padded_pattern
series.append(series_i)
#plotting the synthetic data
for s in series:
plt.plot(range(0,s.shape[1]), s[0])
plt.draw()
plt.show()
#calculating average series with DBA
average_series = performDBA(series)
#plotting the average series
plt.figure()
for d in range(0,n_dims):
plt.plot(range(0,average_series.shape[1]), average_series[d])
plt.show()
if __name__== "__main__":
main()