-
Notifications
You must be signed in to change notification settings - Fork 75
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
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3229 +/- ##
==========================================
+ Coverage 88.62% 88.63% +0.01%
==========================================
Files 125 125
Lines 18775 18803 +28
==========================================
+ Hits 16639 16666 +27
- Misses 2136 2137 +1 ☔ View full report in Codecov by Sentry. |
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 comment
The 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 load_as_list
flag, this block only loads the first extension and never reaches line 97. With load_as_list
set, which is the way I think we need to load these files, I get the error
File [~/Work/jdaviz/jdaviz/configs/specviz/plugins/parsers.py:129](http://localhost:8888/lab/workspaces/auto-b/tree/~/Work/jdaviz/jdaviz/configs/specviz/plugins/parsers.py#line=128), in specviz_spectrum1d_parser(app, data, data_label, format, show_in_viewer, concat_by_file, cache, local_path, timeout, load_as_list)
127 for wlind in range(len(spec.spectral_axis)):
128 wlallorig.append(spec.spectral_axis[wlind].value)
--> 129 fnuallorig.append(spec.flux[wlind].value)
131 # because some spec in the list might have uncertainties and
132 # others may not, if uncert is None, set to list of NaN. later,
133 # if the concatenated list of uncertanties is all nan (meaning
134 # they were all nan to begin with, or all None), it will be set
135 # to None on the final Spectrum1D
136 if spec.uncertainty[wlind] is not None:
File [~/anaconda3/envs/newsv/lib/python3.11/site-packages/astropy/units/quantity.py:1302](http://localhost:8888/lab/workspaces/auto-b/tree/~/anaconda3/envs/newsv/lib/python3.11/site-packages/astropy/units/quantity.py#line=1301), in Quantity.__getitem__(self, key)
1297 return self._new_view(
1298 self.view(np.ndarray)[key], self.unit[key], propagate_info=False
1299 )
1301 try:
-> 1302 out = super().__getitem__(key)
1303 except IndexError:
1304 # We want zero-dimensional Quantity objects to behave like scalars,
1305 # so they should raise a TypeError rather than an IndexError.
1306 if self.isscalar:
IndexError: index 1 is out of bounds for axis 0 with size 1
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.
I think what I've done should fix this bug. It occurs when there's 2D spectra in a SpectrumList
.
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.
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 comment
The 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
Should Most of the multi-extension default loaders are only implemented as |
Also on metadata -- we could make the 2D-to-1D converter split list-like metadata objects with the same length as |
Co-authored-by: rileythai <[email protected]>
@havok2063 @rileythai I pulled in Riley's code from rosteen#6 handling the case of |
Whoops, messed up one thing, stand by... |
Alright, I debugged the previous commit and added some more test coverage, this is ready for another look. |
@rosteen @rileythai I've tested this PR with the latest version of astropy/specutils#1185 against the following file types: |
@havok2063 Great, I'll mark this as ready for review then. |
unc = data.uncertainty[i, :] | ||
if data.mask is not None: | ||
mask = data.mask[i, :] | ||
new_data.append(Spectrum1D(flux=data.flux[i, :], spectral_axis=data.spectral_axis, |
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.
What about the other properties like redshift, metadata, etc? Do they not have to be propagated?
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.
Also... is an utility function like this better suited for specutils upstream?
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.
I just added the meta, I had discussed with Brian and we agreed that it's ok to attach the meta from the original to each split spectrum at least for now. Redshift comes along with the spectral_axis
.
I think I'd prefer to keep this here for now, but we could potentially upstream it at some point. I don't know exactly where I'd want to put it in specutils
.
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.
OK, I opened astropy/specutils#1188
@@ -405,6 +405,24 @@ def test_load_spectrum_list_directory_concat(tmpdir, specviz_helper): | |||
assert len(specviz_helper.app.data_collection) == 41 | |||
|
|||
|
|||
def test_load_2d_flux(specviz_helper): | |||
spec = Spectrum1D(spectral_axis=np.linspace(4000, 6000, 10)*u.Angstrom, |
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.
Maybe add comments to explain what is going on here for future devs. Hard to tell from reading the code without more context.
CHANGES.rst
Outdated
@@ -15,6 +15,7 @@ Mosviz | |||
|
|||
Specviz | |||
^^^^^^^ | |||
- Specviz parser will now split a spectrum with a 2D flux array into multiple spectra on load. [#3229] |
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.
Co-authored-by: P. L. Lim <[email protected]>
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.
Since MAST is happy, I am approving by proxy and also the fact that the test added passes CI. Thanks!
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.
Tested this branch and could not replicate the bug. Code looks good as well so LGTM!
Thanks for the reviews! |
Closes #3223, opening as draft to see if this will fix the desired use case based on upstream changes. This splits a Spectrum1D with 2D flux into separate Spectrum1D objects before loading into Specviz.