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

Unit conversion UI tweaks #3178

Merged
merged 3 commits into from
Sep 6, 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, #3129, #3139, #3149, #3155]
unit conversion in Cubeviz and Specviz. [#2781, #2940, #3088, #3111, #3113, #3129, #3139, #3149, #3155, #3178]

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

Expand Down
4 changes: 2 additions & 2 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,8 @@ def _get_display_unit(self, axis):
raise ValueError(f"could not find units for axis='{axis}'")
uc = self._jdaviz_helper.plugins.get('Unit Conversion')._obj
if axis == 'spectral_y':
# translate options from uc.flux_or_sb to the prefix used in uc.??_unit_selected
axis = {'Surface Brightness': 'sb', 'Flux': 'flux'}[uc.flux_or_sb_selected]
# translate options from uc.spectral_y_type to the prefix used in uc.??_unit_selected
axis = {'Surface Brightness': 'sb', 'Flux': 'flux'}[uc.spectral_y_type_selected]
try:
return getattr(uc, f'{axis}_unit_selected')
except AttributeError:
Expand Down
18 changes: 9 additions & 9 deletions jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@
self.send_state("output_unit_selected")

# either 'Flux' or 'Surface Brightness'
orig_flux_or_sb = self.output_unit_items[0]['label']
orig_spectral_y_type = self.output_unit_items[0]['label']

unit_dict = {orig_flux_or_sb: "",
unit_dict = {orig_spectral_y_type: "",
"Spectral Unit": "",
"Velocity": "km/s",
"Velocity^N": f"km{self.n_moment}/s{self.n_moment}"}
Expand All @@ -200,19 +200,19 @@
# get flux/SB units
if self.spectrum_viewer and hasattr(self.spectrum_viewer.state, 'y_display_unit'):
if self.spectrum_viewer.state.y_display_unit is not None:
unit_dict[orig_flux_or_sb] = self.spectrum_viewer.state.y_display_unit
unit_dict[orig_spectral_y_type] = self.spectrum_viewer.state.y_display_unit
else:
# spectrum_viewer.state will only have x/y_display_unit if unit conversion has
# been done if not, get default flux units which should be the units displayed
unit_dict[orig_flux_or_sb] = data.get_component('flux').units
unit_dict[orig_spectral_y_type] = data.get_component('flux').units
else:
# spectrum_viewer.state will only have x/y_display_unit if unit conversion has
# been done if not, get default flux units which should be the units displayed
unit_dict[orig_flux_or_sb] = data.get_component('flux').units
unit_dict[orig_spectral_y_type] = data.get_component('flux').units

Check warning on line 211 in jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py#L211

Added line #L211 was not covered by tests

# figure out if label should say 'Flux' or 'Surface Brightness'
sb_or_flux_label = "Flux"
is_unit_solid_angle = check_if_unit_is_per_solid_angle(unit_dict[orig_flux_or_sb])
is_unit_solid_angle = check_if_unit_is_per_solid_angle(unit_dict[orig_spectral_y_type])
if is_unit_solid_angle is True:
sb_or_flux_label = "Surface Brightness"

Expand Down Expand Up @@ -353,11 +353,11 @@
flux_sb_unit = data.get_component('flux').units

# convert unit string to u.Unit so moment map data can be converted
flux_or_sb_display_unit = u.Unit(flux_sb_unit)
spectral_y_display_unit = u.Unit(flux_sb_unit)
if SPECUTILS_LT_1_15_1:
moment_new_unit = flux_or_sb_display_unit
moment_new_unit = spectral_y_display_unit

Check warning on line 358 in jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py#L358

Added line #L358 was not covered by tests
else:
moment_new_unit = flux_or_sb_display_unit * self.spectrum_viewer.state.x_display_unit # noqa: E501
moment_new_unit = spectral_y_display_unit * self.spectrum_viewer.state.x_display_unit # noqa: E501
self.moment = self.moment.to(moment_new_unit)

# Reattach the WCS so we can load the result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_momentmap_nirspec_prism(cubeviz_helper, tmp_path):
uc = cubeviz_helper.plugins["Unit Conversion"]
uc.open_in_tray() # plugin has to be open for unit change to take hold
uc._obj.show_translator = True
uc.flux_or_sb.selected = 'Surface Brightness'
uc.spectral_y_type.selected = 'Surface Brightness'
mm = cubeviz_helper.plugins['Moment Maps']._obj
mm.open_in_tray() # plugin has to be open for unit change to take hold
mm._set_data_units()
Expand All @@ -295,7 +295,7 @@ def test_momentmap_nirspec_prism(cubeviz_helper, tmp_path):
(sky_cube.ra.deg, sky_cube.dec.deg))


def test_correct_output_flux_or_sb_units(cubeviz_helper, spectrum1d_cube_custom_fluxunit):
def test_correct_output_spectral_y_units(cubeviz_helper, spectrum1d_cube_custom_fluxunit):
if SPECUTILS_LT_1_15_1:
moment_unit = "Jy / sr"
else:
Expand All @@ -316,7 +316,7 @@ def test_correct_output_flux_or_sb_units(cubeviz_helper, spectrum1d_cube_custom_
uc = cubeviz_helper.plugins["Unit Conversion"]
uc.open_in_tray() # plugin has to be open for unit change to take hold
uc._obj.show_translator = True
uc.flux_or_sb.selected = 'Surface Brightness'
uc.spectral_y_type.selected = 'Surface Brightness'
mm = cubeviz_helper.plugins['Moment Maps']._obj
mm.open_in_tray() # plugin has to be open for unit change to take hold
mm._set_data_units()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_cubeviz_aperphot_unit_conversion(cubeviz_helper, spectrum1d_cube_custom
# check that initial units are synced between plugins
assert uc.flux_unit.selected == 'MJy'
assert uc.angle_unit.selected == 'sr'
assert ap.display_flux_or_sb_unit == 'MJy / sr'
assert ap.display_spectral_y_unit == 'MJy / sr'
assert ap.flux_scaling_display_unit == 'MJy'

# and defaults for inputs are in the correct unit
Expand Down
42 changes: 21 additions & 21 deletions jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
# Cubeviz only
cube_slice = Unicode("").tag(sync=True)
is_cube = Bool(False).tag(sync=True)
display_flux_or_sb_unit = Unicode("").tag(sync=True)
display_spectral_y_unit = Unicode("").tag(sync=True)
flux_scaling_display_unit = Unicode("").tag(sync=True)

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -163,7 +163,7 @@
if self.config == 'cubeviz':

# get previously selected display units
prev_display_flux_or_sb_unit = self.display_flux_or_sb_unit
prev_display_spectral_y_unit = self.display_spectral_y_unit
prev_flux_scale_unit = self.flux_scaling_display_unit

# update display unit traitlets to new selection
Expand All @@ -172,13 +172,13 @@
# convert the previous background and flux scaling values to new unit so
# re-calculating photometry with the current selections will produce
# the previous output with the new unit.
if prev_display_flux_or_sb_unit != '':
if prev_display_spectral_y_unit != '':

# convert background to new unit
if self.background_value is not None:

prev_unit = u.Unit(prev_display_flux_or_sb_unit)
new_unit = u.Unit(self.display_flux_or_sb_unit)
prev_unit = u.Unit(prev_display_spectral_y_unit)
new_unit = u.Unit(self.display_spectral_y_unit)

bg = self.background_value * prev_unit
self.background_value = bg.to_value(
Expand All @@ -196,14 +196,14 @@
def _set_display_unit_of_selected_dataset(self):

"""
Set the display_flux_or_sb_unit and flux_scaling_display_unit traitlets,
Set the display_spectral_y_unit and flux_scaling_display_unit traitlets,
which depend on if the selected data set is flux or surface brightness,
and the corresponding global display unit for either flux or
surface brightness.
"""

if not self.dataset_selected or not self.aperture_selected:
self.display_flux_or_sb_unit = ''
self.display_spectral_y_unit = ''
self.flux_scaling_display_unit = ''
return

Expand All @@ -213,13 +213,13 @@
# if data is something-per-solid-angle, its a SB unit and we should
# use the selected global display unit for SB
if check_if_unit_is_per_solid_angle(comp.units):
flux_or_sb = 'sb'
spectral_y_type = 'sb'
else:
flux_or_sb = 'flux'
spectral_y_type = 'flux'

disp_unit = self.app._get_display_unit(flux_or_sb)
disp_unit = self.app._get_display_unit(spectral_y_type)

self.display_flux_or_sb_unit = disp_unit
self.display_spectral_y_unit = disp_unit

# now get display unit for flux_scaling_display_unit. this unit will always
# be in flux, but it will not be derived from the global flux display unit
Expand All @@ -228,7 +228,7 @@
self.flux_scaling_display_unit = fs_unit.to_string()

else:
self.display_flux_or_sb_unit = ''
self.display_spectral_y_unit = ''

Check warning on line 231 in jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py#L231

Added line #L231 was not covered by tests
self.flux_scaling_display_unit = ''

def _get_defaults_from_metadata(self, dataset=None):
Expand All @@ -251,8 +251,8 @@
mjy2abmag = 0.003631

# if display unit is different, translate
if (self.config == 'cubeviz') and (self.display_flux_or_sb_unit != ''):
disp_unit = u.Unit(self.display_flux_or_sb_unit)
if (self.config == 'cubeviz') and (self.display_spectral_y_unit != ''):
disp_unit = u.Unit(self.display_spectral_y_unit)

Check warning on line 255 in jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py#L255

Added line #L255 was not covered by tests
mjy2abmag = (mjy2abmag * u.Unit("MJy/sr")).to_value(disp_unit)

if 'photometry' in meta and 'pixelarea_arcsecsq' in meta['photometry']:
Expand Down Expand Up @@ -337,7 +337,7 @@

# get correct display unit for newly selected dataset
if self.config == 'cubeviz':
# set display_flux_or_sb_unit and flux_scaling_display_unit
# set display_spectral_y_unit and flux_scaling_display_unit
self._set_display_unit_of_selected_dataset()

# auto-populate background, if applicable.
Expand Down Expand Up @@ -437,9 +437,9 @@

# convert to display unit, if necessary (cubeviz only)

if (self.config == 'cubeviz') and (self.display_flux_or_sb_unit != '') and comp.units:
if (self.config == 'cubeviz') and (self.display_spectral_y_unit != '') and comp.units:
bg_md = (bg_md * u.Unit(comp.units)).to_value(
u.Unit(self.display_flux_or_sb_unit), u.spectral_density(self._cube_wave))
u.Unit(self.display_spectral_y_unit), u.spectral_density(self._cube_wave))

return bg_md

Expand Down Expand Up @@ -542,7 +542,7 @@
img_unit = None

if self.config == 'cubeviz':
display_unit = u.Unit(self.display_flux_or_sb_unit)
display_unit = u.Unit(self.display_spectral_y_unit)

if background is not None and background not in self.background.choices: # pragma: no cover
raise ValueError(f"background must be one of {self.background.choices}")
Expand Down Expand Up @@ -722,7 +722,7 @@
phot_table.add_column(slice_val, name="slice_wave", index=29)

if comp.units: # convert phot. results from image unit to display unit
display_unit = u.Unit(self.display_flux_or_sb_unit)
display_unit = u.Unit(self.display_spectral_y_unit)
# convert units of certain columns in aperture phot. output table
# to reflect display units (i.e if data units are MJy / sr, but
# Jy / sr is selected in Unit Conversion plugin)
Expand Down Expand Up @@ -762,8 +762,8 @@
if update_plots:

# for cubeviz unit conversion display units
if self.display_flux_or_sb_unit != '':
plot_display_unit = self.display_flux_or_sb_unit
if self.display_spectral_y_unit != '':
plot_display_unit = self.display_spectral_y_unit
else:
plot_display_unit = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
v-model.number="background_value"
type="number"
hint="Background to subtract"
:suffix="display_flux_or_sb_unit"
:suffix="display_spectral_y_unit"
:disabled="background_selected!='Manual'"
persistent-hint
>
Expand Down Expand Up @@ -191,7 +191,7 @@
</v-row>

<v-row v-if="!multiselect && plot_available">
<jupyter-widget :widget="plot_widget"/>
<jupyter-widget :widget="plot_widget"/>
</v-row>

<div v-if="!multiselect && plot_available && fit_radial_profile && current_plot_type != 'Curve of Growth'">
Expand Down Expand Up @@ -231,7 +231,7 @@

<div v-if="result_available">
<j-plugin-section-header>Results History</j-plugin-section-header>
<jupyter-widget :widget="table_widget"></jupyter-widget>
<jupyter-widget :widget="table_widget"></jupyter-widget>
</div>
</j-tray-plugin>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_conv_no_data(specviz_helper, spectrum1d):
"""plugin unit selections won't have valid choices yet, preventing
attempting to set display units."""
# spectrum not load is in Flux units, sb_unit and flux_unit
# should be enabled, flux_or_sb should not be
# should be enabled, spectral_y_type should not be
plg = specviz_helper.plugins["Unit Conversion"]
with pytest.raises(ValueError, match="no valid unit choices"):
plg.spectral_unit = "micron"
Expand All @@ -100,7 +100,7 @@ def test_conv_no_data(specviz_helper, spectrum1d):
# make sure we don't expose translations in Specviz
assert hasattr(plg, 'flux_unit')
assert hasattr(plg, 'angle_unit')
assert not hasattr(plg, 'flux_or_sb')
assert not hasattr(plg, 'spectral_y_type')


def test_non_stddev_uncertainty(specviz_helper):
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_unit_translation(cubeviz_helper):

# When the dropdown is displayed, this ensures the loaded
# data collection item units will be used for translations.
assert uc_plg._obj.flux_or_sb_selected == 'Flux'
assert uc_plg._obj.spectral_y_type_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
Expand All @@ -161,9 +161,9 @@ def test_unit_translation(cubeviz_helper):
cubeviz_helper._default_spectrum_viewer_reference_name)

# change global y-units from Flux -> Surface Brightness
uc_plg._obj.flux_or_sb_selected = 'Surface Brightness'
uc_plg._obj.spectral_y_type_selected = 'Surface Brightness'

assert uc_plg._obj.flux_or_sb_selected == 'Surface Brightness'
assert uc_plg._obj.spectral_y_type_selected == 'Surface Brightness'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

# check if units translated
Expand All @@ -190,15 +190,15 @@ def test_sb_unit_conversion(cubeviz_helper):
uc_plg.open_in_tray()

# ensure that per solid angle cube defaults to Flux spectrum
assert uc_plg.flux_or_sb == 'Flux'
assert uc_plg.spectral_y_type == 'Flux'
# flux choices is populated with flux units
assert uc_plg.flux_unit.choices

# to have access to display units
viewer_1d = cubeviz_helper.app.get_viewer(
cubeviz_helper._default_spectrum_viewer_reference_name)

uc_plg.flux_or_sb.selected = 'Surface Brightness'
uc_plg.spectral_y_type.selected = 'Surface Brightness'

# Surface Brightness conversion
uc_plg.flux_unit = 'Jy'
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_sb_unit_conversion(cubeviz_helper):
"World 13h39m59.7037s +27d00m03.2400s (ICRS)",
"204.9987654313 27.0008999946 (deg)")

uc_plg._obj.flux_or_sb_selected = 'Flux'
uc_plg._obj.spectral_y_type_selected = 'Flux'
uc_plg.flux_unit = 'Jy'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

Expand All @@ -266,7 +266,7 @@ def test_contour_unit_conversion(cubeviz_helper, spectrum1d_cube_fluxunit_jy_per

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

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

assert np.allclose(po_plg.contour_max.value, 1.99e-4)
Loading