-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy path_timeviewer.py
558 lines (494 loc) · 19.6 KB
/
_timeviewer.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# Authors: Alexandre Gramfort <[email protected]>
# Eric Larson <[email protected]>
# Guillaume Favelier <[email protected]>
#
# License: Simplified BSD
from itertools import cycle
import time
import numpy as np
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigCanvas
from ..utils import _get_color_list, tight_layout
class MplCanvas(FigCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent, width, height, dpi):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
self.axes.set(xlabel='Time (sec)', ylabel='Activation (AU)')
FigCanvas.__init__(self, fig)
self.setParent(parent)
FigCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigCanvas.updateGeometry(self)
# XXX eventually this should be called in the window resize callback
tight_layout(fig=self.axes.figure)
def plot(self, x, y, vertex_id, **kwargs):
"""Plot a curve."""
line, = self.axes.plot(
x, y, label='vertex id = {}'.format(vertex_id), **kwargs)
self.axes.legend()
self.update_plot()
return line
def update_plot(self):
"""Update the plot."""
self.draw()
class IntSlider(object):
"""Class to set a integer slider."""
def __init__(self, plotter=None, callback=None, name=None):
self.plotter = plotter
self.callback = callback
self.name = name
def __call__(self, value):
"""Round the label of the slider."""
idx = int(round(value))
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name == self.name:
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(idx)
self.callback(idx)
class UpdateColorbarScale(object):
"""Class to update the values of the colorbar sliders."""
def __init__(self, plotter=None, brain=None):
self.plotter = plotter
self.brain = brain
def __call__(self, value):
"""Update the colorbar sliders."""
self.brain.update_fscale(value)
fmin = self.brain._data['fmin']
fmid = self.brain._data['fmid']
fmax = self.brain._data['fmax']
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name == "fmin":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmin)
elif name == "fmid":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmid)
elif name == "fmax":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmax)
elif name == "fscale":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(1.0)
class BumpColorbarPoints(object):
"""Class that ensure constraints over the colorbar points."""
def __init__(self, plotter=None, brain=None, name=None):
self.plotter = plotter
self.brain = brain
self.name = name
self.callback = {
"fmin": brain.update_fmin,
"fmid": brain.update_fmid,
"fmax": brain.update_fmax
}
self.last_update = time.time()
def __call__(self, value):
"""Update the colorbar sliders."""
keys = ('fmin', 'fmid', 'fmax')
vals = {key: self.brain._data[key] for key in keys}
reps = {key: None for key in keys}
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name is not None:
reps[name] = slider.GetRepresentation()
if self.name == "fmin" and reps["fmin"] is not None:
if vals['fmax'] < value:
self.brain.update_fmax(value)
reps['fmax'].SetValue(value)
if vals['fmid'] < value:
self.brain.update_fmid(value)
reps['fmid'].SetValue(value)
reps['fmin'].SetValue(value)
elif self.name == "fmid" and reps['fmid'] is not None:
if vals['fmin'] > value:
self.brain.update_fmin(value)
reps['fmin'].SetValue(value)
if vals['fmax'] < value:
self.brain.update_fmax(value)
reps['fmax'].SetValue(value)
reps['fmid'].SetValue(value)
elif self.name == "fmax" and reps['fmax'] is not None:
if vals['fmin'] > value:
self.brain.update_fmin(value)
reps['fmin'].SetValue(value)
if vals['fmid'] > value:
self.brain.update_fmid(value)
reps['fmid'].SetValue(value)
reps['fmax'].SetValue(value)
if time.time() > self.last_update + 1. / 60.:
self.callback[self.name](value)
self.last_update = time.time()
class ShowView(object):
"""Class that selects the correct view."""
def __init__(self, plotter=None, brain=None, orientation=None,
row=None, col=None, hemi=None, name=None):
self.plotter = plotter
self.brain = brain
self.orientation = orientation
self.short_orientation = [s[:3] for s in orientation]
self.row = row
self.col = col
self.hemi = hemi
self.name = name
def __call__(self, value, update_widget=False):
"""Update the view."""
self.brain.show_view(value, row=self.row, col=self.col,
hemi=self.hemi)
if update_widget:
if len(value) > 3:
idx = self.orientation.index(value)
else:
idx = self.short_orientation.index(value)
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name == self.name:
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(idx)
slider_rep.SetTitleText(self.orientation[idx])
class _TimeViewer(object):
"""Class to interact with _Brain."""
def __init__(self, brain):
self.brain = brain
self.plotter = brain._renderer.plotter
self.point_actor = self.brain._renderer.text2d(0.05, 0.9, "")
self.point_actor.VisibilityOff()
self.point_actor
self.fig = None
self.picked_points = list()
self.act_data = None
self.plotter.enable_point_picking(
callback=self.pick_point,
show_message=False,
show_point=False,
use_mesh=True
)
self.color_cycle = cycle(_get_color_list())
# orientation slider
orientation = [
'lateral',
'medial',
'rostral',
'caudal',
'dorsal',
'ventral',
'frontal',
'parietal'
]
# default: put orientation slider on the first view
if self.brain._hemi == 'split':
self.plotter.subplot(0, 0)
for hemi in self.brain._hemis:
ci = 0 if hemi == 'lh' else 1
# with both, all hemis are on the same view
if self.brain._hemi == 'both':
ci = 0
for ri, view in enumerate(self.brain._views):
self.plotter.subplot(ri, ci)
name = "orientation_" + str(ri) + "_" + str(ci)
self.show_view = ShowView(
plotter=self.plotter,
brain=self.brain,
orientation=orientation,
hemi=hemi,
row=ri,
col=ci,
name=name
)
orientation_slider = self.plotter.add_text_slider_widget(
self.show_view,
value=0,
data=orientation,
pointa=(0.82, 0.74),
pointb=(0.98, 0.74),
event_type='always'
)
orientation_slider.name = name
self.set_slider_style(orientation_slider, show_label=False)
self.show_view(view, update_widget=True)
# necessary because show_view modified subplot
if self.brain._hemi == 'split':
self.plotter.subplot(0, 0)
# scalar bar
if brain._colorbar_added:
scalar_bar = self.plotter.scalar_bar
scalar_bar.SetOrientationToVertical()
scalar_bar.SetHeight(0.6)
scalar_bar.SetWidth(0.05)
scalar_bar.SetPosition(0.02, 0.2)
# smoothing slider
default_smoothing_value = 7
self.set_smoothing = IntSlider(
plotter=self.plotter,
callback=brain.set_data_smoothing,
name="smoothing"
)
smoothing_slider = self.plotter.add_slider_widget(
self.set_smoothing,
value=default_smoothing_value,
rng=[0, 15], title="smoothing",
pointa=(0.82, 0.90),
pointb=(0.98, 0.90)
)
smoothing_slider.name = 'smoothing'
self.set_smoothing(default_smoothing_value)
# time label
self.time_actor = brain._data.get('time_actor')
if self.time_actor is not None:
self.time_actor.SetPosition(0.5, 0.03)
self.time_actor.GetTextProperty().SetJustificationToCentered()
# time slider
max_time = len(brain._data['time']) - 1
time_slider = self.plotter.add_slider_widget(
brain.set_time_point,
value=brain._data['time_idx'],
rng=[0, max_time],
pointa=(0.23, 0.1),
pointb=(0.77, 0.1),
event_type='always'
)
time_slider.name = "time_slider"
# playback speed
default_playback_speed = 0.05
playback_speed_slider = self.plotter.add_slider_widget(
self.set_playback_speed,
value=default_playback_speed,
rng=[0.01, 1], title="playback speed",
pointa=(0.02, 0.1),
pointb=(0.18, 0.1)
)
# colormap slider
scaling_limits = [0.2, 2.0]
pointa = np.array((0.82, 0.26))
pointb = np.array((0.98, 0.26))
shift = np.array([0, 0.08])
fmin = brain._data["fmin"]
self.update_fmin = BumpColorbarPoints(
plotter=self.plotter,
brain=brain,
name="fmin"
)
fmin_slider = self.plotter.add_slider_widget(
self.update_fmin,
value=fmin,
rng=_get_range(brain), title="clim",
pointa=pointa,
pointb=pointb,
event_type="always",
)
fmin_slider.name = "fmin"
fmid = brain._data["fmid"]
self.update_fmid = BumpColorbarPoints(
plotter=self.plotter,
brain=brain,
name="fmid",
)
fmid_slider = self.plotter.add_slider_widget(
self.update_fmid,
value=fmid,
rng=_get_range(brain), title="",
pointa=pointa + shift,
pointb=pointb + shift,
event_type="always",
)
fmid_slider.name = "fmid"
fmax = brain._data["fmax"]
self.update_fmax = BumpColorbarPoints(
plotter=self.plotter,
brain=brain,
name="fmax",
)
fmax_slider = self.plotter.add_slider_widget(
self.update_fmax,
value=fmax,
rng=_get_range(brain), title="",
pointa=pointa + 2 * shift,
pointb=pointb + 2 * shift,
event_type="always",
)
fmax_slider.name = "fmax"
self.update_fscale = UpdateColorbarScale(
plotter=self.plotter,
brain=brain,
)
fscale_slider = self.plotter.add_slider_widget(
self.update_fscale,
value=1.0,
rng=scaling_limits, title="fscale",
pointa=(0.82, 0.10),
pointb=(0.98, 0.10)
)
fscale_slider.name = "fscale"
# add toggle to start/stop playback
self.playback = False
self.playback_speed = default_playback_speed
self.refresh_rate_ms = max(int(round(1000. / 60.)), 1)
self.plotter.add_callback(self.play, self.refresh_rate_ms)
self.plotter.add_key_event('space', self.toggle_playback)
# add toggle to show/hide interface
self.visibility = True
self.plotter.add_key_event('y', self.toggle_interface)
# apply auto-scaling action
self.plotter.add_key_event('t', self.apply_auto_scaling)
# restore user scaling action
self.plotter.add_key_event('u', self.restore_user_scaling)
# set the slider style
self.set_slider_style(smoothing_slider)
self.set_slider_style(fmin_slider)
self.set_slider_style(fmid_slider)
self.set_slider_style(fmax_slider)
self.set_slider_style(fscale_slider)
self.set_slider_style(playback_speed_slider)
self.set_slider_style(time_slider, show_label=False)
# set the text style
_set_text_style(self.time_actor)
# use a matplotlib canvas
win = self.plotter.app_window
dpi = win.windowHandle().screen().logicalDotsPerInch()
w, h = win.geometry().width() / dpi, win.geometry().height() / dpi
h /= 3 # one third of the window
self.mpl_canvas = MplCanvas(win, w, h, dpi)
xlim = [np.min(self.brain._data['time']),
np.max(self.brain._data['time'])]
self.mpl_canvas.axes.set(xlim=xlim)
vlayout = self.plotter.frame.layout()
vlayout.addWidget(self.mpl_canvas)
vlayout.setStretch(0, 2)
vlayout.setStretch(1, 1)
def toggle_interface(self):
self.visibility = not self.visibility
for slider in self.plotter.slider_widgets:
if self.visibility:
slider.On()
else:
slider.Off()
def apply_auto_scaling(self):
self.brain.update_auto_scaling()
fmin = self.brain._data['fmin']
fmid = self.brain._data['fmid']
fmax = self.brain._data['fmax']
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name == "fmin":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmin)
elif name == "fmid":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmid)
elif name == "fmax":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmax)
def restore_user_scaling(self):
self.brain.update_auto_scaling(restore=True)
fmin = self.brain._data['fmin']
fmid = self.brain._data['fmid']
fmax = self.brain._data['fmax']
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name == "fmin":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmin)
elif name == "fmid":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmid)
elif name == "fmax":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(fmax)
def toggle_playback(self):
self.playback = not self.playback
if self.playback:
time_data = self.brain._data['time']
max_time = np.max(time_data)
if self.brain._current_time == max_time: # start over
self.brain.set_time_point(np.min(time_data))
self._last_tick = time.time()
def set_playback_speed(self, speed):
self.playback_speed = speed
def play(self):
from scipy.interpolate import interp1d
if self.playback:
this_time = time.time()
delta = this_time - self._last_tick
self._last_tick = time.time()
time_data = self.brain._data['time']
times = np.arange(self.brain._n_times)
time_shift = delta * self.playback_speed
max_time = np.max(time_data)
time_point = min(self.brain._current_time + time_shift, max_time)
ifunc = interp1d(time_data, times)
idx = ifunc(time_point)
self.brain.set_time_point(idx)
for slider in self.plotter.slider_widgets:
name = getattr(slider, "name", None)
if name == "time_slider":
slider_rep = slider.GetRepresentation()
slider_rep.SetValue(idx)
if time_point == max_time:
self.playback = False
self.plotter.update() # critical for smooth animation
def set_slider_style(self, slider, show_label=True):
if slider is not None:
slider_rep = slider.GetRepresentation()
slider_rep.SetSliderLength(0.02)
slider_rep.SetSliderWidth(0.04)
slider_rep.SetTubeWidth(0.005)
slider_rep.SetEndCapLength(0.01)
slider_rep.SetEndCapWidth(0.02)
slider_rep.GetSliderProperty().SetColor((0.5, 0.5, 0.5))
if not show_label:
slider_rep.ShowSliderLabelOff()
def pick_point(self, mesh, vertex_id):
self.point_actor.VisibilityOn()
self.point_actor.SetInput(str(vertex_id))
if vertex_id != -1:
if hasattr(mesh, "_hemi"):
if vertex_id not in self.picked_points:
# add glyph at picked point
color = next(self.color_cycle)
center = mesh.GetPoints().GetPoint(vertex_id)
actor, mesh = self.brain._renderer.sphere(
center=np.array(center),
color=color,
scale=1.0,
radius=3.0
)
if self.act_data is None:
hemi = self.brain._hemi
hemi_data = self.brain._data.get(hemi)
self.act_data = self.brain._data['array']
smooth_mat = hemi_data['smooth_mat']
if smooth_mat is not None:
self.act_data = smooth_mat.dot(self.act_data)
time = self.brain._data['time']
line = self.mpl_canvas.plot(
time,
self.act_data[vertex_id, :],
vertex_id,
lw=1.,
color=color
)
# add metadata for picking
mesh._line = line
mesh._actor = actor
mesh._vertex_id = vertex_id
self.picked_points.append(vertex_id)
else:
vertex_id = mesh._vertex_id
self.picked_points.remove(vertex_id)
self.plotter.remove_actor(mesh._actor)
mesh._line.remove()
self.mpl_canvas.update_plot()
def remove_point(self, index):
pass
def _set_text_style(text_actor):
if text_actor is not None:
prop = text_actor.GetTextProperty()
prop.BoldOn()
def _get_range(brain):
val = np.abs(brain._data['array'])
return [np.min(val), np.max(val)]
def _normalize(point, shape):
return (point[0] / shape[1], point[1] / shape[0])