Skip to content

Commit

Permalink
Create import_region method for loading region objects (#3104)
Browse files Browse the repository at this point in the history
* Create import_region method for loading region objects

* Remove call to load_regions

* Add deprecated fixture to load_regions methods

* Update combination_mode to listen to UI

* Update import_region to switch from new to replace mode correctly

* Remove unused pytest fixture

* Add docs and fix rampviz test

* Replace load_regions calls with import_region in notebooks

* Replace applpy_roi with import_region in tests

* Add new parameter to set combination mode to new after subset creation

* Update tests to use subset_plugin._obj

* Replace mode and create_as_new with combination_mode
  • Loading branch information
javerbukh authored Sep 30, 2024
1 parent c9425b8 commit f48bc16
Show file tree
Hide file tree
Showing 41 changed files with 898 additions and 520 deletions.
4 changes: 2 additions & 2 deletions docs/cubeviz/import_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ can load the regions into Cubeviz as follows:

.. code-block:: python
cubeviz.load_regions_from_file("/path/to/data/myregions.reg")
cubeviz.plugins['Subset Tools']._obj.import_region("/path/to/data/myregions.reg")
Unsupported regions will be skipped and trigger a warning. Those that
failed to load, if any, can be returned as a list of tuples of the
form ``(region, reason)``:

.. code-block:: python
bad_regions = cubeviz.load_regions_from_file("/path/to/data/myregions.reg", return_bad_regions=True)
bad_regions = cubeviz.plugins['Subset Tools']._obj.import_region("/path/to/data/myregions.reg", return_bad_regions=True)
.. note:: Sky regions are currently unsupported in Cubeviz, unlike Imviz.

Expand Down
4 changes: 2 additions & 2 deletions docs/imviz/import_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ can load the regions into Imviz as follows:

.. code-block:: python
imviz.load_regions_from_file("/path/to/data/myregions.reg")
imviz.plugins['Subset Tools']._obj.import_region("/path/to/data/myregions.reg")
Unsupported regions will be skipped and trigger a warning. Those that
failed to load, if any, can be returned as a list of tuples of the
form ``(region, reason)``:

.. code-block:: python
bad_regions = imviz.load_regions_from_file("/path/to/data/myregions.reg", return_bad_regions=True)
bad_regions = imviz.plugins['Subset Tools']._obj.import_region("/path/to/data/myregions.reg", return_bad_regions=True)
You could also define :ref:`regions:shapes` programmatically and load them; e.g.:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from astropy.io import fits
from astropy.nddata import CCDData
from astropy.wcs import WCS
from glue.core.roi import XRangeROI
from numpy.testing import assert_allclose
from specutils import SpectralRegion


@pytest.mark.parametrize("cube_type", ["Surface Brightness", "Flux"])
Expand Down Expand Up @@ -233,8 +233,10 @@ def test_moment_frequency_unit_conversion(cubeviz_helper, spectrum1d_cube_larger

uc = cubeviz_helper.plugins['Unit Conversion']
mm = cubeviz_helper.plugins['Moment Maps']
viewer = cubeviz_helper.app.get_viewer('spectrum-viewer')
viewer.apply_roi(XRangeROI(4.624e-07, 4.627e-07))

unit = u.Unit(uc.spectral_unit.selected)
cubeviz_helper.plugins['Subset Tools']._obj.import_region(SpectralRegion(4.624e-07 * unit,
4.627e-07 * unit))

uc.spectral_unit = 'Hz'
mm.spectral_subset = 'Subset 1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from astropy.utils.exceptions import AstropyUserWarning

from glue.core.roi import CircularROI, RectangularROI
from glue.core.edit_subset_mode import ReplaceMode, AndNotMode, NewMode
from numpy.testing import assert_allclose, assert_array_equal
from regions import (CirclePixelRegion, CircleAnnulusPixelRegion, EllipsePixelRegion,
RectanglePixelRegion, PixCoord)
Expand Down Expand Up @@ -84,7 +83,7 @@ def test_gauss_smooth_before_spec_extract(cubeviz_helper, spectrum1d_cube_with_u
# two-pixel region:
CirclePixelRegion(PixCoord(0.5, 0), radius=1.2)
]
cubeviz_helper.load_regions(regions)
cubeviz_helper.plugins['Subset Tools']._obj.import_region(regions, combination_mode='new')

extract_plugin = cubeviz_helper.plugins['Spectral Extraction']
extract_plugin.function = "Sum"
Expand Down Expand Up @@ -124,7 +123,7 @@ def test_subset(
]

cubeviz_helper.load_data(spectrum1d_cube_with_uncerts)
cubeviz_helper.load_regions(regions)
cubeviz_helper.plugins['Subset Tools']._obj.import_region(regions, combination_mode='new')

plg = cubeviz_helper.plugins['Spectral Extraction']
plg.function = function
Expand Down Expand Up @@ -199,7 +198,8 @@ def test_save_collapsed_to_fits(cubeviz_helper, spectrum1d_cube_with_uncerts, tm
def test_aperture_markers(cubeviz_helper, spectrum1d_cube):

cubeviz_helper.load_data(spectrum1d_cube)
cubeviz_helper.load_regions([CirclePixelRegion(PixCoord(0.5, 0), radius=1.2)])
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
[CirclePixelRegion(PixCoord(0.5, 0), radius=1.2)])

extract_plg = cubeviz_helper.plugins['Spectral Extraction']
slice_plg = cubeviz_helper.plugins['Slice']
Expand Down Expand Up @@ -251,9 +251,10 @@ def test_cone_aperture_with_different_methods(cubeviz_helper, spectrum1d_cube_la
expected_flux_2400):
cubeviz_helper.load_data(spectrum1d_cube_largest)
center = PixCoord(5, 10)
cubeviz_helper.load_regions([
CirclePixelRegion(center, radius=2.5),
EllipsePixelRegion(center, width=5, height=5)])
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
CirclePixelRegion(center, radius=2.5), combination_mode='new')
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
EllipsePixelRegion(center, width=5, height=5), combination_mode='new')

