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

LAMBERT-HUYGHE Antigone antigone.lambert-huyghe at cea.fr
Mon Aug 26 09:16:47 EDT 2019


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)
    644

~/.local/lib/python3.6/site-packages/astropy/nddata/decorators.py in wrapper(data, *args, **kwargs)
    244                                   AstropyUserWarning)
    245
--> 246             result = func(data, *args, **kwargs)
    247
    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
    474
--> 475         return self._result_as_quantity(result, unit, out)
    476
    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
    516

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

Thank you for all the answeres that I had here.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20190826/15bf3a6d/attachment-0001.html>


More information about the AstroPy mailing list