Example #1
0
def test_fit_2gauss_2psf(seed=45):
    import images
    from fimage import ellip2mom

    print >>stderr,"seed:",seed
    numpy.random.seed(seed)

    Tpsf1 = 2.0
    Tpsf2 = 4.0
    epsf = 0.2
    theta_psf = 80.0
    cov_psf1 = ellip2mom(Tpsf1, e=epsf, theta=theta_psf)
    cov_psf2 = ellip2mom(Tpsf2, e=epsf, theta=theta_psf)
    psf=[{'p':0.7, 'irr':cov_psf1[0], 'irc':cov_psf1[1], 'icc':cov_psf1[2]},
         {'p':0.3, 'irr':cov_psf2[0], 'irc':cov_psf2[1], 'icc':cov_psf2[2]}]


    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.

    theta=23.7*numpy.pi/180.
    eta=-0.7
    ellip=(1+tanh(eta))/2
    p1=0.4
    p2=0.6
    print >>stderr,'ellip:',ellip
    pars=array([cen[0],cen[1],eta,theta,p1,p2,T1,T2])
    print >>stderr,'pars'

    gmix = gmix_fit.pars2gmix_coellip_pick(pars,ptype='eta')
    im=gmix2image_em(gmix,dims, psf=psf)
    
    p0=pars.copy()
    p0[0] += 1*(randu()-0.5)  # cen0
    p0[1] += 1*(randu()-0.5)  # cen1
    p0[2] += 1*(randu()-0.5)  # eta
    p0[3] += 0.5*(randu()-0.5)   # theta radians
    p0[4] += 0.2*(randu()-0.5)  # p1
    p0[5] += 0.2*(randu()-0.5)  # p2
    p0[6] += 1*(randu()-0.5)   # T1
    p0[7] += 1*(randu()-0.5)   # T2

    print_pars(pars,front='pars:  ')
    print_pars(p0,  front='guess: ')
    gf=gmix_fit.GMixFitCoellip(im, 
                               p0, 
                               psf=psf,
                               ptype='eta',
                               verbose=True)

    print 'numiter:',gf.numiter
    print gf.popt
    for i in xrange(len(pars)):
        print '%10.6f %10.6f' % (pars[i],gf.popt[i])
Example #2
0
def test_fit_1gauss_psf(seed=45):
    from fimage import ellip2mom
    import images

    print >>stderr,"seed:",seed
    numpy.random.seed(seed)


    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]}]

    T=3.0

    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
    print >>stderr,'ellip:',ellip
    print >>stderr,'e1:',ellip*cos(2*theta)
    print >>stderr,'e2:',ellip*sin(2*theta)

    pars=array([cen[0],cen[1],eta,theta,1.,T])
    print >>stderr,'pars'
    gmix = gmix_fit.pars2gmix_coellip_pick(pars,ptype='eta')

    im=gmix2image_em(gmix,dims,psf=psf)
    #images.multiview(im)
    
    p0=pars.copy()
    p0[0] += 1*(randu()-0.5)  # cen0
    p0[1] += 1*(randu()-0.5)  # cen1
    p0[2] += 1*(randu()-0.5)  # eta
    #p0[3] += 1*(randu()-0.5)   # theta radians
    p0[3] += 0.5*(randu()-0.5)   # theta radians
    p0[4] += 0.2*(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='eta',
                               verbose=True)

    print 'numiter:',gf.numiter
    print gf.popt
    print gf.pcov
Example #3
0
def test_fit_exp_simple(s2n=1.e5):
    import biggles
    import admom
    import fimage
    numpy.random.seed(35)

    e = 0.2
    theta = 23.7
    e1,e2 = etheta2e1e2(e,theta)
    sigma=4.0

    sigma_psf = sigma/2.

    fwhm_psf = 2.35*sigma_psf

    print >>stderr,"e:",e,"e1:",e1,"e2:",e2

    print '-'*70
    print 'sigma:',sigma
    T = 2*sigma**2

    cov = ellip2mom(T, e=e, theta=theta)

    ci=fimage.convolved.ConvolverTurbulence({'model':'exp', 'cov':cov},
                                            {'model':'turb','psf_fwhm':fwhm_psf})
    cin=fimage.convolved.NoisyConvolvedImage(ci, s2n, 1.e6)

    cen=cin['cen']

    gmpsf=gmix_image.gmix_em.GMixEMBoot(cin.psf, 2, cen)
    gmix_psf=gmpsf.get_gmix()

    ares = admom.admom(cin.image,cen[0],cen[1],guess=T/2,nsub=16)

    verbose=False

    skysig=cin['skysig']
    gf=gmix_fit.GMixFitSimple(cin.image, 1./skysig**2, gmix_psf, 'exp', ares)

    res=gf.get_result()
    print 'chi2/deg:',res['chi2per']

    model = gf.get_model()
    images.compare_images(cin.image,model)
