radio-astro-tools / spectral-cube

Library for reading and analyzing astrophysical spectral data cubes
http://spectral-cube.rtfd.org
BSD 3-Clause "New" or "Revised" License
95 stars 61 forks source link

Slicing masked data throws TypeError with numpy >=1.24.2 #884

Closed drtobybrown closed 6 months ago

drtobybrown commented 1 year ago

spectral-cube version 0.6.1.dev263+ge4f31e3

(Upgraded from 0.6 for the same reason given in #880)

Problem:

TypeError: Field elements must be 2- or 3-tuples, got 'dtype('float64')'

MWE:

from spectral_cube import SpectralCube
from astropy.wcs import WCS
import astropy.units as u

wcs_dict = {
'CTYPE1': 'WAVE    ', 'CUNIT1': 'Angstrom', 'CDELT1': 0.2, 'CRPIX1': 0, 'CRVAL1': 10, 'NAXIS1': 5,
'CTYPE2': 'HPLT-TAN', 'CUNIT2': 'deg', 'CDELT2': 0.5, 'CRPIX2': 2, 'CRVAL2': 0.5, 'NAXIS2': 4,
'CTYPE3': 'HPLN-TAN', 'CUNIT3': 'deg', 'CDELT3': 0.4, 'CRPIX3': 2, 'CRVAL3': 1, 'NAXIS3': 3}
wcs = WCS(wcs_dict)
data = np.ones((3,5,4)) * u.Jy

cube = SpectralCube(data=data, wcs=wcs)
cube = cube.with_mask(cube < 1 * u.Jy)

cube[0]
~/.conda/envs/gistdev/lib/python3.9/site-packages/spectral_cube/masks.py in _filled(self, data, wcs, fill, view, use_memmap, **kwargs)
    226         # Must convert to floating point, but should not change from inherited
    227         # type otherwise
--> 228         dt = np.result_type([data.dtype, 0.0])
    229 
    230         if use_memmap and data.size > 0:

~/.conda/envs/gistdev/lib/python3.9/site-packages/numpy/core/overrides.py in result_type(*args, **kwargs)

TypeError: Field elements must be 2- or 3-tuples, got 'dtype('float64')'

Fix: I think removing the square brackets in dt = np.result_type([data.dtype, 0.0]) should fix the issue.

keflavich commented 1 year ago

Looks like this was solved earlier today: https://github.com/radio-astro-tools/spectral-cube/pull/883

tvwenger commented 1 year ago

@keflavich @drtobybrown Hello fellow radio astronomers! Toby is right that the change introduced by https://github.com/radio-astro-tools/spectral-cube/pull/883 had incorrect syntax for numpy.result_type. I have just submitted a PR fix!

keflavich commented 1 year ago

ah, sorry, I read things wrong - the bug was introduced today. Thanks for the fix; we clearly need test coverage.

keflavich commented 6 months ago

Solved by https://github.com/radio-astro-tools/spectral-cube/pull/886