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
Changes from 3 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
39 changes: 33 additions & 6 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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
Expand All @@ -22,6 +22,15 @@
__all__ = ['CoordsInfo']


def _convert_surface_brightness_units(data, from_unit, to_unit):
try:
quantity = data * u.Unit(from_unit)
converted_quantity = quantity.to(u.Unit(to_unit))
return converted_quantity.value
except u.UnitConversionError:
raise ValueError(f"Conversion from {from_unit} to {to_unit} is not possible.")
haticekaratay marked this conversation as resolved.
Show resolved Hide resolved


@tool_registry('g-coords-info')
class CoordsInfo(TemplateMixin, DatasetSelectMixin):
template_file = __file__, "coords_info.vue"
Expand All @@ -34,7 +43,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,7 +67,9 @@ 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 = "MJy/sr"
self.previous_unit = "MJy/sr"
self.unit_changed = False
# subscribe/unsubscribe to mouse events across all existing viewers
viewer_refs = []
for viewer in self.app._viewer_store.values():
Expand All @@ -73,6 +86,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 +128,14 @@ 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 new_unit != self.current_unit and new_unit in uc.sb_unit.choices:
haticekaratay marked this conversation as resolved.
Show resolved Hide resolved
self.previous_unit = self.current_unit
self.current_unit = new_unit
self.unit_changed = True

@property
def marks(self):
"""
Expand Down Expand Up @@ -457,8 +481,11 @@ def _image_viewer_update(self, viewer, x, y):
unit = image.get_component(attribute).units
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)
unit = self.current_unit
value = self._get_cube_value(
image, arr, x, y, viewer
)._convert_surface_brightness_units(value, self.previous_unit, unit)
haticekaratay marked this conversation as resolved.
Show resolved Hide resolved
self.unit_changed = False

if associated_dq_layers is not None:
associated_dq_layer = associated_dq_layers[0]
Expand All @@ -475,7 +502,7 @@ def _image_viewer_update(self, viewer, x, y):
dq_text = f' (DQ: {int(dq_value):d})'
else:
dq_text = ''
self.row1b_text = f'{value:+10.5e} {unit}{dq_text}'
self.row1b_text = f'{value:+10.5e} {self.current_unit}{dq_text}'
self._dict['value'] = float(value)
self._dict['value:unit'] = unit
self._dict['value:unreliable'] = unreliable_pixel
Expand Down
Loading