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

loading line lists at redshifted wavelength #2726

Merged
merged 4 commits into from
Feb 26, 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: 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
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
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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone has a suggestion to get around using a loop here and in the second test. It is here to get the SpectralLines in the viewer, and make sure the redshift is applied. The Qtable doesn't update automatically if redshift is applied so I went with this methodology for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kecnry might know.

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
Loading