From 17c8bc502fc6e6e616626eff2750d0abf7fd3349 Mon Sep 17 00:00:00 2001 From: Derek Homeier Date: Tue, 15 Aug 2023 14:17:57 +0200 Subject: [PATCH 1/3] Prevent resizing of `CircularAnnulusROI` below `inner_radius` --- glue_jupyter/bqplot/common/tools.py | 3 ++- glue_jupyter/bqplot/image/viewer.py | 2 +- glue_jupyter/bqplot/tests/test_bqplot.py | 31 +++++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/glue_jupyter/bqplot/common/tools.py b/glue_jupyter/bqplot/common/tools.py index 6b20eb0c..97ad97e3 100644 --- a/glue_jupyter/bqplot/common/tools.py +++ b/glue_jupyter/bqplot/common/tools.py @@ -441,7 +441,8 @@ def update_selection(self, *args): if self._roi is None: inner_r = outer_r * 0.5 # Hardcoded for now, user can edit later. else: - inner_r = self._roi.inner_radius + # 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) diff --git a/glue_jupyter/bqplot/image/viewer.py b/glue_jupyter/bqplot/image/viewer.py index 4b79a7ef..07d13551 100644 --- a/glue_jupyter/bqplot/image/viewer.py +++ b/glue_jupyter/bqplot/image/viewer.py @@ -31,7 +31,7 @@ class BqplotImageView(BqplotBaseView): _options_cls = ImageViewerStateWidget tools = ['bqplot:home', 'bqplot:panzoom', 'bqplot:rectangle', 'bqplot:circle', 'bqplot:polygon', - 'bqplot:ellipse', 'bqplot:truecircle'] + 'bqplot:ellipse', 'bqplot:truecircle', 'bqplot:circannulus'] def __init__(self, session): diff --git a/glue_jupyter/bqplot/tests/test_bqplot.py b/glue_jupyter/bqplot/tests/test_bqplot.py index 008e6438..b12db277 100644 --- a/glue_jupyter/bqplot/tests/test_bqplot.py +++ b/glue_jupyter/bqplot/tests/test_bqplot.py @@ -4,7 +4,7 @@ from numpy.testing import assert_allclose from nbconvert.preprocessors import ExecutePreprocessor from glue.core import Data -from glue.core.roi import EllipticalROI +from glue.core.roi import CircularAnnulusROI, EllipticalROI from ..common.tools import TrueCircularROI DATA = os.path.join(os.path.dirname(__file__), 'data') @@ -305,6 +305,35 @@ def test_imshow_elliptical_brush(app, data_image): assert_allclose(roi.yc, 276.75) +def test_imshow_circular_annulus_brush(app, data_image): + + v = app.imshow(data=data_image) + v.state.aspect = 'auto' + + tool = v.toolbar.tools['bqplot:circannulus'] + tool.activate() + tool.interact.brushing = True + tool.interact.selected = [(1.5, 3.5), (300.5, 550)] + tool.interact.brushing = False + + roi = data_image.subsets[0].subset_state.roi + assert isinstance(roi, CircularAnnulusROI) + assert_allclose(roi.xc, 151.00) + assert_allclose(roi.yc, 276.75) + assert_allclose(roi.outer_radius, 211.375) + assert_allclose(roi.inner_radius, 105.6875) + + tool.interact.brushing = True + tool.interact.selected = [(150.0, 400.0), (150.0, 450.0)] + tool.interact.brushing = False + + # roi = data_image.subsets[0].subset_state.roi + assert_allclose(roi.xc, 151.00) + # assert_allclose(roi.yc, 326.75) # trying to use `move`, but this is not how it works... + assert_allclose(roi.outer_radius, 211.375) + assert_allclose(roi.inner_radius, 105.6875) + + def test_imshow_equal_aspect(app, data_image): data = Data(array=np.random.random((100, 5))) app.data_collection.append(data) From 2c2aa8677dc5584c5f7dc55e6b4a184b5bf3c1a9 Mon Sep 17 00:00:00 2001 From: Derek Homeier Date: Tue, 15 Aug 2023 16:53:30 +0200 Subject: [PATCH 2/3] TST: no move+resize tests for now --- glue_jupyter/bqplot/tests/test_bqplot.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/glue_jupyter/bqplot/tests/test_bqplot.py b/glue_jupyter/bqplot/tests/test_bqplot.py index b12db277..f8ec6c4d 100644 --- a/glue_jupyter/bqplot/tests/test_bqplot.py +++ b/glue_jupyter/bqplot/tests/test_bqplot.py @@ -323,15 +323,8 @@ def test_imshow_circular_annulus_brush(app, data_image): assert_allclose(roi.outer_radius, 211.375) assert_allclose(roi.inner_radius, 105.6875) - tool.interact.brushing = True - tool.interact.selected = [(150.0, 400.0), (150.0, 450.0)] - tool.interact.brushing = False - - # roi = data_image.subsets[0].subset_state.roi - assert_allclose(roi.xc, 151.00) - # assert_allclose(roi.yc, 326.75) # trying to use `move`, but this is not how it works... - assert_allclose(roi.outer_radius, 211.375) - assert_allclose(roi.inner_radius, 105.6875) + # should try to test `move` and `resize` as well, but this probably + # needs to go through `update_selection` directly def test_imshow_equal_aspect(app, data_image): From ca5099513baff2fc1d66e7f5422cc6fcd3a3eb4f Mon Sep 17 00:00:00 2001 From: Derek Homeier Date: Fri, 18 Aug 2023 13:41:39 +0200 Subject: [PATCH 3/3] Remove circannulus and truecircle from default tools --- glue_jupyter/bqplot/image/viewer.py | 4 ++-- glue_jupyter/bqplot/tests/test_bqplot.py | 28 ++++++++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/glue_jupyter/bqplot/image/viewer.py b/glue_jupyter/bqplot/image/viewer.py index 07d13551..ba627ed2 100644 --- a/glue_jupyter/bqplot/image/viewer.py +++ b/glue_jupyter/bqplot/image/viewer.py @@ -30,8 +30,8 @@ class BqplotImageView(BqplotBaseView): _state_cls = BqplotImageViewerState _options_cls = ImageViewerStateWidget - tools = ['bqplot:home', 'bqplot:panzoom', 'bqplot:rectangle', 'bqplot:circle', 'bqplot:polygon', - 'bqplot:ellipse', 'bqplot:truecircle', 'bqplot:circannulus'] + tools = ['bqplot:home', 'bqplot:panzoom', 'bqplot:rectangle', 'bqplot:circle', + 'bqplot:ellipse', 'bqplot:polygon'] def __init__(self, session): diff --git a/glue_jupyter/bqplot/tests/test_bqplot.py b/glue_jupyter/bqplot/tests/test_bqplot.py index f8ec6c4d..d282df53 100644 --- a/glue_jupyter/bqplot/tests/test_bqplot.py +++ b/glue_jupyter/bqplot/tests/test_bqplot.py @@ -3,6 +3,7 @@ import numpy as np from numpy.testing import assert_allclose from nbconvert.preprocessors import ExecutePreprocessor +from glue.config import viewer_tool from glue.core import Data from glue.core.roi import CircularAnnulusROI, EllipticalROI from ..common.tools import TrueCircularROI @@ -271,38 +272,43 @@ def test_imshow_circular_brush(app, data_image): assert_allclose(roi.radius_y, 273.25) -def test_imshow_true_circular_brush(app, data_image): - +def test_imshow_elliptical_brush(app, data_image): v = app.imshow(data=data_image) v.state.aspect = 'auto' - tool = v.toolbar.tools['bqplot:truecircle'] + tool = v.toolbar.tools['bqplot:ellipse'] tool.activate() tool.interact.brushing = True tool.interact.selected = [(1.5, 3.5), (300.5, 550)] tool.interact.brushing = False roi = data_image.subsets[0].subset_state.roi - assert isinstance(roi, TrueCircularROI) + assert isinstance(roi, EllipticalROI) assert_allclose(roi.xc, 151.00) assert_allclose(roi.yc, 276.75) - assert_allclose(roi.radius, 220.2451) -def test_imshow_elliptical_brush(app, data_image): +# Tools that are not part of the default set of BqplotImageView; manually added for testing +def test_imshow_true_circular_brush(app, data_image): + v = app.imshow(data=data_image) v.state.aspect = 'auto' - tool = v.toolbar.tools['bqplot:ellipse'] + tool_id = 'bqplot:truecircle' + mode_cls = viewer_tool.members[tool_id] + v.toolbar.add_tool(mode_cls(v)) + + tool = v.toolbar.tools[tool_id] tool.activate() tool.interact.brushing = True tool.interact.selected = [(1.5, 3.5), (300.5, 550)] tool.interact.brushing = False roi = data_image.subsets[0].subset_state.roi - assert isinstance(roi, EllipticalROI) + assert isinstance(roi, TrueCircularROI) assert_allclose(roi.xc, 151.00) assert_allclose(roi.yc, 276.75) + assert_allclose(roi.radius, 220.2451) def test_imshow_circular_annulus_brush(app, data_image): @@ -310,7 +316,11 @@ def test_imshow_circular_annulus_brush(app, data_image): v = app.imshow(data=data_image) v.state.aspect = 'auto' - tool = v.toolbar.tools['bqplot:circannulus'] + tool_id = 'bqplot:circannulus' + mode_cls = viewer_tool.members[tool_id] + v.toolbar.add_tool(mode_cls(v)) + + tool = v.toolbar.tools[tool_id] tool.activate() tool.interact.brushing = True tool.interact.selected = [(1.5, 3.5), (300.5, 550)]