def test_em_sdss(ngauss=2, run=756, field=45, camcol=1, filter='r', row=123.1, col=724.8, cocenter=False, show=False): import sdsspy fnum=sdsspy.FILTERNUM[filter] psfield=sdsspy.read('psField', run=run, camcol=camcol, field=field, lower=True) psfkl=sdsspy.read('psField', run=run, camcol=camcol, field=field, filter=filter) if psfkl is None: print 'no such field' return im_nonoise=psfkl.rec(row, col, trim=True) im0,skysig=add_noise_matched(im_nonoise, 1.e8) ivar=1./skysig**2 dims=im0.shape cen=[(dims[0]-1.)/2., (dims[1]-1.)/2.] fwhm=psfield['psf_width'][0,fnum] sigma_guess=fimage.fwhm2sigma(fwhm) sigma_guess /= 0.4 # pixels print 'guess fwhm:',fwhm gm=gmix_image.gmix_em.GMixEMBoot(im0, ngauss, cen, sigma_guess=sigma_guess, ivar=ivar, cocenter=cocenter) res=gm.get_result() gmx=gm.get_gmix() print 'numiter:',res['numiter'] if show and have_images: import biggles biggles.configure('screen','width',1000) biggles.configure('screen','height',1000) mod=gmix2image(gmx,im0.shape) counts=im0.sum() mod *= counts/mod.sum() if cocenter: title='cocenter ngauss: %d' % ngauss else: title='free centers: %d' % ngauss images.compare_images(im0, mod, label1='image',label2='model', title=title) print gmx return res
def test_em(s2n=100., show=False, ngauss=2, offcen=True): #tol=1.e-6 tol=1.e-6 maxiter=5000 dims=[31,31] if ngauss==2: if offcen: gd = [{'p':0.6,'row':17.1,'col':17.6,'irr':4.0,'irc':0.0,'icc':4.0}, {'p':0.4,'row':14.2,'col':15.4,'irr':3.2,'irc':0.3,'icc':2.0}] else: gd = [{'p':0.6,'row':15.1,'col':15.6,'irr':4.0,'irc':0.0,'icc':4.0}, {'p':0.4,'row':15.1,'col':15.6,'irr':3.2,'irc':0.3,'icc':2.0}] guess=[{'p':0.5+0.02*srandu(), 'row':15+2*srandu(), 'col':15+2*srandu(), 'irr':2.0+0.5*srandu(), 'irc':0.0+0.1*srandu(), 'icc':2.0+0.5*srandu()}, {'p':0.5+0.02*srandu(), 'row':15+2*srandu(), 'col':15+2*srandu(), 'irr':2.0+0.5*srandu(), 'irc':0.0+0.1*srandu(), 'icc':2.0+0.5*srandu()} ] elif ngauss==1: gd = [{'p':0.6,'row':17.1,'col':17.6,'irr':4.0,'irc':0.0,'icc':4.0}] guess=[{'p':0.5+0.02*srandu(), 'row':15+2*srandu(), 'col':15+2*srandu(), 'irr':2.0+0.5*srandu(), 'irc':0.0+0.1*srandu(), 'icc':2.0+0.5*srandu()} ] else: raise ValueError("1 or 2 gauss") counts=1000 im_nonoise = gmix2image_em(gd, dims, counts=counts) im,skysig=add_noise_matched(im_nonoise,s2n) # pretend it is poisson #sky = skysig**2 #im += sky im_min=im.min() sky = 0.01 + abs(im_min) im += sky verbose=False gm = gmix_image.GMixEM(im, guess, sky=sky, counts=counts, maxiter=maxiter, tol=tol, verbose=verbose) gm.write() print 'truth' for i,d in enumerate(gd): print '%i'% i, for k in ['p','row','col','irr','irc','icc']: print '%s: %9.6lf' % (k,d[k]), print if show and have_images: model_image=gm.get_model() msum=model_image.sum() if msum > 0: model_image *= counts/msum images.compare_images(im-sky, model_image, label1='im', label2='model', cross_sections=False) return gm
def test_em_jacob(s2n=100., ngauss=2, offcen=True, scale=0.27, # ''/pixel theta=20.0, show=False): import fimage import math from math import cos,sin,sqrt from lensing.util import rotate_shape thetarad=math.radians(theta) ctheta=cos(thetarad) stheta=sin(thetarad) c2theta=cos(2*thetarad) s2theta=sin(2*thetarad) e1=0.2 e2=0.1 counts=100. sigma=5.0 # arcsec T=2*sigma**2 # for simulation, in pixels # rotate backwards to the pixel coords e1pix,e2pix=rotate_shape(e1,e2,-theta) sigma_pix=sigma/scale Tpix=2*sigma_pix**2 counts_pix=counts/(scale*scale) dim=2*5*sigma_pix dims=[dim]*2 cenpix=[(dim-1.)/2.]*2 print 'sky' print ' sigma(''):',sigma print ' e1:',e1,'e2:',e2 print 'pix' print ' sigma(pix):',sigma_pix print ' e1:',e1pix,'e2:',e2pix print 'dims:',dims print gmix_pix=GMixCoellip([cenpix[0], cenpix[1], e1pix, e2pix, Tpix, counts_pix]) im0=gmix2image(gmix_pix, dims) imnoise0,skysig=add_noise_matched(im0,s2n) im,sky=_em_prep(imnoise0) jacob={'row0':cenpix[0], 'col0':cenpix[1], 'dudrow':ctheta*scale, 'dudcol':-stheta*scale, 'dvdrow':stheta*scale, 'dvdcol': ctheta*scale} guess_pix=[{'p':1.0, 'row':cenpix[0], 'col':cenpix[1], 'irr':Tpix/2., 'irc':0.0, 'icc':Tpix/2.}] gm_pix=gmix_image.GMixEM(im, guess_pix, sky=sky) print 'pixel result' print gm_pix gmix=gm_pix.get_gmix() print gmix e1m_pix,e2m_pix,Tm_pix=gmix.get_e1e2T() sigma_meas_pix = sqrt(Tm_pix/2.) print 'measured pixels' print ' sigma:',sigma_meas_pix print ' e1:',e1m_pix,'e2:',e2m_pix print guess=[{'p':1.0, 'row':0.0, 'col':0.0, 'irr':4., 'irc':0.0, 'icc':4.}] pprint.pprint(guess) gm=gmix_image.GMixEM(im, guess, sky=sky, jacobian=jacob, verbose=1) print 'sky result' print gm gmix=gm.get_gmix() print gmix print 'measured:' e1m,e2m,Tm=gmix.get_e1e2T() sigma_meas = sqrt(Tm/2.) print ' sigma:',sigma_meas print ' e1:',e1m,'e2:',e2m