-
Notifications
You must be signed in to change notification settings - Fork 74
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
Add ability to load 2D flux arrays in Specviz #3229
base: main
Are you sure you want to change the base?
Changes from 8 commits
d6745a5
ccc30fa
9124201
aa0cb56
180467a
620c6ae
4703b9d
fd5dc17
cec0bea
505674e
2147377
47a69b1
44dcb6d
fbba848
8680144
f5e2f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
|
||
@data_parser_registry("specviz-spectrum1d-parser") | ||
def specviz_spectrum1d_parser(app, data, data_label=None, format=None, show_in_viewer=True, | ||
concat_by_file=False, cache=None, local_path=os.curdir, timeout=None): | ||
concat_by_file=False, cache=None, local_path=os.curdir, timeout=None, | ||
load_as_list=False): | ||
""" | ||
Loads a data file or `~specutils.Spectrum1D` object into Specviz. | ||
|
||
|
@@ -46,6 +47,9 @@ | |
remote requests in seconds (passed to | ||
`~astropy.utils.data.download_file` or | ||
`~astroquery.mast.Conf.timeout`). | ||
load_as_list : bool, optional | ||
Force the parser to load the input file with the `~specutils.SpectrumList` read function | ||
instead of `~specutils.Spectrum1D`. | ||
""" | ||
|
||
spectrum_viewer_reference_name = app._jdaviz_helper._default_spectrum_viewer_reference_name | ||
|
@@ -56,8 +60,12 @@ | |
raise TypeError("SpectrumCollection detected." | ||
" Please provide a Spectrum1D or SpectrumList") | ||
elif isinstance(data, Spectrum1D): | ||
data_label = [app.return_data_label(data_label, alt_name="specviz_data")] | ||
data = [data] | ||
# Handle the possibility of 2D spectra by splitting into separate spectra | ||
if data.flux.ndim == 1: | ||
data_label = [app.return_data_label(data_label, alt_name="specviz_data")] | ||
data = [data] | ||
elif data.flux.ndim == 2: | ||
data, data_label = split_spectrum_with_2D_flux_array(data, data_label, app) | ||
# No special processing is needed in this case, but we include it for completeness | ||
elif isinstance(data, SpectrumList): | ||
pass | ||
|
@@ -77,19 +85,23 @@ | |
|
||
path = pathlib.Path(data) | ||
|
||
if path.is_file(): | ||
if path.is_dir() or load_as_list: | ||
data = SpectrumList.read(str(path), format=format) | ||
if data == []: | ||
raise ValueError(f"`specutils.SpectrumList.read('{str(path)}')` " | ||
"returned an empty list") | ||
elif path.is_file(): | ||
try: | ||
data = [Spectrum1D.read(str(path), format=format)] | ||
data_label = [app.return_data_label(data_label, alt_name="specviz_data")] | ||
data = Spectrum1D.read(str(path), format=format) | ||
if data.flux.ndim == 2: | ||
data, data_label = split_spectrum_with_2D_flux_array(data, data_label, app) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In our file case, there are 2 data extensions, with the 1st having a 1d flux array, and the 2nd one having the nd flux array. Without the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what I've done should fix this bug. It occurs when there's 2D spectra in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, with your PR it loads correctly, but your PR is basically unpacking the nd arrays into individual Spectrum1D objects, which this draft code is also doing. In principle, with the change here, we don't need to manually unpack the nd fluxes inside the loader. This draft code should work loading nd-flux data more generally, outside of the specific mwm loader There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I've already reverted that back to how it was initially, see specutils#1185, ca4d8c |
||
else: | ||
data = [data] | ||
data_label = [app.return_data_label(data_label, alt_name="specviz_data")] | ||
|
||
except IORegistryError: | ||
# Multi-extension files may throw a registry error | ||
data = SpectrumList.read(str(path), format=format) | ||
elif path.is_dir(): | ||
data = SpectrumList.read(str(path), format=format) | ||
if data == []: | ||
raise ValueError(f"`specutils.SpectrumList.read('{str(path)}')` " | ||
"returned an empty list") | ||
else: | ||
raise FileNotFoundError("No such file: " + str(path)) | ||
|
||
|
@@ -246,3 +258,20 @@ | |
spec = Spectrum1D(flux=fnuall * flux_units, spectral_axis=wlall * wave_units, | ||
uncertainty=unc) | ||
return spec | ||
|
||
|
||
def split_spectrum_with_2D_flux_array(data, data_label, app): | ||
temp_data = [] | ||
temp_data_label = [] | ||
for i in range(data.flux.shape[0]): | ||
unc = None | ||
mask = None | ||
if data.uncertainty is not None: | ||
unc = data.uncertainty[i, :] | ||
if mask is not None: | ||
mask = data.mask[i, :] | ||
temp_data.append(Spectrum1D(flux=data.flux[i, :], spectral_axis=data.spectral_axis, | ||
uncertainty=unc)) | ||
temp_data_label.append(f'{app.return_data_label(data_label, alt_name="specviz_data")}[{i}]') # noqa | ||
|
||
return temp_data, temp_data_label |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is added to support a specific telescope/instrument, please state also.