Exemple #1
0
def test_gmix_exp():
    from fimage import model_image
    
    numpy.random.seed(35)

    ngauss=3
    nsig=7.
    #dims=[41,41]
    #cen=[(dims[0]-1)/2., (dims[1]-1)/2.]
    #cov=[10.5,0.0,10.5]

    T = 2*3
    e = 0.3
    theta = randu()*360.
    e1 = e*cos(2*theta*numpy.pi/180.0)
    e2 = e*sin(2*theta*numpy.pi/180.0)

    Irc = e2*T/2.0
    #Icc = (1+e1)*T/2.0
    #Irr = (1-e1)*T/2.0
    Icc = (1+e1)*T/2.0
    Irr = (1-e1)*T/2.0

    #T = 2.*3.0
    sigma = sqrt(T/2.)
    dim = int(2*nsig*sigma)
    if (dim % 2) == 0:
        dim += 1
    dims=array([dim,dim])
    print 'dims:',dims
    cen=(dims-1)/2.
    #cov = [T/2.,0.0,T/2.]
    cov = [Irr,Irc,Icc]

    # need order='c' since we will use in in C code!
    im = model_image('exp',dims,cen,cov,order='c')

    sky = 0.01*im.max()
    im_fakesky = im + sky

    ntry=0
    max_try = 10
    flags=9999
    while flags != 0 and ntry < max_try:
        stderr.write('.')
        guess = get_exp_guess(cen,cov,ngauss)

        gm=gmix_image.GMixEM(im_fakesky, guess, sky=sky, maxiter=5000)
        flags = gm.flags

        ntry += 1
    if ntry == max_try:
        raise ValueError("too many tries")

    gmix_image.gmix_print(gm.pars)
    stderr.write('\n')
    model = gmix2image_em(gm.pars,im.shape)
    images.compare_images(im, model)
Exemple #2
0
    def process(self):
        c=self['filternum']
        self.set_admom()

        ntrials = len(self['psf']['trials'])

        chi2arr=zeros(ntrials) + 1.e9
        gmlist=[]

        im=self.psf.copy()
        im_min = im.min()
        if im_min <= 0:
            im -= im_min
            sky=0.001*im.max()
            im += sky
        else:
            sky = im_min


        for i,trial in enumerate(self['psf']['trials']):
            #prior,width = self.get_prior_turb(trial)
            guess = self.get_em_guess(trial)

            if self['verbose']:
                wlog('guess')
                gmix_print(guess,title='guess:')

            gm = gmix_image.GMixEM(im,guess,
                                   sky=sky,
                                   maxiter=4000,
                                   tol=1.e-6,
                                   coellip=False,
                                   cocenter=False) # true required for deconv



            chi2arr[i] = gm.get_fdiff()
            if self['verbose']:
                gmix_print(gm.pars,title='pars:')
                wlog("chi2/pdeg:",chi2arr[i])

            gmlist.append(gm)

        w=chi2arr.argmin()
        self.gm = gmlist[w]

        flags = gm.get_flags()
        if flags != 0:
            printflags('em',flags)
            raise ValueError("error")
        if self['verbose']:
            gmix_print(gm.pars,title='popt:')

            wlog("\n")

            wlog("numiter gmix:",gm.numiter)