Skip to content

Commit

Permalink
Implement slice in output table
Browse files Browse the repository at this point in the history
and do not display slice for cubeviz 2D data,
as requested.
  • Loading branch information
pllim committed Jan 30, 2024
1 parent b545067 commit f85ec71
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
1 change: 1 addition & 0 deletions jdaviz/configs/cubeviz/plugins/slice/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def _on_slider_updated(self, event):

self.hub.broadcast(SliceWavelengthUpdatedMessage(slice=value,
wavelength=self.wavelength,
wavelength_unit=self.wavelength_unit,
sender=self))

def vue_goto_first(self, *args):
Expand Down
22 changes: 11 additions & 11 deletions jdaviz/configs/cubeviz/plugins/tests/test_cubeviz_aperphot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import pytest
from astropy import units as u
from astropy.tests.helper import assert_quantity_allclose
from astropy.utils.exceptions import AstropyUserWarning
from numpy.testing import assert_allclose
from regions import RectanglePixelRegion, PixCoord
Expand Down Expand Up @@ -33,7 +35,7 @@ def test_cubeviz_aperphot_cube_orig_flux(cubeviz_helper, image_cube_hdu_obj_micr
assert_allclose(row["sum"], 75 * flux_unit) # 3 (w) x 5 (h) x 5 (v)
assert_allclose(row["sum_aper_area"], 15 * (u.pix * u.pix)) # 3 (w) x 5 (h)
assert_allclose(row["mean"], 5 * flux_unit)
assert row["slice"] == 4
assert_quantity_allclose(row["slice"], 4.894499866699333 * u.um)

# Move slider and make sure it recomputes for a new slice automatically.
cube_slice_plg = cubeviz_helper.plugins["Slice"]._obj
Expand All @@ -50,27 +52,21 @@ def test_cubeviz_aperphot_cube_orig_flux(cubeviz_helper, image_cube_hdu_obj_micr
assert_allclose(row["sum"], 15 * flux_unit) # 3 (w) x 5 (h) x 1 (v)
assert_allclose(row["sum_aper_area"], 15 * (u.pix * u.pix)) # 3 (w) x 5 (h)
assert_allclose(row["mean"], 1 * flux_unit)
assert row["slice"] == 0


def test_cubeviz_aperphot_generated_2d_collapse(cubeviz_helper, image_cube_hdu_obj_microns):
cubeviz_helper.load_data(image_cube_hdu_obj_microns, data_label="test")
flux_unit = u.Unit("1E-17 erg*s^-1*cm^-2*Angstrom^-1")
assert_quantity_allclose(row["slice"], 4.8904998665093435 * u.um)

# We continue on with test_cubeviz_aperphot_generated_2d_collapse here
# because we want to make sure the result would append properly between 3D and 2D.
collapse_plg = cubeviz_helper.plugins["Collapse"]._obj
collapse_plg.vue_collapse()

# Need this to make it available for photometry data drop-down.
cubeviz_helper.app.add_data_to_viewer("uncert-viewer", "test[FLUX] collapsed")

aper = RectanglePixelRegion(center=PixCoord(x=1, y=2), width=3, height=5)
cubeviz_helper.load_regions(aper)

plg = cubeviz_helper.plugins["Aperture Photometry"]._obj
plg.dataset_selected = "test[FLUX] collapsed"
plg.aperture_selected = "Subset 1"
plg.vue_do_aper_phot()
row = cubeviz_helper.get_aperture_photometry_results()[0]
row = cubeviz_helper.get_aperture_photometry_results()[2]

# Basically, we should recover the input rectangle here.
assert_allclose(row["xcenter"], 1 * u.pix)
Expand All @@ -81,6 +77,7 @@ def test_cubeviz_aperphot_generated_2d_collapse(cubeviz_helper, image_cube_hdu_o
assert_allclose(row["sum"], 540 * flux_unit) # 3 (w) x 5 (h) x 36 (v)
assert_allclose(row["sum_aper_area"], 15 * (u.pix * u.pix)) # 3 (w) x 5 (h)
assert_allclose(row["mean"], 36 * flux_unit)
assert np.isnan(row["slice"])


def test_cubeviz_aperphot_generated_2d_moment(cubeviz_helper, image_cube_hdu_obj_microns):
Expand Down Expand Up @@ -111,6 +108,7 @@ def test_cubeviz_aperphot_generated_2d_moment(cubeviz_helper, image_cube_hdu_obj
assert_allclose(row["sum"], 540 * flux_unit) # 3 (w) x 5 (h) x 36 (v)
assert_allclose(row["sum_aper_area"], 15 * (u.pix * u.pix)) # 3 (w) x 5 (h)
assert_allclose(row["mean"], 36 * flux_unit)
assert np.isnan(row["slice"])

# Moment 1 has no compatible unit, so should not be available for photometry.
moment_plg.n_moment = 1
Expand Down Expand Up @@ -152,6 +150,7 @@ def test_cubeviz_aperphot_generated_3d_gaussian_smooth(cubeviz_helper, image_cub
assert_allclose(row["sum"], 48.54973 * flux_unit) # 3 (w) x 5 (h) x <5 (v)
assert_allclose(row["sum_aper_area"], 15 * (u.pix * u.pix)) # 3 (w) x 5 (h)
assert_allclose(row["mean"], 3.236648941040039 * flux_unit)
assert_quantity_allclose(row["slice"], 4.894499866699333 * u.um)


def test_cubeviz_aperphot_cube_orig_flux_mjysr(cubeviz_helper, spectrum1d_cube_custom_fluxunit):
Expand Down Expand Up @@ -182,3 +181,4 @@ def test_cubeviz_aperphot_cube_orig_flux_mjysr(cubeviz_helper, spectrum1d_cube_c
assert_allclose(row["pixarea_tot"], 2.350443053909789e-13 * u.sr)
assert_allclose(row["aperture_sum_mag"], 23.72476627732448 * u.mag)
assert_allclose(row["mean"], 5 * (u.MJy / u.sr))
assert_quantity_allclose(row["slice"], 0.46236 * u.um)
25 changes: 20 additions & 5 deletions jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class SimpleAperturePhotometry(PluginTemplateMixin, ApertureSubsetSelectMixin,
fit_radial_profile = Bool(False).tag(sync=True)
fit_results = List().tag(sync=True)

cube_slice = Any(0).tag(sync=True) # Cubeviz only
# Cubeviz only
cube_slice = Any(0).tag(sync=True)
is_cube = Bool(False).tag(sync=True)

icon_radialtocheck = Unicode(read_icon(os.path.join(ICON_DIR, 'radialtocheck.svg'), 'svg+xml')).tag(sync=True) # noqa
icon_checktoradial = Unicode(read_icon(os.path.join(ICON_DIR, 'checktoradial.svg'), 'svg+xml')).tag(sync=True) # noqa
Expand Down Expand Up @@ -139,6 +141,17 @@ def _on_slice_changed(self, msg):
if self.config != "cubeviz":
return
self.cube_slice = msg.slice
self._cube_wave = u.Quantity(msg.wavelength, msg.wavelength_unit)

@observe("dataset_selected")
def _on_dataset_selected_changed(self, event={}):
if self.config != "cubeviz":
return
# self.dataset might not exist when app is setting itself up.
if hasattr(self, "dataset") and self.dataset.selected_dc_item.ndim > 2:
self.is_cube = True
else:
self.is_cube = False

def _get_defaults_from_metadata(self, dataset=None):
defaults = {}
Expand Down Expand Up @@ -439,9 +452,7 @@ def calculate_photometry(self, dataset=None, aperture=None, background=None,
if hasattr(reg, 'to_pixel'):
sky_center = reg.center
if self.config == "cubeviz" and data.ndim > 2:
cube_slice_plg = self.app._jdaviz_helper.plugins["Slice"]._obj
ycenter, xcenter = w.world_to_pixel(
u.Quantity(cube_slice_plg.wavelength, cube_slice_plg.wavelength_unit), sky_center)[1] # noqa: E501
ycenter, xcenter = w.world_to_pixel(self._cube_wave, sky_center)[1]
else: # "imviz"
xcenter, ycenter = w.world_to_pixel(sky_center)
else:
Expand Down Expand Up @@ -528,7 +539,11 @@ def calculate_photometry(self, dataset=None, aperture=None, background=None,
indexes=[1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 18, 18, 18])

if self.config == "cubeviz":
phot_table.add_column(int(self.cube_slice), name="slice", index=29)
if data.ndim > 2:
slice_val = self._cube_wave
else:
slice_val = u.Quantity(np.nan, self._cube_wave.unit)
phot_table.add_column(slice_val, name="slice", index=29)

if add_to_table:
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
hint="Select the data for photometry."
/>

<div v-if='config == "cubeviz"'>
<div v-if='config == "cubeviz" && is_cube'>
<v-row class="row-no-outside-padding row-min-bottom-padding">
<v-col>
<v-text-field
Expand Down
3 changes: 2 additions & 1 deletion jdaviz/core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ def slice(self):

class SliceWavelengthUpdatedMessage(Message):
'''Message generated by the slice plugin when the selected slice and wavelength are updated'''
def __init__(self, slice, wavelength, *args, **kwargs):
def __init__(self, slice, wavelength, wavelength_unit, *args, **kwargs):
super().__init__(*args, **kwargs)
self.slice = slice
self.wavelength = wavelength
self.wavelength_unit = wavelength_unit


class SliceToolStateMessage(Message):
Expand Down

0 comments on commit f85ec71

Please sign in to comment.