Skip to content

Commit

Permalink
fix lingering issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Sep 9, 2024
1 parent 174b3d0 commit c62565f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def test_register_cube_model(cubeviz_helper, spectrum1d_cube):
assert test_label in cubeviz_helper.app.data_collection


@pytest.mark.skip()
def test_fit_cube_no_wcs(cubeviz_helper):
# This is like when user do something to a cube outside of Jdaviz
# and then load it back into a new instance of Cubeviz for further analysis.
Expand Down
21 changes: 16 additions & 5 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from jdaviz.core.marks import PluginScatter, PluginLine
from jdaviz.core.registries import tool_registry
from jdaviz.core.template_mixin import TemplateMixin, DatasetSelectMixin
from jdaviz.core.validunits import check_if_unit_is_per_solid_angle
from jdaviz.utils import flux_conversion, _eqv_pixar_sr

__all__ = ['CoordsInfo']
Expand Down Expand Up @@ -124,8 +125,8 @@ def _on_viewer_added(self, msg):

def _on_global_display_unit_changed(self, msg):

# even if data loaded is in 'flux' it can be represented as a
# per-pixel sb unit, so all cube data will be 'sb' (cubeviz)
# all cubes are converted to surface brightness so we just need to
# listen to SB for cubeviz unit changes
if msg.axis == "sb":
self.image_unit = u.Unit(msg.unit)

Expand Down Expand Up @@ -486,9 +487,18 @@ def _image_viewer_update(self, viewer, x, y):
image, arr, x, y, viewer
)

# We don't want to convert for things like moment maps
if str(u.Unit(unit).physical_type) not in ("spectral flux density",
"surface brightness"):
# We don't want to convert for things like moment maps, so check physical type
# If unit is flux per pix2, the type will be 'unknown' rather
# than surface brightness, so have to multiply the pix2 part out
# and check if the numerator is a spectral flux density
if check_if_unit_is_per_solid_angle(unit, return_unit=True) == u.pix*u.pix:
un = u.Unit(unit) * u.pix*u.pix
physical_type = un.physical_type
else:
physical_type = u.Unit(unit).physical_type

if str(physical_type) not in ("spectral flux density",
"surface brightness"):
skip_spectral_density_eqv = True

if self.image_unit is not None and not skip_spectral_density_eqv:
Expand All @@ -504,6 +514,7 @@ def _image_viewer_update(self, viewer, x, y):
unit = self.image_unit

elif self.image_unit.is_equivalent(unit):

value = (value * u.Unit(unit)).to_value(u.Unit(self.image_unit))
unit = self.image_unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def test_non_stddev_uncertainty(specviz_helper):
)


