-
Notifications
You must be signed in to change notification settings - Fork 1
/
dba.py
140 lines (94 loc) · 3.27 KB
/
dba.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
from __future__ import division
from warnings import warn
from itertools import izip
from functools import partial
import numpy as np
from dtw import dtw as _dtw
_RELATIVE_TOLERANCE = 1e-2
_MAX_ITERS = 15
_WARP_PENALTY = 2
dtw = partial(_dtw, warp_penalty = _WARP_PENALTY)
# TODO: normalization
# TODO: regularization
# TODO: stochastic descent
# TODO: parameter sensitivity
# TODO: tests
# TODO: check for cycles (2-cycles, n-cycles?)
def dba(sequences, size = None, tol = _RELATIVE_TOLERANCE, max_iters = _MAX_ITERS):
"""
Performs DTW barycenter averaging on an iterable of sequences.
Returns: center, errors
"""
if size is None:
size = max(len(sequence) for sequence in sequences)
center = np.zeros(size)
last_error = np.inf
previous_centers = []
previous_errors = []
for i in xrange(max_iters):
new_center, errors = _update_center(center, sequences)
error = np.sum(np.square(errors))
delta_error = abs(1 - error / last_error)
delta_center = np.linalg.norm(center - new_center, 2) / np.linalg.norm(center, 2)
if delta_error < tol or np.allclose(center, new_center):# or delta_center < tol:
break
if any(np.allclose(new_center, previous) for previous in previous_centers):
best = np.argmin([np.sum(np.square(ers)) for ers in previous_errors])
center = previous_centers[best]
errors = previous_errors[best]
break
else:
previous_centers.append(center)
previous_errors.append(errors)
center = new_center
last_error = error
else:
warn(
"failed to converge (dE/E = {}, d|v|/|v| = {})".format(delta_error, delta_center)
)
return center, errors
# new method that combines k-means with the DBA algorithm
def k_dba(sequences, k, tol = _RELATIVE_TOLERANCE, max_iters = 30):
n_sequences = len(sequences)
size = max(len(sequence) for sequence in sequences)
weights = np.random.random((k, len(sequences)))
weights /= weights.sum(0)
errors = np.empty_like(weights)
centers = [
_update_center(np.zeros(size), sequences, center_weights)[0]
for center_weights in weights
]
for iteration in xrange(max_iters):
for i, (center, center_weights) in enumerate(izip(centers, weights)):
new_center, new_errors = _update_center(center, sequences, center_weights)
errors[i, :] = new_errors
centers[i] = new_center
weights.fill(0)
weights[np.argmin(errors, 0), np.arange(n_sequences)] = 1
weights[np.isnan(weights)] = 0
weights[np.isinf(weights)] = 1
return centers, errors, weights
def _update_center(center, sequences, weights = None):
accumulated = np.zeros_like(center)
n_sequences = len(sequences)
errors = np.empty(n_sequences)
if weights is None:
weights = np.ones_like(errors)
minsize = center.size
n_observations = np.zeros(center.size, np.float64)
for i, (sequence, weight) in enumerate(izip(sequences, weights)):
dist, alignment1, alignment2 = dtw(
center,
sequence,
)
aligned = (
np.bincount(alignment1, sequence[alignment2], minsize) /
np.bincount(alignment1, None, minsize)
)
aligned[np.isnan(aligned)] = 0
errors[i] = dist*np.log(alignment1.size)*np.log(alignment2.size)
accumulated += weight*aligned
n_observations += weight*(np.bincount(alignment1, None, minsize) > 0)
new_center = accumulated / n_observations
new_center[np.isnan(new_center)] = 0
return new_center, errors