Skip to content

Commit

Permalink
Merge pull request #92 from pllim/annulus-of-green-gables
Browse files Browse the repository at this point in the history
FEAT: Support new CircularAnnulusROI
  • Loading branch information
astrofrog authored Jun 1, 2023
2 parents 9fa59a0 + 8ce9ecc commit 755874d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
63 changes: 51 additions & 12 deletions glue_astronomy/translators/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
__all__ = ["range_to_rect", "AstropyRegionsHandler"]

GLUE_LT_1_10 = Version(glue_version) < Version('1.10')
GLUE_LT_1_10_1 = Version(glue_version) < Version('1.10.1.dev') # remove .dev after it is released


def range_to_rect(data, ori, low, high):
Expand Down Expand Up @@ -54,16 +55,31 @@ def range_to_rect(data, ori, low, high):


def _is_annulus(subset_state):
# There is a new way to make annulus in newer glue.
if not GLUE_LT_1_10_1:
from glue.core.roi import CircularAnnulusROI
res1 = (isinstance(subset_state, RoiSubsetState) and
isinstance(subset_state.roi, CircularAnnulusROI))
else:
res1 = False

# subset_state.state1 = outer circle
# subset_state.state2 = inner circle
# subset_state.state2 is inverted, so we need its state1
return ((not isinstance(subset_state.state1, InvertState)) and
isinstance(subset_state.state1.roi, CircularROI) and
isinstance(subset_state.state2, InvertState) and
isinstance(subset_state.state2.state1.roi, CircularROI) and
(subset_state.state1.roi.xc == subset_state.state2.state1.roi.xc) and
(subset_state.state1.roi.yc == subset_state.state2.state1.roi.yc) and
(subset_state.state1.roi.radius > subset_state.state2.state1.roi.radius))
if not res1:
res2 = (hasattr(subset_state, 'state1') and
isinstance(subset_state.state1, RoiSubsetState) and
isinstance(subset_state.state1.roi, CircularROI) and
isinstance(subset_state.state2, InvertState) and
isinstance(subset_state.state2.state1, RoiSubsetState) and
isinstance(subset_state.state2.state1.roi, CircularROI) and
(subset_state.state1.roi.xc == subset_state.state2.state1.roi.xc) and
(subset_state.state1.roi.yc == subset_state.state2.state1.roi.yc) and
(subset_state.state1.roi.radius > subset_state.state2.state1.roi.radius))
else:
res2 = False

return res1 or res2


# Put this here because there is nowhere else to put it.
Expand All @@ -78,11 +94,22 @@ def _annulus_to_subset_state(reg, data):

xcen = reg.center.x
ycen = reg.center.y
state1 = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0],
CircularROI(xcen, ycen, reg.outer_radius))
state2 = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0],
CircularROI(xcen, ycen, reg.inner_radius))
return AndState(state1, ~state2)

# There is a new way to make annulus in newer glue.
if not GLUE_LT_1_10_1:
from glue.core.roi import CircularAnnulusROI
sbst = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0],
CircularAnnulusROI(xc=xcen, yc=ycen,
inner_radius=reg.inner_radius,
outer_radius=reg.outer_radius))
else:
state1 = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0],
CircularROI(xcen, ycen, reg.outer_radius))
state2 = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0],
CircularROI(xcen, ycen, reg.inner_radius))
sbst = AndState(state1, ~state2)

return sbst


@subset_state_translator('astropy-regions')
Expand Down Expand Up @@ -140,6 +167,18 @@ def to_object(self, subset):
raise NotImplementedError("ROIs of type {0} are not yet supported"
.format(roi.__class__.__name__))

# There is a new way to make annulus in newer glue.
elif not GLUE_LT_1_10_1:
from glue.core.roi import CircularAnnulusROI
if isinstance(roi, CircularAnnulusROI):
return CircleAnnulusPixelRegion(
center=PixCoord(x=roi.xc, y=roi.yc),
inner_radius=roi.inner_radius,
outer_radius=roi.outer_radius)
else:
raise NotImplementedError("ROIs of type {0} are not yet supported"
.format(roi.__class__.__name__))

else:
raise NotImplementedError("ROIs of type {0} are not yet supported"
.format(roi.__class__.__name__))
Expand Down
9 changes: 8 additions & 1 deletion glue_astronomy/translators/tests/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from glue.viewers.image.pixel_selection_subset_state import PixelSubsetState
from glue import __version__ as glue_version

from glue_astronomy.translators.regions import _annulus_to_subset_state
from glue_astronomy.translators.regions import _annulus_to_subset_state, GLUE_LT_1_10_1


class TestAstropyRegions:
Expand Down Expand Up @@ -311,6 +311,13 @@ def test_circular_annulus(self):
reg_orig = CircleAnnulusPixelRegion(
center=PixCoord(x=50, y=25), inner_radius=7, outer_radius=13)
subset_state = _annulus_to_subset_state(reg_orig, self.data)

# There is a new way to make annulus in newer glue.
if not GLUE_LT_1_10_1:
from glue.core.roi import CircularAnnulusROI
assert (isinstance(subset_state, RoiSubsetState) and
isinstance(subset_state.roi, CircularAnnulusROI))

self.dc.new_subset_group(subset_state=subset_state, label='annulus_1')
reg = self.data.get_selection_definition(subset_id='annulus_1', format='astropy-regions')

Expand Down

0 comments on commit 755874d

Please sign in to comment.