-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcore.py
243 lines (182 loc) · 6.6 KB
/
core.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
import math
import numpy as np
import multiprocessing
from numpy.random import randint
from numpy.linalg import norm, eigh
from numpy.fft import fft, ifft
from sklearn.base import ClusterMixin, BaseEstimator
def zscore(a, axis=0, ddof=0):
a = np.asanyarray(a)
mns = a.mean(axis=axis)
sstd = a.std(axis=axis, ddof=ddof)
if axis and mns.ndim < a.ndim:
res = ((a - np.expand_dims(mns, axis=axis)) /
np.expand_dims(sstd, axis=axis))
else:
res = (a - mns) / sstd
return np.nan_to_num(res)
def roll_zeropad(a, shift, axis=None):
a = np.asanyarray(a)
if shift == 0:
return a
if axis is None:
n = a.size
reshape = True
else:
n = a.shape[axis]
reshape = False
if np.abs(shift) > n:
res = np.zeros_like(a)
elif shift < 0:
shift += n
zeros = np.zeros_like(a.take(np.arange(n-shift), axis))
res = np.concatenate((a.take(np.arange(n-shift, n), axis), zeros), axis)
else:
zeros = np.zeros_like(a.take(np.arange(n-shift, n), axis))
res = np.concatenate((zeros, a.take(np.arange(n-shift), axis)), axis)
if reshape:
return res.reshape(a.shape)
else:
return res
def _ncc_c_3dim(data):
x, y = data[0], data[1]
den = norm(x, axis=(0, 1)) * norm(y, axis=(0, 1))
if den < 1e-9:
den = np.inf
x_len = x.shape[0]
fft_size = 1 << (2*x_len-1).bit_length()
cc = ifft(fft(x, fft_size, axis=0) * np.conj(fft(y, fft_size, axis=0)), axis=0)
cc = np.concatenate((cc[-(x_len-1):], cc[:x_len]), axis=0)
return np.real(cc).sum(axis=-1) / den
def _sbd(x, y):
ncc = _ncc_c_3dim([x, y])
idx = np.argmax(ncc)
yshift = roll_zeropad(y, (idx + 1) - max(len(x), len(y)))
return yshift
def collect_shift(data):
x, cur_center = data[0], data[1]
if np.all(cur_center==0):
return x
else:
return _sbd(cur_center, x)
def _extract_shape(idx, x, j, cur_center):
_a=[]
for i in range(len(idx)):
if idx[i] == j:
_a.append(collect_shift([x[i], cur_center]))
a = np.array(_a)
if len(a) == 0:
indices = np.random.choice(x.shape[0], 1)
return np.squeeze(x[indices].copy())
#return np.zeros((x.shape[1]))
columns = a.shape[1]
y = zscore(a, axis=1, ddof=1)
s = np.dot(y[:, :, 0].transpose(), y[:, :, 0])
p = np.empty((columns, columns))
p.fill(1.0/columns)
p = np.eye(columns) - p
m = np.dot(np.dot(p, s), p)
_, vec = eigh(m)
centroid = vec[:, -1]
finddistance1 = np.sum(np.linalg.norm(a - centroid.reshape((x.shape[1], 1)), axis=(1, 2)))
finddistance2 = np.sum(np.linalg.norm(a + centroid.reshape((x.shape[1], 1)), axis=(1, 2)))
if finddistance1 >= finddistance2:
centroid *= -1
return zscore(centroid, ddof=1)
def _kshape(x, k, centroid_init='zero', max_iter=100, n_jobs=1):
m = x.shape[0]
idx = randint(0, k, size=m)
if centroid_init == 'zero':
centroids = np.zeros((k, x.shape[1], x.shape[2]))
elif centroid_init == 'random':
indices = np.random.choice(x.shape[0], k)
centroids = x[indices].copy()
distances = np.empty((m, k))
for it in range(max_iter):
old_idx = idx
for j in range(k):
for d in range(x.shape[2]):
centroids[j, :, d] = _extract_shape(idx, np.expand_dims(x[:, :, d], axis=2), j, np.expand_dims(centroids[j, :, d], axis=1))
#centroids[j] = np.expand_dims(_extract_shape(idx, x, j, centroids[j]), axis=1)
pool = multiprocessing.Pool(n_jobs)
args = []
for p in range(m):
for q in range(k):
args.append([x[p, :], centroids[q, :]])
result = pool.map(_ncc_c_3dim, args)
pool.close()
r = 0
for p in range(m):
for q in range(k):
distances[p, q] = 1 - result[r].max()
r = r + 1
idx = distances.argmin(1)
if np.array_equal(old_idx, idx):
break
return idx, centroids
def kshape(x, k, centroid_init='zero', max_iter=100):
idx, centroids = _kshape(np.array(x), k, centroid_init=centroid_init, max_iter=max_iter)
clusters = []
for i, centroid in enumerate(centroids):
series = []
for j, val in enumerate(idx):
if i == val:
series.append(j)
clusters.append((centroid, series))
return clusters
class KShapeClusteringCPU(ClusterMixin,BaseEstimator):
labels_= None
centroids_ = None
def __init__(self,n_clusters, centroid_init='zero', max_iter=100, n_jobs=None):
self.n_clusters = n_clusters
self.centroid_init = centroid_init
self.max_iter = max_iter
if n_jobs is None:
self.n_jobs=1
elif n_jobs == -1:
self.n_jobs = multiprocessing.cpu_count()
else:
self.n_jobs=n_jobs
def fit(self,X,y=None):
clusters = self._fit(X,self.n_clusters, self.centroid_init, self.max_iter,self.n_jobs)
self.labels_ = np.zeros(X.shape[0])
self.centroids_ =np.zeros((self.n_clusters, X.shape[1], X.shape[2]))
for i in range(self.n_clusters):
self.labels_[clusters[i][1]] = i
self.centroids_[i]=clusters[i][0]
return self
def predict(self, X):
labels, _ = self._predict(X,self.centroids_)
return labels
def _predict(self,x, centroids):
m = x.shape[0]
idx = randint(0, self.n_clusters, size=m)
distances = np.empty((m, self.n_clusters))
pool = multiprocessing.Pool(self.n_jobs)
args = []
for p in range(m):
for q in range(self.n_clusters):
args.append([x[p, :], centroids[q, :]])
result = pool.map(_ncc_c_3dim, args)
pool.close()
r = 0
for p in range(m):
for q in range(self.n_clusters):
distances[p, q] = 1 - result[r].max()
r = r + 1
idx = distances.argmin(1)
return idx, centroids
def _fit(self,x, k, centroid_init='zero', max_iter=100,n_jobs=1):
idx, centroids = _kshape(np.array(x), k, centroid_init=centroid_init, max_iter=max_iter, n_jobs=n_jobs)
clusters = []
for i, centroid in enumerate(centroids):
series = []
for j, val in enumerate(idx):
if i == val:
series.append(j)
clusters.append((centroid, series))
return clusters
if __name__ == "__main__":
import sys
import doctest
sys.exit(doctest.testmod()[0])