Skip to content

Commit

Permalink
Enable contour unit conversion (spacetelescope#3149)
Browse files Browse the repository at this point in the history
* Add Jesse's first pass code

* Debugging

* Debugging

* No longer errors but the contour labels still don't update

* Now working with glue-jupyter main, added test

* Changelog

* Codestyle

* Remove debugging print statements

* Update glue-jupyter pin

* Don't loop through all viewers if not necessary

* Skip anything not in surface brightness

* Update jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py

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

* Skip subset layers

* Codestyle

---------

Co-authored-by: Kyle Conroy <[email protected]>
  • Loading branch information
2 people authored and cshanahan1 committed Sep 6, 2024
1 parent cd20859 commit 03a4e14
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ New Features
------------

- Added flux/surface brightness translation and surface brightness
unit conversion in Cubeviz and Specviz. [#2781, #2940, #3088, #3111, #3113, #3129, #3139, #3155]
unit conversion in Cubeviz and Specviz. [#2781, #2940, #3088, #3111, #3113, #3129, #3139, #3149, #3155]

- Plugin tray is now open by default. [#2892]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,22 @@ def test_sb_unit_conversion(cubeviz_helper):

la = cubeviz_helper.plugins['Line Analysis']._obj
assert la.dataset.get_selected_spectrum(use_display_units=True)


def test_contour_unit_conversion(cubeviz_helper, spectrum1d_cube_fluxunit_jy_per_steradian):
# custom cube to have Surface Brightness units
cubeviz_helper.load_data(spectrum1d_cube_fluxunit_jy_per_steradian, data_label="test")

uc_plg = cubeviz_helper.plugins['Unit Conversion']
uc_plg.open_in_tray()

po_plg = cubeviz_helper.plugins['Plot Options']
# Make sure that the contour values get updated
po_plg.contour_visible = True

assert np.allclose(po_plg.contour_max.value, 199)

uc_plg._obj.flux_or_sb_selected = 'Surface Brightness'
uc_plg.flux_unit = 'MJy'

assert np.allclose(po_plg.contour_max.value, 1.99e-4)
40 changes: 38 additions & 2 deletions jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import numpy as np
from astropy import units as u
from glue.core.subset_group import GroupedSubset
from glue_jupyter.bqplot.image import BqplotImageView
import numpy as np
from traitlets import List, Unicode, observe, Bool

from jdaviz.core.events import GlobalDisplayUnitChanged
from jdaviz.core.events import GlobalDisplayUnitChanged, AddDataToViewerMessage
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin, UnitSelectPluginComponent,
SelectPluginComponent, PluginUserApi)
Expand Down Expand Up @@ -92,6 +94,9 @@ def __init__(self, *args, **kwargs):
self.spectrum_viewer.state.add_callback('y_display_unit',
self._on_glue_y_display_unit_changed)

self.session.hub.subscribe(self, AddDataToViewerMessage,
handler=self._find_and_convert_contour_units)

self.spectral_unit = UnitSelectPluginComponent(self,
items='spectral_unit_items',
selected='spectral_unit_selected')
Expand Down Expand Up @@ -269,13 +274,44 @@ def _on_flux_unit_changed(self, msg):
else:
self.flux_or_sb_selected = 'Surface Brightness'

# Always send a surface brightness unit to contours
if self.flux_or_sb_selected == 'Flux':
yunit = self._append_angle_correctly(yunit, self.angle_unit.selected)
self._find_and_convert_contour_units(yunit=yunit)

# for displaying message that PIXAR_SR = 1 if it is not found in the FITS header
if (
len(self.app.data_collection) > 0
and not self.app.data_collection[0].meta.get('PIXAR_SR')
):
self.pixar_sr_exists = False

def _find_and_convert_contour_units(self, msg=None, yunit=None):
if not yunit:
yunit = self.sb_unit_selected

if msg is not None:
viewers = [self.app.get_viewer(msg.viewer_reference)]
else:
viewers = self._app._viewer_store.values()

if self.angle_unit_selected is None or self.angle_unit_selected == '':
# Can't do this before the plugin is initialized completely
return

for viewer in viewers:
if not isinstance(viewer, BqplotImageView):
continue
for layer in viewer.state.layers:

# DQ layer doesn't play nicely with this attribute
if "DQ" in layer.layer.label or isinstance(layer.layer, GroupedSubset):
continue
elif u.Unit(layer.layer.get_component("flux").units).physical_type != 'surface brightness': # noqa
continue
if hasattr(layer, 'attribute_display_unit'):
layer.attribute_display_unit = yunit

def _translate(self, flux_or_sb=None):
# currently unsupported, can be supported with a scale factor
if self.app.config == 'specviz':
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"bqplot>=0.12.37",
"bqplot-image-gl>=1.4.11",
"glue-core>=1.20.0",
"glue-jupyter>=0.21.0",
"glue-jupyter>=0.22.1",
"echo>=0.5.0",
"ipykernel>=6.19.4",
"ipyvue>=1.6",
Expand Down

0 comments on commit 03a4e14

Please sign in to comment.