Skip to content

Commit

Permalink
support lists of objects when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Jan 22, 2025
1 parent 03449c5 commit bcef771
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jdaviz/configs/specviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def load_data(self, data, data_label=None, format=None, show_in_viewer=True,

default_labels = {'mosviz-spec2d-parser': '2D Spectrum',
'specreduce-trace': 'Trace',
'specviz-spectrum1d-parser': 'Spectrum 1D'}
'specviz-spectrum1d-parser': 'Spectrum'}
if data_label is None:
data_label = default_labels.get(parser, 'Unknown')
data_label = self.app.return_unique_name(data_label)
Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/specviz/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_load_spectrum_list_no_labels(self):
self.spec_app.load_data(self.spec_list)
assert len(self.spec_app.app.data_collection) == 4
for i in (1, 2, 3):
assert "specviz_data" in self.spec_app.app.data_collection[i].label
assert "Spectrum" in self.spec_app.app.data_collection[i].label

def test_load_spectrum_list_with_labels(self):
# now load three more spectra from a SpectrumList, with labels:
Expand Down Expand Up @@ -412,7 +412,7 @@ def test_load_2d_flux(specviz_helper):
# 1D Spectrum1D objects to load in Specviz.
spec = Spectrum1D(spectral_axis=np.linspace(4000, 6000, 10)*u.Angstrom,
flux=np.ones((4, 10))*u.Unit("1e-17 erg / (Angstrom cm2 s)"))
specviz_helper.load_data(spec, data_label="test")
specviz_helper.load_data(spec, data_label="test", load_as_list=True)

assert len(specviz_helper.app.data_collection) == 4
assert specviz_helper.app.data_collection[0].label == "test [0]"
Expand Down
18 changes: 16 additions & 2 deletions jdaviz/core/data_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,29 @@ def get_parser(obj, load_as_list=False):
parser : str
The parser for the data object
"""
if isinstance(obj, (SpectrumList, SpectrumCollection)):
return 'specviz-spectrum1d-parser'
if isinstance(obj, Trace):
return 'specreduce-trace'
elif isinstance(obj, Spectrum1D):
if isinstance(obj, Spectrum1D):
if obj.flux.ndim == 1:
return 'specviz-spectrum1d-parser'
else:
if load_as_list:
return 'specviz-spectrumlist-parser'
return 'specviz-spectrum1d-parser'
return 'mosviz-spec2d-parser'
if isinstance(obj, fits.HDUList):
columns = [c.name.lower() for hduitem in obj for c in getattr(hduitem, 'columns', [])]
if 'wavelength' in columns and 'flux' in columns:
return 'specviz-spectrum1d-parser'
else:
raise ValueError("cannot find valid parser for HDUList")
if isinstance(obj, list):
parsers = [get_parser(o, load_as_list=load_as_list) for o in obj]
if len(set(parsers)) > 1:
raise ValueError("cannot find single parser for list of objects")
return parsers[0]

_, config = get_valid_format(obj)
parsers = {'specviz': 'specviz-spectrum1d-parser', 'specviz2d': 'mosviz-spec2d-parser'}
return parsers.get(config)
Expand Down

0 comments on commit bcef771

Please sign in to comment.