extract_plg = cubeviz_helper.plugins['Spectral Extraction']

Expand Down Expand Up @@ -283,9 +284,9 @@ def test_cylindrical_aperture_with_different_methods(cubeviz_helper, spectrum1d_
subset, aperture_method, expected_flux_wav):
cubeviz_helper.load_data(spectrum1d_cube_largest, data_label="test")
center = PixCoord(5, 10)
cubeviz_helper.load_regions([
cubeviz_helper.plugins['Subset Tools']._obj.import_region([
CirclePixelRegion(center, radius=2.5),
EllipsePixelRegion(center, width=5, height=5)])
EllipsePixelRegion(center, width=5, height=5)], combination_mode='new')

extract_plg = cubeviz_helper.plugins['Spectral Extraction']

Expand All @@ -307,7 +308,8 @@ def test_cylindrical_aperture_with_different_methods(cubeviz_helper, spectrum1d_
# NOTE: Not as thorough as circle and ellipse above but good enough.
def test_rectangle_aperture_with_exact(cubeviz_helper, spectrum1d_cube_largest):
cubeviz_helper.load_data(spectrum1d_cube_largest)
cubeviz_helper.load_regions(RectanglePixelRegion(PixCoord(5, 10), width=4, height=4))
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
RectanglePixelRegion(PixCoord(5, 10), width=4, height=4))

extract_plg = cubeviz_helper.plugins['Spectral Extraction']

Expand All @@ -334,9 +336,9 @@ def test_background_subtraction(cubeviz_helper, spectrum1d_cube_largest):
spectrum1d_cube_largest = spectrum1d_cube_largest + 1 * u.Jy

cubeviz_helper.load_data(spectrum1d_cube_largest)
cubeviz_helper.load_regions([
cubeviz_helper.plugins['Subset Tools']._obj.import_region([
CirclePixelRegion(PixCoord(5, 10), radius=2.5),
EllipsePixelRegion(PixCoord(13, 10), width=3, height=5)])
EllipsePixelRegion(PixCoord(13, 10), width=3, height=5)], combination_mode='new')

extract_plg = cubeviz_helper.plugins['Spectral Extraction']
with extract_plg.as_active():
Expand Down Expand Up @@ -387,9 +389,9 @@ def test_background_subtraction(cubeviz_helper, spectrum1d_cube_largest):
def test_cone_and_cylinder_errors(cubeviz_helper, spectrum1d_cube_largest):
cubeviz_helper.load_data(spectrum1d_cube_largest)
center = PixCoord(5, 10)
cubeviz_helper.load_regions([
cubeviz_helper.plugins['Subset Tools']._obj.import_region([
CirclePixelRegion(center, radius=2.5),
CircleAnnulusPixelRegion(center, inner_radius=2.5, outer_radius=4)])
CircleAnnulusPixelRegion(center, inner_radius=2.5, outer_radius=4)], combination_mode='new')

