-
Notifications
You must be signed in to change notification settings - Fork 0
/
nslr_hmm.py
executable file
·339 lines (285 loc) · 11.8 KB
/
nslr_hmm.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
# Copyleft 2017 Jami Pekkanen <[email protected]>.
# Released under AGPL-3.0, see LICENSE.
import numpy as np
import scipy.stats
import nslr
import itertools
class ObservationModel:
def __init__(self, dists):
self.classidx = {}
self.idxclass = []
self.dists = []
for i, (cls, dist) in enumerate(dists.items()):
self.idxclass.append(cls)
self.classidx[cls] = i
self.dists.append(dist)
self.idxclass = np.array(self.idxclass)
def liks(self, d):
scores = []
scores = [dist.pdf(d) for dist in self.dists]
return np.array(scores).T
def classify(self, d):
return np.argmax(self.liks(d), axis=1)
def dist(self, cls):
return self.dists[self.classidx[cls]]
FIXATION = 1
SACCADE = 2
PSO = 3
SMOOTH_PURSUIT = 4
def gaze_observation_model():
# Estimated from data by doi:10.3758/s13428-016-0738-9.
params = {
FIXATION: [[0.6039844795867605, -0.7788440631929878], [[0.1651734722683456, 0.0], [0.0, 1.5875256060544993]]],
SACCADE: [[2.3259276064858194, 1.1333265634427712], [[0.080879690559802, 0.0], [0.0, 2.0718979621084372]]],
PSO: [[1.7511546389160744, -1.817487032170937], [[0.0752678429860497, 0.0], [0.0, 1.356411391040218]]],
SMOOTH_PURSUIT: [[0.8175021916433242, 0.3047120126632254], [[0.13334607025750783, 0.0], [0.0, 2.5328705587328173]]]
}
dists = {
cls: scipy.stats.multivariate_normal(m, c)
for cls, (m, c) in params.items()
}
return ObservationModel(dists)
def gaze_transition_model():
transitions = np.ones((4, 4))
transitions[0, 2] = 0
transitions[2, 1] = 0
transitions[3, 2] = 0
transitions[3, 0] = 0.5
transitions[0, 3] = 0.5
for i in range(len(transitions)):
transitions[i] /= np.sum(transitions[i])
return transitions
GazeObservationModel = gaze_observation_model()
GazeTransitionModel = gaze_transition_model()
def safelog(x):
return np.log10(np.clip(x, 1e-6, None))
def viterbi(initial_probs, transition_probs, emissions):
n_states = len(initial_probs)
emissions = iter(emissions)
emission = next(emissions)
transition_probs = safelog(transition_probs)
probs = safelog(emission) + safelog(initial_probs)
state_stack = []
for emission in emissions:
emission /= np.sum(emission)
trans_probs = transition_probs + np.row_stack(probs)
most_likely_states = np.argmax(trans_probs, axis=0)
probs = safelog(emission) + trans_probs[most_likely_states, np.arange(n_states)]
state_stack.append(most_likely_states)
state_seq = [np.argmax(probs)]
while state_stack:
most_likely_states = state_stack.pop()
state_seq.append(most_likely_states[state_seq[-1]])
state_seq.reverse()
return state_seq
def forward_backward(transition_probs, observations, initial_probs=None):
observations = np.array(list(observations))
N = len(transition_probs)
T = len(observations)
if initial_probs is None:
initial_probs = np.ones(N)
initial_probs /= np.sum(initial_probs)
forward_probs = np.zeros((T, N))
backward_probs = forward_probs.copy()
probs = initial_probs
for i in range(T):
probs = np.dot(probs, transition_probs)*observations[i]
probs /= np.sum(probs)
forward_probs[i] = probs
probs = np.ones(N)
probs /= np.sum(probs)
for i in range(T-1, -1, -1):
probs = np.dot(transition_probs, (probs*observations[i]).T)
probs /= np.sum(probs)
backward_probs[i] = probs
state_probs = forward_probs*backward_probs
state_probs /= np.sum(state_probs, axis=1).reshape(-1, 1)
return state_probs, forward_probs, backward_probs
def dataset_features(data, **nslrargs):
segments = ((nslr.fit_gaze(ts, xs, **nslrargs), outliers) for ts, xs, outliers in data)
features = [list(segment_features(s.segments, o)) for s, o in segments]
return features
def transition_estimates(obs, trans, forward, backward):
T, N = len(obs), len(trans)
ests = np.zeros((T, N, N))
for start, end, i in itertools.product(range(N), range(N), range(T)):
if i == T - 1:
b = 1/N
else:
b = backward[i+1, end]
ests[i,start,end] = forward[i,start]*b*trans[start,end]
return ests
def reestimate_observations_baum_welch(sessions,
transition_probs=GazeTransitionModel,
observation_model=GazeObservationModel,
initial_probs=None,
estimate_observation_model=True,
estimate_transition_model=True,
n_iterations=30,
plot_process=False):
all_observations = np.vstack(sessions)
if plot_process:
import matplotlib.pyplot as plt
CLASS_COLORS = {
1: 'b',
2: 'r',
3: 'y',
4: 'g',
5: 'm',
6: 'c',
22: 'orange'
}
for iteration in range(n_iterations): # Should probably have a nicer stopping criterion
all_state_probs = []
all_transition_probs = []
# Compute state and transition probabilities
# for all segments using the forward-backward algorithm
for features in sessions:
liks = np.array([observation_model.liks(f) for f in features])
probs, forward, backward = forward_backward(transition_probs, liks, initial_probs)
all_state_probs.extend(probs)
all_transition_probs.append(transition_estimates(liks, transition_probs, forward, backward))
all_state_probs = np.array(all_state_probs)
all_transition_probs = np.vstack(all_transition_probs)
if plot_process:
winner = np.argmax(all_state_probs, axis=1)
for cls in np.unique(winner):
my = winner == cls
plt.plot(all_observations[my,0], all_observations[my,1], '.', alpha=0.1, color=CLASS_COLORS[cls+1])
dists = {}
# Estimate the observation model using
# mean and covariance of the observations weighted
# by probability of a segment belonging to a given
# class.
for i, cls in enumerate(observation_model.idxclass):
w = all_state_probs[:,i]
wsum = np.sum(w)
w /= wsum
mean = np.average(all_observations, weights=w, axis=0)
cov = np.cov(all_observations, aweights=w, rowvar=False)
if plot_process:
plt.plot(mean[0], mean[1], 'o', color=CLASS_COLORS[cls])
dists[cls] = scipy.stats.multivariate_normal(mean, cov)
if plot_process:
plt.pause(0.1)
plt.cla()
if estimate_transition_model:
# Take a mean of all sessions. This may be unoptimal.
transition_probs = np.mean(all_transition_probs, axis=0)
transition_probs /= np.sum(transition_probs, axis=1).reshape(-1, 1)
if estimate_observation_model:
observation_model=ObservationModel(dists)
return transition_probs, observation_model
def reestimate_observations_viterbi_robust(
sessions,
transition_probs=GazeTransitionModel,
observation_model=GazeObservationModel,
initial_probs=None,
estimate_observation_model=True,
estimate_transition_model=True,
n_iterations=30,
plot_process=False):
from sklearn.covariance import MinCovDet
all_observations = np.vstack(sessions)
if plot_process:
import matplotlib.pyplot as plt
CLASS_COLORS = {
1: 'b',
2: 'r',
3: 'y',
4: 'g',
5: 'm',
6: 'c',
22: 'orange'
}
N = len(transition_probs)
if initial_probs is None:
initial_probs = np.ones(N)
initial_probs /= np.sum(initial_probs)
for iteration in range(n_iterations):
all_states = []
all_transitions = np.zeros((N, N))
for features in sessions:
liks = np.array([observation_model.liks(f) for f in features])
states = viterbi(initial_probs, transition_probs, liks)
for i in range(len(states) - 1):
all_transitions[states[i], states[i+1]] += 1
all_states.extend(states)
all_states = np.array(all_states)
if plot_process:
for cls in np.unique(states):
my = all_states == cls
plt.plot(all_observations[my,0], all_observations[my,1], '.', alpha=0.1, color=CLASS_COLORS[cls+1])
dists = {}
for i, cls in enumerate(observation_model.idxclass):
my = all_states == i
# Don't reestimate if such class is not found
if np.sum(my) < 2:
dists[cls] = observation_model.dists[cls]
continue
# Use this for non-robust
#mean = np.average(all_observations[my], axis=0)
#cov = observation_model.dists[i].cov
robust = MinCovDet().fit(all_observations[my])
mean = robust.location_
cov = robust.covariance_
dists[cls] = scipy.stats.multivariate_normal(mean, cov)
if plot_process:
plt.plot(mean[0], mean[1], 'o', color=CLASS_COLORS[cls])
if plot_process:
plt.pause(0.1)
plt.cla()
if estimate_transition_model:
new_transition_probs = all_transitions
totals = np.sum(new_transition_probs, axis=1).reshape(-1, 1)
new_transition_probs /= totals
# Avoid nans in transitions. If the algorithm
# gets zeros here, the estimate will likely be quite bad
not_seen = totals.flatten() == 0
new_transition_probs[not_seen,:] = transition_probs[not_seen,:]
transition_probs = new_transition_probs
if estimate_observation_model:
observation_model=ObservationModel(dists)
return transition_probs, observation_model
def segment_features(segments, outliers=None):
prev_direction = np.array([0.0, 0.0])
if outliers is None:
outliers = np.zeros(segments[-1].i[-1], dtype=bool)
for segment in segments:
if np.any(outliers[segment.i[0]:segment.i[1]]): continue
duration = float(np.diff(segment.t))
speed = np.diff(segment.x, axis=0)/duration
velocity = float(np.linalg.norm(speed))
direction = speed/velocity
cosangle = float(np.dot(direction, prev_direction.T))
# Fisher transform, avoid exact |1|
cosangle *= (1 - 1e-6)
cosangle = np.arctanh(cosangle)
if cosangle != cosangle:
cosangle = 0.0
yield safelog(velocity), cosangle
prev_direction = direction
def classify_segments(segments,
observation_model=GazeObservationModel,
transition_model=GazeTransitionModel,
initial_probabilities=None):
if initial_probabilities is None:
initial_probabilities = np.ones(len(transition_model))
initial_probabilities /= np.sum(initial_probabilities)
observation_likelihoods = (observation_model.liks(f) for f in segment_features(segments))
path = viterbi(initial_probabilities, transition_model, observation_likelihoods)
return observation_model.idxclass[path]
def classify_gaze(ts, xs, **kwargs):
fit_params = {k: kwargs[k]
for k in ('structural_error', 'optimize_noise', 'split_likelihood') if k in kwargs
}
print('\tStarting Segmentation...')
segmentation = nslr.fit_gaze(ts, xs, **fit_params)
print('\tClassifying Segments...')
seg_classes = classify_segments(segmentation.segments)
sample_classes = np.zeros(len(ts))
for c, s in zip(seg_classes, segmentation.segments):
start = s.i[0]
end = s.i[1]
sample_classes[start:end] = c
return sample_classes, segmentation, seg_classes