Skip to content

Commit

Permalink
ensure continuum marks don't double translate, test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen committed Oct 24, 2024
1 parent 42d05dc commit ff503d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from numpy.testing import assert_allclose
from regions import PixCoord, CirclePixelRegion, RectanglePixelRegion
from specutils import Spectrum1D
from glue.core.roi import XRangeROI

from jdaviz.core.custom_units import PIX2, SPEC_PHOTON_FLUX_DENSITY_UNITS

Expand Down Expand Up @@ -150,6 +151,7 @@ def test_flux_unit_choices(cubeviz_helper, flux_unit, expected_choices):
def test_unit_translation(cubeviz_helper, angle_unit):
# custom cube so PIXAR_SR is in metadata, and Flux units, and in MJy
w, wcs_dict = cubeviz_wcs_dict()
wcs_dict['PIXAR_SR'] = 10
flux = np.zeros((30, 20, 3001), dtype=np.float32)
flux[5:15, 1:11, :] = 1
cube = Spectrum1D(flux=flux * u.MJy / angle_unit, wcs=w, meta=wcs_dict)
Expand All @@ -175,9 +177,29 @@ def test_unit_translation(cubeviz_helper, angle_unit):
viewer_1d = cubeviz_helper.app.get_viewer(
cubeviz_helper._default_spectrum_viewer_reference_name)

la = cubeviz_helper.plugins['Line Analysis']
la.keep_active = True
viewer_1d.apply_roi(XRangeROI(6e-7, 6.2e-7))
la.spectral_subset.selected = 'Subset 2'

marks_before = [la._obj.continuum_marks['left'].y,
la._obj.continuum_marks['right'].y]

# change global y-units from Flux -> Surface Brightness
uc_plg._obj.spectral_y_type_selected = 'Surface Brightness'

if angle_unit == PIX2:
# translation does not alter spectral_y values in viewer
pixar_sr = 1
else:
pixar_sr = wcs_dict['PIXAR_SR']

marks_after = [la._obj.continuum_marks['left'].y * pixar_sr,
la._obj.continuum_marks['right'].y * pixar_sr]

# ensure continuum marks update when spectral_y_type is changed
assert_allclose(marks_before, marks_after, rtol=1e-5)

assert uc_plg._obj.spectral_y_type_selected == 'Surface Brightness'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

Expand Down
7 changes: 7 additions & 0 deletions jdaviz/core/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from glue.core import HubListener
from specutils import Spectrum1D
from jdaviz.utils import _eqv_pixar_sr, _eqv_flux_to_sb_pixel
from jdaviz.core.validunits import check_if_unit_is_per_solid_angle

from jdaviz.core.events import GlobalDisplayUnitChanged
from jdaviz.core.events import (SliceToolStateMessage, LineIdentifyMessage,
Expand Down Expand Up @@ -125,6 +126,12 @@ def set_y_unit(self, unit=None):
unit = self.viewer.state.y_display_unit
unit = u.Unit(unit)

# spectrum y-values in viewer have already been converted, don't convert again
# if a spectral_y_type is changed, just update the unit
if self.yunit is not None and check_if_unit_is_per_solid_angle(self.yunit) != check_if_unit_is_per_solid_angle(unit): # noqa
self.yunit = unit
return

if self.yunit is not None and not np.all([s == 0 for s in self.y.shape]):
if self.viewer.default_class is Spectrum1D:
# used to obtain spectral density equivalencies with previous data and units
Expand Down

0 comments on commit ff503d8

Please sign in to comment.