-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
For tabular_fits
output, force uncertainty
to StdDev
#1027
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1027 +/- ##
==========================================
- Coverage 70.01% 70.00% -0.02%
==========================================
Files 64 64
Lines 4346 4350 +4
==========================================
+ Hits 3043 3045 +2
- Misses 1303 1305 +2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Thanks for this! Unfortunately I tried to write a test, and the very first SDSS file I tried this on threw a I think there are two ways we can resolve this:
Any opinions on those two options? |
unc = ( | ||
spectrum | ||
.uncertainty | ||
.represent_as(StdDevUncertainty) | ||
.quantity | ||
.to(funit, equivalencies=u.spectral_density(disp)) | ||
) | ||
columns.append(unc.astype(ftype)) | ||
colnames.append("uncertainty") |
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.
unc = ( | |
spectrum | |
.uncertainty | |
.represent_as(StdDevUncertainty) | |
.quantity | |
.to(funit, equivalencies=u.spectral_density(disp)) | |
) | |
columns.append(unc.astype(ftype)) | |
colnames.append("uncertainty") | |
if spectrum.uncertainty.uncertainty_type == 'var': | |
uunit = funit**2 | |
elif spectrum.uncertainty.uncertainty_type == 'ivar': | |
uunit = funit**-2 | |
else: | |
uunit = funit | |
unc = spectrum.uncertainty.quantity.to(uunit, equivalencies=u.spectral_density(disp)) | |
columns.append(unc.astype(ftype)) | |
colnames.append(f"{spectrum.uncertainty.uncertainty_type}") |
The original behaviour definitely needs fixing, but I'd strongly recommend to allow preserving the original type. This could be one approach to set consistent units.
I'd very much vote for this option – there may be use cases for choosing any of the 3 uncertainty types, and the writer should not needlessly force them to a certain type. Zeros ending up as Inf in the transformation as in your test case are just one example. This would also get in the way of better round-tripping of standard spectral formats @kelle has brought on the agenda. specutils/specutils/io/parsing_utils.py Lines 268 to 270 in 1c2ba04
to the search for matching unit powers for Variance and InverseVariance, but that would need some agreement on the order of precedence, and perhaps colnames should be considered in addition.
|
Thanks for the thoughts @dhomeier - I think I agree that keeping the original type is the way to go, but that's a big enough change (I think) that I'm leaning toward adding a slightly better warning to this and merging this, and then opening a separate follow-up PR for your suggestion to merge into the |
Agreed, from experience with the wcs1d loader that would become a rather more complex endeavour. |
In the ``tabular_fits_writer``, the uncertainty is written out as the standard deviation. Some readers, however, load 1D spectra into ``Spectrum1D`` with the uncertainty specified as the inverse variance. Without explicitly converting the uncertainty to standard deviation, the code raises a ``astropy.units.core.UnitConversionError`` for these cases when trying to conform the units of the uncertainty to those of the spectral flux array. This commit utilizes the ``represent_as`` functionality of ``astropy.nddata.NDUncertainty`` arrays to force the representation of the uncertainty in standard deviation form before checking units and writing to FITS. modified: specutils/io/default_loaders/tabular_fits.py
f15ea4a
to
3153d57
Compare
In the
tabular_fits_writer
, the uncertainty is written out as the standard deviation. Some readers (e.g., SDSS), however, load 1D spectra intoSpectrum1D
with the uncertainty specified as the inverse variance. Without explicitly converting the uncertainty to standard deviation, the code raises aastropy.units.core.UnitConversionError
for these cases when trying to conform the units of the uncertainty to those of the spectral flux array using the standardtabular_fits_writer
with theSpectrum1D.write()
method.This PR utilizes the
represent_as
functionality ofastropy.nddata.NDUncertainty
arrays to force the representation of the uncertainty in standard deviation form before checking units and writing to FITS.