def test_unit_translation(cubeviz_helper):
@pytest.mark.parametrize("angle_unit", [u.sr, u.pix*u.pix])
def test_unit_translation(cubeviz_helper, angle_unit):
# custom cube so PIXAR_SR is in metadata, and Flux units, and in MJy
wcs_dict = {"CTYPE1": "WAVE-LOG", "CTYPE2": "DEC--TAN", "CTYPE3": "RA---TAN",
"CRVAL1": 4.622e-7, "CRVAL2": 27, "CRVAL3": 205,
Expand All @@ -137,7 +138,7 @@ def test_unit_translation(cubeviz_helper):
w = WCS(wcs_dict)
flux = np.zeros((30, 20, 3001), dtype=np.float32)
flux[5:15, 1:11, :] = 1
cube = Spectrum1D(flux=flux * u.MJy / u.sr, wcs=w, meta=wcs_dict)
cube = Spectrum1D(flux=flux * u.MJy / angle_unit, wcs=w, meta=wcs_dict)
cubeviz_helper.load_data(cube, data_label="test")

center = PixCoord(5, 10)
Expand Down Expand Up @@ -167,14 +168,18 @@ def test_unit_translation(cubeviz_helper):
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

# check if units translated
assert y_display_unit == u.MJy / u.sr
assert y_display_unit == u.MJy / angle_unit

# get_data(use_display_units=True) should return surface brightness-like units
assert cubeviz_helper.app._get_display_unit('spectral_y') == u.MJy / u.sr
assert cubeviz_helper.get_data('Spectrum (sum)', use_display_units=True).unit == u.MJy / u.sr
assert cubeviz_helper.app._get_display_unit('spectral_y') == u.MJy / angle_unit
assert cubeviz_helper.get_data('Spectrum (sum)', use_display_units=True).unit == u.MJy / angle_unit # noqa


def test_sb_unit_conversion(cubeviz_helper):
@pytest.mark.parametrize("angle_unit", [u.sr, u.pix*u.pix])
def test_sb_unit_conversion(cubeviz_helper, angle_unit):

angle_str = angle_unit.to_string()

# custom cube to have Surface Brightness units
wcs_dict = {"CTYPE1": "WAVE-LOG", "CTYPE2": "DEC--TAN", "CTYPE3": "RA---TAN",
"CRVAL1": 4.622e-7, "CRVAL2": 27, "CRVAL3": 205,
Expand All @@ -183,7 +188,7 @@ def test_sb_unit_conversion(cubeviz_helper):
w = WCS(wcs_dict)
flux = np.zeros((30, 20, 3001), dtype=np.float32)
flux[5:15, 1:11, :] = 1
cube = Spectrum1D(flux=flux * (u.MJy / u.sr), wcs=w, meta=wcs_dict)
cube = Spectrum1D(flux=flux * (u.MJy / angle_unit), wcs=w, meta=wcs_dict)
cubeviz_helper.load_data(cube, data_label="test")

uc_plg = cubeviz_helper.plugins['Unit Conversion']
Expand All @@ -203,7 +208,7 @@ def test_sb_unit_conversion(cubeviz_helper):
# Surface Brightness conversion
uc_plg.flux_unit = 'Jy'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)
assert y_display_unit == u.Jy / u.sr
assert y_display_unit == u.Jy / angle_unit
label_mouseover = cubeviz_helper.app.session.application._tools["g-coords-info"]
flux_viewer = cubeviz_helper.app.get_viewer(
cubeviz_helper._default_flux_viewer_reference_name
Expand All @@ -212,21 +217,28 @@ def test_sb_unit_conversion(cubeviz_helper):
flux_viewer, {"event": "mousemove", "domain": {"x": 10, "y": 8}}
)
assert label_mouseover.as_text() == (
"Pixel x=00010.0 y=00008.0 Value +1.00000e+06 Jy / sr",
f"Pixel x=00010.0 y=00008.0 Value +1.00000e+06 Jy / {angle_str}",
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

# Try a second conversion
uc_plg.flux_unit = 'W / Hz m2'

if angle_unit == u.pix * u.pix: # unit string order is different for pix2 vs sr
str_unit = 'W / (Hz m2 pix2)'
elif angle_unit == u.sr:
str_unit = 'W / (Hz sr m2)'

y_display_unit = u.Unit(viewer_1d.state.y_display_unit)
assert y_display_unit == u.Unit("W / (Hz sr m2)")
assert y_display_unit == u.Unit(str_unit)

y_display_unit = u.Unit(viewer_1d.state.y_display_unit)
label_mouseover._viewer_mouse_event(
flux_viewer, {"event": "mousemove", "domain": {"x": 10, "y": 8}}
)

assert label_mouseover.as_text() == (
"Pixel x=00010.0 y=00008.0 Value +1.00000e-20 W / (Hz sr m2)",
f"Pixel x=00010.0 y=00008.0 Value +1.00000e-20 {str_unit}",
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

Expand All @@ -239,7 +251,7 @@ def test_sb_unit_conversion(cubeviz_helper):
flux_viewer, {"event": "mousemove", "domain": {"x": 10, "y": 8}}
)
assert label_mouseover.as_text() == (
"Pixel x=00010.0 y=00008.0 Value +1.00000e+00 MJy / sr",
f"Pixel x=00010.0 y=00008.0 Value +1.00000e+00 MJy / {angle_str}",
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

Expand Down
4 changes: 3 additions & 1 deletion jdaviz/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def test_spec_sb_flux_conversion():
returned_values, return_units, unit_flag = _indirect_conversion(
values=values, orig_units=(u.MJy),
targ_units=(u.ph / (u.s * u.cm**2 * u.Hz * solid_angle)), # noqa
eqv=eqv, spec_unit=spec_unit, image_data=None
eqv=eqv,
spec_unit=spec_unit,
image_data=None
)
assert_allclose(returned_values, target_values)
assert (return_units == expected_units)
Expand Down

0 comments on commit c62565f

Please sign in to comment.