Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pllim committed Oct 1, 2024
1 parent 9978c60 commit 913f80e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_basic_unit_conversions(cubeviz_helper, angle_unit):
# load cube with flux units of MJy
w, wcs_dict = cubeviz_wcs_dict()
flux = np.zeros((30, 20, 3001), dtype=np.float32)
cube = Spectrum1D(flux=flux * u.MJy / angle_unit, wcs=w, meta=wcs_dict)
cube = Spectrum1D(flux=flux * (u.MJy / angle_unit), wcs=w, meta=wcs_dict)
cubeviz_helper.load_data(cube, data_label="test")

# get all available flux units for translation. Since cube is loaded
Expand Down
25 changes: 18 additions & 7 deletions jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
SubsetSelect, ApertureSubsetSelectMixin,
TableMixin, PlotMixin, MultiselectMixin, with_spinner)
from jdaviz.core.validunits import check_if_unit_is_per_solid_angle
from jdaviz.utils import PRIHDR_KEY
from jdaviz.utils import PRIHDR_KEY, _eqv_flux_to_sb_pixel, _eqv_pixar_sr

__all__ = ['SimpleAperturePhotometry']

Expand Down Expand Up @@ -205,15 +205,26 @@ def _on_display_units_changed(self, event={}):
# convert background to new unit
if self.background_value is not None:

prev_unit = u.Unit(prev_display_unit)
new_unit = u.Unit(self.display_unit)
# FIXME: Kinda hacky, better solution welcomed.
# Basically we want to forget about PIX2 and do normal conversion.
# Since traitlet does not keep unit, we do not need to reapply PIX2.
if "pix2" in prev_display_unit and "pix2" in self.display_unit:
prev_unit = u.Unit(prev_display_unit) * PIX2
new_unit = u.Unit(self.display_unit) * PIX2
else:
prev_unit = u.Unit(prev_display_unit)
new_unit = u.Unit(self.display_unit)

bg = self.background_value * prev_unit

# will need to add additonal custom equiv here once
# pix2<>angle is enabled
self.background_value = bg.to_value(
new_unit, u.spectral_density(self._cube_wave))
# TODO: Do we support multi-select here?
with u.set_enabled_equivalencies(
u.spectral() + u.spectral_density(self._cube_wave) +
_eqv_flux_to_sb_pixel() +
_eqv_pixar_sr(self.dataset.selected_dc_item.meta.get(
'_pixel_scale_factor', 1))):
self.background_value = bg.to_value(new_unit)


# convert flux scaling to new unit
if self.flux_scaling is not None:
Expand Down
27 changes: 22 additions & 5 deletions jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from astropy import units as u
from contextlib import nullcontext
from functools import cached_property

from astropy import units as u
from glue.core.subset_group import GroupedSubset
from glue_jupyter.bqplot.image import BqplotImageView
from specutils import Spectrum1D
from traitlets import List, Unicode, observe, Bool

from jdaviz.configs.default.plugins.viewers import JdavizProfileView
from jdaviz.core.custom_units import PIX2
from jdaviz.core.events import GlobalDisplayUnitChanged, AddDataMessage
from jdaviz.core.events import GlobalDisplayUnitChanged, AddDataMessage, SliceValueUpdatedMessage
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin, UnitSelectPluginComponent,
SelectPluginComponent, PluginUserApi)
Expand All @@ -16,6 +18,7 @@
check_if_unit_is_per_solid_angle,
create_angle_equivalencies_list,
supported_sq_angle_units)
from jdaviz.utils import _eqv_flux_to_sb_pixel, _eqv_pixar_sr

__all__ = ['UnitConversion']

Expand Down Expand Up @@ -121,6 +124,8 @@ def __init__(self, *args, **kwargs):

self.session.hub.subscribe(self, AddDataMessage,
handler=self._on_add_data_to_viewer)
self.session.hub.subscribe(self, SliceValueUpdatedMessage,
handler=self._on_slice_changed)

self.has_spectral = self.config in ('specviz', 'cubeviz', 'specviz2d', 'mosviz')
self.spectral_unit = UnitSelectPluginComponent(self,
Expand Down Expand Up @@ -278,6 +283,11 @@ def _on_add_data_to_viewer(self, msg):
self._handle_attribute_display_unit(self.sb_unit_selected, layers=layers)
self._clear_cache('image_layers')

def _on_slice_changed(self, msg):
if self.config != "cubeviz":
return
self._cube_wave = u.Quantity(msg.value, msg.value_unit)

@observe('spectral_unit_selected', 'flux_unit_selected',
'angle_unit_selected', 'sb_unit_selected',
'time_unit_selected')
Expand Down Expand Up @@ -379,6 +389,13 @@ def _handle_attribute_display_unit(self, attr_unit, layers=None):
or u.Unit(layer.layer.get_component("flux").units).physical_type != 'surface brightness'): # noqa
continue
if hasattr(layer.state, 'attribute_display_unit'):
layer.state.attribute_display_unit = _valid_glue_display_unit(attr_unit,
layer,
'attribute')
if self.config == "cubeviz":
ctx = u.set_enabled_equivalencies(
u.spectral() + u.spectral_density(self._cube_wave) +
_eqv_flux_to_sb_pixel() +
_eqv_pixar_sr(layer.layer.meta.get('_pixel_scale_factor', 1)))
else:
ctx = nullcontext()
with ctx:
layer.state.attribute_display_unit = _valid_glue_display_unit(
attr_unit, layer, 'attribute')
7 changes: 5 additions & 2 deletions jdaviz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,11 @@ def flux_conversion(values, original_units, target_units, spec=None, eqv=None, s
# the unit of the data collection item object, could be flux or surface brightness
spec_unit = str(spec.flux.unit)

# Need this for setting the y-limits
eqv = u.spectral_density(spectral_values)
# Need this for setting the y-limits but values from viewer might be downscaled
if len(values) == spectral_values.size:
eqv = u.spectral_density(spectral_values)
else:
eqv = u.spectral_density(spec.spectral_axis[0])
elif slice is not None and eqv:
image_data = True
# Need this to convert Flux to Flux for complex conversions/translations of cube image data
Expand Down

0 comments on commit 913f80e

Please sign in to comment.