Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_data(use_display_units=True) to respect flux or sb selection #3129

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
unit conversion in Cubeviz and Specviz. [#2781, #2940, #3088, #3111, #3113, #3129]

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_unit_translation(cubeviz_helper):
w = WCS(wcs_dict)
flux = np.zeros((30, 20, 3001), dtype=np.float32)
flux[5:15, 1:11, :] = 1
cube = Spectrum1D(flux=flux * u.MJy, wcs=w, meta=wcs_dict)
cube = Spectrum1D(flux=flux * u.MJy / u.sr, wcs=w, meta=wcs_dict)
cubeviz_helper.load_data(cube, data_label="test")

center = PixCoord(5, 10)
Expand All @@ -152,6 +152,10 @@ def test_unit_translation(cubeviz_helper):
# data collection item units will be used for translations.
assert uc_plg._obj.flux_or_sb_selected == 'Flux'

# accessing from get_data(use_display_units=True) should return flux-like units
assert cubeviz_helper.app._get_display_unit('spectral_y') == u.MJy
assert cubeviz_helper.get_data('Spectrum (sum)', use_display_units=True).unit == u.MJy

# to have access to display units
viewer_1d = cubeviz_helper.app.get_viewer(
cubeviz_helper._default_spectrum_viewer_reference_name)
Expand All @@ -165,6 +169,10 @@ def test_unit_translation(cubeviz_helper):
# check if units translated
assert y_display_unit == u.MJy / u.sr

# get_data(use_display_units=True) should return surface brightness-like units
assert cubeviz_helper.app._get_display_unit('spectral_y') == u.MJy / u.sr
assert cubeviz_helper.get_data('Spectrum (sum)', use_display_units=True).unit == u.MJy / u.sr


def test_sb_unit_conversion(cubeviz_helper):
# custom cube to have Surface Brightness units
Expand Down
16 changes: 8 additions & 8 deletions jdaviz/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def _handle_display_units(self, data, use_display_units=True):
spectral_unit = self.app._get_display_unit('spectral')
if not spectral_unit:
return data
flux_unit = self.app._get_display_unit('flux')
y_unit = self.app._get_display_unit('spectral_y')
# TODO: any other attributes (meta, wcs, etc)?
# TODO: implement uncertainty.to upstream
uncertainty = data.uncertainty
Expand All @@ -488,26 +488,26 @@ def _handle_display_units(self, data, use_display_units=True):
new_uncert = uncertainty
if ('_pixel_scale_factor' in data.meta):
new_uncert_converted = flux_conversion(data, new_uncert.quantity.value,
new_uncert.unit, flux_unit)
new_uncert = StdDevUncertainty(new_uncert_converted, unit=flux_unit)
new_uncert.unit, y_unit)
new_uncert = StdDevUncertainty(new_uncert_converted, unit=y_unit)
else:
new_uncert = StdDevUncertainty(new_uncert, unit=data.flux.unit)

else:
new_uncert = None
if ('_pixel_scale_factor' in data.meta):
new_flux = flux_conversion(data, data.flux.value, data.flux.unit,
flux_unit) * u.Unit(flux_unit)
new_y = flux_conversion(data, data.flux.value, data.flux.unit,
y_unit) * u.Unit(y_unit)
else:
new_flux = flux_conversion(data, data.flux.value, data.flux.unit,
data.flux.unit) * u.Unit(data.flux.unit)
new_y = flux_conversion(data, data.flux.value, data.flux.unit,
data.flux.unit) * u.Unit(data.flux.unit)
new_spec = (spectral_axis_conversion(data.spectral_axis.value,
data.spectral_axis.unit,
spectral_unit)
* u.Unit(spectral_unit))

data = Spectrum1D(spectral_axis=new_spec,
flux=new_flux,
flux=new_y,
uncertainty=new_uncert,
mask=data.mask)
else: # pragma: nocover
Expand Down
11 changes: 0 additions & 11 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2955,17 +2955,6 @@ def _get_continuum(self, dataset, spectral_subset, update_marks=False, per_pixel
raise ValueError("per-pixel only supported for cubeviz")
full_spectrum = self.app._jdaviz_helper.get_data(self.dataset.selected,
use_display_units=True)
# TODO: Something like the following code may be needed to get continuum
# with display units working
# temp_spec = self.app._jdaviz_helper.get_data(self.dataset.selected,
# use_display_units=True)
# flux_values = np.sum(np.ones_like(temp_spec.flux.value), axis=(0, 1))
# pix_scale = self.dataset.selected_dc_item.meta.get('PIXAR_SR', 1.0)
# pix_scale_factor = (flux_values * pix_scale)
# temp_spec.meta['_pixel_scale_factor'] = pix_scale_factor
# full_spectrum = self._specviz_helper._handle_display_units(temp_spec,
# use_display_units=True)

else:
full_spectrum = dataset.get_selected_spectrum(use_display_units=True)

Expand Down