Skip to content

Commit

Permalink
Create private method for importing spectral region objects (#2941)
Browse files Browse the repository at this point in the history
* Create private method for importing spectral region objects

* Address review comments

* Add tests for list of modes and single SpectralRegion

* Fix style
  • Loading branch information
javerbukh authored Jul 19, 2024
1 parent dfb4bfa commit 5c92eae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
32 changes: 31 additions & 1 deletion jdaviz/configs/default/plugins/subset_plugin/subset_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import astropy.units as u
from glue.core.message import EditSubsetMessage, SubsetUpdateMessage
from glue.core.edit_subset_mode import (AndMode, AndNotMode, OrMode,
ReplaceMode, XorMode)
ReplaceMode, XorMode, NewMode)
from glue.core.roi import CircularROI, CircularAnnulusROI, EllipticalROI, RectangularROI
from glue.core.subset import RoiSubsetState, RangeSubsetState, CompositeSubsetState
from glue.icons import icon_path
Expand Down Expand Up @@ -655,3 +655,33 @@ def _set_value_in_subset_definition(self, index, name, desired_key, new_value):
if self.subset_definitions[index][i]['name'] == name:
self.subset_definitions[index][i]['value'] = new_value
break

def _import_spectral_regions(self, spec_region, mode=NewMode, viewer=None):
"""
Method for importing a SpectralRegion object or list of SpectralRegion objects.
Parameters
----------
spec_region : ~`specutils.SpectralRegion` object or list
The object that contains bounds for the spectral region or regions
mode : AndMode, AndNotMode, OrMode, ReplaceMode, XorMode, NewMode, or list
By default this is set to NewMode which creates a new subset per subregion.
OrMode will create a composite subset with each subregion corresponding
to a subregion of a single subset.
"""
viewer_name = viewer or self.app._jdaviz_helper._default_spectrum_viewer_reference_name
range_viewer = self.app.get_viewer(viewer_name)
for index, sub_region in enumerate(spec_region):
if isinstance(mode, list):
if len(mode) != (len(spec_region) - 1):
raise ValueError("list of mode must be size of spec_region minus one")
if index == 0:
m = NewMode
else:
m = mode[index - 1]
else:
m = mode
self.app.session.edit_subset_mode.mode = m
s = RangeSubsetState(lo=sub_region.lower.value, hi=sub_region.upper.value,
att=range_viewer.slice_component_label)
range_viewer.apply_subset_state(s)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import numpy as np
from astropy.nddata import NDData
import astropy.units as u
from specutils import SpectralRegion
from glue.core.roi import EllipticalROI, CircularROI, CircularAnnulusROI, RectangularROI, XRangeROI
from glue.core.edit_subset_mode import OrMode, NewMode, AndNotMode
from numpy.testing import assert_allclose

from jdaviz.configs.default.plugins.subset_plugin import utils
Expand Down Expand Up @@ -168,3 +171,29 @@ def test_circle_recenter_linking(roi_class, subset_info, imviz_helper, image_2d_
for i, attr in enumerate(subset_info):
assert subset_defs[0][i+1]['name'] == subset_info[attr]['wcs_name']
assert_allclose(subset_defs[0][i+1]['value'], true_values_final[i]['value'])


@pytest.mark.parametrize(
('spec_regions', 'mode', 'len_subsets', 'len_subregions'),
[([SpectralRegion(5.772486091213352 * u.um, 6.052963676101135 * u.um),
SpectralRegion(6.494371022809778 * u.um, 6.724270682553864 * u.um),
SpectralRegion(7.004748267441649 * u.um, 7.3404016303483965 * u.um)], NewMode, 3, 1),
((SpectralRegion(5.772486091213352 * u.um, 6.052963676101135 * u.um) +
SpectralRegion(6.494371022809778 * u.um, 6.724270682553864 * u.um) +
SpectralRegion(7.004748267441649 * u.um, 7.3404016303483965 * u.um)), OrMode, 1, 3),
((SpectralRegion(5.772486091213352 * u.um, 6.052963676101135 * u.um) +
SpectralRegion(5.8 * u.um, 5.9 * u.um) +
SpectralRegion(6.494371022809778 * u.um, 6.724270682553864 * u.um) +
SpectralRegion(7 * u.um, 7.2 * u.um)), [AndNotMode, OrMode, OrMode], 1, 4),
(SpectralRegion(5.8 * u.um, 5.9 * u.um), None, 1, 1)
]
)
def test_import_spectral_region(cubeviz_helper, spectrum1d_cube, spec_regions, mode, len_subsets,
len_subregions):
cubeviz_helper.load_data(spectrum1d_cube)
cubeviz_helper.app.get_tray_item_from_name('g-subset-plugin')
plg = cubeviz_helper.plugins['Subset Tools']._obj
plg._import_spectral_regions(spec_region=spec_regions, mode=mode)
subsets = cubeviz_helper.app.get_subsets()
assert len(subsets) == len_subsets
assert len(subsets['Subset 1']) == len_subregions

0 comments on commit 5c92eae

Please sign in to comment.