Skip to content

Commit

Permalink
Fix empty flux menu in Specviz with spectrum in surface brightness un…
Browse files Browse the repository at this point in the history
…its (spacetelescope#3185)

* Debugging

* Return from flux_unit_selected if no angle unit

* Trying to set spectral y type on first data load

* Working fix that doesn't break tests

* Changelog

* Don't set this if unit conversion doesn't exist

* Add test that fails on main

* Codestyle

* Update jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py
  • Loading branch information
rosteen authored Sep 9, 2024
1 parent 3f80eb5 commit 9c1f6a9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 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, #3149, #3155, #3178]
unit conversion in Cubeviz and Specviz. [#2781, #2940, #3088, #3111, #3113, #3129, #3139, #3149, #3155, #3178, #3185]

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

Expand Down
10 changes: 10 additions & 0 deletions jdaviz/configs/specviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from jdaviz.core.events import SnackbarMessage
from jdaviz.core.registries import data_parser_registry
from jdaviz.utils import standardize_metadata, download_uri_to_path
from jdaviz.core.validunits import check_if_unit_is_per_solid_angle


__all__ = ["specviz_spectrum1d_parser"]
Expand Down Expand Up @@ -159,6 +160,15 @@ def specviz_spectrum1d_parser(app, data, data_label=None, format=None, show_in_v
# Make metadata layout conform with other viz.
spec.meta = standardize_metadata(spec.meta)

# If this is the first loaded data, we want to set spectral y unit type to Flux or
# Surface Brightness as appropriate
if len(app.data_collection) == 0 and "Unit Conversion" in app._jdaviz_helper.plugins:
uc = app._jdaviz_helper.plugins["Unit Conversion"]
if check_if_unit_is_per_solid_angle(flux_units):
uc._obj.spectral_y_type = "Surface Brightness"
else:
uc._obj.spectral_y_type = "Flux"

app.add_data(spec, data_label[i])

# handle display, with the SpectrumList special case in mind.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def test_value_error_exception(specviz_helper, spectrum1d, new_spectral_axis, ne
assert u.Unit(viewer.state.y_display_unit) == u.Unit(expected_flux)


def test_initialize_specviz_sb(specviz_helper, spectrum1d):
spec_sb = Spectrum1D(spectrum1d.flux/u.sr, spectrum1d.spectral_axis)
specviz_helper.load_data(spec_sb, data_label="Test 1D Spectrum")
plg = specviz_helper.plugins["Unit Conversion"]
assert plg._obj.flux_unit == "Jy"
assert plg._obj.spectral_y_type == "Surface Brightness"
assert plg._obj.angle_unit == "sr"


@pytest.mark.parametrize('uncert', (False, True))
def test_conv_wave_only(specviz_helper, spectrum1d, uncert):
if uncert is False:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _on_glue_x_display_unit_changed(self, x_unit_str):
self.spectral_unit.selected = x_unit_str
if not len(self.flux_unit.choices) or not len(self.angle_unit.choices):
# in case flux_unit was triggered first (but could not be set because there
# as no spectral_unit to determine valid equivalencies)
# was no spectral_unit to determine valid equivalencies)
self._on_glue_y_display_unit_changed(self.spectrum_viewer.state.y_display_unit)

def _on_glue_y_display_unit_changed(self, y_unit_str):
Expand Down Expand Up @@ -204,6 +204,14 @@ def _on_glue_y_display_unit_changed(self, y_unit_str):
if check_if_unit_is_per_solid_angle(y_unit_str):
flux_choices = create_flux_equivalencies_list(y_unit * u.sr, x_unit)
self.flux_unit.choices = flux_choices
flux_unit = str(y_unit * u.sr)
# We need to set the angle_unit before triggering _on_flux_unit_changed via
# setting self.flux_unit.selected below, or the lack of angle unit will make it think
# we're in Flux units.
self.angle_unit.choices = create_angle_equivalencies_list(y_unit)
self.angle_unit.selected = self.angle_unit.choices[0]
if flux_unit in self.flux_unit.choices and flux_unit != self.flux_unit.selected:
self.flux_unit.selected = flux_unit

# sets the angle unit drop down and the surface brightness read-only text
if self.app.data_collection[0]:
Expand Down

0 comments on commit 9c1f6a9

Please sign in to comment.