-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot_dataset.py
805 lines (676 loc) · 30.8 KB
/
plot_dataset.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
import math
import copy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.widgets import Slider
from typing import Sequence
from functools import reduce, lru_cache
from torch.utils.data import DataLoader
from scipy.ndimage.measurements import center_of_mass
vox_views = [['sag', 'vox', 50], ['ax', 'vox', 50], ['cor', 'vox', 50]]
class View:
def __init__(self, view, type2idx, subject, figure, axis, channel,
show_text, cmap, alpha, threshold):
self.view_type, self.coordinate_system, self.position = view
self.view_idx = type2idx.index(self.view_type)
self.subject = subject
self.figure = figure
self.axis = axis
self.channel = channel
self.show_text = show_text
self.is_drawn = False
self.bg_cache = self.figure.canvas.copy_from_bbox(self.axis.bbox)
self.axis_img = None
self.axis_label = None
self.cmap = cmap
self.alpha = alpha
self.threshold = threshold
self.img = None
self.affine = None
self.label = None
def set_position(self, position):
self.position = position
def set_channel(self, channel):
self.channel = channel
def set_img_affine_and_label(self, img, affine, label):
self.img = img
self.affine = affine
self.label = label
def set_is_drawn(self, is_drawn):
self.is_drawn = is_drawn
@lru_cache(100)
def mapped_position(self, position):
if self.coordinate_system == "vox":
if position < 0:
position = 0
if position >= 100:
position = 99
mapped_position = self.img.shape[self.view_idx] * position / 100
else:
position_vector = np.zeros(4)
position_vector[self.view_idx] = position
position_vector[3] = 1
mapped_position = np.linalg.solve(
self.affine, position_vector)[self.view_idx]
# Clip mapped_position to match image shape and clip position
if mapped_position < 0:
mapped_position = 0
if mapped_position >= self.img.shape[self.view_idx] - 0.5:
mapped_position = self.img.shape[self.view_idx] - 1
position_vector[self.view_idx] = mapped_position
position = np.dot(self.affine, position_vector)[self.view_idx]
return int(round(mapped_position)), int(round(position))
def center_of_mass_position(self):
# Visualize label from center of mass
com = center_of_mass(self.label[self.channel])
mapped_position = com[self.view_idx]
if self.coordinate_system == "vox":
position = 100 * mapped_position / self.img.shape[self.view_idx]
else:
position_vector = np.zeros(4)
position_vector[3] = 1
position_vector[self.view_idx] = mapped_position
position = np.dot(self.affine, position_vector)[self.view_idx]
return int(round(mapped_position)), int(round(position))
def max_volume_position(self):
# Visualize label from the slice with maximum label volume
axes = [(1, 2), (0, 2), (0, 1)]
s = np.sum(self.label[self.channel], axis=axes[self.view_idx])
maximum = -1
idx = 0
for i, item in enumerate(s):
if item > maximum:
maximum = item
idx = i
mapped_position = idx
if self.coordinate_system == "vox":
position = 100 * mapped_position / self.img.shape[self.view_idx]
else:
position_vector = np.zeros(4)
position_vector[3] = 1
position_vector[self.view_idx] = mapped_position
position = np.dot(self.affine, position_vector)[self.view_idx]
return int(round(mapped_position)), int(round(position))
@lru_cache(100)
def img_slice(self, position):
if self.view_idx == 0:
view_slice = self.img[position, :, :]
elif self.view_idx == 1:
view_slice = self.img[:, position, :]
else:
view_slice = self.img[:, :, position]
return np.flipud(view_slice.T)
@property
def legend(self):
complete_view_types = {
'sag': 'sagittal', 'ax': 'axial', 'cor': 'coronal'}
if self.coordinate_system == 'vox':
return f'Subject {self.subject}: ' \
f'{complete_view_types[self.view_type]} section, ' \
f'{self.position}% voxels'
else:
return f'Subject {self.subject}: ' \
f'{complete_view_types[self.view_type]} section, ' \
f'{self.position} mm'
@lru_cache(100)
def label_slice(self, position, channel):
if self.view_idx == 0:
view_slice = self.label[channel][position, :, :]
elif self.view_idx == 1:
view_slice = self.label[channel][:, position, :]
else:
view_slice = self.label[channel][:, :, position]
return np.flipud(view_slice.T)
def render(self, init=False, synchronize=None):
if self.label is not None:
if synchronize == 'center_of_mass':
mapped_position, self.position = self.center_of_mass_position()
elif synchronize == 'max_volume':
mapped_position, self.position = self.max_volume_position()
else:
mapped_position, self.position = self.mapped_position(self.position)
else:
mapped_position, self.position = self.mapped_position(self.position)
img_slice = self.img_slice(mapped_position)
label_slice = None
if self.label is not None:
label_slice = self.label_slice(mapped_position, self.channel)
if init:
if self.show_text:
self.axis.text(0.5, -0.1, self.legend, size=8, ha="center",
transform=self.axis.transAxes)
self.axis_img = self.axis.imshow(img_slice, cmap='gray')
if self.label is not None:
self.axis_label = self.axis.imshow(
label_slice, cmap=self.cmap, alpha=self.alpha,
clim=[self.threshold, 1])
if self.is_drawn:
if self.show_text:
text_box = self.axis.texts[0]
text_box.set_text(self.legend)
self.axis_img.set_data(img_slice)
if self.label is not None:
self.axis_label.set_data(label_slice)
self.axis.draw_artist(self.axis_img)
if self.label is not None:
self.axis.draw_artist(self.axis_label)
self.figure.canvas.blit(self.axis.bbox)
class Figure:
def __init__(self, dataset, subject_idx, views, view_org,
image_key_name, subject_org, update_all_on_scroll, add_text,
label_key_name, alpha, cmap, threshold, type2idx,
patch_sampler, nb_patches):
self.dataset = dataset
self.subject_idx = subject_idx
self.views = views
self.view_org = view_org
self.image_key_name = image_key_name
self.subject_org = subject_org
self.update_all_on_scroll = update_all_on_scroll
self.add_text = add_text
self.label_key_name = label_key_name
self.alpha = alpha
self.cmap = cmap
self.threshold = threshold
self.type2idx = type2idx
self.patch_sampler = patch_sampler
self.nb_patches = nb_patches
self.fig = None
self.axes = None
self.slider = None
self.is_drawn = False
self.view_objects = {}
self.axes2view_keys = {}
self.idx = None
self.other_axes = []
self.num_labels = 1
def set_attributes(self, fig, axes, slider, subject_org):
self.fig = fig
self.axes = axes
self.slider = slider
self.subject_org = subject_org
@lru_cache(maxsize=10)
def load_subjects(self):
# DataLoader case
print(f' suj idx is {self.subject_idx}')
if self.subject_idx is None:
subjects = next(iter(self.dataset))
images, affines, labels = self.get_values_from_batch(subjects)
print(f'data loader suj {subjects["name"]}')
else:
subjects = [self.dataset[int(idx)] for idx in self.subject_idx]
images, affines, labels = self.get_values_from_samples(subjects)
for ss in subjects:
print(f' loaded subject {ss.name}')
self.idx = tuple(range(len(images)))
self.adapt_subject_org()
if self.label_key_name is not None:
self.num_labels = len(labels[0])
return images, affines, labels
def get_values_from_batch(self, batch):
if isinstance(batch, list):
batch_len = len(batch[0][self.image_key_name]['data'])
else:
batch_len = len(batch[self.image_key_name]['data'])
self.subject_idx = list(range(batch_len))
images, affines, labels = [], [], None
if self.label_key_name is not None:
labels = []
for i in range(batch_len):
if isinstance(batch, list):
batch_list = batch
else:
batch_list = [batch]
for b in batch_list:
images.append(b[self.image_key_name]['data'][i][0].numpy())
affines.append(b[self.image_key_name]['affine'][i])
if self.label_key_name is not None:
labels.append(b[self.label_key_name]['data'][i].numpy())
return images, affines, labels
def get_values_from_samples(self, samples):
images, affines, labels = [], [], None
if self.label_key_name is not None:
labels = []
for sample in samples:
if isinstance(sample, list):
sample_list = sample
else:
sample_list = [sample]
for s in sample_list:
image = s[self.image_key_name]['data'][0].numpy()
lab = None
if self.label_key_name is not None:
lab = s[self.label_key_name]['data'].numpy()
if self.patch_sampler is not None:
image, lab = self.sample_patches(s, image, lab)
images.append(image)
affines.append(s[self.image_key_name]['affine'])
if self.label_key_name is not None:
labels.append(lab)
return images, affines, labels
def sample_patches(self, sample, image, label):
for _ in range(self.nb_patches):
patch = next(self.patch_sampler(sample))
spatial_shape = np.array(patch.spatial_shape)
i_ini, j_ini, k_ini = patch['index_ini']
i_fin, j_fin, k_fin = patch['index_ini'] + spatial_shape
im_patch = patch[self.image_key_name]['data'].numpy()[0].copy()
im_patch[:2, :, :] = 1
im_patch[-2:, :, :] = 1
im_patch[:, :2, :] = 1
im_patch[:, -2:, :] = 1
im_patch[:, :, :2] = 1
im_patch[:, :, -2:] = 1
image[i_ini:i_fin, j_ini:j_fin, k_ini:k_fin] = im_patch
if self.label_key_name is not None:
label_patch = patch[self.label_key_name]['data'].numpy().copy()
label[
:,
i_ini:i_fin,
j_ini:j_fin,
k_ini:k_fin
] = label_patch
return image, label
def adapt_subject_org(self):
if np.prod(self.subject_org) < len(self.idx):
self.subject_org = (1, len(self.idx))
def set_views(self):
indices = [(i, j) for i in range(self.view_org[0] * self.subject_org[0])
for j in range(self.view_org[1] * self.subject_org[1])]
# Assign each view to its axis
for i in self.idx:
for j, view in enumerate(self.views):
x = j // self.view_org[1] \
+ i // self.subject_org[1] * self.view_org[0]
y = j % self.view_org[1] \
+ i % self.subject_org[1] * self.view_org[1]
indices.remove((x, y))
axis = self.axes[x, y]
view_type, coordinate_system, _ = view
view_key = (i, view_type, coordinate_system)
if self.view_objects.get(view_key) is None:
self.view_objects[view_key] = View(
view, self.type2idx, i, self.fig, axis,
channel=0,
show_text=self.add_text,
cmap=self.cmap,
alpha=self.alpha,
threshold=self.threshold)
self.axes2view_keys[axis] = view_key
for i, j in indices:
self.other_axes.append(self.axes[i, j])
for view_object in self.view_objects.values():
view_object.set_is_drawn(self.is_drawn)
def render_views(self, images, affines, labels, init=False, synchronize=None):
for i in self.idx:
img, affine, label = images[i], affines[i], None
if self.label_key_name is not None:
label = labels[i]
for view_key in filter(
lambda key: key[0] == i, self.view_objects.keys()):
self.view_objects[view_key].set_img_affine_and_label(
img, affine, label
)
# Render each view
for view_object in self.view_objects.values():
view_object.axis.set_visible(True)
view_object.render(init=init, synchronize=synchronize)
# Update slider value
if self.label_key_name is not None:
self.on_slide(self.threshold)
# Hide other axes
for axis in self.other_axes:
axis.set_visible(False)
def display_figure(self, synchronize=None):
# Load subjects
images, affines, labels = self.load_subjects()
# Set views
self.set_views()
# Render views
self.render_views(images, affines, labels, init=True, synchronize=synchronize)
def set_is_drawn(self, is_drawn):
self.is_drawn = is_drawn
for view_object in self.view_objects.values():
view_object.set_is_drawn(is_drawn)
def clear_figure(self):
# Clear View objects
for view_key, view_object in self.view_objects.items():
view_object.set_img_affine_and_label(None, None, None)
def update(self, view_keys, update_function, synchronize=None):
for key in view_keys:
view_object = self.view_objects[key]
update_function(view_object)
view_object.render(synchronize=synchronize)
self.fig.canvas.flush_events()
def on_scroll(self, event):
view_key = self.axes2view_keys.get(event.inaxes)
if view_key is not None:
if event.button == 'down':
delta = -1
else:
delta = 1
if self.update_all_on_scroll:
subject, view_type, coordinate_system = view_key
view_keys = [(s, view_type, coordinate_system) for s in
self.idx]
else:
view_keys = [view_key]
self.update(
view_keys,
lambda x: x.set_position(x.position + delta)
)
def on_key_press(self, event, synchronize):
if event.key == 'down':
delta = -1
else:
delta = 1
self.update(
self.view_objects.keys(),
lambda x: x.set_channel((x.channel + delta) % self.num_labels),
synchronize
)
def on_slide(self, val):
self.threshold = val
self.slider.set_val(val)
if self.is_drawn:
for view_object in self.view_objects.values():
view_object.threshold = self.threshold
self.fig.canvas.restore_region(view_object.bg_cache)
view_object.axis_label.set_clim([self.threshold, 1])
view_object.axis.draw_artist(view_object.axis_img)
view_object.axis.draw_artist(view_object.axis_label)
self.fig.canvas.blit(view_object.axis.bbox)
class PlotDataset:
"""Draw an interactive plot of a few subjects from a torchio dataset.
Scrolling on the different images enable to navigate sections.
Hitting up and down keys enable to navigate label maps.
Hitting pageup and pagedown keys enable to navigate figures.
Args:
dataset: a :py:class:`~torchio.ImagesDataset` or a
:py:class:`~torch.data.utils.DataLoader` constructed from torchio.
views: None or a sequence of views, each view is given as
(view_type, coordinate_system, position),
view_type is one among "sag", "ax" and "cor" which corresponds
to sagittal, axial and coronal slices;
coordinate_system is one among "vox" or "mm" and is responsible
for placing the slice using position in term of voxel number
or number of millimeters;
position is either an integer between 0 and 100 if
coordinate_system is "vox" or a float otherwise.
If no value is provided, the default views are used:
[['sag', 'vox', 50], ['ax', 'vox', 50], ['cor', 'vox', 50]].
view_org: None or a sequence of length 2, responsible for the
organisation of views in subplots. Default is (len(views), 1).
image_key_name: a string that gives the key of the volume of interest
in the dataset's samples.
subject_idx: None, an integer or a sequence of integers. Defines
which subjects are plotted.
If subject_idx is a sequence of integers, it is directly used
as the list of indexes,
if subject_idx is an integer, subjects_idx subjects are taken at
random in the dataset.
Finally, if subject_idx is None, all subjects are taken.
Default value is 5.
subject_org: None or a sequence of length 2, responsible for the
organisation of subjects in subplots.
Default is (1, len(subject_idx)).
figsize: Sequence of length 2, size of the figure.
update_all_on_scroll: bool, if True all views with the same view_type
and coordinate_system are updated when scrolling on one of them.
Doing so supposes that they all have the same shape.
Default is False.
add_text: Boolean to choose if you want the axis legend to be printed.
Default is True.
label_key_name: a string or a list of strings that gives the key(s) of
the label maps of interest in the dataset's samples.
alpha: overlay opacity, used when plotting label, default is 0.2.
cmap: the colormap used to plot label, default is 'RdBu'.
patch_sampler: a sampler used to generate patches from the volumes,
if sampler is not None, the patches are superimposed on the
volumes with white borders. Default is None.
nb_patches: the number of patches to draw from each sample.
Used only if patch_sampler is not None.
threshold: the threshold below which label are not shown.
preload: Boolean to choose whether to preload all subjects are not.
Default is True.
synchronize_view_to_label: show images from center of mass or maximum volume of the label.
Possible values are 'center_of_mass' or 'max_volume'. Dataset must have labels.
Default is None (meaning that the position will be set by the 'views' parameter).
"""
def __init__(self, dataset, views=None, view_org=None,
image_key_name='image', subject_idx=5, subject_org=None,
figsize=(16, 9), update_all_on_scroll=False, add_text=True,
label_key_name=None, alpha=0.2, cmap='RdBu',
patch_sampler=None, nb_patches=4, threshold=0.01,
preload=True, synchronize_view_to_label=None):
self.dataset = dataset
self.views = views if views is not None else vox_views
self.view_org = self.parse_view_org(view_org)
self.image_key_name = image_key_name
self.subject_idx = self.parse_subject_idx(subject_idx)
self.subject_org = self.parse_subject_org(subject_org)
self.figsize = figsize
self.update_all_on_scroll = update_all_on_scroll
self.add_text = add_text
self.label_key_name = label_key_name
self.alpha = alpha
self.cmap = copy.copy(cm.get_cmap(cmap))
self.cmap.set_under(color='k', alpha=0)
self.patch_sampler = patch_sampler
self.nb_patches = nb_patches
self.threshold = threshold
self.preload = preload
self.synchronize_view_to_label = synchronize_view_to_label
self.figure_objects = []
self.fig = None
self.axes = None
self.slider = None
self.current_figure = 0
self.updating_views = False
self.coordinate_system_list = ["vox", "mm"]
self.type2idx = ["sag", "cor", "ax"]
self.check_views()
self.init_plot()
def parse_view_org(self, view_org):
if isinstance(view_org, Sequence) and len(view_org) == 2:
return view_org
else:
return len(self.views), 1
def parse_subject_idx(self, subject_idx):
data_len = len(self.dataset)
if hasattr(self.dataset, 'batch_size'):
data_len *= self.dataset.batch_size
print(f'data set length is {data_len} batch size is {self.dataset.batch_size}')
if isinstance(subject_idx, Sequence):
valid = reduce(lambda acc, val: acc and 0 <= val < data_len,
subject_idx, True)
if not valid:
raise ValueError('Invalid index sequence')
return subject_idx
elif isinstance(subject_idx, int):
return range(min(data_len, subject_idx))
#return np.random.choice(range(data_len), min(data_len, subject_idx), replace=False).astype(int)
else:
return list(range(data_len))
def parse_subject_org(self, subject_org):
if isinstance(subject_org, Sequence) and len(subject_org) == 2:
return subject_org
else:
return 1, len(self.subject_idx)
def check_views(self):
for view_type, coordinate_system, _ in self.views:
if coordinate_system not in self.coordinate_system_list:
raise ValueError(
f'coordinate_system {coordinate_system} not recognized '
f'among {self.coordinate_system_list}'
)
if view_type not in self.type2idx:
raise ValueError(f'view_type {view_type} not recognized '
f'among {self.type2idx}')
def create_subplot(self):
subplot_shape = (self.view_org[0] * self.subject_org[0],
self.view_org[1] * self.subject_org[1])
self.fig, axes = plt.subplots(*subplot_shape, figsize=self.figsize)
self.fig.tight_layout()
self.axes = axes.reshape(subplot_shape)
# Add sliders to set overlay display threshold
if self.label_key_name is not None:
axis = plt.axes([0.01, 0.05, 0.02, 0.9])
print('aaAAAAAAAAAAAAAAAAAAAAAAAAaaa')
self.slider = NewSlider(
axis, 'Threshold', 0, 1, valinit=self.threshold,
valstep=0.01, orientation='vertical')
# Add event handlers
self.fig.canvas.mpl_connect('scroll_event', self.on_scroll)
self.fig.canvas.mpl_connect('key_press_event', self.on_key_press)
self.fig.canvas.mpl_connect('key_press_event', self.on_go_to)
if self.label_key_name is not None:
self.slider.on_changed(self.on_slide)
# Remove axis for all subplots
for axis in self.axes.ravel():
axis.axis('off')
# Remove space between subplots
if self.label_key_name is None:
plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0,
hspace=0)
else:
plt.subplots_adjust(left=0.04, right=0.96, bottom=0, top=1,
wspace=0, hspace=0)
# Add colorbar
if self.label_key_name is not None:
# Create a fake label to make the colorbar invariant
# to threshold changes
axis = plt.axes([0., 0., 0., 0.])
fake_label = axis.imshow(np.linspace(0, 1, 100).reshape(10, 10),
cmap=self.cmap, alpha=self.alpha)
fake_label.set_visible(False)
color_bar_axis = self.fig.add_axes([0.96, 0.05, 0.02, 0.9])
self.fig.colorbar(fake_label, cax=color_bar_axis)
def init_plot(self):
nb_subject_per_figure = np.product(self.subject_org)
nb_figures = math.ceil(len(self.subject_idx) / nb_subject_per_figure)
# Create figure objects
for i in range(nb_figures):
if isinstance(self.dataset, DataLoader):
subject_idx = None
else:
subject_idx = self.subject_idx[
i * nb_subject_per_figure: (i + 1) * nb_subject_per_figure
]
self.figure_objects.append(
Figure(self.dataset, subject_idx, self.views, self.view_org,
self.image_key_name, self.subject_org,
self.update_all_on_scroll, self.add_text,
self.label_key_name, self.alpha, self.cmap,
self.threshold, self.type2idx, self.patch_sampler,
self.nb_patches)
)
# Preload subjects
if self.preload:
for figure_object in self.figure_objects:
figure_object.load_subjects()
else:
self.figure_objects[0].load_subjects()
self.subject_org = self.figure_objects[0].subject_org
# Create matplotlib figure and axes
self.create_subplot()
# Set figure and axes
for figure_object in self.figure_objects:
figure_object.set_attributes(
self.fig, self.axes, self.slider, self.subject_org)
# Display first figure object
self.figure_objects[0].display_figure()
# Draw canvas
self.fig.canvas.draw()
self.figure_objects[0].set_is_drawn(True)
def on_scroll(self, event):
if not self.updating_views:
self.updating_views = True
self.figure_objects[self.current_figure].on_scroll(event)
self.updating_views = False
def on_key_press(self, event):
if self.label_key_name is not None and not self.updating_views:
if event.key == 'down' or event.key == 'up':
self.updating_views = True
self.figure_objects[self.current_figure].on_key_press(event, self.synchronize_view_to_label)
self.updating_views = False
def on_go_to(self, event):
if not self.updating_views:
if event.key == 'pageup' or event.key == 'pagedown':
self.updating_views = True
if event.key == 'pageup':
delta = -1
else:
delta = 1
# Find next figure index
num = (self.current_figure + delta) % len(self.figure_objects)
self.figure_objects[num].set_is_drawn(True)
# Add consistency with position/label when changing subject with update_all_on_scroll
if self.update_all_on_scroll:
# Get current position/label for each image
keys = list(self.figure_objects[self.current_figure].view_objects.keys())
positions = []
channels = []
for i, key in enumerate(keys):
if i < len(self.views):
positions.append(self.figure_objects[self.current_figure].view_objects[key].position)
channels.append(self.figure_objects[self.current_figure].view_objects[key].channel)
# Load subjects and set views
images, affines, labels = self.figure_objects[num].load_subjects()
self.figure_objects[num].set_views()
# Set position/label for next figure to show
next_keys = list(self.figure_objects[num].view_objects.keys())
for i, key in enumerate(next_keys):
j = i % len(self.views)
self.figure_objects[num].view_objects[key].position = positions[j]
self.figure_objects[num].view_objects[key].channel = channels[j]
self.figure_objects[num].render_views(images, affines, labels, True)
# If update_all_on_scroll is False, just load next subjects and display them
else:
self.figure_objects[num].display_figure()
# Clear previous figure, update parameters
if len(self.figure_objects[self.current_figure].view_objects) != \
len(self.figure_objects[num].view_objects):
self.fig.canvas.resize_event()
self.figure_objects[self.current_figure].clear_figure()
self.current_figure = num
self.updating_views = False
def on_slide(self, val):
if not self.updating_views:
self.updating_views = True
self.figure_objects[self.current_figure].on_slide(val)
self.fig.canvas.resize_event()
self.updating_views = False
class NewSlider(Slider):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bg_cache = self.ax.figure.canvas.copy_from_bbox(self.ax.bbox)
def set_val(self, val):
"""
Set slider value to *val*
Parameters
----------
val : float
"""
bg_cache = self.ax.figure.canvas.copy_from_bbox(self.ax.bbox)
xy = self.poly.xy
if self.orientation == 'vertical':
xy[1] = 0, val
xy[2] = 1, val
else:
xy[2] = val, 1
xy[3] = val, 0
self.poly.xy = xy
val_text = self.valfmt % val if self.valfmt is not None else val
self.valtext.set_text(val_text)
if self.drawon and self.ax.figure._cachedRenderer is None:
self.ax.figure.canvas.draw_idle()
elif self.drawon:
self.ax.figure.canvas.restore_region(bg_cache)
self.ax.draw_artist(self.poly)
self.ax.draw_artist(self.valtext)
self.ax.figure.canvas.blit()
self.val = val
if self.eventson:
self._observers.process('changed', val)