astropy / pyregion

ds9 region parser for python
https://pyregion.readthedocs.io
MIT License
39 stars 38 forks source link

Header errors in pyregion 1.2 #124

Closed janerigby closed 6 years ago

janerigby commented 6 years ago

A year ago, I wrote a tutorial showing how to use pyregion for simple aperture photometry. It worked then, but now the same code breaks.

Here's the tutorial: https://github.com/janerigby/NB/blob/master/Example%20pyds9%20and%20pyregion.ipynb

The error seems to be from get_mask(): "ValueError: header must be an instance of pyfits.Header or astropy.io.fits.Header".

Below is a an awkward workaround using get_filter() and then mask(), but I'd really like to solve the underlying issue -- why pyregion doesn't like the fits header.

import numpy as np
from astropy.io import fits
from astropy.utils.data import download_file
import pyregion

# Here is a short example showing what breaks.
image_file = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )
f = fits.open(image_file)
dum = f[0].data  # screwing around to get hdu as pyregions wants it
im = dum.astype(np.float64)
reg = 'image;circle(727.0,603.0,21)' # example region
object_region = pyregion.parse(reg)
total_flux = np.nansum(object_region.get_mask(f[0]) * im)   # get_mask breaks, complains of header

#This is my awkward workaround, using get_filter() and then mask()
myfilter = object_region.get_filter()
total_flux = np.nansum(myfilter.mask(im) * im)  #nansum treats nans as zeros for summation
bsipocz commented 6 years ago

@janerigby - I've edited the comment above, so the snippet is copy-pasteable

What versions do you use? This works without errors for me with the latest astropy (2.0.2) and pyregion (2.0)

bsipocz commented 6 years ago

Alternatively, if for some reason you cannot update pyregion, then you need to downgrade astropy to <1.3, as those rather old deprecations were removed in that version.

larrybradley commented 6 years ago

@janerigby Your example above works fine for me too (using pytest 2.0 and astropy 2.0.2).

janerigby commented 6 years ago

Thanks for the help, all. It turns out the error was caused by a mismatch between an old pyregion (1.2) and a new astropy (2.0). I have forced an upgrade of pyregion, and now it works.