1

I am performing aperture photometry over thousands of images and have this piece of code in my module.

b_aperture = SkyCircularAperture(b_position, r= r*u.arcsec)
b_annulus_aperture = SkyCircularAnnulus(b_position, r_in= r_in*  u.arcsec, r_out= r_out* u.arcsec) 
b_ap_pix = b_aperture.to_pixel(w_n)
b_ap_pix_mask= b_ap_pix.to_mask(method='exact')[0]
c_img_data = b_ap_pix_mask.apply(masked_img_aper)

This works fine on most of the images but in some of the images it triggers following error;

> <ipython-input-41-d3d69b9fd615> in <module>()
 51         b_ap_pix = b_aperture.to_pixel(w_n)
 52         b_ap_pix_mask= b_ap_pix.to_mask(method='exact')[0]   
 53         c_img_data = b_ap_pix_mask.apply(masked_img_aper)
 55         b_phot_table = aperture_photometry(masked_img_aper, b_aperture, method ='exact', wcs =w_n )
 /Users/aprakash/Library/Enthought/Canopy/edm/envs/User/lib/python3.5/site-packages/photutils/aperture/core.py in apply(self, data, fill_value)
719         """ 
721         return self.cutout(data, fill_value=fill_value) * self.data
722 
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'}

This error usually happens when the variables are not defined but I have checked that the variable here "masked_img_aper" is defined and looks like a normal image. The mask "b_ap_pix_mask" also look normal like other masks on previous images. So, I am not sure what's going on and how to fix this. My code runs in a loop over thousands of images and this is only happening for a few images which breaks down the code. I would like to circumvent this issue (possibly by using "if" statement) or fix it.

I tried following but it didn't work;

if (isinstance(np.array(b_ap_pix_mask.apply(masked_img_aper)), NoneType) == False;

Any ideas would be appreciated. Best, Abhi

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Abhishek
  • 49
  • 1
  • 7
  • What you tried would always evaluate to `False` (also note, in Python you don't need to write `== False`; unctions like `isinstance` already return a `True` or `False` value that can be evaluated directly by the `if` statement). You're wrapping the result of `b_ap_pix_mask.apply(masked_img_aper)` (which is already probably a Numpy array) with `np.array()` which, if it succeeds, will always return a Numpy `ndarray` object, not `None`. From the traceback you can see that the exception is not originating directly from your code, but rather from code in `photutils.aperture.core`. – Iguananaut Apr 28 '18 at 16:21
  • I also don't see a line of code like that in the current version of the code. What version of photutils are you using? Also, is that the full traceback that you got, or is there more to it? I think there's probably more to it. – Iguananaut Apr 28 '18 at 16:26
  • @Iguananaut thanks for looking into it. I did 2 things; 1) uninstall photoutils reinstall it. "Successfully installed photutils-0.4". 2) Upgraded b_ap_pix_mask.apply(masked_img_aper) ------> b_ap_pix_mask.multiply(masked_img_aper). The result is still the same. The message I posted is the whole error message. – Abhishek Apr 28 '18 at 21:53

2 Answers2

0

It looks like this is a bug that was fixed in the current version under development: https://github.com/astropy/photutils/pull/646

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
-1

Okay, I have found a way to circumvent this issue using following;

try:
    c_img_data = b_ap_pix_mask.multiply(masked_img_aper)
except:
    print('Error calling b_ap_pix_mask.multiply(), ignoring')
    pass

This will just skip the image in the loop. Since, I am working with thousands of images, leaving out a few won't make much of a difference.

Abhishek
  • 49
  • 1
  • 7
  • In that case you might at the very least only catch specific exceptions, like `except TypeError`, though better still you could check first ("look before you leap") for the conditions specifically described in the but report at https://github.com/astropy/photutils/pull/646 "when the aperture x and y positions were both negative and the aperture had no overlap with the data". Or, at the very least don't use a bare `except:` that will also ignore any other exceptions that might not be expected... – Iguananaut Apr 29 '18 at 17:13