spacetelescope / imexam

imexam is a python tool for simple image examination, and plotting, with similar functionality to IRAF's imexamine
http://imexam.readthedocs.io
BSD 3-Clause "New" or "Revised" License
74 stars 45 forks source link

Imexamine radial_profile: background subtraction gives 'UFuncTypeError' #219

Open ross-dobson opened 3 years ago

ross-dobson commented 3 years ago

I'm not using a viewer, so I have it set up that:

from imexam.imexamine import Imexamine
plots=Imexamine()

I then have an image, that I am attempting to generate the radial profile of, loaded in from a FITS as img.

plots.radial_profile(x=581,y=1907,data=img,genplot=True)

This outputs:

xc=581.2780 yc=1906.6478
Background per pixel: 2124.4217152419224
---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-32-c96832faaa47> in <module>()
----> 1 plots.radial_profile(x=581,y=1907,data=img1,genplot=True)

/usr/local/lib/python3.6/dist-packages/imexam/imexamine.py in radial_profile(self, x, y, data, form, genplot, fig)
   1161                 self.log.info("Sky background negative, setting to zero")
   1162             self.log.info(f"Background per pixel: {sky_per_pix}")
-> 1163             flux -= sky_per_pix
   1164 
   1165             if getdata:

UFuncTypeError: Cannot cast ufunc 'subtract' output from dtype('float64') to dtype('uint16') with casting rule 'same_kind'

If I disable the background parameter in the radial_profile_pars dict (not done in the most Pythonic way, I admit):

d=imexam.imexam_defpars.radial_profile_pars
d['background'] = [False, 'Subtract background? [bool]']

The function then runs as expected, (because it skips the line causing the UFuncTypeError). However, I would quite like the background subtraction. I wonder if this may just be as simple as changing line in radial_profile shown at 1163 to be flux = flux - sky_per_pix, rather than using -=?

I also notice this is similar to a problem with rimexam, with a similar fix suggested in issue #210:

The offending line seems to be

flux -= sky_per_pix This seems relevant: https://techoverflow.net/2019/05/22/how-to-fix-numpy-typeerror-cannot-cast-ufunc-subtract-output-from-dtypefloat64-to-dtypeint64-with-casting-rule-same_kind/

Is it as simple as making the line

flux = flux-sky_per_pix

?

Originally posted by @karlglazebrook in https://github.com/spacetelescope/imexam/issues/210#issuecomment-671664422

I tried changing this in a new function and adding it with .register(), but I wasn't sure on exactly how to register the function, so wasn't able to check this myself.


~Additionally, when I have had it working (i.e. without the background subtraction), I am getting clearly inaccurate FWHM values (nearly 6 rather than nearly 2.5, measured in IRAF), similar to issue 165 .~ EDIT: Apologies, mistake on my half. IRAF was returning the half-FWHM, so the FWHM is being returned correctly. However, the main issue still persists.