Example #4
0
    def new_random_convolved_image(self, ellip):
        p = self.rsp.get()
        psfpars={'model':'dgauss',
                 'Irr1':p['sigma1']**2,
                 'Irc1':0.0,
                 'Icc1':p['sigma1']**2,
                 'sizerat': p['sigma2']/p['sigma1'],
                 'cenrat': p['b'],
                 'dims': [31,31],
                 'cen':[15,15]}

        # parameters for a model galaxy of the right size

        # just relative to the first, dominant one
        Tobj = (psfpars['Irr1']+psfpars['Icc1'])/self['s2']

        # random angle
        theta = numpy.random.random()*360
        Irr,Irc,Icc = fimage.ellip2mom(Tobj, e=ellip, theta=theta)

        objpars={'model':self['objmodel'],
                 'Irr':Irr,
                 'Irc':Irc,
                 'Icc':Icc}

        ci = fimage.ConvolvedImage(objpars, psfpars, conv=self['conv'])
        ci['theta'] = theta

        if self['debug']:
            self.rsp.load_kl(self.rsp.current_i)
            psf = self.rsp.kl.rec(1000,1000, trim=True)
            images.compare_images(ci.psf, psf)
            ci.show()
            key=raw_input('hit a key: ')

        self.rsp.next()

        return ci
Example #5
0
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)
Example #6
0
def test_fit_dev_eta_bysigma():
    """
    Round object as a function of sigma
    """
    import admom
    import biggles
    from fimage import model_image, ellip2mom
    numpy.random.seed(35)

    ngauss=4
    nsig=15
    npars=2*ngauss+4
    nsigma_vals=20

    e=0.3
    eta = ellip2eta(e)
    theta=23.7
    print >>stderr,'ellip:',e
    print >>stderr,'eta:',eta
    print >>stderr,'theta:',theta*pi/180.


    f='test-opt-dev-bysigma.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([7.0])
        for isigma,sigma in enumerate(sigvals):
            print '-'*70
            print 'sigma:',sigma
            T = 2*sigma**2

            dim = int(2*nsig*sigma)
            if (dim % 2) == 0:
                dim += 1
            dims=array([dim,dim])
            cen=(dims-1)/2.
            cov=ellip2mom(T, e=e, theta=theta)
            im = model_image('dev',dims,cen,cov,nsub=16)
            #images.multiview(im)

            ares = admom.admom(im,cen[0],cen[1],guess=T/2,nsub=16)
            if ares['whyflag'] != 0:
                raise ValueError("admom failed")
            Tadmom = ares['Irr']+ares['Icc']
            print >>stderr,'admom sigma:',sqrt(Tadmom/2)
            print >>stderr,'admom T:',Tadmom
            print >>stderr,'admom e:',sqrt(ares['e1']**2 + ares['e2']**2)
            print >>stderr,'T input:',T
            #Tuse = Tadmom
            Tuse = T
            p0 = array([cen[0],
                        cen[1],
                        eta,
                        theta*pi/180.,
                        0.22,
                        0.35,
                        0.25,
                        0.15,
                        Tuse*0.15,
                        Tuse*0.5,
                        Tuse*2.0,
                        Tuse*5.0])
            #0.18450384   2.09205287  10.31125635  67.13233512

            print_pars(p0,  front='guess: ')
            gf=gmix_fit.GMixFitCoellip(im, p0, ptype='eta',verbose=True)
            flags = gf.flags

            print 'numiter:',gf.numiter
            print_pars(gf.popt,  front='popt:  ')
            #for i in xrange(len(gf.popt)):
            #    print '%.6g' % gf.popt[i]

            if gf.flags != 0:
                raise RuntimeError("failed")

            print >>stderr,'T relative to T uw:',gf.popt[4+ngauss:]/T
            print >>stderr,'T relative to T admom:',gf.popt[4+ngauss:]/Tadmom
            data['sigma'][isigma] = sigma
            data['pars'][isigma,:] = gf.popt

        # plot the last one
        gmix = gmix_fit.pars2gmix_coellip_eta(gf.popt)
        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)
