Skip to content
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

Uncertainty handling in 'find_lines_threshold' leads to unit conversion errors #1153

Open
DavidT3 opened this issue Jul 18, 2024 · 0 comments

Comments

@DavidT3
Copy link

DavidT3 commented Jul 18, 2024

Setup
MacOS v12.1
Python v3.10
SpecUtils v1.15.0

File required to reproduce
cutdown_6dF_g2344182-042249.fits.fits.zip

Problem
I'm taking a look at this 6dFGS spectrum, loading it in with the Spectrum1D read method:

bcg1_spec_6df = Spectrum1D.read('spec/cutdown_6dF_g2344182-042249.fits.fits')

Everything loads in fine, I can access wavelength, flux, meta['sky'], etc. properties - however when I come to use a line-finding method:

find_lines_threshold(bcg1_spec_fast, 3)

An exception occurs:

UnitConversionError Traceback (most recent call last)
Cell In[118], line 1
----> 1 find_lines_threshold(bcg1_spec_6df, 3)
File /opt/anaconda3/envs/astropyspec/lib/python3.10/site-packages/specutils/analysis/flux.py:276, in warn_continuum_below_threshold..actual_decorator..wrapper(*args, **kwargs)
274 if conf.do_continuum_function_check:
275 spectrum = args[0]
--> 276 if not is_continuum_below_threshold(spectrum, threshold):
277 if hasattr(threshold, 'unit'):
278 levelorsnr = 'value'
File /opt/anaconda3/envs/astropyspec/lib/python3.10/site-packages/specutils/analysis/flux.py:257, in is_continuum_below_threshold(spectrum, threshold)
253 # If threshold does not have a unit, ie it is not a quantity, then
254 # we are going to calculate based on the S/N if the uncertainty
255 # exists.
256 if uncertainty and uncertainty.uncertainty_type != 'std':
--> 257 return np.median(flux / uncertainty.quantity) < threshold
258 else:
259 return np.median(flux) / mad_std(flux) < threshold
File /opt/anaconda3/envs/astropyspec/lib/python3.10/site-packages/astropy/units/quantity.py:696, in Quantity.array_ufunc(self, function, method, *inputs, **kwargs)
694 return NotImplemented
695 else:
--> 696 raise e
File /opt/anaconda3/envs/astropyspec/lib/python3.10/site-packages/astropy/units/quantity.py:641, in Quantity.array_ufunc(self, function, method, *inputs, **kwargs)
636 # Determine required conversion functions -- to bring the unit of the
637 # input to that expected (e.g., radian for np.sin), or to get
638 # consistent units between two inputs (e.g., in np.add) --
639 # and the unit of the result (or tuple of units for nout > 1).
640 try:
--> 641 converters, unit = converters_and_unit(function, method, *inputs)
643 out = kwargs.get("out", None)
644 # Avoid loop back by turning any Quantity output into array views.
File /opt/anaconda3/envs/astropyspec/lib/python3.10/site-packages/astropy/units/quantity_helper/converters.py:199, in converters_and_unit(function, method, *args)
197 converters[i] = None
198 else:
--> 199 raise UnitConversionError(
200 f"Can only apply '{function.name}' function to "
201 "dimensionless quantities when other argument is not "
202 "a quantity (unless the latter is all zero/infinity/nan)."
203 )
204 except TypeError:
205 # _can_have_arbitrary_unit failed: arg could not be compared
206 # with zero or checked to be finite. Then, ufunc will fail too.
207 raise TypeError(
208 "Unsupported operand type(s) for ufunc {}: '{}'".format(
209 function.name,
210 ",".join([arg.class.name for arg in args]),
211 )
212 )
UnitConversionError: Can only apply 'less' function to dimensionless quantities when other argument is not a quantity (unless the latter is all zero/infinity/nan).

Which seems to be down to the flux-to-uncertainty ratio still having units when compared to the integer/float threshold - I think this is because the 6dF read-in decorator declares the uncertainty as a 'VarianceUncertainty' instance (which as far as I know is the correct type of uncertainty) - but then the uncertainty.quantity object has units of count/angstrom squared, so when the flux quantity is divided by it there remains a unit.

I'm also working with some FAST spectroscopy data, for which I've written my own read-in function, and the same problem occurs because of the VarianceUncertainty, but if I change the declaration of uncertainty to 'StdDevUncertainty' before setting up the Spectrum1D instance (just as a test), then the line-finding functionality works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant