Skip to content

Commit

Permalink
,
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Nov 14, 2024
1 parent 3022d41 commit 66243ef
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 66 deletions.
8 changes: 8 additions & 0 deletions jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def calculate_moment(self, add_data=True):

# Check to make sure API use hasn't put us into an invalid state.

self.hub.broadcast(SnackbarMessage('in calculate moment', sender=self, color='warning'))

try:
n_moment = int(self.n_moment)
if n_moment < 0:
Expand Down Expand Up @@ -322,21 +324,27 @@ def calculate_moment(self, add_data=True):
# convert units for moment 0, which is the only currently supported
# moment for using converted units.
if n_moment == 0:
self.hub.broadcast(SnackbarMessage('in moment == 0', sender=self, color='warning'))

# multiply out spectral axis unit so we are converting between
# surface brightnesses only
x_unit = u.Unit(self.spectrum_viewer.state.x_display_unit)
self.hub.broadcast(SnackbarMessage(f'x_unit={x_unit}', sender=self, color='warning'))

# only flux<> flux conversions will occur, so we only
# need the spectral_density equivalency. should I just
# be using the mean wavelength here?
eqv = u.spectral_density(np.mean(slab.spectral_axis))


self.hub.broadcast(SnackbarMessage(f'self.moment.unit before={self.moment.unit}', sender=self, color='warning'))
self.moment = flux_conversion_general(self.moment.value,
u.Unit(self.moment.unit) / x_unit,
u.Unit(self.moment_zero_unit) / x_unit,
eqv, with_unit=True)
self.hub.broadcast(SnackbarMessage(f'self.moment.unit after={self.moment.unit}', sender=self, color='warning'))
self.moment *= x_unit
self.hub.broadcast(SnackbarMessage(f'self.moment.unit * x_unit r={self.moment.unit}', sender=self, color='warning'))



Expand Down
7 changes: 4 additions & 3 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,13 @@ def _image_viewer_update(self, viewer, x, y):
unit = u.Unit(unit) # native data unit
image_unit = u.Unit(self.image_unit) # display unit, maybe a misleading name
if self.image_unit is not None and (physical_type in valid_types) and (unit != image_unit):
self.hub.broadcast(SnackbarMessage('converting in coords_info', sender=self, color='warning'))
# convert native data unit to display unit, if necessary
pixar_sr = self.app.data_collection[0].meta.get('PIXAR_SR', 1)
cube_wave = viewer.slice_value * u.Unit(self.app._get_display_unit('spectral'))
value = flux_conversion_general(value, unit, image_unit,
flux_uc_equivs(pixar_sr, cube_wave),
with_unit=False)
# value = flux_conversion_general(value, unit, image_unit,
# flux_uc_equivs(pixar_sr, cube_wave),
# with_unit=False)

if associated_dq_layers is not None:
associated_dq_layer = associated_dq_layers[0]
Expand Down
14 changes: 9 additions & 5 deletions jdaviz/core/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
from jdaviz.core.events import (SliceToolStateMessage, LineIdentifyMessage,
SpectralMarksChangedMessage,
RedshiftMessage)
from jdaviz.core.unit_conversion_utils import _eqv_pixar_sr, _eqv_flux_to_sb_pixel
from jdaviz.utils import flux_conversion_general
from jdaviz.core.unit_conversion_utils import (_eqv_pixar_sr,
_eqv_flux_to_sb_pixel,
flux_conversion_general)


__all__ = ['OffscreenLinesMarks', 'BaseSpectrumVerticalLine', 'SpectralLine',
'SliceIndicatorMarks', 'ShadowMixin', 'ShadowLine', 'ShadowLabelFixedY',
Expand All @@ -37,11 +39,13 @@ def __init__(self, viewer):
handler=self._update_counts)

self.left = Label(text=[''], x=[0.02], y=[0.8],
scales={'x': LinearScale(min=0, max=1), 'y': LinearScale(min=0, max=1)},
scales={'x': LinearScale(min=0, max=1),
'y': LinearScale(min=0, max=1)},
colors=['gray'], default_size=12,
align='start')
self.right = Label(text=[''], x=[0.98], y=[0.8],
scales={'x': LinearScale(min=0, max=1), 'y': LinearScale(min=0, max=1)},
scales={'x': LinearScale(min=0, max=1),
'y': LinearScale(min=0, max=1)},
colors=['gray'], default_size=12,
align='end')

