def test_fit_1gauss_noisy(ellip=0.2, s2n=10000): import images import fimage numpy.random.seed(35) sigma = 1.4 T=2*sigma**2 nsig=5 dim = int(nsig*T) if (dim % 2) == 0: dim += 1 dims=array([dim,dim]) cen=(dims-1.)/2. theta=23.7*numpy.pi/180. #eta=-0.7 #ellip=(1+tanh(eta))/2 eta = ellip2eta(ellip+1.e-8) print >>stderr,'ellip:',ellip pars=array([cen[0],cen[1],eta,theta,1.,T]) print >>stderr,'pars' gmix = gmix_fit.pars2gmix_coellip_pick(pars,ptype='eta') nsub=1 im0=gmix2image_em(gmix,dims,nsub=nsub) im,skysig = fimage.add_noise(im0, s2n,check=True) images.multiview(im,title='nsub: %d' % nsub) p0=pars.copy() p0[0] += 1*(randu()-0.5) # cen0 p0[1] += 1*(randu()-0.5) # cen1 p0[2] += 0.2*(randu()-0.5) # eta p0[3] += 0.5*(randu()-0.5) # theta radians p0[4] += 0.1*(randu()-0.5) # p p0[5] += 1*(randu()-0.5) # T print_pars(pars,front='pars: ') print_pars(p0, front='guess: ') gf=gmix_fit.GMixFitCoellip(im, p0, ptype='eta',verbose=True) print 'numiter:',gf.numiter print_pars(gf.popt, front='popt: ') print_pars(gf.perr, front='perr: ') images.imprint(gf.pcov)
def test_fit_1gauss_galsim(ellip=0.2, s2n=10000): import images import galsim import admom numpy.random.seed(35) #sigma = 1.4 sigma = 1 T=2*sigma**2 e = 0.2 theta=23.7 e1,e2 = etheta2e1e2(e,theta) fimage_cov = ellip2mom(T, e=e, theta=theta) print 'e: ',e print 'e1:',e1 print 'e2:',e2 pixel_scale = 1. nsig=5 dim = int(nsig*T) if (dim % 2) == 0: dim += 1 dims=array([dim,dim]) cen=(dims-1)/2 pix = galsim.Pixel(xw=pixel_scale, yw=pixel_scale) gobj = galsim.Gaussian(sigma=sigma) gobj.applyDistortion(galsim.Ellipse(e1=e1,e2=e2)) gcobj = galsim.Convolve([gobj,pix]) im0 = galsim.ImageD(int(dims[1]),int(dims[0])) gcobj.draw(image=im0, dx=pixel_scale) images.multiview(im0.array) galsim_nsub=16 ares = admom.admom(im0.array,cen[0],cen[1],guess=T/2,nsub=galsim_nsub) print 'galsim sigma:',sqrt( (ares['Irr']+ares['Icc'])/2 ) print 'galsim admom e1:',ares['e1'] print 'galsim admom e2:',ares['e2'] print 'galsim center:',ares['row'],ares['col'] fnsub=16 fim0 = model_image('gauss',dims,cen,fimage_cov,nsub=fnsub) fares = admom.admom(fim0,cen[0],cen[1],guess=T/2,nsub=fnsub) print 'fimage sigma:',sqrt( (fares['Irr']+fares['Icc'])/2 ) print 'fimage admom e1:',fares['e1'] print 'fimage admom e2:',fares['e2'] print 'fimage center:',fares['row'],fares['col'] return theta=23.7*numpy.pi/180. print >>stderr,'ellip:',ellip pars=array([cen[0],cen[1],eta,theta,1.,T]) print >>stderr,'pars' gmix = gmix_fit.pars2gmix_coellip_pick(pars,ptype='eta') nsub=1 im0=gmix2image_em(gmix,dims,nsub=nsub) im,skysig = fimage.add_noise(im0, s2n,check=True) images.multiview(im,title='nsub: %d' % nsub) p0=pars.copy() p0[0] += 1*(randu()-0.5) # cen0 p0[1] += 1*(randu()-0.5) # cen1 p0[2] += 0.2*(randu()-0.5) # eta p0[3] += 0.5*(randu()-0.5) # theta radians p0[4] += 0.1*(randu()-0.5) # p p0[5] += 1*(randu()-0.5) # T print_pars(pars,front='pars: ') print_pars(p0, front='guess: ') gf=gmix_fit.GMixFitCoellip(im, p0, ptype='eta',verbose=True) print 'numiter:',gf.numiter print_pars(gf.popt, front='popt: ') print_pars(gf.perr, front='perr: ') images.imprint(gf.pcov)
def test_fit_1gauss_e1e2(ellip=0.2, seed=35, s2n=-9999, dopsf=False): import images from fimage import etheta2e1e2, add_noise, ellip2mom ptype='e1e2' numpy.random.seed(seed) if dopsf: print >>stderr,"DOING PSF" Tpsf = 2.0 epsf = 0.2 theta_psf = 80.0 cov_psf = ellip2mom(Tpsf, e=epsf, theta=theta_psf) psf=[{'p':1.0, 'irr':cov_psf[0], 'irc':cov_psf[1], 'icc':cov_psf[2]}] else: psf=None theta=23.7 e1,e2 = etheta2e1e2(ellip, theta) sigma = 1.4 T=2*sigma**2 nsig=5 dim = int(nsig*T) if (dim % 2) == 0: dim += 1 dims=array([dim,dim]) cen=(dims-1.)/2. pars=array([cen[0],cen[1],e1,e2,1.,T]) print >>stderr,'pars' gmix = gmix_fit.pars2gmix_coellip_pick(pars,ptype=ptype) nsub=1 im0=gmix2image_em(gmix,dims,nsub=nsub,psf=psf) if s2n > 0: im,skysig = add_noise(im0, s2n,check=True) else: im=im0 #images.multiview(im,title='nsub: %d' % nsub) p0=pars.copy() """ p0[0] += 1*(randu()-0.5) # cen0 p0[1] += 1*(randu()-0.5) # cen1 p0[2] += 0.2*(randu()-0.5) # e1 p0[3] += 0.2*(randu()-0.5) # e2 p0[4] += 0.1*(randu()-0.5) # p p0[5] += 1*(randu()-0.5) # T """ ''' p0[0] += 0.2*(randu()-0.5) # cen0 p0[1] += 0.2*(randu()-0.5) # cen1 p0[2] += 0.1*(randu()-0.5) # e1 p0[3] += 0.1*(randu()-0.5) # e2 p0[4] += 0.1*(randu()-0.5) # p p0[5] += 1*(randu()-0.5) # T ''' print_pars(pars,front='pars: ') print_pars(p0, front='guess: ') gf=gmix_fit.GMixFitCoellip(im, p0, psf=psf, ptype=ptype, verbose=True) print >>stderr,'numiter:',gf.numiter print_pars(gf.popt,front='popt: ') print_pars(gf.perr,front='perr: ')
def test_fit_dev_e1e2(ngauss=4, s2n=1.e5): import biggles import admom import fimage numpy.random.seed(35) ptype='e1e2' e = 0.2 theta = 23.7 e1,e2 = etheta2e1e2(e,theta) print >>stderr,"e:",e,"e1:",e1,"e2:",e2 nsig=15 #nsig=7 npars=2*ngauss+4 nsigma_vals=20 f='test-opt-dev-bysigma' f += '-s2n%d' % s2n f += '.rec' pngf=f.replace('.rec','.png') if not os.path.exists(f): data=numpy.zeros(nsigma_vals,dtype=[('sigma','f8'),('pars','f8',npars)]) #sigvals = numpy.linspace(1.5,5.0,nsigma_vals) sigvals = numpy.linspace(3.,5.0,nsigma_vals) #sigvals=array([3.0]) for isigma,sigma in enumerate(sigvals): print '-'*70 print 'sigma:',sigma T = 2*sigma**2 cov = ellip2mom(T, e=e, theta=theta) dim = int(2*nsig*sigma) if (dim % 2) == 0: dim += 1 dims=array([dim,dim]) cen=(dims-1)/2. im0 = model_image('dev',dims,cen,cov,nsub=16) im,skysig = fimage.add_noise(im0, s2n, check=True) dim = int(2*nsig*sigma) if (dim % 2) == 0: dim += 1 dims=array([dim,dim]) cen=(dims-1)/2. ares = admom.admom(im0,cen[0],cen[1],guess=T/2,nsub=16) Tadmom=ares['Irr'] + ares['Icc'] if ngauss == 4: Tmax = Tadmom*100 # 0.02620127 0.09348825 0.23987656 0.63958437 p0 = array([cen[0], cen[1], e1, e2, 0.026, 0.093, 0.24, 0.64, Tmax, Tmax*0.18, Tmax*0.04, Tmax*0.0027]) else: p0 = array([cen[0], cen[1], e1, e2, 1./ngauss, 1./ngauss, 1./ngauss, Tadmom*4.9, Tadmom*0.82, Tadmom*0.18]) print_pars(p0, front='guess: ') verbose=False gf=gmix_fit.GMixFitCoellip(im, p0, ptype=ptype, verbose=verbose) chi2per = gf.get_chi2per(gf.popt,skysig) print 'numiter:',gf.numiter print_pars(gf.popt, front='popt: ') if gf.perr is not None: print_pars(gf.perr, front='perr: ') print 'chi2/deg:',chi2per if gf.flags != 0: gmix_image.printflags('fit',gf.flags) raise RuntimeError("failed") data['sigma'][isigma] = sigma data['pars'][isigma,:] = gf.popt tvals = gf.popt[4+ngauss:] tmax=tvals.max() print 't ratios:',tvals/tmax print 'p values:',gf.popt[4:4+ngauss] print 'T vals/Tadmom:',tvals/Tadmom # plot the last one gmix = gmix_fit.pars2gmix_coellip_pick(gf.popt,ptype=ptype) model = gmix2image_em(gmix,im.shape) images.compare_images(im,model) else: data=eu.io.read(f) biggles.configure('fontsize_min', 1.0) biggles.configure('linewidth',1.0) # frame only nrows=3 ncols=4 tab=biggles.Table(nrows,ncols) for par in xrange(npars): plt=biggles.FramedPlot() plt.add(biggles.Curve(data['sigma'],data['pars'][:,par])) plt.add(biggles.Points(data['sigma'],data['pars'][:,par], type='filled circle')) plt.xlabel = r'$\sigma$' plt.ylabel = 'p%d' % par tab[par//ncols,par%ncols] = plt tab.show() tab.write_img(1024,1024,pngf)
def test_fit_2gauss_e1e2(ellip=0.2, seed=35, s2n=-9999, dopsf=False): import images from fimage import etheta2e1e2, add_noise, ellip2mom numpy.random.seed(seed) ptype='e1e2' nsub=1 if dopsf: print >>stderr,"DOING PSF" Tpsf = 2.0 epsf = 0.2 #epsf = 0.2 theta_psf = 80.0 cov_psf = ellip2mom(Tpsf, e=epsf, theta=theta_psf) psf=[{'p':1.0, 'irr':cov_psf[0], 'irc':cov_psf[1], 'icc':cov_psf[2]}] else: psf=None theta=23.7 e1,e2 = etheta2e1e2(ellip, theta) T1=3.0 T2=6.0 nsig=5 dim = int(nsig*T2) if (dim % 2) == 0: dim += 1 dims=array([dim,dim]) cen=(dims-1)/2. p1=0.4 p2=0.6 pars=array([cen[0],cen[1],e1,e2,p1,p2,T1,T2]) gmix = gmix_fit.pars2gmix_coellip_pick(pars,ptype=ptype) im0=gmix2image_em(gmix,dims,psf=psf,nsub=nsub) if s2n > 0: im,skysig = add_noise(im0, s2n,check=True) else: im=im0 p0=pars.copy() p0[0] += 1*(randu()-0.5) # cen0 p0[1] += 1*(randu()-0.5) # cen1 p0[2] += 0.2*(randu()-0.5) # e1 p0[3] += 0.2*(randu()-0.5) # e2 p0[4] += 0.2*(randu()-0.5) # p1 p0[5] += 0.2*(randu()-0.5) # p2 p0[6] += 1*(randu()-0.5) # p2 p0[7] += 1*(randu()-0.5) # p2 print_pars(pars,front='pars: ') print_pars(p0, front='guess: ') gf=gmix_fit.GMixFitCoellip(im, p0, ptype=ptype, psf=psf, verbose=True) print >>stderr,'numiter:',gf.numiter print_pars(gf.popt,front='popt: ') if gf.perr is not None: print_pars(gf.perr,front='perr: ') return pars, gf.popt, gf.perr, gf.pcov