Skip to content

Commit

Permalink
rename to get_regions
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Dec 11, 2024
1 parent 703b6ab commit 84b30ad
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ API Changes
``metadata_plugin.meta`` instead, which will return a Python dictionary instead of
list of tuples. [#3292]

- Add ``get_subsets_as_regions`` method to subset plugin to retrieve spatial/spectral subsets as
- Add ``get_regions`` method to subset plugin to retrieve spatial/spectral subsets as
``regions`` or ``SpectralRegions``, deprecate ``get_interactive_regions`` and ``get_spectral_regions``. [#3340]

Cubeviz
Expand Down
6 changes: 3 additions & 3 deletions jdaviz/configs/cubeviz/plugins/tests/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_regions_pixel(self):
[my_reg], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('Subset 1', count=1)
assert len(self.cubeviz.plugins['Subset Tools']._obj.get_subsets_as_regions()) == 1
assert len(self.cubeviz.plugins['Subset Tools']._obj.get_regions()) == 1

def test_regions_sky_has_wcs(self):
sky = SkyCoord(205.4397, 27.0035, unit='deg')
Expand All @@ -65,10 +65,10 @@ def test_spatial_spectral_mix(self):

# Get spatial regions only.
st = self.cubeviz.plugins['Subset Tools']._obj
spatial_subsets_as_regions = st.get_subsets_as_regions(region_type='spatial')
spatial_subsets_as_regions = st.get_regions(region_type='spatial')
assert list(spatial_subsets_as_regions.keys()) == ['Subset 1'], spatial_subsets_as_regions
assert isinstance(spatial_subsets_as_regions['Subset 1'], EllipsePixelRegion)
# ensure agreement between app.get_subsets and subset_tools.get_subsets_as_regions
# ensure agreement between app.get_subsets and subset_tools.get_regions
ss = self.cubeviz.app.get_subsets()
assert ss['Subset 1'][0]['region'] == spatial_subsets_as_regions['Subset 1']

Expand Down
8 changes: 4 additions & 4 deletions jdaviz/configs/default/plugins/subset_tools/subset_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ def __init__(self, *args, **kwargs):

@property
def user_api(self):
expose = ['subset', 'combination_mode', 'recenter_dataset', 'recente',
'get_center', 'set_center', 'import_region', 'get_subsets_as_regions']
expose = ['subset', 'combination_mode', 'recenter_dataset', 'recenter',
'get_center', 'set_center', 'import_region', 'get_regions']
return PluginUserApi(self, expose)

def get_subsets_as_regions(self, region_type=None, list_of_subset_labels=None,
use_display_units=False):
def get_regions(self, region_type=None, list_of_subset_labels=None,
use_display_units=False):
"""
Return spatial and/or spectral subsets of ``region_type`` (spatial or
spectral, default both) as ``regions`` or ``SpectralRegions`` objects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ def test_import_spectral_regions_file(cubeviz_helper, spectrum1d_cube, tmp_path)
plg.combination_mode = 'test'


def test_get_subsets_as_regions(cubeviz_helper, spectrum1d_cube, imviz_helper):
def test_get_regions(cubeviz_helper, spectrum1d_cube, imviz_helper):

"""Test Subset Tools.get regions."""

cubeviz_helper.load_data(spectrum1d_cube)
plg = cubeviz_helper.plugins['Subset Tools']
Expand All @@ -241,21 +243,21 @@ def test_get_subsets_as_regions(cubeviz_helper, spectrum1d_cube, imviz_helper):
spatial_reg = CirclePixelRegion(center=PixCoord(x=2, y=2), radius=2)
plg.import_region(spatial_reg, combination_mode='new')

# call get_subsets_as_regions, which by default for cubeviz will return both
# spatial and spectral regoins
all_regions = plg.get_subsets_as_regions()
# call get_regions, which by default for cubeviz will return both
# spatial and spectral regions
all_regions = plg.get_regions()
assert len(all_regions) == 2

# make sure filtering by subset label works
only_s1 = plg.get_subsets_as_regions(list_of_subset_labels=['Subset 1'])
only_s1 = plg.get_regions(list_of_subset_labels=['Subset 1'])
assert len(only_s1) == 1
assert only_s1['Subset 1']

# now specify region type and check output
spatial_regions = plg.get_subsets_as_regions(region_type='spatial')
spatial_regions = plg.get_regions(region_type='spatial')
assert len(spatial_regions) == 1
assert spatial_regions['Subset 2']
spectral_regions = plg.get_subsets_as_regions(region_type='spectral')
spectral_regions = plg.get_regions(region_type='spectral')
assert len(spectral_regions) == 1
assert spectral_regions['Subset 1']

Expand All @@ -264,26 +266,26 @@ def test_get_subsets_as_regions(cubeviz_helper, spectrum1d_cube, imviz_helper):
sr2 = CirclePixelRegion(center=PixCoord(x=2.5, y=3), radius=2)
plg.import_region(sr1, combination_mode='new')
plg.import_region(sr2, combination_mode='and')
spatial_regions = plg.get_subsets_as_regions(region_type='spatial')
spatial_regions = plg.get_regions(region_type='spatial')
assert spatial_regions['Subset 3']

# test errors
with pytest.raises(ValueError, match='No spectral subests in imviz.'):
imviz_helper.plugins['Subset Tools'].get_subsets_as_regions('spectral')
imviz_helper.plugins['Subset Tools'].get_regions('spectral')
with pytest.raises(ValueError, match="`region_type` must be 'spectral', 'spatial', or None for any."): # noqa E501
plg.get_subsets_as_regions(region_type='fail')
plg.get_regions(region_type='fail')


@pytest.mark.xfail(reason='Unskip once issue XXXX is resolved.')
def test_composite_get_subset_as_region(cubeviz_helper, spectrum1d_cube):
def test_get_regions_composite(cubeviz_helper, spectrum1d_cube):
"""
If you apply a circular subset mask to a circular subset to make a
composite subset, and they aren't exactly aligned at the center to form a
circular annulus, obtaining the region through ``get_interactive_regions``
(now deprecated, replaced with get_subsets_as_regions in Subset Tools) fails.
(now deprecated, replaced with get_regions in Subset Tools) fails.
However, you can retrieve the compound subset as a ``region`` with
``app.get_subsets``. This test ensures that a region is returned through
both ``app.get_subsets`` and ``get_subsets_as_regions``.
both ``app.get_subsets`` and ``get_regions``.
"""
cubeviz_helper.load_data(spectrum1d_cube)
plg = cubeviz_helper.plugins['Subset Tools']
Expand All @@ -299,7 +301,7 @@ def test_composite_get_subset_as_region(cubeviz_helper, spectrum1d_cube):
cubeviz_helper.default_viewer._obj.apply_subset_state(new_subset)

# make sure Subset 3, the composite subset, is retrieved.
regions = plg.get_subsets_as_regions()
regions = plg.get_regions()
ss_labels = ['Subset 1', 'Subset 2', 'Subset 3']
assert np.all([regions[ss] for ss in ss_labels])

Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/imviz/tests/test_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def test_wcslink_affine_with_extras(self):
all_labels = [layer.layer.label for layer in self.viewer.state.layers]
# Retrieved subsets as sky regions from Subset plugin, and ensure they
# match what was loaded and that they are in sky coordinates.
subset_as_regions = self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()
subset_as_regions = self.imviz.plugins['Subset Tools']._obj.get_regions()
assert sorted(subset_as_regions) == ['Subset 1', 'Subset 2']
assert_allclose(subset_as_regions['Subset 1'].center.ra.deg, 337.519449)
assert_allclose(subset_as_regions['Subset 2'].center.ra.deg, 337.518498)
# ensure agreement between app.get_subsets and subset_tools.get_subsets_as_regions
# ensure agreement between app.get_subsets and subset_tools.get_regions
ss = self.imviz.app.get_subsets(include_sky_region=True)
assert ss['Subset 1'][0]['sky_region'] == subset_as_regions['Subset 1']
assert ss['Subset 2'][0]['sky_region'] == subset_as_regions['Subset 2']
Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/imviz/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ def test_parse_jwst_nircam_level2(self, imviz_helper):
imviz_helper._apply_interactive_region('bqplot:rectangle',
(982, 1088),
(1008, 1077)) # Background
subsets = imviz_helper.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = imviz_helper.plugins['Subset Tools']._obj.get_regions()
assert list(subsets.keys()) == ['Subset 1', 'Subset 2'], subsets
# check that retrieved subsets-as-regions from subset plugin match what was loaded.
assert isinstance(subsets['Subset 1'], CirclePixelRegion)
assert subsets['Subset 1'].center == PixCoord(970.95, 1116.05)
assert isinstance(subsets['Subset 2'], RectanglePixelRegion)
assert subsets['Subset 2'].center == PixCoord(995.0, 1082.5)
# ensure agreement between app.get_subsets and subset_tools.get_subsets_as_regions
# ensure agreement between app.get_subsets and subset_tools.get_regions
ss = imviz_helper.app.get_subsets()
assert ss['Subset 1'][0]['region'] == subsets['Subset 1']
assert ss['Subset 2'][0]['region'] == subsets['Subset 2']
Expand Down
34 changes: 17 additions & 17 deletions jdaviz/configs/imviz/tests/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ def test_regions_invalid(self):

# Make sure nothing is actually loaded
self.verify_region_loaded('MaskedSubset 1', count=0)
assert self.imviz.plugins['Subset Tools'].get_subsets_as_regions() == {}
assert self.imviz.plugins['Subset Tools'].get_regions() == {}

def test_regions_fully_out_of_bounds(self):
"""Glue ROI will not error when out of bounds."""
my_reg = CirclePixelRegion(center=PixCoord(x=100, y=100), radius=5)
bad_regions = self.subset_plugin.import_region([my_reg], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('Subset 1')
assert len(self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()) == 1
assert len(self.imviz.plugins['Subset Tools']._obj.get_regions()) == 1

def test_regions_mask(self):
mask = np.zeros((10, 10), dtype=np.bool_)
mask[0, 0] = True
bad_regions = self.subset_plugin.import_region([mask], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('MaskedSubset 1')
assert self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions() == {}
assert self.imviz.plugins['Subset Tools']._obj.get_regions() == {}

mask[1, 1] = True
bad_regions = self.subset_plugin.import_region([mask], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('MaskedSubset 2')
assert self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions() == {}
assert self.imviz.plugins['Subset Tools']._obj.get_regions() == {}

# Also test deletion by label here.
self.imviz._delete_region('MaskedSubset 1')
Expand All @@ -103,7 +103,7 @@ def test_regions_mask(self):
bad_regions = self.subset_plugin.import_region([mask], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('MaskedSubset 3')
assert self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions() == {}
assert self.imviz.plugins['Subset Tools']._obj.get_regions() == {}

# Deletion of non-existent label is silent no-op.
self.imviz._delete_region('foo')
Expand All @@ -115,7 +115,7 @@ def test_regions_pixel(self):
combination_mode='new')
assert len(bad_regions) == 0
self.verify_region_loaded('Subset 1')
assert len(self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()) == 1
assert len(self.imviz.plugins['Subset Tools']._obj.get_regions()) == 1

def test_regions_sky_has_wcs(self):
# Mimic interactive region (before)
Expand All @@ -141,7 +141,7 @@ def test_regions_sky_has_wcs(self):
# Check interactive regions. We do not check if the translation is correct,
# that check hopefully is already done in glue-astronomy.
# Apparently, static region ate up one number...
subsets = self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = self.imviz.plugins['Subset Tools']._obj.get_regions()
assert list(subsets.keys()) == ['Subset 1', 'Subset 2', 'Subset 3', 'Subset 5', 'Subset 6'], subsets # noqa: E501
assert isinstance(subsets['Subset 1'], CirclePixelRegion)
assert isinstance(subsets['Subset 2'], CirclePixelRegion)
Expand All @@ -161,7 +161,7 @@ def test_regions_annulus_from_load_data(self):
# Test data is set up such that 1 pixel is 1 arcsec.
subset_radii = {"Subset 1": [0.5, 1], "Subset 2": [1, 3]}

subsets = self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = self.imviz.plugins['Subset Tools']._obj.get_regions()
subset_names = sorted(subsets.keys())
assert subset_names == ['Subset 1', 'Subset 2']
for n in subset_names:
Expand All @@ -174,15 +174,15 @@ def test_photutils_pixel(self):
bad_regions = self.subset_plugin.import_region([my_aper], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('Subset 1')
assert len(self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()) == 1
assert len(self.imviz.plugins['Subset Tools']._obj.get_regions()) == 1

def test_photutils_sky_has_wcs(self):
sky = SkyCoord(ra=337.5202808, dec=-20.833333059999998, unit='deg')
my_aper_sky = SkyCircularAperture(sky, 0.5 * u.arcsec)
bad_regions = self.subset_plugin.import_region([my_aper_sky], return_bad_regions=True)
assert len(bad_regions) == 0
self.verify_region_loaded('Subset 1')
assert len(self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()) == 1
assert len(self.imviz.plugins['Subset Tools']._obj.get_regions()) == 1


class TestLoadRegionsFromFile(BaseRegionHandler):
Expand All @@ -204,7 +204,7 @@ def test_ds9_load_all(self, imviz_helper):
assert len(bad_regions) == 1

# Will load 8/9 and 7 of that become ROIs.
subsets = imviz_helper.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = imviz_helper.plugins['Subset Tools']._obj.get_regions()
assert list(subsets.keys()) == ['Subset 1', 'Subset 2', 'Subset 3',
'Subset 4', 'Subset 5', 'Subset 6', 'Subset 7'], subsets

Expand All @@ -217,7 +217,7 @@ def test_ds9_load_two_good(self, imviz_helper):
bad_regions = imviz_helper.plugins['Subset Tools'].import_region(
self.region_file, combination_mode='new', max_num_regions=2, return_bad_regions=True)
assert len(bad_regions) == 0
subsets = imviz_helper.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = imviz_helper.plugins['Subset Tools']._obj.get_regions()
assert list(subsets.keys()) == ['Subset 1', 'Subset 2'], subsets
self.verify_region_loaded('MaskedSubset 1', count=0)

Expand All @@ -227,7 +227,7 @@ def test_ds9_load_one_bad(self, imviz_helper):
bad_regions = imviz_helper.plugins['Subset Tools'].import_region(
self.raw_regions[6], return_bad_regions=True)
assert len(bad_regions) == 1
assert imviz_helper.plugins['Subset Tools']._obj.get_subsets_as_regions() == {}
assert imviz_helper.plugins['Subset Tools']._obj.get_regions() == {}
self.verify_region_loaded('MaskedSubset 1', count=0)

def test_ds9_load_one_good_one_bad(self, imviz_helper):
Expand All @@ -237,7 +237,7 @@ def test_ds9_load_one_good_one_bad(self, imviz_helper):
[self.raw_regions[3], self.raw_regions[6]], return_bad_regions=True)
assert len(bad_regions) == 1

subsets = imviz_helper.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = imviz_helper.plugins['Subset Tools']._obj.get_regions()
assert list(subsets.keys()) == ['Subset 1'], subsets
self.verify_region_loaded('MaskedSubset 1', count=0)

Expand All @@ -250,13 +250,13 @@ def test_annulus(self):
self.imviz._apply_interactive_region('bqplot:truecircle', (2, 2), (7, 7))

# At this point, there should be two normal circles.
subsets = self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = self.imviz.plugins['Subset Tools']._obj.get_regions()
assert list(subsets.keys()) == ['Subset 1', 'Subset 2'], subsets
assert isinstance(subsets['Subset 1'], CirclePixelRegion)
assert isinstance(subsets['Subset 2'], CirclePixelRegion)
assert subsets['Subset 1'].center == PixCoord(4.5, 4.5)
assert subsets['Subset 2'].center == PixCoord(4.5, 4.5)
# ensure agreement between app.get_subsets and subset_tools.get_subsets_as_regions
# ensure agreement between app.get_subsets and subset_tools.get_regions
ss = self.imviz.app.get_subsets()
assert ss['Subset 1'][0]['region'] == subsets['Subset 1']
assert ss['Subset 2'][0]['region'] == subsets['Subset 2']
Expand All @@ -266,7 +266,7 @@ def test_annulus(self):
new_subset = subset_groups[0].subset_state & ~subset_groups[1].subset_state
self.viewer.apply_subset_state(new_subset)

subsets = self.imviz.plugins['Subset Tools']._obj.get_subsets_as_regions()
subsets = self.imviz.plugins['Subset Tools']._obj.get_regions()
assert len(self.imviz.app.data_collection.subset_groups) == 3
assert list(subsets.keys()) == ['Subset 1', 'Subset 2', 'Subset 3'], subsets
assert isinstance(subsets['Subset 1'], CirclePixelRegion)
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/specviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_spectra(self, data_label=None, spectral_subset=None, apply_slider_redshi

return output_spectra

@deprecated(since="4.1", alternative="subset_tools.get_subsets_as_regions")
@deprecated(since="4.1", alternative="subset_tools.get_regions")
def get_spectral_regions(self, use_display_units=False):
"""
A simple wrapper around the app-level call to retrieve only spectral
Expand Down
Loading

0 comments on commit 84b30ad

Please sign in to comment.