Skip to content

Commit

Permalink
Update Subset Tools plugin to use display units (#2195)
Browse files Browse the repository at this point in the history
* bump glue-core to necessary version for display units

* cubeviz/slice plugin unit conversion support

(we might want to generalize some of this logic more into the mark's _update_data when adding support for spectral lines as well)

* display units: fix failing tests (#2132)

* fix error raised when spectrum uncertainty is None
* fix RTD build failure because of missing mixin available in import
* fix test setting display unit now that um is preferred to micron
* add disabled unit conversion plugin to mosviz
* avoid unit-conversion error in cubeviz tests
* bump versions of glue/glue-jupyter
* fix deg intialization in cubeviz slice plugin
* add LinesAutoUnit to __all__ so RTD can find it
* add use_display_units kwarg to get_spectral_regions
* fix setting display units for unitless data
* Apply suggestions from code review

Co-authored-by: P. L. Lim <[email protected]>

* remove unit conversion plugin from all bug specviz

* for initial implementation - eventually we'll want to implement across all plugins/viewers

* Working on unit conversion handling in subset plugin

* Get subset updating working in non-default units

* Add changelog

* Update jdaviz/configs/default/plugins/subset_plugin/subset_plugin.vue

Co-authored-by: Kyle Conroy <[email protected]>

---------

Co-authored-by: Kyle Conroy <[email protected]>
Co-authored-by: P. L. Lim <[email protected]>
  • Loading branch information
3 people authored May 16, 2023
1 parent bff781b commit 15e755d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ New Features

- Histogram showing image values in stretch limits section of plot options plugin. [#2153]

- Subset Plugin now respects the chosen display unit after using Unit Conversion. [#2195]

Cubeviz
^^^^^^^

Expand Down
37 changes: 31 additions & 6 deletions jdaviz/configs/default/plugins/subset_plugin/subset_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import astropy.units as u
from glue.core.message import EditSubsetMessage, SubsetUpdateMessage
from glue.core.edit_subset_mode import (AndMode, AndNotMode, OrMode,
ReplaceMode, XorMode)
Expand All @@ -7,7 +8,7 @@
from glue_jupyter.widgets.subset_mode_vuetify import SelectionModeMenu
from traitlets import Any, List, Unicode, Bool, observe

from jdaviz.core.events import SnackbarMessage
from jdaviz.core.events import SnackbarMessage, GlobalDisplayUnitChanged
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import PluginTemplateMixin, DatasetSelectMixin, SubsetSelect

Expand Down Expand Up @@ -52,12 +53,15 @@ def __init__(self, *args, **kwargs):
handler=self._sync_selected_from_state)
self.session.hub.subscribe(self, SubsetUpdateMessage,
handler=self._on_subset_update)
self.session.hub.subscribe(self, GlobalDisplayUnitChanged,
handler=self._on_display_unit_changed)

self.subset_select = SubsetSelect(self,
'subset_items',
'subset_selected',
default_text="Create New")
self.subset_states = []
self.spectral_display_unit = None

def _sync_selected_from_state(self, *args):
if not hasattr(self, 'subset_select'):
Expand Down Expand Up @@ -127,7 +131,8 @@ def _unpack_get_subsets_for_ui(self):
-------
"""
subset_information = self.app.get_subsets(self.subset_selected, simplify_spectral=False)
subset_information = self.app.get_subsets(self.subset_selected, simplify_spectral=False,
use_display_units=True)
_around_decimals = 6 # Avoid 30 degrees from coming back as 29.999999999999996
if not subset_information:
return
Expand Down Expand Up @@ -175,10 +180,12 @@ def _unpack_get_subsets_for_ui(self):
subset_type = subset_state.roi.__class__.__name__

elif isinstance(subset_state, RangeSubsetState):
lo = subset_state.lo
hi = subset_state.hi
subset_definition = [{"name": "Lower bound", "att": "lo", "value": lo, "orig": lo},
{"name": "Upper bound", "att": "hi", "value": hi, "orig": hi}]
lo = spec['region'].lower
hi = spec['region'].upper
subset_definition = [{"name": "Lower bound", "att": "lo", "value": lo.value,
"orig": lo.value, "unit": str(lo.unit)},
{"name": "Upper bound", "att": "hi", "value": hi.value,
"orig": hi.value, "unit": str(hi.unit)}]
subset_type = "Range"
if len(subset_definition) > 0:
# Note: .append() does not work for List traitlet.
Expand All @@ -199,6 +206,13 @@ def _get_subset_definition(self, *args):

self._unpack_get_subsets_for_ui()

def _on_display_unit_changed(self, msg):
# We only care about the spectral units, since flux units don't affect spectral subsets
if msg.axis == "spectral":
self.spectral_display_unit = msg.unit
if self.subset_selected != self.subset_select.default_text:
self._get_subset_definition(self.subset_selected)

def vue_update_subset(self, *args):
for index, sub in enumerate(self.subset_definitions):
sub_states = self.subset_states[index]
Expand All @@ -207,6 +221,17 @@ def vue_update_subset(self, *args):
d_val = np.radians(d_att["value"])
else:
d_val = float(d_att["value"])

# Convert from display unit to original unit if necessary
if self.subset_types[index] == "Range":
if self.spectral_display_unit is not None:
x_att = sub_states.att
base_units = self.app.data_collection[0].get_component(x_att).units
if self.spectral_display_unit != base_units:
d_val = d_val*u.Unit(self.spectral_display_unit)
d_val = d_val.to(u.Unit(base_units))
d_val = d_val.value

if float(d_att["orig"]) != d_val:
if self.subset_types[index] == "Range":
setattr(sub_states, d_att["att"], d_val)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
v-model.number="item.value"
type="number"
:disabled="!is_editable"
:suffix="item.unit.replace('Angstrom', 'A')"
></v-text-field>
</v-row>
</div>
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies = [
"traitlets>=5.0.5",
"bqplot>=0.12.37",
"bqplot-image-gl>=1.4.11",
"glue-core>=1.10.1",
"glue-core>=1.10.0",
"glue-jupyter>=0.16.3",
"echo>=0.5.0",
"ipykernel>=6.19.4",
Expand Down

0 comments on commit 15e755d

Please sign in to comment.