Skip to content

Commit

Permalink
Merge pull request #2131 from kecnry/display-units-slice
Browse files Browse the repository at this point in the history
display units: cubeviz/slice plugin
  • Loading branch information
kecnry authored Apr 10, 2023
2 parents 72aafdc + d3eb782 commit 0e7f608
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
30 changes: 24 additions & 6 deletions jdaviz/configs/cubeviz/plugins/slice/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import warnings

import numpy as np
import astropy.units as u
from astropy.units import UnitsWarning
from glue_jupyter.bqplot.image import BqplotImageView
from glue_jupyter.bqplot.profile import BqplotProfileView
from traitlets import Bool, Float, observe, Any, Int
from specutils.spectra.spectrum1d import Spectrum1D

from jdaviz.core.events import AddDataMessage, SliceToolStateMessage, SliceSelectSliceMessage
from jdaviz.core.events import (AddDataMessage, SliceToolStateMessage,
SliceSelectSliceMessage, GlobalDisplayUnitChanged)
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import PluginTemplateMixin
from jdaviz.core.user_api import PluginUserApi
Expand Down Expand Up @@ -83,11 +85,19 @@ def __init__(self, *args, **kwargs):
self.session.hub.subscribe(self, AddDataMessage,
handler=self._on_data_added)

# update internal wavelength when x display unit is changed (preserving slice)
self.session.hub.subscribe(self, GlobalDisplayUnitChanged,
handler=self._on_global_display_unit_changed)

@property
def user_api(self):
return PluginUserApi(self, expose=('slice', 'wavelength',
'show_indicator', 'show_wavelength'))

@property
def slice_indicator(self):
return self.spectrum_viewer.slice_indicator

def _watch_viewer(self, viewer, watch=True):
if isinstance(viewer, BqplotImageView):
if watch and viewer not in self._watched_viewers:
Expand Down Expand Up @@ -125,11 +135,7 @@ def _update_reference_data(self, reference_data):
self._update_data(reference_data.get_object(cls=Spectrum1D).spectral_axis)

def _update_data(self, x_all):
if hasattr(x_all, 'unit'):
self.wavelength_unit = str(x_all.unit)
x_all = x_all.value

self._x_all = x_all
self._x_all = x_all.value

if self.wavelength == -1:
if len(x_all):
Expand All @@ -142,6 +148,9 @@ def _update_data(self, x_all):
# force wavelength to update from the current slider value
self._on_slider_updated({'new': self.slice})

# update data held inside slice indicator and force reverting to original active status
self.slice_indicator._update_data(x_all)

def _viewer_slices_changed(self, value):
# the slices attribute on the viewer state was changed,
# so we'll update the slider to match which will trigger
Expand All @@ -157,6 +166,15 @@ def _on_select_slice_message(self, msg):
warnings.simplefilter('ignore', category=UnitsWarning)
self.slice = msg.slice

def _on_global_display_unit_changed(self, msg):
if msg.axis != 'spectral':
return
prev_unit = self.wavelength_unit
self.wavelength_unit = msg.unit.to_string()
if self._x_all is None or prev_unit == '':
return
self._update_data((self._x_all * u.Unit(prev_unit)).to(msg.unit, u.spectral()))

@observe('wavelength')
def _on_wavelength_updated(self, event):
# convert to float (JS handles stripping any invalid characters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class UnitConversion(PluginTemplateMixin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

if self.config not in ['specviz']:
if self.config not in ['specviz', 'cubeviz']:
# TODO [specviz2d, mosviz] x_display_unit is not implemented in glue for image viewer
# used by spectrum-2d-viewer
# TODO [mosviz]: add to yaml file
Expand Down
2 changes: 2 additions & 0 deletions jdaviz/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ def _handle_display_units(data, use_display_units):
if use_display_units:
if isinstance(data, Spectrum1D):
spectral_unit = self.app._get_display_unit('spectral')
if not spectral_unit:
return data
flux_unit = self.app._get_display_unit('flux')
# TODO: any other attributes (meta, wcs, etc)?
# TODO: implement uncertainty.to upstream
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/core/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _update_colors_opacities(self):
self.colors = ["#c75109" if self._active else "#007BA1"]
self.opacities = [1.0 if self._active else 0.9]

def _on_change_state(self, msg):
def _on_change_state(self, msg={}):
if isinstance(msg, dict):
changes = msg
else:
Expand Down

0 comments on commit 0e7f608

Please sign in to comment.