From 9c1f6a91a984239a53d39b6fd0f5b7a4de57f25a Mon Sep 17 00:00:00 2001 From: Ricky O'Steen <39831871+rosteen@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:49:56 -0400 Subject: [PATCH] Fix empty flux menu in Specviz with spectrum in surface brightness units (#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 --- CHANGES.rst | 2 +- jdaviz/configs/specviz/plugins/parsers.py | 10 ++++++++++ .../unit_conversion/tests/test_unit_conversion.py | 9 +++++++++ .../specviz/plugins/unit_conversion/unit_conversion.py | 10 +++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1aea7de067..b0660b8951 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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] diff --git a/jdaviz/configs/specviz/plugins/parsers.py b/jdaviz/configs/specviz/plugins/parsers.py index 51fbfc1ee6..0ae7546d95 100644 --- a/jdaviz/configs/specviz/plugins/parsers.py +++ b/jdaviz/configs/specviz/plugins/parsers.py @@ -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"] @@ -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. diff --git a/jdaviz/configs/specviz/plugins/unit_conversion/tests/test_unit_conversion.py b/jdaviz/configs/specviz/plugins/unit_conversion/tests/test_unit_conversion.py index 7501bb83ea..f6607d745d 100644 --- a/jdaviz/configs/specviz/plugins/unit_conversion/tests/test_unit_conversion.py +++ b/jdaviz/configs/specviz/plugins/unit_conversion/tests/test_unit_conversion.py @@ -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: diff --git a/jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py b/jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py index e5212dcaa7..bca8ff5bde 100644 --- a/jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py +++ b/jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py @@ -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): @@ -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]: