diff --git a/CHANGES.rst b/CHANGES.rst index 77469eaa..e55807b3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,9 @@ New Features - Added the DS9 'boxcircle' point symbol. [#387] +- Support rotation of the ``as_mpl_selector`` widgets for rectangular + and ellipse regions. [#390] + Bug Fixes --------- diff --git a/regions/shapes/tests/test_ellipse.py b/regions/shapes/tests/test_ellipse.py index 26affb1d..c927d1e7 100644 --- a/regions/shapes/tests/test_ellipse.py +++ b/regions/shapes/tests/test_ellipse.py @@ -15,7 +15,7 @@ from ...tests.helpers import make_simple_wcs from ..ellipse import EllipsePixelRegion, EllipseSkyRegion from .test_common import BaseTestPixelRegion, BaseTestSkyRegion -from .utils import HAS_MATPLOTLIB # noqa +from .utils import HAS_MATPLOTLIB, MATPLOTLIB_HAS_ROTATING_SELECTORS # noqa @pytest.fixture(scope='session', name='wcs') @@ -115,12 +115,16 @@ def update_mask(reg): # works with rotated ellipses, the following exception check can # be removed as well as the ``angle=0 * u.deg`` in the call to # copy() below. - with pytest.raises(NotImplementedError, - match=('Cannot create matplotlib selector for ' - 'rotated ellipse.')): - self.reg.as_mpl_selector(ax) + if not MATPLOTLIB_HAS_ROTATING_SELECTORS: + with pytest.raises(NotImplementedError, + match=('Cannot create matplotlib selector for rotated ellipse.')): + self.reg.as_mpl_selector(ax) - region = self.reg.copy(angle=0 * u.deg) + angle = 0 * u.deg + else: + angle = self.reg.angle + + region = self.reg.copy(angle=angle) selector = region.as_mpl_selector(ax, callback=update_mask, sync=sync) # noqa @@ -162,7 +166,7 @@ def update_mask(reg): assert_allclose(region.center.y, 4) assert_allclose(region.width, 4) assert_allclose(region.height, 3) - assert_quantity_allclose(region.angle, 0 * u.deg) + assert_quantity_allclose(region.angle, angle) assert_equal(mask, 0) diff --git a/regions/shapes/tests/test_rectangle.py b/regions/shapes/tests/test_rectangle.py index b66b35f4..4fb8c63d 100644 --- a/regions/shapes/tests/test_rectangle.py +++ b/regions/shapes/tests/test_rectangle.py @@ -15,7 +15,7 @@ from ...tests.helpers import make_simple_wcs from ..rectangle import RectanglePixelRegion, RectangleSkyRegion from .test_common import BaseTestPixelRegion, BaseTestSkyRegion -from .utils import HAS_MATPLOTLIB # noqa +from .utils import HAS_MATPLOTLIB, MATPLOTLIB_HAS_ROTATING_SELECTORS # noqa @pytest.fixture(scope='session', name='wcs') @@ -118,12 +118,16 @@ def update_mask(reg): # this works with rotated rectangles, the following exception # check can be removed as well as the ``angle=0 * u.deg`` in the # call to copy() below. - with pytest.raises(NotImplementedError, - match=('Cannot create matplotlib selector for ' - 'rotated rectangle.')): - self.reg.as_mpl_selector(ax) + if not MATPLOTLIB_HAS_ROTATING_SELECTORS: + with pytest.raises(NotImplementedError, + match=('Cannot create matplotlib selector for rotated rectangle.')): + self.reg.as_mpl_selector(ax) - region = self.reg.copy(angle=0 * u.deg) + angle = 0 * u.deg + else: + angle = self.reg.angle + + region = self.reg.copy(angle=angle) selector = region.as_mpl_selector(ax, callback=update_mask, sync=sync) # noqa @@ -162,7 +166,7 @@ def update_mask(reg): assert_allclose(region.center.y, 4) assert_allclose(region.width, 4) assert_allclose(region.height, 3) - assert_quantity_allclose(region.angle, 0 * u.deg) + assert_quantity_allclose(region.angle, angle) assert_equal(mask, 0) diff --git a/regions/shapes/tests/utils.py b/regions/shapes/tests/utils.py index f879f625..646f3715 100644 --- a/regions/shapes/tests/utils.py +++ b/regions/shapes/tests/utils.py @@ -1,7 +1,11 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst +MATPLOTLIB_HAS_ROTATING_SELECTORS = False try: import matplotlib # noqa HAS_MATPLOTLIB = True + import matplotlib.widgets + if hasattr(matplotlib.widgets.EllipseSelector, '_rotation'): + MATPLOTLIB_HAS_ROTATING_SELECTORS = True except ImportError: HAS_MATPLOTLIB = False