extract_plg = cubeviz_helper.plugins['Spectral Extraction']

Expand All @@ -414,7 +416,8 @@ def test_cone_and_cylinder_errors(cubeviz_helper, spectrum1d_cube_largest):
def test_cone_aperture_with_frequency_units(cubeviz_helper, spectral_cube_wcs):
data = Spectrum1D(flux=np.ones((128, 129, 256)) * u.nJy, wcs=spectral_cube_wcs)
cubeviz_helper.load_data(data, data_label="Test Flux")
cubeviz_helper.load_regions([CirclePixelRegion(PixCoord(14, 15), radius=2.5)])
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
[CirclePixelRegion(PixCoord(14, 15), radius=2.5)])

extract_plg = cubeviz_helper.plugins['Spectral Extraction']

Expand All @@ -434,16 +437,17 @@ def test_cube_extraction_with_nan(cubeviz_helper, image_cube_hdu_obj):
sp = extract_plg.extract() # Default settings (sum)
assert_allclose(sp.flux.value, 9.6E-16) # (10 x 10) - 4

cubeviz_helper.load_regions(RectanglePixelRegion(PixCoord(1.5, 1.5), width=4, height=4))
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
RectanglePixelRegion(PixCoord(1.5, 1.5), width=4, height=4))
extract_plg.aperture = 'Subset 1'
sp_subset = extract_plg.extract() # Default settings but on Subset
assert_allclose(sp_subset.flux.value, 1.2E-16) # (4 x 4) - 4


def test_autoupdate_results(cubeviz_helper, spectrum1d_cube_largest):
cubeviz_helper.load_data(spectrum1d_cube_largest)
fv = cubeviz_helper.viewers['flux-viewer']._obj
fv.apply_roi(CircularROI(xc=5, yc=5, radius=2))
cubeviz_helper.plugins['Subset Tools']._obj.import_region(
CircularROI(xc=5, yc=5, radius=2))

extract_plg = cubeviz_helper.plugins['Spectral Extraction']
extract_plg.aperture = 'Subset 1'
Expand All @@ -454,8 +458,8 @@ def test_autoupdate_results(cubeviz_helper, spectrum1d_cube_largest):
# orig_med_flux = np.median(cubeviz_helper.get_data('extracted').flux)

# replace Subset 1 with a larger subset, resulting fluxes should increase
cubeviz_helper.app.session.edit_subset_mode.mode = ReplaceMode
fv.apply_roi(CircularROI(xc=5, yc=5, radius=3))
cubeviz_helper.plugins['Subset Tools']._obj.combination_mode.selected = 'replace'
cubeviz_helper.plugins['Subset Tools']._obj.import_region(CircularROI(xc=5, yc=5, radius=3))

# update should take place automatically, but since its async, we'll call manually to ensure
# the update is complete before comparing results
Expand All @@ -469,26 +473,22 @@ def test_autoupdate_results(cubeviz_helper, spectrum1d_cube_largest):

def test_aperture_composite_detection(cubeviz_helper, spectrum1d_cube):
cubeviz_helper.load_data(spectrum1d_cube)
flux_viewer = cubeviz_helper.app.get_viewer('flux-viewer')
subset_plugin = cubeviz_helper.plugins['Subset Tools']._obj
spec_extr_plugin = cubeviz_helper.plugins['Spectral Extraction']._obj

# create a rectangular subset with all spaxels:
rectangle = RectangularROI(-0.5, 1.5, -0.5, 3.5)
flux_viewer.toolbar.active_tool = flux_viewer.toolbar.tools['bqplot:rectangle']
flux_viewer.apply_roi(rectangle)
subset_plugin.import_region(rectangle)

# select subset 1, ensure it's not a composite subset:
spec_extr_plugin.aperture_selected = 'Subset 1'
assert not spec_extr_plugin.aperture.is_composite

# now remove from this subset a circular region in the center:
flux_viewer.toolbar.active_tool = flux_viewer.toolbar.tools['bqplot:truecircle']
subset_plugin.subset_selected = 'Subset 1'
cubeviz_helper.app.session.edit_subset_mode.mode = AndNotMode

circle = CircularROI(0.5, 1.5, 1)
flux_viewer.apply_roi(circle)

