Skip to content

Commit

Permalink
WIP: update to use upstream generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Oct 17, 2024
1 parent 1b794ca commit 1e8bd93
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
86 changes: 70 additions & 16 deletions lcviz/plugins/unit_conversion/unit_conversion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from traitlets import List, Unicode

from jdaviz.core.template_mixin import UnitSelectPluginComponent
from jdaviz.configs.specviz.plugins import UnitConversion
from jdaviz.configs.specviz.plugins.unit_conversion.unit_conversion import (UnitConversion,
_valid_glue_display_unit, # noqa
_flux_to_sb_unit)
from jdaviz.core.events import GlobalDisplayUnitChanged
from jdaviz.core.registries import tray_registry

from lcviz.viewers import (CubeView, TimeScatterView)

__all__ = ['UnitConversion']


Expand All @@ -21,18 +23,14 @@ class UnitConversion(UnitConversion):
* :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.open_in_tray`
* :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.close_in_tray`
"""
time_unit_items = List().tag(sync=True)
time_unit_selected = Unicode().tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.docs_link = f"https://lcviz.readthedocs.io/en/{self.vdocs}/plugins.html#unit-conversion"
self.docs_description = "Choose units for the time and flux axes."
self.disabled_msg = "" # otherwise disabled upstream - remove once upstream no longer has a config check

self.time_unit = UnitSelectPluginComponent(self,
items='time_unit_items',
selected='time_unit_selected')
self.has_time = True
self.has_flux = True

@property
def spectrum_viewer(self):
Expand All @@ -44,10 +42,66 @@ def spectrum_viewer(self):

return self.app.get_viewer(viewer_reference)

@property
def user_api(self):
api = super().user_api
expose = ['time_unit', 'flux_unit']
api._expose = expose
def _on_add_data_to_viewer(self, msg):
viewer = msg.viewer

if (not len(self.time_unit_selected) or not len(self.flux_unit_selected)):
# TODO: default based on the native units of the data (LC or TPF)
self.time_unit.choices = ['d', 'hr', 'min', 's']
self.time_unit_selected = 'd'
self.flux_unit.choices = ['erg / (Angstrom cm2 s)']
self.flux_unit_selected = 'erg / (Angstrom cm2 s)'
# setting default values will trigger the observes to set the units
# in _on_unit_selected, so return here to avoid setting twice
return

# TODO: when enabling unit-conversion in rampviz, this may need to be more specific
# or handle other cases for ramp profile viewers
if isinstance(viewer, TimeScatterView):
if (viewer.state.x_display_unit == self.time_unit_selected
and viewer.state.y_display_unit == self.flux_unit_selected):
# data already existed in this viewer and display units were already set
return

# this spectral viewer was empty (did not have display units set yet),˜
# but global selections are available in the plugin,
# so we'll set them to the viewer here
viewer.state.x_display_unit = self.time_unit_selected
viewer.state.y_display_unit = self.flux_unit_selected

elif isinstance(viewer, CubeView):
# set the attribute display unit (contour and stretch units) for the new layer
# NOTE: this assumes that all image data is coerced to surface brightness units
layers = [lyr for lyr in msg.viewer.layers if lyr.layer.data.label == msg.data.label]
self._handle_attribute_display_unit(self.flux_unit_selected, layers=layers)
self._clear_cache('image_layers')

def _on_unit_selected(self, msg):
"""
When any user selection is made, update the relevant viewer(s) with the new unit,
and then emit a GlobalDisplayUnitChanged message to notify other plugins of the change.
"""
print("*** _on_unit_selected", msg.get('name'), msg.get('new'))
if not len(msg.get('new', '')):
# empty string, nothing to set yet
return

axis = msg.get('name').split('_')[0]

if axis == 'time':
xunit = _valid_glue_display_unit(self.time_unit.selected, self.spectrum_viewer, 'x')
# TODO: iterate over all TimeScatterViewers
self.spectrum_viewer.state.x_display_unit = xunit
self.spectrum_viewer.set_plot_axes()

elif axis == 'flux':
yunit = _valid_glue_display_unit(self.flux_unit.selected, self.spectrum_viewer, 'y')
# TODO: iterate over all Time and Phase Scatter Viewers
self.spectrum_viewer.state.y_display_unit = yunit
self.spectrum_viewer.set_plot_axes()

# TODO: handle setting surface brightness units for CubeView

return api
# axis (first) argument will be one of: time, flux
self.hub.broadcast(GlobalDisplayUnitChanged(axis,
msg.new, sender=self))
1 change: 1 addition & 0 deletions lcviz/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _reset_y_limits(self, *event):
self._reset_att_limits('y')

def reset_limits(self, *event):
# TODO: this is not working correctly when units are changed (probably the same for _reset_att_limits)
x_min, x_max = np.inf, -np.inf
y_min, y_max = np.inf, -np.inf

Expand Down
2 changes: 1 addition & 1 deletion lcviz/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def set_plot_axes(self):
def _set_plot_x_axes(self, dc, component_labels, light_curve):
self.state.x_att = dc[0].components[component_labels.index('dt')]

x_unit = self.time_unit
x_unit = self.time_unit # TODO: use get display unit instead? Or at least update self.time_unit
reference_time = light_curve.meta.get('reference_time', None)

if reference_time is not None:
Expand Down

0 comments on commit 1e8bd93

Please sign in to comment.