-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtools.py
728 lines (576 loc) · 27.5 KB
/
tools.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
import os
from contextlib import nullcontext
import numpy as np
from bqplot import PanZoom, Lines
from bqplot.interacts import BrushSelector, BrushIntervalSelector
from bqplot_image_gl.interacts import BrushEllipseSelector
from glue import __version__ as glue_version
from glue.core.roi import RectangularROI, RangeROI, CircularROI, EllipticalROI, PolygonalROI
from glue.core.subset import RoiSubsetState
from glue.config import viewer_tool
from glue.viewers.common.tool import Tool, CheckableTool
from packaging.version import Version
GLUE_LT_1_11 = Version(glue_version) < Version("1.11")
if not GLUE_LT_1_11:
from glue.core.roi import CircularAnnulusROI
else:
CircularAnnulusROI = None
__all__ = []
ICON_WIDTH = 20
INTERACT_COLOR = '#cbcbcb'
ICONS_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'icons')
class TrueCircularROI(CircularROI):
pass
class InteractCheckableTool(CheckableTool):
def __init__(self, viewer):
self.viewer = viewer
def activate(self):
# Disable any active tool in other viewers
if self.viewer.session.application.get_setting('single_global_active_tool'):
for viewer in self.viewer.session.application.viewers:
if viewer is not self.viewer:
viewer.toolbar.active_tool = None
self.viewer._mouse_interact.next = self.interact
def deactivate(self):
self.viewer._mouse_interact.next = None
class BqplotSelectionTool(InteractCheckableTool):
def activate(self):
# Jumps back to "create new" if that setting is active
if self.viewer.session.application.get_setting('new_subset_on_selection_tool_change'):
self.viewer.session.edit_subset_mode.edit_subset = None
super().activate()
@viewer_tool
class BqplotPanZoomMode(InteractCheckableTool):
icon = 'glue_move'
tool_id = 'bqplot:panzoom'
action_text = 'Pan and Zoom'
tool_tip = 'Interactively pan (click-drag) and zoom (scroll) around'
def __init__(self, viewer, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = PanZoom(scales={'x': [self.viewer.scale_x],
'y': [self.viewer.scale_y]})
def activate(self):
if hasattr(self.viewer, '_composite_image'):
self.viewer.state.add_callback('image_external_padding', self._sync_padding)
self._sync_padding()
super().activate()
def _sync_padding(self, *args):
self.viewer._composite_image.external_padding = self.viewer.state.image_external_padding
def deactivate(self):
if hasattr(self.viewer, '_composite_image'):
self.viewer.state.remove_callback('image_external_padding', self._sync_padding)
self.viewer._composite_image.external_padding = 0
super().deactivate()
@viewer_tool
class BqplotPanZoomXMode(InteractCheckableTool):
icon = 'glue_move_x'
tool_id = 'bqplot:panzoom_x'
action_text = 'Pan and Zoom X Axis'
tool_tip = 'Interactively pan and zoom x axis only'
def __init__(self, viewer, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = PanZoom(scales={'x': [self.viewer.scale_x]})
@viewer_tool
class BqplotPanZoomYMode(InteractCheckableTool):
icon = 'glue_move_y'
tool_id = 'bqplot:panzoom_y'
action_text = 'Pan and Zoom Y Axis'
tool_tip = 'Interactively pan and zoom y axis only'
def __init__(self, viewer, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = PanZoom(scales={'y': [self.viewer.scale_y]})
@viewer_tool
class BqplotRectangleMode(BqplotSelectionTool):
icon = 'glue_square'
tool_id = 'bqplot:rectangle'
action_text = 'Rectangular ROI'
tool_tip = 'Define a rectangular region of interest'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = BrushSelector(x_scale=self.viewer.scale_x,
y_scale=self.viewer.scale_y,
color=INTERACT_COLOR)
self._roi = kwargs.get("roi", None)
if roi is not None:
self.update_from_roi(roi)
self.interact.observe(self.update_selection, "brushing")
self.interact.observe(self.on_selection_change, "selected_x")
self.interact.observe(self.on_selection_change, "selected_y")
self.finalize_callback = finalize_callback
def update_selection(self, *args):
if self.interact.brushing:
return
with self.viewer._output_widget or nullcontext():
if self.interact.selected_x is not None and self.interact.selected_y is not None:
x = self.interact.selected_x
y = self.interact.selected_y
theta = None if self._roi is None else self._roi.theta
roi = RectangularROI(xmin=min(x), xmax=max(x), ymin=min(y), ymax=max(y), theta=theta) # noqa: E501
self._roi = roi
self.viewer.apply_roi(roi)
if self.finalize_callback is not None:
self.finalize_callback()
def update_from_roi(self, roi):
self._roi = roi
with self.viewer._output_widget or nullcontext():
if isinstance(roi, RectangularROI):
self.interact.selected_x = [roi.xmin, roi.xmax]
self.interact.selected_y = [roi.ymin, roi.ymax]
elif isinstance(roi, PolygonalROI):
self.interact.selected_x = [np.min(roi.vx), np.max(roi.vx)]
self.interact.selected_y = [np.min(roi.vy), np.max(roi.vy)]
else:
raise TypeError(f'Cannot initialize a BqplotRectangleMode from a {type(roi)}')
# FIXME: the brush selector does not actually update unless the
# widget is resized/refreshed, see
# https://github.com/bloomberg/bqplot/issues/1067
def on_selection_change(self, *args):
if self.interact.selected_x is None or self.interact.selected_y is None:
if self.finalize_callback is not None:
self.finalize_callback()
def activate(self):
with self.viewer._output_widget or nullcontext():
self.interact.selected_x = None
self.interact.selected_y = None
super().activate()
@viewer_tool
class BqplotPolygonMode(BqplotSelectionTool):
"""
Since Bqplot LassoSelector does not allow us to get the coordinates of the
selection (see https://github.com/bqplot/bqplot/pull/674), we simply use
a callback on the default viewer MouseInteraction and a patch to display
the selection. The parent class defaults to setting polygon vertices by
clicking in the selector.
The polygon is closed by clicking within 2 % distance (of maximum extent)
of the initial vertex, or on deactivating the tool.
"""
icon = os.path.join(ICONS_DIR, 'glue_polygon')
tool_id = 'bqplot:polygon'
action_text = 'Polygonal ROI'
tool_tip = 'Define a polygonal region of interest'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self.patch = Lines(x=[[]], y=[[]], fill_colors=[INTERACT_COLOR], colors=[INTERACT_COLOR],
opacities=[0.6], fill='inside', close_path=True,
scales={'x': self.viewer.scale_x, 'y': self.viewer.scale_y})
self.interact = BrushSelector(x_scale=self.viewer.scale_x,
y_scale=self.viewer.scale_y,
color=INTERACT_COLOR)
if roi is not None:
self.update_from_roi(roi)
self._lasso = False
self.interact.observe(self.update_selection, "brushing")
self.interact.observe(self.on_selection_change, "selected_x")
self.interact.observe(self.on_selection_change, "selected_y")
self.finalize_callback = finalize_callback
def update_selection(self, *args):
if self.interact.brushing:
return
with self.viewer._output_widget or nullcontext():
if self.interact.selected_x is not None and self.interact.selected_y is not None:
x = self.interact.selected_x
y = self.interact.selected_y
dx = x.mean() - self._roi.centroid()[0]
dy = y.mean() - self._roi.centroid()[1]
roi = PolygonalROI(vx=np.array(self._roi.vx) + dx, vy=np.array(self._roi.vy) + dy)
self._roi = roi
self.viewer.apply_roi(roi)
if self.finalize_callback is not None:
self.finalize_callback()
def update_from_roi(self, roi):
"""
Dragging with the `BrushSelector` using this method is still somewhat
unintuitive, as the ROI position is not updated live; rather the target
(centroid) position has to be set using the bounding bax shape.
The method could potentially be extended to Circular and Rectangular
ROIs using their `to_polygon` methods.
"""
with self.viewer._output_widget or nullcontext():
if isinstance(roi, PolygonalROI):
self.patch.x = roi.vx
self.patch.y = roi.vy
self._roi = roi
else:
raise TypeError(f'Cannot initialize a BqplotPolygonMode from a {type(roi)}')
def activate(self):
"""
We do not call super().activate() because we don't have a separate interact,
instead we just add a callback to the default viewer MouseInteraction.
"""
# We need to make sure any existing callbacks associated with this
# viewer are cleared. This can happen if the user switches between
# different viewers without deactivating the tool.
try:
self.viewer.remove_event_callback(self.on_msg)
except KeyError:
pass
# Disable any active tool in other viewers
if self.viewer.session.application.get_setting('single_global_active_tool'):
for viewer in self.viewer.session.application.viewers:
if viewer is not self.viewer:
viewer.toolbar.active_tool = None
self.viewer.add_event_callback(self.on_msg, events=['dragstart', 'dragmove', 'dragend'])
def deactivate(self):
if len(self.patch.x) > 1:
self.close_vertices()
try:
self.viewer.remove_event_callback(self.on_msg)
except KeyError:
pass
super().deactivate()
def on_selection_change(self, *args):
if self.interact.selected_x is None or self.interact.selected_y is None:
if self.finalize_callback is not None:
self.finalize_callback()
def on_msg(self, event):
name = event['event']
domain = event['domain']
x, y = domain['x'], domain['y']
if name == 'dragstart' and len(self.patch.x) < 1:
self.original_marks = list(self.viewer.figure.marks)
self.viewer.figure.marks = self.original_marks + [self.patch]
self.patch.x = [x]
self.patch.y = [y]
elif name == 'dragmove' and self._lasso:
self.patch.x = np.append(self.patch.x, x)
self.patch.y = np.append(self.patch.y, y)
elif name == 'dragend':
# If new point is within 2% of maximum extent of the origin, finalise the polygon.
sz = max(self.patch.x) - min(self.patch.x), max(self.patch.y) - min(self.patch.y)
if self._lasso or (abs(x - self.patch.x[0]) < 0.02 * sz[0] and
abs(y - self.patch.y[0]) < 0.02 * sz[1]):
self.close_vertices()
else:
self.patch.x = np.append(self.patch.x, x)
self.patch.y = np.append(self.patch.y, y)
def close_vertices(self):
roi = PolygonalROI(vx=self.patch.x, vy=self.patch.y)
self.viewer.apply_roi(roi)
new_marks = []
for mark in self.viewer.figure.marks:
if mark == self.patch:
pass
else:
new_marks.append(mark)
self.viewer.figure.marks = new_marks
self.patch.x = [[]]
self.patch.y = [[]]
if self.finalize_callback is not None:
self.finalize_callback()
@viewer_tool
class BqplotLassoMode(BqplotPolygonMode):
"""
Subclass that defaults to creating a continous (lasso) polygonal selection
by dragging the pointer along the outline.
"""
icon = 'glue_lasso'
tool_id = 'bqplot:lasso'
tool_tip = 'Lasso a region of interest'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self._lasso = True
@viewer_tool
class BqplotCircleMode(BqplotSelectionTool):
icon = 'glue_circle'
tool_id = 'bqplot:circle'
action_text = 'Circular ROI'
tool_tip = 'Define a circular region of interest'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = BrushEllipseSelector(x_scale=self.viewer.scale_x,
y_scale=self.viewer.scale_y,
pixel_aspect=1)
# Workaround for bug that causes the `color` trait to not be recognized
style = self.interact.style.copy()
style['fill'] = INTERACT_COLOR
border_style = self.interact.border_style.copy()
border_style['fill'] = INTERACT_COLOR
border_style['stroke'] = INTERACT_COLOR
self.interact.style = style
self.interact.border_style = border_style
self._strict_circle = False
self._roi = kwargs.get("roi", None)
if roi is not None:
self.update_from_roi(roi)
self.interact.observe(self.update_selection, "brushing")
self.interact.observe(self.on_selection_change, "selected_x")
self.interact.observe(self.on_selection_change, "selected_y")
self.finalize_callback = finalize_callback
def update_selection(self, *args):
if self.interact.brushing:
return
with self.viewer._output_widget or nullcontext():
if self.interact.selected_x is not None and self.interact.selected_y is not None:
x = self.interact.selected_x
y = self.interact.selected_y
# similar to https://github.com/glue-viz/glue/blob/b14ccffac6a5
# 271c2869ead9a562a2e66232e397/glue/core/roi.py#L1275-L1297
# If _strict_circle set, enforce returning a circle; otherwise check
# if the radius in data coordinates is (nearly) the same along x and y,
# to return a circle as well, else we should return an ellipse.
xc = x.mean()
yc = y.mean()
rx = abs(x[1] - x[0])/2
ry = abs(y[1] - y[0])/2
# We use a tolerance of 1e-2 below to match the tolerance set in glue-core
# https://github.com/glue-viz/glue/blob/6b968b352bc5ad68b95ad5e3bb25550782a69ee8/glue/viewers/matplotlib/state.py#L198
if self._strict_circle:
roi = TrueCircularROI(xc=xc, yc=yc, radius=np.sqrt((rx**2 + ry**2) * 0.5))
elif np.allclose(rx, ry, rtol=1e-2):
roi = CircularROI(xc=xc, yc=yc, radius=(rx + ry) * 0.5)
else:
theta = 0 if (self._roi is None or not hasattr(self._roi, "theta")) else self._roi.theta # noqa: E501
roi = EllipticalROI(xc=xc, yc=yc, radius_x=rx, radius_y=ry, theta=theta)
self._roi = roi
self.viewer.apply_roi(roi)
if self.finalize_callback is not None:
self.finalize_callback()
def update_from_roi(self, roi):
self._roi = roi
if isinstance(roi, CircularROI):
rx = ry = roi.radius
if isinstance(roi, TrueCircularROI):
self._strict_circle = True
elif isinstance(roi, EllipticalROI):
if self._strict_circle:
rx, ry = np.sqrt((roi.radius_x ** 2 + roi.radius_y ** 2) * 0.5)
else:
rx, ry = roi.radius_x, roi.radius_y
else:
raise TypeError(f'Cannot initialize a BqplotCircleMode from a {type(roi)}')
self.interact.selected_x = [roi.xc - rx, roi.xc + rx]
self.interact.selected_y = [roi.yc - ry, roi.yc + ry]
def on_selection_change(self, *args):
if self.interact.selected_x is None or self.interact.selected_y is None:
if self.finalize_callback is not None:
self.finalize_callback()
def activate(self):
with self.viewer._output_widget or nullcontext():
self.interact.selected_x = None
self.interact.selected_y = None
super().activate()
@viewer_tool
class BqplotTrueCircleMode(BqplotCircleMode):
tool_id = 'bqplot:truecircle'
tool_tip = 'Define a strictly circular region of interest'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self._strict_circle = True
@viewer_tool
class BqplotEllipseMode(BqplotCircleMode):
icon = os.path.join(ICONS_DIR, 'glue_ellipse.svg')
tool_id = 'bqplot:ellipse'
action_text = 'Elliptical ROI'
tool_tip = 'Define an elliptical region of interest'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = BrushEllipseSelector(x_scale=self.viewer.scale_x,
y_scale=self.viewer.scale_y)
# Workaround for bug that causes the `color` trait to not be recognized
style = self.interact.style.copy()
style['fill'] = INTERACT_COLOR
border_style = self.interact.border_style.copy()
border_style['fill'] = INTERACT_COLOR
border_style['stroke'] = INTERACT_COLOR
self.interact.style = style
self.interact.border_style = border_style
self._roi = kwargs.get("roi", None)
if roi is not None:
self.update_from_roi(roi)
self.interact.observe(self.update_selection, "brushing")
self.interact.observe(self.on_selection_change, "selected_x")
self.interact.observe(self.on_selection_change, "selected_y")
self.finalize_callback = finalize_callback
@viewer_tool
class BqplotCircularAnnulusMode(BqplotCircleMode):
icon = os.path.join(ICONS_DIR, 'glue_annulus.svg')
tool_id = 'bqplot:circannulus'
action_text = 'Circular Annulus ROI'
tool_tip = 'Define a circular annulus region of interest'
def __init__(self, *args, **kwargs):
if GLUE_LT_1_11:
raise NotImplementedError("This tool requires glue-core>=1.11")
super().__init__(*args, **kwargs)
self._roi = kwargs.get("roi", None)
def update_selection(self, *args):
if self.interact.brushing:
return
with self.viewer._output_widget or nullcontext():
if self.interact.selected_x is not None and self.interact.selected_y is not None:
x = self.interact.selected_x
y = self.interact.selected_y
# similar to https://github.com/glue-viz/glue/blob/b14ccffac6a5
# 271c2869ead9a562a2e66232e397/glue/core/roi.py#L1275-L1297
# We should now check if the radius in data coordinates is the same
# along x and y, as if so then we can return a circle, otherwise we
# make assumption to keep it circular.
# Need extra float casting because of strict type check in CircularAnnulusROI.
xc = float(x.mean())
yc = float(y.mean())
rx = abs(x[1] - x[0]) * 0.5
ry = abs(y[1] - y[0]) * 0.5
outer_r = float(rx + ry) * 0.5
if self._roi is None:
inner_r = outer_r * 0.5 # Hardcoded for now, user can edit later.
else:
# Resizing only changes outer r, avoid having inner_r >= outer_r afterwards.
inner_r = min(self._roi.inner_radius, outer_r * 0.999999)
roi = CircularAnnulusROI(xc=xc, yc=yc, inner_radius=inner_r, outer_radius=outer_r)
self._roi = roi
self.viewer.apply_roi(roi)
if self.finalize_callback is not None:
self.finalize_callback()
def update_from_roi(self, roi):
self._roi = roi
if isinstance(roi, CircularAnnulusROI):
rx = ry = roi.outer_radius
else:
raise TypeError(f'Cannot initialize a BqplotCircularAnnulusMode from a {type(roi)}')
self.interact.selected_x = [roi.xc - rx, roi.xc + rx]
self.interact.selected_y = [roi.yc - ry, roi.yc + ry]
@viewer_tool
class BqplotXRangeMode(BqplotSelectionTool):
icon = 'glue_xrange_select'
tool_id = 'bqplot:xrange'
action_text = 'X range ROI'
tool_tip = 'Select a range of x values'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = BrushIntervalSelector(scale=self.viewer.scale_x,
color=INTERACT_COLOR)
if roi is not None:
self.update_from_roi(roi)
self.interact.observe(self.update_selection, "brushing")
self.interact.observe(self.on_selection_change, "selected")
self.finalize_callback = finalize_callback
def update_selection(self, *args):
if self.interact.brushing:
return
with self.viewer._output_widget or nullcontext():
if self.interact.selected is not None:
x = self.interact.selected
if x is not None and len(x):
roi = RangeROI(min=min(x), max=max(x), orientation='x')
self.viewer.apply_roi(roi)
if self.finalize_callback is not None:
self.finalize_callback()
def update_from_roi(self, roi):
with self.viewer._output_widget or nullcontext():
if isinstance(roi, RangeROI):
self.interact.selected = [roi.min, roi.max]
else:
raise TypeError(f'Cannot initialize a BqplotXRangeMode from a {type(roi)}')
def on_selection_change(self, *args):
if self.interact.selected is None:
if self.finalize_callback is not None:
self.finalize_callback()
def activate(self):
with self.viewer._output_widget or nullcontext():
self.interact.selected = None
super().activate()
@viewer_tool
class BqplotYRangeMode(BqplotSelectionTool):
icon = 'glue_yrange_select'
tool_id = 'bqplot:yrange'
action_text = 'Y range ROI'
tool_tip = 'Select a range of y values'
def __init__(self, viewer, roi=None, finalize_callback=None, **kwargs):
super().__init__(viewer, **kwargs)
self.interact = BrushIntervalSelector(scale=self.viewer.scale_y,
orientation='vertical',
color=INTERACT_COLOR)
if roi is not None:
self.update_from_roi(roi)
self.interact.observe(self.update_selection, "brushing")
self.interact.observe(self.on_selection_change, "selected")
self.finalize_callback = finalize_callback
def update_selection(self, *args):
if self.interact.brushing:
return
with self.viewer._output_widget or nullcontext():
if self.interact.selected is not None:
y = self.interact.selected
if y is not None and len(y):
roi = RangeROI(min=min(y), max=max(y), orientation='y')
self.viewer.apply_roi(roi)
if self.finalize_callback is not None:
self.finalize_callback()
def update_from_roi(self, roi):
with self.viewer._output_widget or nullcontext():
if isinstance(roi, RangeROI):
self.interact.selected = [roi.min, roi.max]
else:
raise TypeError(f'Cannot initialize a BqplotYRangeMode from a {type(roi)}')
def on_selection_change(self, *args):
if self.interact.selected is None:
if self.finalize_callback is not None:
self.finalize_callback()
def activate(self):
with self.viewer._output_widget or nullcontext():
self.interact.selected = None
super().activate()
# The following is deliberately not a viewer_tool, it is an 'invisible' mode
# that can be activated when other tools are inactive.
class ROIClickAndDrag(InteractCheckableTool):
"""
A tool that enables clicking and dragging of existing ROIs.
"""
def __init__(self, viewer, **kwargs):
super().__init__(viewer, **kwargs)
self.viewer = viewer
self._edit_subset_mode = viewer.session.edit_subset_mode
self.interact = None
self._active_tool = None
def activate(self):
super().activate()
self.viewer.add_event_callback(self.on_msg, events=['dragstart'])
def deactivate(self):
self.viewer.remove_event_callback(self.on_msg)
super().deactivate()
def on_msg(self, event):
name = event['event']
domain = event['domain']
x, y = domain['x'], domain['y']
if name == 'dragstart':
self.press(x, y)
def press(self, x, y):
from glue_jupyter.bqplot.image.layer_artist import BqplotImageSubsetLayerArtist
for layer in self.viewer.layers:
if not isinstance(layer, BqplotImageSubsetLayerArtist):
continue
subset_state = layer.state.layer.subset_state
if layer.visible and isinstance(subset_state, RoiSubsetState):
roi = subset_state.roi
if roi.defined() and roi.contains(x, y):
if isinstance(roi, EllipticalROI):
self._active_tool = BqplotEllipseMode(
self.viewer, roi=roi, finalize_callback=self.release)
elif isinstance(roi, CircularROI):
self._active_tool = BqplotCircleMode(
self.viewer, roi=roi, finalize_callback=self.release)
elif isinstance(roi, RectangularROI):
self._active_tool = BqplotRectangleMode(
self.viewer, roi=roi, finalize_callback=self.release)
elif isinstance(roi, PolygonalROI):
self._active_tool = BqplotPolygonMode(
self.viewer, roi=roi, finalize_callback=self.release)
elif not GLUE_LT_1_11 and isinstance(roi, CircularAnnulusROI):
self._active_tool = BqplotCircularAnnulusMode(
self.viewer, roi=roi, finalize_callback=self.release)
else:
raise TypeError(f"Unexpected ROI type: {type(roi)}")
self.viewer._mouse_interact.next = self._active_tool.interact
self._edit_subset_mode.edit_subset = [layer.state.layer.group]
break
else:
self._selected = False
def release(self):
self.viewer._mouse_interact.next = None
@viewer_tool
class HomeTool(Tool):
tool_id = 'bqplot:home'
icon = 'glue_home'
action_text = 'Home'
tool_tip = 'Reset original zoom'
def activate(self):
self.viewer.state.reset_limits()