Skip to content

Commit

Permalink
loading line lists at redshifted wavelength (spacetelescope#2726)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gibsongreen authored Feb 26, 2024
1 parent 3adc659 commit 128d642
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^

Expand Down
8 changes: 4 additions & 4 deletions jdaviz/configs/default/plugins/line_lists/line_list_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions jdaviz/configs/default/plugins/line_lists/line_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
53 changes: 51 additions & 2 deletions jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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']
Expand All @@ -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)
18 changes: 14 additions & 4 deletions jdaviz/configs/specviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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.
"""
Expand All @@ -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)
Expand Down

0 comments on commit 128d642

Please sign in to comment.