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

Need help with some functionalities #191

Closed pjpessi closed 4 years ago

pjpessi commented 4 years ago

Hi, I am trying to switch from IRAF to Python so I tried this package but I think I might be missing something. I need to get the wcs coordinates and the fwhm of some stars in an image I have. To do this in IRAF I run imexa -> a , and that does the trick. I can not figure out how to obtain that sort of data with this package. Could someone help me out? Cheers.

pjpessi commented 4 years ago

Sorry, maybe I should've said, that the a option here after I run viewer.imexam() display the following error: TypeError Traceback (most recent call last)

in 2 viewer.load_fits('b0001_reg.fits') 3 viewer.scale() ----> 4 viewer.imexam() ~/anaconda2/envs/py3/lib/python3.7/site-packages/imexam/connect.py in imexam(self) 150 self._run_event_imexam() 151 else: --> 152 self._run_imexam() 153 else: 154 warnings.warn("No valid image loaded in viewer") ~/anaconda2/envs/py3/lib/python3.7/site-packages/imexam/connect.py in _run_imexam(self) 266 self._check_slice() 267 self.exam.do_option( --> 268 x, y, current_key) 269 except KeyError: 270 print( ~/anaconda2/envs/py3/lib/python3.7/site-packages/imexam/imexamine.py in do_option(self, x, y, key) 189 """ 190 self.log.debug("pressed: {0}, {1:s}".format(key, self.imexam_option_funcs[key][0].__name__)) --> 191 self.imexam_option_funcs[key][0](x, y, self._data) 192 193 def get_options(self): ~/anaconda2/envs/py3/lib/python3.7/site-packages/imexam/imexamine.py in aper_phot(self, x, y, data, fig) 566 # The bkg sum with the circular aperture is then 567 # then mean local background tims the circular apreture area. --> 568 aperture_area = apertures.area() 569 annulus_area = annulus_apertures.area() 570 TypeError: 'float' object is not callable
pjpessi commented 4 years ago

Update: this issue occurs when using python3, numpy version 1.17.2 but not when using python2.7 numpy version 1.16.4. I tried downgrading the numpy version in python3 but didn't solve the problem

sosey commented 4 years ago

try installing from the current master, with python3, and see if you get the same issue?

pjpessi commented 4 years ago

@sosey After having Successfully installed imexam-0.8.2a1.dev26+g86e8616 , the issue keeps happening with the same error message, right now I am running the package in python 2.7, but would be great to run it in python 3.

sosey commented 4 years ago

definitely - the master branch is meant to be running in python 3 only, give that a go!

pjpessi commented 4 years ago

@sosey I still get the following error when runing imexam+a in python 3:

/anaconda2/envs/py3/lib/python3.7/site-packages/imexam/imexamine.py in aper_phot(self, x, y, data, fig)
    566                 # The bkg sum with the circular aperture is then
    567                 # then mean local background tims the circular apreture area.
--> 568                 aperture_area = apertures.area()
    569                 annulus_area = annulus_apertures.area()
    570 

TypeError: 'float' object is not callable

It seems like this option has a problem calling a float. I don't have issues when running other options (letters).

In addition I wanted to ask you if there is any way to get the FWHM automatically (without having to click on the stars) once I know the coordinates of my stars. I want to use plots.aper_phot, as appear in the documentation but when using my coordinates I get nothing. I do get a blank plot when using the data from the examples random array.

pjpessi commented 4 years ago

Ok, my guess now is that I am too dumb to understand this package. @sosey if you ever have time maybe I can tell you what I wanna do and you can walk me through. If not thanks anyway for writing the code. Cheers

sosey commented 4 years ago

@pjpessi you are not too dumb! If you can't find the information it's because I probably need to organize it better. Don't be discouraged...you managed to ping me during a few weeks where I am working overtime and traveling.

One place to look for information on how to run the package without any GUI interaction is here: https://imexam.readthedocs.io/en/latest/imexam/examples.html#return-information-to-variables-without-plotting

the methods for imexam.Imexamine may also be called through the library, and save plots directly, or return the information that used to make them as arrays...or as the information itself. You can call the functions in imexam.math_helper as well.

I'm traveling again tomorrow, but the plane time might give me a chance to make an example for you.

pjpessi commented 4 years ago

@sosey Thanks for the reply, I've read the docs in the link you sent. I found Imexamine. I have a coordinates array and I want the FWHM of the stars at those coordinates. I found that plots.aper_phot() return that. Although I am not able to return anything. Only a blank (no star or anything just the green and red circles) plot when using a toy array.

If you could send me an example it would be great. I'll wait.

Thanks again for answering while being busy, I understand that you might have a lot of work.

sosey commented 4 years ago

Here's an example that doesn't use a display connection:

Start by creating a control object

from imexam.imexamine import Imexamine from astropy.io import fits # to get my data array i=Imexamine()

grab the data you want and attach it to the control object i:

data = fits.getdata('iacs01t4q_flt.fits') i.set_data(data)

x=476 y=463

get the results and save them to variables

amplitude, xcenter, ycenter, xsigma, ysigma = i.gauss_center(x,y)

Now we'd like to calculate the gaussian FWHM for this star, we'll use one of the functions in math_helper:

from imexam.math_helper import gfwhm fwhmx, fwhmy = gfwhm(xsigma, ysigma)

If you want to do this for a list of objects, one way would be to iterate over the locations and save to a dict:

star_locations = [(476, 463), (621, 604), (426, 627)] star_measures={} for x,y in star_locations: amplitude, xcenter, ycenter, xsigma, ysigma = i.gauss_center(x,y) fwhmx, fwhmy = gfwhm(xsigma, ysigma) star_measures[(x,y)] = [amplitude, xcenter, ycenter, xsigma, ysigma, fwhmx, fwhmy]

In [30]: for x,y in star_locations: ...: amplitude, xcenter, ycenter, xsigma, ysigma = i.gauss_center(x,y) ...: fwhmx, fwhmy = gfwhm(xsigma, ysigma) ...: star_measures[(x,y)] = [amplitude, xcenter, ycenter, xsigma, ysigma, fwhmx, fwhmy] ...:
xc=476.079945 yc=463.334336 xc=621.746534 yc=604.025901 xc=426.098969 yc=627.217134

In [31]: star_measures
Out[31]: {(476, 463): [186.85484771427207, 476.07994512670734, 463.33433586987957, 0.5884975412258683, 0.6835437241428306, 1.3858058065301022, 1.6096224632666432], (621, 604): [209.5162833512038, 621.7465337987292, 604.0259014678302, 0.6310587900801216, 0.5938579399164946, 1.4860298884736483, 1.3984285808161465], (426, 627): [21.245743148440987, 426.0989685548685, 627.2171342445665, 0.5735289443714519, 0.6140807497649734, 1.350557454611335, 1.4460496588141938]}

if you have a viewer like DS9 connected, you can mark the locations of the stars on the viewer like so, it will use the (x,y) locations that are the keys into the star_measures dictionary:

a.mark_region_from_array(star_measures) # a is the object linked to my ds9

If you want to display a plot, you can see the fitting models that are available:

In [43]: i.show_fit_models()
The available astropy models for fittingare: ['Gaussian1D', 'Moffat1D', 'MexicanHat1D', 'AiryDisk2D', 'Polynomial1D']

Display the line fit to the star with the fit for a 1D Gaussian

i.line_fit(x,y) example

I saved the image above using i.save('example.jpg')

pjpessi commented 4 years ago

@sosey thanks a lot for the example, it worked ok. Could you tell me where to find the documentation for math_helper.gfwhm?, I can not find it. Or let me know why is there a fwhmx and fwhmy?. Thanks again.

sosey commented 4 years ago

In general when you are in a notebook or ipython session you can use syntax like this to get the document string help for a function: imexam.math_helper.gfwhm?. I don't think I have more extensive documentation about the gaussian fwhm function at this point. The function is returning the fwhm for each dimension, x and y, as it's not expecting symmetry.

pjpessi commented 4 years ago

@sosey Ok, great. Thank you very much.