subset_plugin.combination_mode.selected = 'andnot'
subset_plugin.import_region(circle)

# now the subset is composite:
assert spec_extr_plugin.aperture.is_composite
Expand All @@ -497,35 +497,32 @@ def test_aperture_composite_detection(cubeviz_helper, spectrum1d_cube):
def test_extraction_composite_subset(cubeviz_helper, spectrum1d_cube):
cubeviz_helper.load_data(spectrum1d_cube)

flux_viewer = cubeviz_helper.app.get_viewer('flux-viewer')
subset_plugin = cubeviz_helper.plugins['Subset Tools']._obj
spec_extr_plugin = cubeviz_helper.plugins['Spectral Extraction']._obj

lower_aperture = RectangularROI(-0.5, 0.5, -0.5, 1.5)
upper_aperture = RectangularROI(2.5, 3.5, -0.5, 1.5)

flux_viewer.toolbar.active_tool = flux_viewer.toolbar.tools['bqplot:rectangle']
flux_viewer.apply_roi(lower_aperture)

cubeviz_helper.app.session.edit_subset_mode.mode = NewMode
flux_viewer.apply_roi(upper_aperture)
subset_plugin.import_region(lower_aperture)
subset_plugin.combination_mode.selected = 'new'
subset_plugin.import_region(upper_aperture)

spec_extr_plugin.aperture_selected = 'Subset 1'
spectrum_1 = spec_extr_plugin.extract()

spec_extr_plugin.aperture_selected = 'Subset 2'
spectrum_2 = spec_extr_plugin.extract()

subset_plugin.subset_selected = 'Create New'
rectangle = RectangularROI(-0.5, 3.5, -0.5, 1.5)
flux_viewer.toolbar.active_tool = flux_viewer.toolbar.tools['bqplot:rectangle']
flux_viewer.apply_roi(rectangle)

flux_viewer.toolbar.active_tool = flux_viewer.toolbar.tools['bqplot:truecircle']
subset_plugin.combination_mode.selected = 'new'
subset_plugin.import_region(rectangle)

subset_plugin.subset_selected = 'Subset 3'
cubeviz_helper.app.session.edit_subset_mode.mode = AndNotMode
circle = CircularROI(1.5, 0.5, 1.1)
flux_viewer.apply_roi(circle)

subset_plugin.combination_mode.selected = 'andnot'
subset_plugin.import_region(circle)

spec_extr_plugin.aperture_selected = 'Subset 3'

