Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Support sky regions and allow direct call to translator function #93

Merged
merged 4 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions glue_astronomy/translators/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,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
GLUE_LT_1_11 = Version(glue_version) < Version('1.11')
Copy link
Contributor Author

@pllim pllim Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhomeier , I am also cleaning up the version check here, as discussed in #92 (comment) . True annulus ROI support wasn't added till glue-core 1.11.



def range_to_rect(data, ori, low, high):
Expand Down Expand Up @@ -56,7 +55,7 @@ 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:
if not GLUE_LT_1_11:
from glue.core.roi import CircularAnnulusROI
res1 = (isinstance(subset_state, RoiSubsetState) and
isinstance(subset_state.roi, CircularAnnulusROI))
Expand Down Expand Up @@ -96,7 +95,7 @@ def _annulus_to_subset_state(reg, data):
ycen = reg.center.y

# There is a new way to make annulus in newer glue.
if not GLUE_LT_1_10_1:
if not GLUE_LT_1_11:
from glue.core.roi import CircularAnnulusROI
sbst = RoiSubsetState(data.pixel_component_ids[1], data.pixel_component_ids[0],
CircularAnnulusROI(xc=xcen, yc=ycen,
Expand Down Expand Up @@ -124,16 +123,20 @@ def to_object(self, subset):
subset : `glue.core.subset.Subset`
The subset to convert to a Region object
"""
data = subset.data

if data.pixel_component_ids[0].axis == 0:
x_pix_att = data.pixel_component_ids[1]
y_pix_att = data.pixel_component_ids[0]
else:
x_pix_att = data.pixel_component_ids[0]
y_pix_att = data.pixel_component_ids[1]

subset_state = subset.subset_state
if not isinstance(subset, RoiSubsetState):
subset_state = subset.subset_state
data = subset.data
if data.pixel_component_ids[0].axis == 0:
x_pix_att = data.pixel_component_ids[1]
y_pix_att = data.pixel_component_ids[0]
else:
x_pix_att = data.pixel_component_ids[0]
y_pix_att = data.pixel_component_ids[1]
else: # Special handling if RoiSubsetState is passed in directly
subset_state = subset
data = None
x_pix_att = None
y_pix_att = None

if isinstance(subset_state, RoiSubsetState):

Expand All @@ -148,7 +151,7 @@ def to_object(self, subset):
elif isinstance(roi, PolygonalROI):
return PolygonPixelRegion(PixCoord(roi.vx, roi.vy))
elif isinstance(roi, CircularROI):
xcen, ycen = roi.get_center() if GLUE_LT_1_10 else roi.center()
xcen, ycen = roi.get_center() if GLUE_LT_1_11 else roi.center()
return CirclePixelRegion(PixCoord(xcen, ycen), roi.get_radius())
elif isinstance(roi, EllipticalROI):
return EllipsePixelRegion(
Expand All @@ -168,7 +171,7 @@ def to_object(self, subset):
.format(roi.__class__.__name__))

# There is a new way to make annulus in newer glue.
elif not GLUE_LT_1_10_1:
elif not GLUE_LT_1_11:
from glue.core.roi import CircularAnnulusROI
if isinstance(roi, CircularAnnulusROI):
return CircleAnnulusPixelRegion(
Expand Down
14 changes: 12 additions & 2 deletions glue_astronomy/translators/tests/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
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, GLUE_LT_1_10_1
from glue_astronomy.translators.regions import (_annulus_to_subset_state, GLUE_LT_1_11,
AstropyRegionsHandler)


class TestAstropyRegions:
Expand All @@ -45,6 +46,15 @@ def test_rectangular_roi(self):
assert_allclose(reg.width, 2.5)
assert_allclose(reg.height, 3.5)

# Test direct call to handler
handler = AstropyRegionsHandler()
reg_2 = handler.to_object(subset_state)
assert isinstance(reg_2, RectanglePixelRegion)
assert_allclose(reg_2.center.x, 2.25)
assert_allclose(reg_2.center.y, 1.55)
assert_allclose(reg_2.width, 2.5)
assert_allclose(reg_2.height, 3.5)

def test_polygonal_roi(self):

xv = [1.3, 2, 3, 1.5, 0.5]
Expand Down Expand Up @@ -313,7 +323,7 @@ def test_circular_annulus(self):
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:
if not GLUE_LT_1_11:
from glue.core.roi import CircularAnnulusROI
assert (isinstance(subset_state, RoiSubsetState) and
isinstance(subset_state.roi, CircularAnnulusROI))
Expand Down