[AstroPy] Problem with the latest version of astropy and numpy

LAMBERT-HUYGHE Antigone antigone.lambert-huyghe at cea.fr
Tue Aug 27 03:24:40 EDT 2019

Ok, thank you very much. I didn't know that a kernel was a quantity.

De : AstroPy [astropy-bounces+antigone.lambert-huyghe=cea.fr at python.org] de la part de Simon Conseil [simon at sconseil.fr]
Envoyé : lundi 26 août 2019 17:04
À : astropy at python.org
Objet : Re: [AstroPy] Problem with the latest version of astropy and numpy


As mentioned in the ticket, you use units for the kernel (kernel.array is a Quantity) so at least this explains why the crash happen in a quantity method.


Le 26/08/2019 à 15:16, LAMBERT-HUYGHE Antigone a écrit :

So I report here the problem as a bug.

Here is the minimal example of my problem (with the imports) :

import astropy.units as u
from astropy.convolution import convolve
import numpy as np
from astropy.io import fits
from astropy.convolution import Gaussian2DKernel
from copy import deepcopy

#The aim is to convolve the map in the file file.fits from a resolution of 1.6 arcsec to 6 arcsec
inhdu = fits.open('file.fits')

fromfwhm = 1.6*u.arcsec
tofwhm = 6*u.arcsec

#calculate kernel size
f2s = 2.*np.sqrt(2*np.log(2))

pixel_size = (abs(inhdu[0].header['CDELT1']) *u.deg).to('arcsec')
#CDELT is a keyword in the header that gives the size of the pixel in an angular unit.

kernel_sigma = np.sqrt( (tofwhm)**2 - (fromfwhm)**2 ) / pixel_size / f2s

kernel = Gaussian2DKernel(stddev=kernel_sigma)

result = convolve(inhdu[0].data, kernel, boundary = 'fill', fill_value = float('NaN'), preserve_nan=True)

outhdu = hdudeepcopy(inhdu)
outhdu[0].data = result

outhdu.writeto(WriteTo, overwrite=True)

And, as a reminder, here is the error that I had:

~/Bureau/Codes/python_lib/mylib/fits.py in fits_convolve(infits, fromfwhm, tofwhm, GaussianTrim, WriteTo, pixkey, pixkey_unit, returnHDU, verbose)
    640             result[i, :, :] = convolve(plane, kernel, boundary = 'fill', fill_value = float('NaN'), preserve_nan=True)
    641     else:
--> 642         result = convolve(inhdu[0].data, kernel, boundary = 'fill', fill_value = float('NaN'), preserve_nan=True)
    643         #result = convolve_fft(inhdu[0].data, kernel)

~/.local/lib/python3.6/site-packages/astropy/nddata/decorators.py in wrapper(data, *args, **kwargs)
    244                                   AstropyUserWarning)
--> 246             result = func(data, *args, **kwargs)
    248             if unpack and repack:

~/.local/lib/python3.6/site-packages/astropy/convolution/convolve.py in convolve(array, kernel, boundary, fill_value, nan_treatment, normalize_kernel, mask, preserve_nan, normalization_zero_tol)
    336     if normalize_kernel:
    337         if not nan_interpolate:
--> 338             result /= kernel_sum
    339     else:
    340         if nan_interpolate:

~/.local/lib/python3.6/site-packages/astropy/units/quantity.py in __array_ufunc__(self, function, method, *inputs, **kwargs)
    473             return result
--> 475         return self._result_as_quantity(result, unit, out)
    477     def _result_as_quantity(self, result, unit, out):

~/.local/lib/python3.6/site-packages/astropy/units/quantity.py in _result_as_quantity(self, result, unit, out)
    512         # the output is of the correct Quantity subclass, as it was passed
    513         # through check_output.
--> 514         out._set_unit(unit)
    515         return out

AttributeError: 'numpy.ndarray' object has no attribute '_set_unit'

Thank you for all the answeres that I had here.

AstroPy mailing list
AstroPy at python.org<mailto:AstroPy at python.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20190827/f54d2f50/attachment.html>

More information about the AstroPy mailing list