Expand Down Expand Up @@ -559,10 +556,10 @@ def test_default_spectral_extraction(cubeviz_helper, spectrum1d_cube_fluxunit_jy
# regression tests make sure that doesn't happen anymore by accounting
# for non-science pixels in the sums:
cubeviz_helper.load_data(spectrum1d_cube_fluxunit_jy_per_steradian)
flux_viewer = cubeviz_helper.app.get_viewer('flux-viewer')

# create a spatial subset that spans all spaxels:
flux_viewer.apply_roi(CircularROI(1.5, 2, 5))
subset_plugin = cubeviz_helper.plugins['Subset Tools']._obj

subset_plugin.import_region(CircularROI(1.5, 2, 5))

# the first and second spectra correspond to the default extraction
# and the subset extraction. the fluxes in these extractions should agree:
Expand Down Expand Up @@ -646,8 +643,8 @@ def test_spectral_extraction_scientific_validation(
cubeviz_helper.load_data(uri, cache=True)

# add a subset with an aperture centered on each source
flux_viewer = cubeviz_helper.app.get_viewer('flux-viewer')
flux_viewer.apply_roi(CircularROI(*aperture))
subset_plugin = cubeviz_helper.plugins['Subset Tools']._obj
subset_plugin.import_region(CircularROI(*aperture))

# set the slice to the blue end of MIRI CH1
slice_plugin = cubeviz_helper.plugins['Slice']
Expand Down
8 changes: 4 additions & 4 deletions jdaviz/configs/cubeviz/plugins/tests/test_cubeviz_aperphot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_cubeviz_aperphot_cube_orig_flux(cubeviz_helper, image_cube_hdu_obj_micr
solid_angle_unit = u.pix * u.pix

aper = RectanglePixelRegion(center=PixCoord(x=1, y=2), width=3, height=5)
cubeviz_helper.load_regions(aper)
cubeviz_helper.plugins['Subset Tools']._obj.import_region(aper)

# Make sure MASK is not an option even when shown in viewer.
cubeviz_helper.app.add_data_to_viewer("flux-viewer", "test[MASK]", visible=True)
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_cubeviz_aperphot_generated_3d_gaussian_smooth(cubeviz_helper, image_cub
cubeviz_helper.app.add_data_to_viewer("uncert-viewer", "test[FLUX] spatial-smooth stddev-1.0")

aper = RectanglePixelRegion(center=PixCoord(x=1, y=2), width=3, height=5)
cubeviz_helper.load_regions(aper)
cubeviz_helper.plugins['Subset Tools']._obj.import_region(aper)

plg = cubeviz_helper.plugins["Aperture Photometry"]._obj
plg.dataset_selected = "test[FLUX] spatial-smooth stddev-1.0"
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_cubeviz_aperphot_cube_orig_flux_mjysr(cubeviz_helper, spectrum1d_cube_c

aper = RectanglePixelRegion(center=PixCoord(x=3, y=1), width=1, height=1)
bg = RectanglePixelRegion(center=PixCoord(x=2, y=0), width=1, height=1)
cubeviz_helper.load_regions([aper, bg])
cubeviz_helper.plugins['Subset Tools']._obj.import_region([aper, bg], combination_mode='new')

plg = cubeviz_helper.plugins["Aperture Photometry"]._obj
plg.dataset_selected = "test[FLUX]"
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_cubeviz_aperphot_unit_conversion(cubeviz_helper, spectrum1d_cube_custom
bg = RectanglePixelRegion(center=PixCoord(x=1, y=2), width=1, height=1)

cubeviz_helper.load_data(mjy_sr_cube, data_label="test")
cubeviz_helper.load_regions([aper, bg])
cubeviz_helper.plugins['Subset Tools']._obj.import_region([aper, bg], combination_mode='new')

ap = cubeviz_helper.plugins['Aperture Photometry']._obj

Expand Down
7 changes: 4 additions & 3 deletions jdaviz/configs/cubeviz/plugins/tests/test_cubeviz_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from glue.core.roi import XRangeROI
from specutils import SpectralRegion

from jdaviz import Cubeviz
from jdaviz.app import Application
Expand Down Expand Up @@ -43,8 +43,9 @@ def test_get_data_spatial_and_spectral(cubeviz_helper, spectrum1d_cube_larger):
cubeviz_helper._apply_interactive_region('bqplot:ellipse', (0, 0), (9, 8))

# Subset 2 (spectral)
spec_viewer = cubeviz_helper.app.get_viewer(cubeviz_helper._default_spectrum_viewer_reference_name) # noqa
spec_viewer.apply_roi(XRangeROI(4.62440061e-07, 4.62520112e-07))
subset_plugin = cubeviz_helper.plugins['Subset Tools']._obj
unit = spectrum1d_cube_larger.spectral_axis.unit
subset_plugin.import_region(SpectralRegion(4.62440061e-07 * unit, 4.62520112e-07 * unit))

spatial_with_spec = cubeviz_helper.get_data(data_label="Spectrum (Subset 1, sum)",
spectral_subset="Subset 2")
Expand Down
7 changes: 4 additions & 3 deletions jdaviz/configs/cubeviz/plugins/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import pytest
from astropy import units as u
from astropy.wcs import WCS
from specutils import Spectrum1D
from glue.core.roi import XRangeROI
from specutils import Spectrum1D, SpectralRegion
from glue_astronomy.translators.spectrum1d import PaddedSpectrumWCS
from numpy.testing import assert_allclose, assert_array_equal

Expand Down Expand Up @@ -71,7 +70,9 @@ def test_spectrum1d_with_fake_fixed_units(spectrum1d, cubeviz_helper):
dc[0].meta["_orig_spec"] = spectrum1d

cubeviz_helper.app.add_data_to_viewer('spectrum-viewer', 'test')
cubeviz_helper.app.get_viewer('spectrum-viewer').apply_roi(XRangeROI(6600, 7400))
unit = u.Unit(cubeviz_helper.plugins['Unit Conversion'].spectral_unit.selected)
cubeviz_helper.plugins['Subset Tools']._obj.import_region(SpectralRegion(6600 * unit,
7400 * unit))

subsets = cubeviz_helper.app.get_subsets()
reg = subsets.get('Subset 1')
Expand Down
Loading

0 comments on commit f48bc16

Please sign in to comment.