sunpy / ndcube

A base package for multi-dimensional contiguous and non-contiguous coordinate-aware arrays. "Maintainers": @danryanirish & @Cadair
http://docs.sunpy.org/projects/ndcube/
BSD 2-Clause "Simplified" License
44 stars 47 forks source link

Quantity propagation causes failure with specutils.Spectrum1D.rebin #717

Closed baileyji closed 4 months ago

baileyji commented 4 months ago

Describe the bug

Not sure if this is going to be a specutils or ndcube issue, but in attempting to use specutils.Spectrum1D.rebin() I've run into the following issue. At a minimum the resulting error is profoundly unhelpful.

from specutils import Spectrum1D
import astropy.units as u
import numpy as np
import astropy, ndcube, specutils, sys
print(sys.version, astropy.__version__, ndcube.__version__, specutils.__version__)
y=np.arange(4000)*u.ct
x=np.arange(200, 4200)*u.nm
spec = Spectrum1D(flux=y, spectral_axis=x, bin_specification='centers', mask=x>2000*u.nm)
spec.rebin((10,), operation=np.sum, operation_ignores_mask=False)
> 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40) [Clang 14.0.6 ] 6.1.0 2.2.0 1.15.0
> 
> 
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
> Cell In[105], line 9
>       7 x=np.arange(200, 4200)*u.nm
>       8 spec = Spectrum1D(flux=y, spectral_axis=x, bin_specification='centers', mask=x>2000*u.nm)
> ----> 9 spec.rebin((10,), operation=np.sum, operation_ignores_mask=False)
> 
> File ~/miniforge3/envs/zshooter/lib/python3.11/site-packages/ndcube/ndcube.py:1178, in NDCube.rebin(self, bin_shape, operation, operation_ignores_mask, handle_mask, propagate_uncertainties, new_unit, **kwargs)
>    1175 new_wcs = ResampledLowLevelWCS(self.wcs.low_level_wcs, bin_shape[::-1])
>    1177 # Reform NDCube.
> -> 1178 new_cube = type(self)(new_data, new_wcs, uncertainty=new_uncertainty, mask=new_mask,
>    1179                       meta=self.meta, unit=new_unit)
>    1180 new_cube._global_coords = self._global_coords
>    1181 # Reconstitute extra coords
> 
> File ~/miniforge3/envs/zshooter/lib/python3.11/site-packages/specutils/spectra/spectrum1d.py:117, in Spectrum1D.__init__(self, flux, spectral_axis, wcs, velocity_convention, rest_value, redshift, radial_velocity, bin_specification, **kwargs)
>     115 if flux is not None:
>     116     if not isinstance(flux, u.Quantity):
> --> 117         raise ValueError("Flux must be a `Quantity` object.")
>     118     elif flux.isscalar:
>     119         flux = u.Quantity([flux])
> 
> ValueError: Flux must be a `Quantity` object.

To Reproduce

from specutils import Spectrum1D
import astropy.units as u 
import numpy as np
import astropy, ndcube, specutils, sys
print(sys.version, astropy.__version__, ndcube.__version__, specutils.__version__)
y=np.arange(4000)*u.ct
x=np.arange(200, 4200)*u.nm
spec = Spectrum1D(flux=y, spectral_axis=x, bin_specification='centers', mask=x>2000*u.nm)
spec.rebin((10,), operation=np.sum, operation_ignores_mask=False)

System Details

3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40) [Clang 14.0.6 ] astropy: 6.1.0 ndcube: 2.2.0 specutils: 1.15.0

Installation method

conda

nabobalis commented 4 months ago

Thank you for the bug report.

I can replicate this using the release version. The version of ndcube thats is in main (aka the next version) does not have this problem.

I will try to track down what is going on.

nabobalis commented 4 months ago

I think I have this fixed, I am in the process of doing a release with this change.