Example #7
0
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)
Example #8
0
def test_fit_1gauss_e1e2_fix(imove, dopsf=False):

    import images
    from fimage import etheta2e1e2, ellip2mom

    numpy.random.seed(45)

    ptype='e1e2'
    numpy.random.seed(35)

    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
    ellip=0.2
    e1,e2 = etheta2e1e2(ellip, theta)

    sigma = 3.
    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
    im=gmix2image_em(gmix,dims,nsub=nsub, psf=psf)
    
    p0=pars.copy()
    if imove == 0:
        p0[0] += 1*(randu()-0.5)  # cen0
    elif imove == 1:
        p0[1] += 1*(randu()-0.5)  # cen1
    elif imove == 2:
        p0[2] += 0.2*(randu()-0.5)  # e1
    elif imove == 3:
        p0[3] += 0.2*(randu()-0.5)  # e2
    elif imove == 4:
        p0[4] += 0.2*(randu()-0.5)  # p
    elif imove == 5:
        p0[5] += 1*(randu()-0.5)   # T
    print_pars(pars,front='pars:  ')
    print_pars(p0,  front='guess: ')

    gf=gmix_fit.GMixFitCoellipFix(im, p0, imove, ptype=ptype,
                                  psf=psf,
                                  verbose=True)

    print >>stderr,'numiter:',gf.numiter
    print_pars(gf.popt,front='popt:  ')
    print_pars(gf.perr,front='perr:  ')
Example #9
0
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:  ')
Example #10
0
def test_fit_exp_e1e2():
    import admom
    import biggles
    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=7
    ngauss=3
    npars=2*ngauss+4
    nsigma=20
    data=numpy.zeros(nsigma,dtype=[('sigma','f8'),('pars','f8',npars)])
    sigvals = numpy.linspace(1.5,5.0,nsigma)
    #sigvals = array([3.0])
    for isigma,sigma in enumerate(sigvals):
        print '-'*70
        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.

        im = model_image('exp',dims,cen,cov,nsub=16)
        ares = admom.admom(im,cen[0],cen[1],guess=T/2,nsub=16)
        Tadmom=ares['Irr'] + ares['Icc']

        ngauss=3
        # p values: [ 0.61145202  0.33400601  0.03659767]
        # T vals/Tadmom: [ 2.69996659  0.61848985  0.08975346]

        p0 = [cen[0],# + 0.1*(randu()-0.5),
              cen[1],# + 0.1*(randu()-0.5),
              e1,# + .2*(randu()-0.5), 
              e2,# + .2*pi/180.*(randu()-0.5),
              0.62,
              0.34,
              0.04,
              Tadmom*2.7,
              Tadmom*0.62,
              Tadmom*0.09]

        print_pars(p0,  front='guess: ')
        gf=gmix_fit.GMixFitCoellip(im, p0, 
                                   ptype=ptype, 
                                   verbose=True)

        print_pars(gf.popt,  front='popt:  ')

        if gf.flags != 0:
            raise RuntimeError("failed")
        print 'numiter:',gf.numiter

        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 = gf.gmix
    model = gmix2image_em(gmix,im.shape)

    title=None
    plt=images.compare_images(im,model,title=title)

    epsfile='test-opt-exp.eps'
    print >>stderr,'epsfile of image compare:',epsfile
    plt.write_eps(epsfile)

    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.xlabel = r'$\sigma$'
        plt.ylabel = 'p%d' % par
        tab[par//ncols,par%ncols] = plt

    tab.title=title
    tab.show()
Example #11
0
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