Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update the mouse over units by listening to the unit-conversion plugin #2931

Merged
merged 18 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Cubeviz
- Flux units are now correct for collapsed spectra when using the sum function
when units are in per steradian. [#2873]

- Mouse over coordinates now responds to the selected surface brightness unit. [#2931]

Imviz
^^^^^

Expand Down
27 changes: 23 additions & 4 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from jdaviz.configs.mosviz.plugins.viewers import (MosvizImageView, MosvizProfileView,
MosvizProfile2DView)
from jdaviz.configs.specviz.plugins.viewers import SpecvizProfileView
from jdaviz.core.events import ViewerAddedMessage
from jdaviz.core.events import ViewerAddedMessage, GlobalDisplayUnitChanged
from jdaviz.core.helpers import data_has_valid_wcs
from jdaviz.core.marks import PluginScatter, PluginLine
from jdaviz.core.registries import tool_registry
from jdaviz.core.template_mixin import TemplateMixin, DatasetSelectMixin
from jdaviz.utils import _eqv_pixar_sr
from jdaviz.utils import _eqv_pixar_sr, _convert_surface_brightness_units

__all__ = ['CoordsInfo']

Expand All @@ -34,7 +34,9 @@ class CoordsInfo(TemplateMixin, DatasetSelectMixin):

_viewer_classes_with_marker = (SpecvizProfileView, MosvizProfile2DView)

dataset_icon = Unicode("").tag(sync=True) # option for layer (auto, none, or specific layer)
dataset_icon = Unicode("").tag(
sync=True
) # option for layer (auto, none, or specific layer)
icon = Unicode("").tag(sync=True) # currently exposed layer

row1a_title = Unicode("").tag(sync=True)
Expand All @@ -56,6 +58,7 @@ def __init__(self, *args, **kwargs):
self._marks = {}
self._dict = {} # dictionary representation of current mouseover info
self._x, self._y = None, None # latest known cursor positions
self.current_unit = None
haticekaratay marked this conversation as resolved.
Show resolved Hide resolved

# subscribe/unsubscribe to mouse events across all existing viewers
viewer_refs = []
Expand All @@ -73,6 +76,9 @@ def __init__(self, *args, **kwargs):

# subscribe to mouse events on any new viewers
self.hub.subscribe(self, ViewerAddedMessage, handler=self._on_viewer_added)
self.hub.subscribe(
haticekaratay marked this conversation as resolved.
Show resolved Hide resolved
self, GlobalDisplayUnitChanged, handler=self._on_global_display_unit_changed
)

def _create_marks_for_viewer(self, viewer, id=None):
if id is None:
Expand Down Expand Up @@ -112,6 +118,12 @@ def _create_viewer_callbacks(self, viewer):
def _on_viewer_added(self, msg):
self._create_viewer_callbacks(self.app.get_viewer_by_id(msg.viewer_id))

def _on_global_display_unit_changed(self, msg):
new_unit = u.Unit(msg.unit)
uc = self.app._jdaviz_helper.plugins.get("Unit Conversion", None)
if self.config == "cubeviz" and new_unit in uc.sb_unit.choices:
self.current_unit = new_unit

@property
def marks(self):
"""
Expand Down Expand Up @@ -458,7 +470,14 @@ def _image_viewer_update(self, viewer, x, y):
elif isinstance(viewer, CubevizImageView):
arr = image.get_component(attribute).data
unit = image.get_component(attribute).units
value = self._get_cube_value(image, arr, x, y, viewer)
value = self._get_cube_value(
image, arr, x, y, viewer
)
if image.ndim != 2 and self.current_unit is not None:
value = _convert_surface_brightness_units(
value, unit, self.current_unit
)
unit = self.current_unit

haticekaratay marked this conversation as resolved.
Show resolved Hide resolved
if associated_dq_layers is not None:
associated_dq_layer = associated_dq_layers[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,44 @@ def test_sb_unit_conversion(cubeviz_helper):
uc_plg.sb_unit = 'Jy / sr'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)
assert y_display_unit == u.Jy / u.sr
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
)
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+06 Jy / sr",
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

# Try a second conversion
uc_plg.sb_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)")

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)",
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

# really a translation test, test_unit_translation loads a Flux
# cube, this test load a Surface Brightness Cube, this ensures
# two-way translation
uc_plg.sb_unit = 'MJy / sr'
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+00 MJy / sr",
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

uc_plg._obj.flux_or_sb_selected = 'Flux'
uc_plg.flux_unit = 'MJy'
Expand Down
6 changes: 6 additions & 0 deletions jdaviz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ def flux_conversion(spec, values, original_units, target_units):
return (values * orig_units).to_value(targ_units, equivalencies=eqv)


def _convert_surface_brightness_units(data, from_unit, to_unit):
pllim marked this conversation as resolved.
Show resolved Hide resolved
quantity = data * u.Unit(from_unit)
converted_quantity = quantity.to(u.Unit(to_unit))
return converted_quantity.value
haticekaratay marked this conversation as resolved.
Show resolved Hide resolved


def _eqv_pixar_sr(pixar_sr):
def converter_flux(x): # Surface Brightness -> Flux
return x * pixar_sr
Expand Down