Skip to content

Commit

Permalink
TST: Update test_as_mpl_selector to accept rotated regions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhomeier committed Feb 17, 2022
1 parent 2a0dc2b commit feb2be5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ New Features

- Added the DS9 'boxcircle' point symbol. [#387]

- Enable rotation of the ``as_mpl_selector`` widgets for rectangular
and ellipse regions with matplotlib versions supporting this. [#390]

- Added the ability to add and subtract ``PixCoord`` objects. [#396]

- Added an ``origin`` keyword to ``PolygonPixelRegion`` to allow
Expand Down
5 changes: 2 additions & 3 deletions regions/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def as_mpl_selector(self, ax, active=True, sync=True, callback=None,
if hasattr(self, '_mpl_selector'):
raise AttributeError('Cannot attach more than one selector to a region.')

if self.angle.value != 0 and not hasattr(EllipseSelector, '_rotation'):
if self.angle.value != 0 and not hasattr(EllipseSelector, 'rotation'):
raise NotImplementedError('Cannot create matplotlib selector for rotated ellipse.')

if sync:
Expand All @@ -306,8 +306,7 @@ def sync_callback(*args, **kwargs):
xy0[1], self.center.y + self.height / 2)

if self.angle.value != 0:
self._mpl_selector._set_corner_width_rotation(xy0, self.width, self.height,
self.angle.to_value('radian'))
self._mpl_selector.rotation = self.angle.to_value('radian')

self._mpl_selector.set_active(active)
self._mpl_selector_callback = callback
Expand Down
5 changes: 2 additions & 3 deletions regions/shapes/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def as_mpl_selector(self, ax, active=True, sync=True, callback=None,
if hasattr(self, '_mpl_selector'):
raise AttributeError('Cannot attach more than one selector to a region.')

if self.angle.value != 0 and not hasattr(RectangleSelector, '_rotation'):
if self.angle.value != 0 and not hasattr(RectangleSelector, 'rotation'):
raise NotImplementedError('Cannot create matplotlib selector for rotated rectangle.')

if sync:
Expand All @@ -299,8 +299,7 @@ def sync_callback(*args, **kwargs):
xy0[1], self.center.y + self.height / 2)

if self.angle.value != 0:
self._mpl_selector._set_corner_width_rotation(xy0, self.width, self.height,
self.angle.to_value('radian'))
self._mpl_selector.rotation = self.angle.to_value('radian')

self._mpl_selector.set_active(active)
self._mpl_selector_callback = callback
Expand Down
16 changes: 10 additions & 6 deletions regions/shapes/tests/test_ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ def update_mask(reg):
# For now this will only work with unrotated ellipses. Once this
# 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)
# copy() below - should (hopefully) be implemented with mpl 3.6.
if MPL_VERSION < 36:
with pytest.raises(NotImplementedError,
match=('Cannot create matplotlib selector for rotated ellipse.')):
self.reg.as_mpl_selector(ax)
angle = 0 * u.deg
else:
angle = self.reg.angle

region = self.reg.copy(angle=0 * u.deg)
region = self.reg.copy(angle=angle)

selector = region.as_mpl_selector(ax, callback=update_mask, sync=sync) # noqa

Expand All @@ -158,7 +162,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)

Expand Down
17 changes: 11 additions & 6 deletions regions/shapes/tests/test_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ def update_mask(reg):
# For now this will only work with unrotated rectangles. Once
# 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)
# copy() below - should (hopefully) be implemented with mpl 3.6.
if MPL_VERSION < 36:
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

Expand All @@ -162,7 +167,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)

Expand Down

0 comments on commit feb2be5

Please sign in to comment.