Skip to content

Commit

Permalink
fix failing test, add xfailed test for composite subset issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Dec 6, 2024
1 parent cd4b41f commit 08623e2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
5 changes: 4 additions & 1 deletion jdaviz/configs/default/plugins/subset_tools/subset_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def user_api(self):
'get_center', 'set_center', 'import_region', 'get_subsets_as_regions']
return PluginUserApi(self, expose)

def new_get_subsets_as_regions(self, region_type=None, list_of_subset_labels=None, use_display_units=False):
pass

def get_subsets_as_regions(self, region_type=None, list_of_subset_labels=None,
use_display_units=False):
"""
Expand Down Expand Up @@ -206,7 +209,7 @@ def get_subsets_as_regions(self, region_type=None, list_of_subset_labels=None,
if region_type is not None:
region_type = region_type.lower()
if region_type not in ['spectral', 'spatial']:
raise ValueError("`region_type` must be 'spectral' or 'spatial'.")
raise ValueError("`region_type` must be 'spectral', 'spatial', or None for any.")
if ((self.config == 'imviz' and region_type == 'spectral') or
(self.config == 'specviz' and region_type == 'spatial')):
raise ValueError(f"No {region_type} subests in {self.config}.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,80 @@ 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):
def test_get_subsets_as_regions(cubeviz_helper, spectrum1d_cube, imviz_helper):

cubeviz_helper.load_data(spectrum1d_cube)
plg = cubeviz_helper.plugins['Subset Tools']

# load one spectral region, which will become 'Subset 1'
plg.import_region(SpectralRegion(1* u.um, 2 * u.um))
plg.import_region(SpectralRegion(1 * u.um, 2 * u.um))

# load one spatial region, which will become 'Subset 2'
spatial_reg = CirclePixelRegion(center=PixCoord(x=2, y=2), radius=2)
plg.import_region(spatial_reg)
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()
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'])
assert len(only_s1) == 1
assert only_s1['Subset 1']

# now specify region type
# now specify region type and check output
spatial_regions = plg.get_subsets_as_regions(region_type='spatial')
assert len(spatial_regions) == 1

assert spatial_regions['Subset 2']
spectral_regions = plg.get_subsets_as_regions(region_type='spectral')
assert len(spectral_regions) == 1
assert spectral_regions['Subset 1']

# now test a composite spatial subset, make sure it is retrieved
sr1 = CirclePixelRegion(center=PixCoord(x=2.5, y=2.5), radius=2)
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')
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')
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')


@pytest.mark.xfail(reason='Unskip once issue XXXX is resolved.')
def test_composite_get_subset_as_region(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.
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``.
"""
cubeviz_helper.load_data(spectrum1d_cube)
plg = cubeviz_helper.plugins['Subset Tools']

# Outer circle
cubeviz_helper._apply_interactive_region('bqplot:truecircle', (51, 51), (141, 141))
# Inner circle
cubeviz_helper._apply_interactive_region('bqplot:truecircle', (70, 70), (120, 120))

# apply circular annulus
subset_groups = cubeviz_helper.app.data_collection.subset_groups
new_subset = subset_groups[0].subset_state & ~subset_groups[1].subset_state
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()
ss_labels = ['Subset 1', 'Subset 2', 'Subset 3']
assert np.all([regions[ss] for ss in ss_labels])

# make sure the same regions are returned by app.get_subsets
get_subsets = cubeviz_helper.app.get_subsets()
assert np.all([get_subsets[ss][0]['region'] == regions[ss] for ss in ss_labels])

0 comments on commit 08623e2

Please sign in to comment.