Changeset 20279
- Timestamp:
- Oct 20, 2008, 2:34:25 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sj_ippTests_branch_20080929/ippTests/compIPPphoto.py
r20142 r20279 751 751 box() 752 752 753 753 def gaussResid(p,yx,data,blankRadius=0,blankInner=True): 754 """return residuals after fitting two-D gaussian with parameters p 755 = scale,x0,xsig, y0, ysig, theta, blanking pixels inside or 756 outside blankRadius, depending on whether blankInner = True or 757 False""" 758 from numpy import reshape 759 scale,x0,xsig,y0,ysig,theta = p 760 model=scale*gauss2D(yx,x0,xsig,y0,ysig,theta) 761 win.showArray(data-model) 762 resid = reshape(data,(data.shape[0]*data.shape[1],))-reshape(model,(model.shape[0]*model.shape[1],)) 763 if (blankRadius > 0): 764 # set residuals within or outside blankRadius to 0, i.e. set model = data there 765 x=yx[1] 766 y=yx[0] 767 if blankInner: 768 inradius = (((x-x0)**2 + (y-y0)**2) <= blankRadius**2) 769 else: 770 inradius = (((x-x0)**2 + (y-y0)**2) > blankRadius**2) 771 inradius = reshape(inradius,(inradius.shape[0]*inradius.shape[1],)) 772 resid[inradius==1] = 0 773 return resid 774 775 def fitGauss(yx,data,p0,blankrad,blankInner): 776 """fit gaussian to data with starting parameters in p0""" 777 from scipy.optimize import leastsq 778 from numpy import reshape 779 # need to add weighting by errors, if we know them 780 plsq = leastsq(gaussResid,p0,args=(yx,data,blankrad,blankInner)) 781 bestp = plsq[0] 782 # print plsq[1] 783 scale,x0,xsig,y0,ysig,theta = bestp 784 # remove 360 degree ambivalence 785 theta -= int(theta/180)*180 786 # make sure xsig is major axis 787 if xsig < ysig: 788 tmp=xsig 789 xsig=ysig 790 ysig=tmp 791 theta += 90 792 # make sure theta is in [-90,90] 793 if theta > 90: 794 theta -= 180 795 bestp=(scale,x0,xsig,y0,ysig,theta) 796 resid=reshape(gaussResid(bestp,yx,data,blankrad),data.shape) 797 mod=data-resid 798 return bestp,resid,mod 799 800 def gauss2D(coordgrid,x0,xsig,y0,ysig,theta=0): 801 """compute a normalized gaussian at positions given in 2d-array 802 coords, centered at x0, y0, with xsig, ysig for theta=0; 803 coordinates in coordgrid given by ogrid""" 804 from numpy import cos,sin,pi,exp 805 cth = cos(theta/180.*pi) 806 sth = sin(theta/180.*pi) 807 808 x=coordgrid[1]-x0 809 y=coordgrid[0]-y0 810 res = exp(-0.5*(((x*cth+y*sth)/xsig)**2+((-x*sth+y*cth)/ysig)**2)) 811 return res/(2*pi*xsig*ysig) 812 813 def doGaussPsfPhotometry(infile,initialGuessList=[(2000.,1011.,2.,1176.,2.,0)],\ 814 blankrad=15,defHalfwinsize=15,HDU=0): 815 """Fit Gaussians to image in infile's HDU near the 816 positions and with fluxes and sigmas given in initialGuessList""" 817 import pyfits 818 from numpy import nan 819 f_handle=pyfits.open(infile) 820 img = f_handle[HDU].data 821 fitlist = [] 822 for initialGuessTuple in initialGuessList: 823 scale0,x0,xsig0,y0,ysig0,theta0 = initialGuessTuple 824 # Allow for variable blankrad leading to variable halfwinsize 825 # (no information beyond blankrad will be used), but avoid going to 0 826 if blankrad > 0: 827 halfwinsize = blankrad 828 else: 829 halfwinsize = defHalfwinsize 830 subim = img[int(y0)-halfwinsize:int(y0)+halfwinsize+1,int(x0)-halfwinsize:int(x0)+halfwinsize+1] 831 # ds9-based coords start at 1,1, while these start at 0,0, so 832 # we need to add 1 to all internally used coords to get them 833 # to match with what we see in ds9 834 yx=scipy.ogrid[1+y0-halfwinsize:1+y0+halfwinsize+1,1+x0-halfwinsize:1+x0+halfwinsize+1] 835 try: 836 par,res,bestfit=fitGauss(yx,subim,initialGuessTuple,blankrad,blankInner=False) 837 except: 838 par=(nan,nan,nan,nan,nan,nan) 839 (scale,x,xsig,y,ysig,theta)=par 840 print initialGuessTuple, scale,x,xsig,y,ysig,theta 841 fitlist.append(par) 842 f_handle.close() 843 return par 844 845 def doEfbComparison(simfile='simtest.1.00a.fits',efbfile='efbsim.1.00a.fits',simtab='simtest.1.00a.cmf'): 846 import pyfits 847 from numpy import array 848 tab = pyfits.open(simtab) 849 tdata = tab[1].data 850 x = array(tdata.field('x_psf')) 851 y = array(tdata.field('y_psf')) 852 counts = 10**(-0.4*array(tdata.field('psf_inst_mag'))) 853 initlist = [] 854 inparlist = [] 855 nrows = len(tdata.field('IPP_IDET')) 856 for i in range(nrows): 857 initlist.append((counts[i],x[i],2.,y[i],2.,0.)) 858 inparlist.append((counts[i],x[i],y[i])) 859 efbparlist = doGaussPsfPhotometry(efbfile,initlist) 860 simparlist = doGaussPsfPhotometry(simfile,initlist) 861 862 savelist = [ inparlist[i] + efbparlist[i] + simparlist[i] for i in range(nrows)] 863 saverows = numpy.rec.array(savelist,names=['counts_in','x_in','y_in',\ 864 'efbfit_counts','efbfit_x','efbfit_fwhm_x',\ 865 'efbfit_y','efbfit_fwhm_y','efbfit_theta',\ 866 'simfit_counts','simfit_x','simfit_fwhm_x',\ 867 'simfit_y','simfit_fwhm_y','simfit_theta']) 868 for col in ['efbfit_fwhm_x','efbfit_fwhm_y','simfit_fwhm_x','simfit_fwhm_y']: 869 saverows.field(col)[0:] *= 2.35 870 871 saveHDU = tabHDUfromRecArray(saverows) 872 saveHDU.writeto('gaussfit_'+efbfile) 873 874
Note:
See TracChangeset
for help on using the changeset viewer.
