Skip to content

Commit

Permalink
Merge pull request #2273 from duytnguyendtn/specload
Browse files Browse the repository at this point in the history
Replace load_spectrum with load_data
  • Loading branch information
duytnguyendtn authored Jul 3, 2023
2 parents 24ff314 + a569123 commit b365d80
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ New Features

- Plots within plugins can now be popped-out into their own windows. [#2254]

- The ``specviz.load_spectrum`` method is deprecated; use ``specviz.load_data`` instead. [#2273]

Cubeviz
^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions docs/cubeviz/import_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Importing data via the API

Alternatively, users who work in a coding environment like a Jupyter
notebook can access the Cubeviz helper class API. Using this API, users can
load data into the application through code with the :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_spectrum`
load data into the application through code with the :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_data`
method, which takes as input a :class:`~specutils.Spectrum1D` object.

FITS Files
Expand Down Expand Up @@ -162,7 +162,7 @@ object, you can load it into Cubeviz as follows:
# Create your spectrum1
spec3d = Spectrum1D(data, wcs=my_wcs)
cubeviz = Cubeviz()
cubeviz.load_spectrum(spec3d, data_label='My Cube')
cubeviz.load_data(spec3d, data_label='My Cube')
cubeviz.show()
There is no plan to natively load such objects until ``datamodels``
Expand Down
2 changes: 1 addition & 1 deletion docs/specviz/export_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ spectrum containing only your subset by running:
spec = specviz.get_data(spectral_subset='Subset 1')
subset_spec = Spectrum1D(flux=spec.flux[~spec.mask],
spectral_axis=spec.spectral_axis[~spec.mask])
specviz.load_spectrum(subset_spec)
specviz.load_data(subset_spec)
.. seealso::

Expand Down
20 changes: 10 additions & 10 deletions docs/specviz/import_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Importing data via the API
Alternatively, users who work in a coding environment like a Jupyter
notebook can access the Specviz helper class API. Using this API, users can
load data into the application through code with the
:py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_spectrum`
:py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_data`
method, which takes as input a :class:`~specutils.Spectrum1D` object.

FITS Files
Expand All @@ -64,15 +64,15 @@ The example below loads a FITS file into Specviz:
from specutils import Spectrum1D
spec1d = Spectrum1D.read("/path/to/data/file")
specviz = Specviz()
specviz.load_spectrum(spec1d, data_label="my_spec")
specviz.load_data(spec1d, data_label="my_spec")
specviz.show()
You can also pass the path to a file that `~specutils.Spectrum1D` understands directly to the
:py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_spectrum` method:
:py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_data` method:

.. code-block:: python
specviz.load_spectrum("path/to/data/file")
specviz.load_data("path/to/data/file")
Creating Your Own Array
-----------------------
Expand All @@ -90,7 +90,7 @@ You can create your own array to load into Specviz:
wavelength = np.arange(5100, 5300) * u.AA
spec1d = Spectrum1D(spectral_axis=wavelength, flux=flux)
specviz = Specviz()
specviz.load_spectrum(spec1d, data_label="my_spec")
specviz.load_data(spec1d, data_label="my_spec")
specviz.show()
JWST datamodels
Expand All @@ -111,7 +111,7 @@ object, you can load it into Specviz as follows:
spec1d = Spectrum1D(flux=flux, spectral_axis=wave)
specviz = Specviz()
specviz.load_spectrum(spec1d, data_label="MultiSpecModel")
specviz.load_data(spec1d, data_label="MultiSpecModel")
specviz.show()
There is no plan to natively load such objects until ``datamodels``
Expand All @@ -122,7 +122,7 @@ is separated from the ``jwst`` pipeline package.
Importing a SpectrumList
------------------------

The :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_spectrum` also accepts
The :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_data` also accepts
a `~specutils.SpectrumList` object, in which case it will both load the
individual `~specutils.Spectrum1D` objects in the list and additionally attempt
to stitch together the spectra into a single data object so that
Expand All @@ -132,7 +132,7 @@ they can be manipulated and analyzed in the application as a single entity:
from specutils import SpectrumList
spec_list = SpectrumList([spec1d_1, spec1d_2])
specviz.load_spectrum(spec_list)
specviz.load_data(spec_list)
specviz.show()
In the screenshot below, the combined spectrum is plotted in gray, and one of
Expand All @@ -145,13 +145,13 @@ end of the red region in the screenshot below:
.. image:: img/spectrumlist_combined.png

This functionality is also available in limited instances by providing a directory path
to the :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_spectrum` method. Note
to the :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_data` method. Note
that the ``read`` method of :class:`~specutils.SpectrumList` is only set up to handle
directory input in limited cases, for example JWST MIRI MRS data, and will throw an error
in other cases. In cases that it does work, only files in the directory level specified
will be read, with no recursion into deeper folders.

The :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_spectrum` method also takes
The :py:meth:`~jdaviz.configs.specviz.helper.Specviz.load_data` method also takes
an optional keyword argument ``concat_by_file``. When set to ``True``, the spectra
loaded in the :class:`~specutils.SpectrumList` will be concatenated together into one
combined spectrum per loaded file, which may be useful for MIRI observations, for example.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def test_line_lists(specviz_helper):
spec = Spectrum1D(flux=np.random.rand(100)*u.Jy,
spectral_axis=np.arange(6000, 7000, 10)*u.AA)
specviz_helper.load_spectrum(spec)
specviz_helper.load_data(spec)

lt = QTable()
lt['linename'] = ['O III', 'Halpha']
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_redshift(specviz_helper, spectrum1d):
assert plg._obj.disabled_msg

label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

assert not plg._obj.disabled_msg

Expand Down Expand Up @@ -102,7 +102,7 @@ def test_redshift(specviz_helper, spectrum1d):
def test_load_available_preset_lists(specviz_helper, spectrum1d):
""" Loads all available line lists and checks the medium requirement """
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

# Check to make sure we got our line lists
available_linelists = get_available_linelists()
Expand All @@ -125,7 +125,7 @@ def test_load_available_preset_lists(specviz_helper, spectrum1d):

def test_line_identify(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

lt = QTable()
lt['linename'] = ['O III', 'Halpha']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def test_default_model_labels(specviz_helper, spectrum1d):
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
modelfit_plugin = specviz_helper.plugins['Model Fitting']
# By default, the spectral region should be the entire spectrum
assert modelfit_plugin._obj.spectral_subset_selected == "Entire Spectrum"
Expand All @@ -46,7 +46,7 @@ def test_default_model_labels(specviz_helper, spectrum1d):


def test_custom_model_labels(specviz_helper, spectrum1d):
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
modelfit_plugin = specviz_helper.plugins['Model Fitting']

for i, model in enumerate(MODELS):
Expand All @@ -68,7 +68,7 @@ def test_register_model_with_uncertainty_weighting(specviz_helper, spectrum1d):
spectrum1d.uncertainty = StdDevUncertainty(spectrum1d.flux * 0.1)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
modelfit_plugin = specviz_helper.plugins['Model Fitting']

# Test registering a simple linear fit
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_register_model_uncertainty_is_none(specviz_helper, spectrum1d):
spectrum1d.uncertainty = None
with warnings.catch_warnings():
warnings.simplefilter('ignore')
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
modelfit_plugin = specviz_helper.plugins['Model Fitting']

# Test registering a simple linear fit
Expand Down Expand Up @@ -157,7 +157,7 @@ def test_register_cube_model(cubeviz_helper, spectrum1d_cube):
def test_user_api(specviz_helper, spectrum1d):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
p = specviz_helper.plugins['Model Fitting']

# even though the default label is set to C, adding Linear1D should default to its automatic
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_user_api(specviz_helper, spectrum1d):
def test_fit_gaussian_with_fixed_mean(specviz_helper, spectrum1d):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
modelfit_plugin = specviz_helper.plugins['Model Fitting']

modelfit_plugin.create_model_component('Gaussian1D', 'G')
Expand All @@ -217,7 +217,7 @@ def test_fit_gaussian_with_fixed_mean(specviz_helper, spectrum1d):
def test_reestimate_parameters(specviz_helper, spectrum1d):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
mf = specviz_helper.plugins['Model Fitting']

mf.create_model_component('Gaussian1D', 'G')
Expand Down Expand Up @@ -327,12 +327,12 @@ def test_subset_masks(cubeviz_helper, spectrum1d_cube_larger):

def test_invalid_subset(specviz_helper, spectrum1d):
# 6000-8000
specviz_helper.load_spectrum(spectrum1d, data_label="right_spectrum")
specviz_helper.load_data(spectrum1d, data_label="right_spectrum")

# 5000-7000
sp2 = Spectrum1D(spectral_axis=spectrum1d.spectral_axis - 1000*spectrum1d.spectral_axis.unit,
flux=spectrum1d.flux * 1.25)
specviz_helper.load_spectrum(sp2, data_label="left_spectrum")
specviz_helper.load_data(sp2, data_label="left_spectrum")

# apply subset that overlaps on left_spectrum, but not right_spectrum
# NOTE: using a subset that overlaps the right_spectrum (reference) results in errors when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@pytest.mark.filterwarnings('ignore')
def test_plugin(specviz_helper, spectrum1d):
specviz_helper.load_spectrum(spectrum1d)
specviz_helper.load_data(spectrum1d)
p = specviz_helper.plugins['Subset Tools']

# regression test for https://github.com/spacetelescope/jdaviz/issues/1693
Expand Down
26 changes: 25 additions & 1 deletion jdaviz/configs/specviz/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings

from astropy import units as u
from astropy.utils.decorators import deprecated_renamed_argument
from astropy.utils.decorators import deprecated_renamed_argument, deprecated
from regions.core.core import Region
from glue.core.subset_group import GroupedSubset
from specutils import SpectralRegion, Spectrum1D
Expand Down Expand Up @@ -41,11 +41,35 @@ def __init__(self, *args, **kwargs):
self.app.hub.subscribe(self, RedshiftMessage,
handler=self._redshift_listener)

@deprecated(since="3.6", alternative="load_data")
def load_spectrum(self, data, data_label=None, format=None, show_in_viewer=True,
concat_by_file=False):
"""
Loads a data file or `~specutils.Spectrum1D` object into Specviz.
Parameters
----------
data : str, `~specutils.Spectrum1D`, or `~specutils.SpectrumList`
Spectrum1D, SpectrumList, or path to compatible data file.
data_label : str
The Glue data label found in the ``DataCollection``.
format : str
Loader format specification used to indicate data format in
`~specutils.Spectrum1D.read` io method.
show_in_viewer : bool
Show data in viewer(s).
concat_by_file : bool
If True and there is more than one available extension, concatenate
the extensions within each spectrum file passed to the parser and
add a concatenated spectrum to the data collection.
"""
self.load_data(data, data_label, format, show_in_viewer, concat_by_file)

def load_data(self, data, data_label=None, format=None, show_in_viewer=True,
concat_by_file=False):
"""
Load data into Specviz.
Parameters
----------
data : str, `~specutils.Spectrum1D`, or `~specutils.SpectrumList`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

def test_plugin(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_spatial_subset(cubeviz_helper, image_cube_hdu_obj):

def test_user_api(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

sv = specviz_helper.app.get_viewer('spectrum-viewer')
sv.apply_roi(XRangeROI(6500, 7400))
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_user_api(specviz_helper, spectrum1d):

def test_line_identify(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

lt = QTable()
lt['linename'] = ['O III', 'Halpha']
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_coerce_unit():

def test_continuum_surrounding_spectral_subset(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand All @@ -207,7 +207,7 @@ def test_continuum_surrounding_spectral_subset(specviz_helper, spectrum1d):

def test_continuum_spectral_same_value(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand All @@ -234,7 +234,7 @@ def test_continuum_spectral_same_value(specviz_helper, spectrum1d):

def test_continuum_surrounding_invalid_width(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand All @@ -259,7 +259,7 @@ def test_continuum_surrounding_invalid_width(specviz_helper, spectrum1d):

def test_continuum_subset_spectral_entire(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand All @@ -286,7 +286,7 @@ def test_continuum_subset_spectral_entire(specviz_helper, spectrum1d):

def test_continuum_subset_spectral_subset2(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand Down Expand Up @@ -319,7 +319,7 @@ def test_continuum_subset_spectral_subset2(specviz_helper, spectrum1d):

def test_continuum_surrounding_no_right(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand Down Expand Up @@ -347,7 +347,7 @@ def test_continuum_surrounding_no_right(specviz_helper, spectrum1d):

def test_continuum_surrounding_no_left(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand Down Expand Up @@ -375,7 +375,7 @@ def test_continuum_surrounding_no_left(specviz_helper, spectrum1d):

def test_subset_changed(specviz_helper, spectrum1d):
label = "Test 1D Spectrum"
specviz_helper.load_spectrum(spectrum1d, data_label=label)
specviz_helper.load_data(spectrum1d, data_label=label)

plugin = specviz_helper.app.get_tray_item_from_name('specviz-line-analysis')
plugin.open_in_tray()
Expand Down Expand Up @@ -406,12 +406,12 @@ def test_subset_changed(specviz_helper, spectrum1d):

def test_invalid_subset(specviz_helper, spectrum1d):
# 6000-8000
specviz_helper.load_spectrum(spectrum1d, data_label="right_spectrum")
specviz_helper.load_data(spectrum1d, data_label="right_spectrum")

# 5000-7000
sp2 = Spectrum1D(spectral_axis=spectrum1d.spectral_axis - 1000*spectrum1d.spectral_axis.unit,
flux=spectrum1d.flux * 1.25)
specviz_helper.load_spectrum(sp2, data_label="left_spectrum")
specviz_helper.load_data(sp2, data_label="left_spectrum")

# apply subset that overlaps on left_spectrum, but not right_spectrum
# NOTE: using a subset that overlaps the right_spectrum (reference) results in errors when
Expand Down
Loading

0 comments on commit b365d80

Please sign in to comment.