forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_viewer_widget.py
473 lines (401 loc) · 15.3 KB
/
multiple_viewer_widget.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
"""
Multiple viewer widget
======================
This is an example on how to have more than one viewer in the same napari window.
Additional viewers state will be synchronized with the main viewer.
Switching to 3D display will only impact the main viewer.
This example also contain option to enable cross that will be moved to the
current dims point (`viewer.dims.point`).
.. tags:: gui
"""
from copy import deepcopy
from typing import Optional
import numpy as np
from packaging.version import parse as parse_version
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (
QCheckBox,
QDoubleSpinBox,
QPushButton,
QSplitter,
QTabWidget,
QVBoxLayout,
QWidget,
)
from superqt.utils import qthrottled
import napari
from napari.components.layerlist import Extent
from napari.components.viewer_model import ViewerModel
from napari.layers import Image, Labels, Layer, Vectors
from napari.qt import QtViewer
from napari.utils.action_manager import action_manager
from napari.utils.events.event import WarningEmitter
from napari.utils.notifications import show_info
NAPARI_GE_4_16 = parse_version(napari.__version__) > parse_version('0.4.16')
def copy_layer_le_4_16(layer: Layer, name: str = ''):
res_layer = deepcopy(layer)
# this deepcopy is not optimal for labels and images layers
if isinstance(layer, (Image, Labels)):
res_layer.data = layer.data
res_layer.metadata['viewer_name'] = name
res_layer.events.disconnect()
res_layer.events.source = res_layer
for emitter in res_layer.events.emitters.values():
emitter.disconnect()
emitter.source = res_layer
return res_layer
def copy_layer(layer: Layer, name: str = ''):
if not NAPARI_GE_4_16:
return copy_layer_le_4_16(layer, name)
res_layer = Layer.create(*layer.as_layer_data_tuple())
res_layer.metadata['viewer_name'] = name
return res_layer
def get_property_names(layer: Layer):
klass = layer.__class__
res = []
for event_name, event_emitter in layer.events.emitters.items():
if isinstance(event_emitter, WarningEmitter):
continue
if event_name in ('thumbnail', 'name'):
continue
if (
isinstance(getattr(klass, event_name, None), property)
and getattr(klass, event_name).fset is not None
):
res.append(event_name)
return res
def center_cross_on_mouse(
viewer_model: napari.components.viewer_model.ViewerModel,
):
"""move the cross to the mouse position"""
if not getattr(viewer_model, 'mouse_over_canvas', True):
# There is no way for napari 0.4.15 to check if mouse is over sending canvas.
show_info(
'Mouse is not over the canvas. You may need to click on the canvas.'
)
return
viewer_model.dims.current_step = tuple(
np.round(
[
max(min_, min(p, max_)) / step
for p, (min_, max_, step) in zip(
viewer_model.cursor.position, viewer_model.dims.range
)
]
).astype(int)
)
action_manager.register_action(
name='napari:move_point',
command=center_cross_on_mouse,
description='Move dims point to mouse position',
keymapprovider=ViewerModel,
)
action_manager.bind_shortcut('napari:move_point', 'C')
class own_partial:
"""
Workaround for deepcopy not copying partial functions
(Qt widgets are not serializable)
"""
def __init__(self, func, *args, **kwargs) -> None:
self.func = func
self.args = args
self.kwargs = kwargs
def __call__(self, *args, **kwargs):
return self.func(*(self.args + args), **{**self.kwargs, **kwargs})
def __deepcopy__(self, memodict=None):
if memodict is None:
memodict = {}
return own_partial(
self.func,
*deepcopy(self.args, memodict),
**deepcopy(self.kwargs, memodict),
)
class QtViewerWrap(QtViewer):
def __init__(self, main_viewer, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.main_viewer = main_viewer
def _qt_open(
self,
filenames: list,
stack: bool,
plugin: Optional[str] = None,
layer_type: Optional[str] = None,
**kwargs,
):
"""for drag and drop open files"""
self.main_viewer.window._qt_viewer._qt_open(
filenames, stack, plugin, layer_type, **kwargs
)
class CrossWidget(QCheckBox):
"""
Widget to control the cross layer. because of the performance reason
the cross update is throttled
"""
def __init__(self, viewer: napari.Viewer) -> None:
super().__init__('Add cross layer')
self.viewer = viewer
self.setChecked(False)
self.stateChanged.connect(self._update_cross_visibility)
self.layer = None
self.viewer.dims.events.order.connect(self.update_cross)
self.viewer.dims.events.ndim.connect(self._update_ndim)
self.viewer.dims.events.current_step.connect(self.update_cross)
self._extent = None
self._update_extent()
self.viewer.dims.events.connect(self._update_extent)
@qthrottled(leading=False)
def _update_extent(self):
"""
Calculate the extent of the data.
Ignores the the cross layer itself in calculating the extent.
"""
if NAPARI_GE_4_16:
layers = [
layer
for layer in self.viewer.layers
if layer is not self.layer
]
self._extent = self.viewer.layers.get_extent(layers)
else:
extent_list = [
layer.extent
for layer in self.viewer.layers
if layer is not self.layer
]
self._extent = Extent(
data=None,
world=self.viewer.layers._get_extent_world(extent_list),
step=self.viewer.layers._get_step_size(extent_list),
)
self.update_cross()
def _update_ndim(self, event):
if self.layer in self.viewer.layers:
self.viewer.layers.remove(self.layer)
self.layer = Vectors(name='.cross', ndim=event.value)
self.layer.edge_width = 1.5
self.update_cross()
def _update_cross_visibility(self, state):
if state:
self.viewer.layers.append(self.layer)
else:
self.viewer.layers.remove(self.layer)
self.update_cross()
def update_cross(self):
if self.layer not in self.viewer.layers:
return
point = self.viewer.dims.current_step
vec = []
for i, (lower, upper) in enumerate(self._extent.world.T):
if (upper - lower) / self._extent.step[i] == 1:
continue
point1 = list(point)
point1[i] = (lower + self._extent.step[i] / 2) / self._extent.step[
i
]
point2 = [0 for _ in point]
point2[i] = (upper - lower) / self._extent.step[i]
vec.append((point1, point2))
if np.any(self.layer.scale != self._extent.step):
self.layer.scale = self._extent.step
self.layer.data = vec
class ExampleWidget(QWidget):
"""
Dummy widget showcasing how to place additional widgets to the right
of the additional viewers.
"""
def __init__(self) -> None:
super().__init__()
self.btn = QPushButton('Perform action')
self.spin = QDoubleSpinBox()
layout = QVBoxLayout()
layout.addWidget(self.spin)
layout.addWidget(self.btn)
layout.addStretch(1)
self.setLayout(layout)
class MultipleViewerWidget(QSplitter):
"""The main widget of the example."""
def __init__(self, viewer: napari.Viewer) -> None:
super().__init__()
self.viewer = viewer
self.viewer_model1 = ViewerModel(title='model1')
self.viewer_model2 = ViewerModel(title='model2')
self._block = False
self.qt_viewer1 = QtViewerWrap(viewer, self.viewer_model1)
self.qt_viewer2 = QtViewerWrap(viewer, self.viewer_model2)
self.tab_widget = QTabWidget()
w1 = ExampleWidget()
w2 = ExampleWidget()
self.tab_widget.addTab(w1, 'Sample 1')
self.tab_widget.addTab(w2, 'Sample 2')
viewer_splitter = QSplitter()
viewer_splitter.setOrientation(Qt.Vertical)
viewer_splitter.addWidget(self.qt_viewer1)
viewer_splitter.addWidget(self.qt_viewer2)
viewer_splitter.setContentsMargins(0, 0, 0, 0)
self.addWidget(viewer_splitter)
self.addWidget(self.tab_widget)
self.viewer.layers.events.inserted.connect(self._layer_added)
self.viewer.layers.events.removed.connect(self._layer_removed)
self.viewer.layers.events.moved.connect(self._layer_moved)
self.viewer.layers.selection.events.active.connect(
self._layer_selection_changed
)
self.viewer.dims.events.current_step.connect(self._point_update)
self.viewer_model1.dims.events.current_step.connect(self._point_update)
self.viewer_model2.dims.events.current_step.connect(self._point_update)
self.viewer.dims.events.order.connect(self._order_update)
self.viewer.events.reset_view.connect(self._reset_view)
self.viewer_model1.events.status.connect(self._status_update)
self.viewer_model2.events.status.connect(self._status_update)
def _status_update(self, event):
self.viewer.status = event.value
def _reset_view(self):
self.viewer_model1.reset_view()
self.viewer_model2.reset_view()
def _layer_selection_changed(self, event):
"""
update of current active layer
"""
if self._block:
return
if event.value is None:
self.viewer_model1.layers.selection.active = None
self.viewer_model2.layers.selection.active = None
return
self.viewer_model1.layers.selection.active = self.viewer_model1.layers[
event.value.name
]
self.viewer_model2.layers.selection.active = self.viewer_model2.layers[
event.value.name
]
def _point_update(self, event):
for model in [self.viewer, self.viewer_model1, self.viewer_model2]:
if model.dims is event.source:
continue
if len(self.viewer.layers) != len(model.layers):
continue
model.dims.current_step = event.value
def _order_update(self):
order = list(self.viewer.dims.order)
if len(order) <= 2:
self.viewer_model1.dims.order = order
self.viewer_model2.dims.order = order
return
order[-3:] = order[-2], order[-3], order[-1]
self.viewer_model1.dims.order = order
order = list(self.viewer.dims.order)
order[-3:] = order[-1], order[-2], order[-3]
self.viewer_model2.dims.order = order
def _layer_added(self, event):
"""add layer to additional viewers and connect all required events"""
self.viewer_model1.layers.insert(
event.index, copy_layer(event.value, 'model1')
)
self.viewer_model2.layers.insert(
event.index, copy_layer(event.value, 'model2')
)
for name in get_property_names(event.value):
getattr(event.value.events, name).connect(
own_partial(self._property_sync, name)
)
if isinstance(event.value, Labels):
event.value.events.set_data.connect(self._set_data_refresh)
event.value.events.labels_update.connect(self._set_data_refresh)
self.viewer_model1.layers[
event.value.name
].events.set_data.connect(self._set_data_refresh)
self.viewer_model2.layers[
event.value.name
].events.set_data.connect(self._set_data_refresh)
event.value.events.labels_update.connect(self._set_data_refresh)
self.viewer_model1.layers[
event.value.name
].events.labels_update.connect(self._set_data_refresh)
self.viewer_model2.layers[
event.value.name
].events.labels_update.connect(self._set_data_refresh)
if event.value.name != '.cross':
self.viewer_model1.layers[event.value.name].events.data.connect(
self._sync_data
)
self.viewer_model2.layers[event.value.name].events.data.connect(
self._sync_data
)
event.value.events.name.connect(self._sync_name)
self._order_update()
def _sync_name(self, event):
"""sync name of layers"""
index = self.viewer.layers.index(event.source)
self.viewer_model1.layers[index].name = event.source.name
self.viewer_model2.layers[index].name = event.source.name
def _sync_data(self, event):
"""sync data modification from additional viewers"""
if self._block:
return
for model in [self.viewer, self.viewer_model1, self.viewer_model2]:
layer = model.layers[event.source.name]
if layer is event.source:
continue
try:
self._block = True
layer.data = event.source.data
finally:
self._block = False
def _set_data_refresh(self, event):
"""
synchronize data refresh between layers
"""
if self._block:
return
for model in [self.viewer, self.viewer_model1, self.viewer_model2]:
layer = model.layers[event.source.name]
if layer is event.source:
continue
try:
self._block = True
layer.refresh()
finally:
self._block = False
def _layer_removed(self, event):
"""remove layer in all viewers"""
self.viewer_model1.layers.pop(event.index)
self.viewer_model2.layers.pop(event.index)
def _layer_moved(self, event):
"""update order of layers"""
dest_index = (
event.new_index
if event.new_index < event.index
else event.new_index + 1
)
self.viewer_model1.layers.move(event.index, dest_index)
self.viewer_model2.layers.move(event.index, dest_index)
def _property_sync(self, name, event):
"""Sync layers properties (except the name)"""
if event.source not in self.viewer.layers:
return
try:
self._block = True
setattr(
self.viewer_model1.layers[event.source.name],
name,
getattr(event.source, name),
)
setattr(
self.viewer_model2.layers[event.source.name],
name,
getattr(event.source, name),
)
finally:
self._block = False
if __name__ == '__main__':
from qtpy import QtCore, QtWidgets
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
# above two lines are needed to allow to undock the widget with
# additional viewers
view = napari.Viewer()
dock_widget = MultipleViewerWidget(view)
cross = CrossWidget(view)
view.window.add_dock_widget(dock_widget, name='Sample')
view.window.add_dock_widget(cross, name='Cross', area='left')
view.open_sample('napari', 'cells3d')
napari.run()