From 128d642365e722c97b777c90a91d8c9bbea4547e Mon Sep 17 00:00:00 2001 From: Gilbert Green <42986583+gibsongreen@users.noreply.github.com> Date: Mon, 26 Feb 2024 14:48:54 -0500 Subject: [PATCH] loading line lists at redshifted wavelength (#2726) * updating lines so they receive redshift if redshift applied prior to new line being added to viewer * test: cut/simplied logic and excess data, allclose for asserts; viewer: more readable if; line_list: cut conditional logic of redshift * added change log * change log text altered --- CHANGES.rst | 2 + .../plugins/line_lists/line_list_mixin.py | 8 +-- .../default/plugins/line_lists/line_lists.py | 8 +-- .../line_lists/tests/test_line_lists.py | 53 ++++++++++++++++++- jdaviz/configs/specviz/plugins/viewers.py | 18 +++++-- 5 files changed, 75 insertions(+), 14 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a89110ec4c..897274de0e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -122,6 +122,8 @@ Other Changes and Additions Bug Fixes --------- +- Fix redshifted line lists that were displaying at rest wavelengths, by assuming a global redshift. [#2726] + Cubeviz ^^^^^^^ diff --git a/jdaviz/configs/default/plugins/line_lists/line_list_mixin.py b/jdaviz/configs/default/plugins/line_lists/line_list_mixin.py index e3d3266dc7..f3bee5d3ea 100644 --- a/jdaviz/configs/default/plugins/line_lists/line_list_mixin.py +++ b/jdaviz/configs/default/plugins/line_lists/line_list_mixin.py @@ -42,17 +42,17 @@ def erase_spectral_lines(self, name=None): self._default_spectrum_viewer_reference_name ).erase_spectral_lines(name=name) - def plot_spectral_line(self, line): + def plot_spectral_line(self, line, global_redshift=None): """Convenience function to get to the viewer function""" self.app.get_viewer( self._default_spectrum_viewer_reference_name - ).plot_spectral_line(line) + ).plot_spectral_line(line, global_redshift) - def plot_spectral_lines(self): + def plot_spectral_lines(self, global_redshift=None): """Convenience function to get to the viewer function""" self.app.get_viewer( self._default_spectrum_viewer_reference_name - ).plot_spectral_lines() + ).plot_spectral_lines(global_redshift) @property def spectral_lines(self): diff --git a/jdaviz/configs/default/plugins/line_lists/line_lists.py b/jdaviz/configs/default/plugins/line_lists/line_lists.py index 0d0b888625..fcb4ecb9d7 100644 --- a/jdaviz/configs/default/plugins/line_lists/line_lists.py +++ b/jdaviz/configs/default/plugins/line_lists/line_lists.py @@ -524,7 +524,7 @@ def _list_from_notebook(self, msg): self.send_state('loaded_lists') self.send_state('list_contents') - self._viewer.plot_spectral_lines(tmp_names_rest) + self._viewer.plot_spectral_lines(tmp_names_rest, global_redshift=self._global_redshift) self.update_line_mark_dict() msg_text = ("Spectral lines loaded from notebook. Lines can be hidden" @@ -664,7 +664,7 @@ def vue_show_all_in_list(self, listname): self.list_contents = lc self.send_state('list_contents') - self._viewer.plot_spectral_lines() + self._viewer.plot_spectral_lines(global_redshift=self._global_redshift) self.update_line_mark_dict() def vue_hide_all_in_list(self, listname): @@ -697,7 +697,7 @@ def vue_plot_all_lines(self, event): self.send_state('list_contents') - self._viewer.plot_spectral_lines() + self._viewer.plot_spectral_lines(global_redshift=self._global_redshift) self.update_line_mark_dict() def vue_erase_all_lines(self, event): @@ -735,7 +735,7 @@ def vue_change_visible(self, data): self.list_contents = list_contents if show: - self._viewer.plot_spectral_line(name_rest) + self._viewer.plot_spectral_line(name_rest, global_redshift=self._global_redshift) else: self._viewer.erase_spectral_lines(name_rest=name_rest) diff --git a/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py b/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py index b954614dc2..37062dde72 100644 --- a/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py +++ b/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py @@ -6,6 +6,7 @@ from astropy.table import QTable from specutils import Spectrum1D +from jdaviz.core.marks import SpectralLine from jdaviz.core.linelists import get_available_linelists @@ -123,8 +124,7 @@ def test_load_available_preset_lists(specviz_helper, spectrum1d): def test_line_identify(specviz_helper, spectrum1d): - label = "Test 1D Spectrum" - specviz_helper.load_data(spectrum1d, data_label=label) + specviz_helper.load_data(spectrum1d) lt = QTable() lt['linename'] = ['O III', 'Halpha'] @@ -141,3 +141,52 @@ def test_line_identify(specviz_helper, spectrum1d): ll_plugin.vue_change_visible(('Test List', line, 0)) assert line.get('show') is False assert line.get('identify', False) is False + + +def test_global_redshift_applied(specviz_helper, spectrum1d): + specviz_helper.load_data(spectrum1d) + + # Create a table with redshift included + lt = QTable({'linename': ['O III', 'Halpha'], + 'rest': [5007, 6563] * u.AA, + 'redshift': u.Quantity([0, 0])}) + + with pytest.warns(UserWarning, match='per line/list redshifts not supported, use viz.set_redshift'): # noqa + specviz_helper.load_line_list(lt) + + # Load a line, and apply redshift globally + specviz_helper.plot_spectral_line("Halpha") + specviz_helper.set_redshift(0.01) + # Load second line, redshift should also be applied to it + specviz_helper.plot_spectral_line("O III") + + viewer_lines = [mark for mark in specviz_helper.app.get_viewer( + specviz_helper._default_spectrum_viewer_reference_name).figure.marks + if isinstance(mark, SpectralLine)] + + assert np.allclose([line.redshift for line in viewer_lines], 0.01) + + +def test_global_redshift_applied_to_all(specviz_helper, spectrum1d): + specviz_helper.load_data(spectrum1d) + + # Create a table with redshift included + lt = QTable({'linename': ['O III', 'Halpha', 'O I'], + 'rest': [5007, 6563, 6300] * u.AA, + 'redshift': u.Quantity([0, 0, 0])}) + + with pytest.warns(UserWarning, match='per line/list redshifts not supported, use viz.set_redshift'): # noqa + specviz_helper.load_line_list(lt) + + # Load a line, so we can apply redshift + specviz_helper.plot_spectral_line("Halpha") + global_redshift = 0.01 + specviz_helper.set_redshift(global_redshift) + # Load remaining lines + specviz_helper.plot_spectral_lines(global_redshift) + + viewer_lines = [mark for mark in specviz_helper.app.get_viewer( + specviz_helper._default_spectrum_viewer_reference_name).figure.marks + if isinstance(mark, SpectralLine)] + + assert np.allclose([line.redshift for line in viewer_lines], 0.01) diff --git a/jdaviz/configs/specviz/plugins/viewers.py b/jdaviz/configs/specviz/plugins/viewers.py index d23e1e8c86..e92f4abfa4 100644 --- a/jdaviz/configs/specviz/plugins/viewers.py +++ b/jdaviz/configs/specviz/plugins/viewers.py @@ -261,7 +261,7 @@ def get_scales(self): self.toolbar.active_tool = None return {'x': fig.interaction.x_scale, 'y': fig.interaction.y_scale} - def plot_spectral_line(self, line, plot_units=None, **kwargs): + def plot_spectral_line(self, line, global_redshift=None, plot_units=None, **kwargs): if isinstance(line, str): # Try the full index first (for backend calls), otherwise name only try: @@ -271,9 +271,14 @@ def plot_spectral_line(self, line, plot_units=None, **kwargs): if plot_units is None: plot_units = self.data()[0].spectral_axis.unit + if global_redshift is None: + redshift = self.redshift + else: + redshift = global_redshift + line_mark = SpectralLine(self, line['rest'].to_value(plot_units), - self.redshift, + redshift, name=line["linename"], table_index=line["name_rest"], colors=[line["colors"]], **kwargs) @@ -285,7 +290,7 @@ def plot_spectral_line(self, line, plot_units=None, **kwargs): line["show"] = True self._broadcast_plotted_lines() - def plot_spectral_lines(self, colors=["blue"], **kwargs): + def plot_spectral_lines(self, colors=["blue"], global_redshift=None, **kwargs): """ Plots a user-provided astropy table of spectral lines in the viewer. """ @@ -301,13 +306,18 @@ def plot_spectral_lines(self, colors=["blue"], **kwargs): lines = self.spectral_lines plot_units = self.data()[0].spectral_axis.unit + if global_redshift is None: + redshift = self.redshift + else: + redshift = global_redshift + marks = [] for line, color in zip(lines, colors): if not line["show"]: continue line = SpectralLine(self, line['rest'].to_value(plot_units), - redshift=self.redshift, + redshift, name=line["linename"], table_index=line["name_rest"], colors=[color], **kwargs)