Expand Down Expand Up @@ -128,7 +132,7 @@ def set_y_unit(self, unit=None):

if self.yunit is not None and not np.all([s == 0 for s in self.y.shape]):
if self.viewer.default_class is Spectrum1D:
# used to obtain spectral density equivalencies with previous data and units

eqv = u.spectral_density(self.x*self.xunit)

spec = self.viewer.state.reference_data.get_object(cls=Spectrum1D)
Expand Down
3 changes: 2 additions & 1 deletion jdaviz/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

from jdaviz.core.custom_units import PIX2, SPEC_PHOTON_FLUX_DENSITY_UNITS
from jdaviz.core.unit_conversion_utils import (combine_flux_and_angle_units,
flux_conversion_general
_eqv_flux_to_sb_pixel,
_eqv_pixar_sr)
from jdaviz.utils import (alpha_index, download_uri_to_path, flux_conversion_general,
from jdaviz.utils import (alpha_index, download_uri_to_path,
flux_conversion, _indirect_conversion)

PHOTUTILS_LT_1_12_1 = not minversion(photutils, "1.12.1.dev")
Expand Down
59 changes: 2 additions & 57 deletions jdaviz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

__all__ = ['SnackbarQueue', 'enable_hot_reloading', 'bqplot_clear_figure',
'standardize_metadata', 'ColorCycler', 'alpha_index', 'get_subset_type',
'download_uri_to_path', 'flux_conversion', 'flux_conversion_general',
'download_uri_to_path', 'flux_conversion',
'handle_squared_conversions', 'spectral_axis_conversion',
'layer_is_2d', 'layer_is_2d_or_3d', 'layer_is_image_data', 'layer_is_wcs_only',
'get_wcs_only_layer_labels', 'get_top_layer_index', 'get_reference_image_data',
Expand Down Expand Up @@ -342,62 +342,7 @@ def indirect_units():
]

return units


def flux_conversion_general(values, original_unit, target_unit,
equivalencies=None, with_unit=True):
"""
This function converts `values` from `original_unit` to `target_unit` using the
provided `equivalencies` and accounting for special cases where unit converison is
not directly possible (e.g when u.spectral_density is needed and units are in surface
brightness per square pixel). Unit conversions in plugins should be passed through
this function rather than using astropy's unit.to() because this function handles
the special cases that might be encountered.
Note: This is essentially a simplified version of the function utils.flux_conversion.
The difference is that all required equivalencies must be passed in (rather than being
generated from an input spectrum or slice value, and combined with input equivalences).
I didn't want to replace calls to that function entirely because there is some extra
logic there that i'm unsure of, but in theory we could/should.
"""
if original_unit == target_unit:
if not with_unit:
return (values * original_unit).value
return values * original_unit

solid_angle_in_orig = check_if_unit_is_per_solid_angle(original_unit, return_unit=True)
solid_angle_in_targ = check_if_unit_is_per_solid_angle(target_unit, return_unit=True)

with u.set_enabled_equivalencies(equivalencies):

# first possible case we want to catch before trying to translate: both
# the original and target unit are per-pixel-squared SB units
# and also require an additional equivalency, so we need to multiply out
# the pix2 before conversion and re-apply. if this doesn't work, something else
# is going on (missing equiv, etc)
if solid_angle_in_orig == solid_angle_in_targ == PIX2:
converted_values = (values * original_unit * PIX2).to(target_unit * PIX2)
converted_values = converted_values / PIX2 # re-apply pix2 unit
else:
try:
# if units can be converted straight away with provided
# equivalencies, return converted values
converted_values = (values * original_unit).to(target_unit)
except UnitConversionError:
# the only other case where units with the correct equivs wouldn't
# convert directly is if one unit is a flux and one is a sb and
# they also require an additional equivalency
if not bool(solid_angle_in_targ) == bool(solid_angle_in_orig):
converted_values = (values * original_unit * (solid_angle_in_orig or 1)).to(target_unit * (solid_angle_in_targ or 1)) # noqa
converted_values = (converted_values / (solid_angle_in_orig or 1)).to(target_unit) # noqa
else:
raise UnitConversionError(f'Could not convert {original_unit} to {target_unit} with provided equivalencies.') # noqa

if not with_unit:
return converted_values.value
return converted_values



def flux_conversion(values, original_units, target_units, spec=None, eqv=None, slice=None):
"""
Expand Down

0 comments on commit 66243ef

Please sign in to comment.