示例#1
0
    def __call__(self, n=None, get_sigmas=False, prior=None):
        if n is None:
            n=1
            is_scalar=True
        else:
            is_scalar=False

        pars=self.pars
        npars=pars.size

        widths = self.widths

        guess=numpy.zeros( (n, npars) )

        for j in xrange(n):
            for i in xrange(npars):
                if self.scaling=='linear' and i >= 4:
                    guess[j,i] = pars[i]*(1.0 + widths[i]*srandu())
                else:
                    guess[j,i] = pars[i] + widths[i]*srandu()


        if prior is not None:
            self._fix_guess(guess, prior)

        if is_scalar:
            guess=guess[0,:]

        if get_sigmas:
            return guess, self.pars_err
        else:
            return guess
示例#2
0
def get_shape_guess(g1, g2, n, width, max=0.99):
    """
    Get guess, making sure in range
    """

    g=numpy.sqrt(g1**2 + g2**2)
    if g > max:
        fac = max/g

        g1 = g1 * fac
        g2 = g2 * fac

    guess=numpy.zeros( (n, 2) )
    shape=ngmix.Shape(g1, g2)

    for i in xrange(n):

        while True:
            try:
                g1_offset = width[0]*srandu()
                g2_offset = width[1]*srandu()
                shape_new=shape.copy()
                shape_new.shear(g1_offset, g2_offset)
                break
            except GMixRangeError:
                pass

        guess[i,0] = shape_new.g1
        guess[i,1] = shape_new.g2

    return guess
示例#3
0
    def __call__(self, n=1, **keys):
        """
        center, shape are just distributed around zero
        """
        fluxes=self.fluxes
        log_fluxes=self.log_fluxes

        nband=fluxes.size
        np = 5+nband

        guess = self.prior.sample(n)

        if self.scaling=='linear':
            guess[:,4] = self.T*(1.0 + 0.1*srandu(n))
        else:
            guess[:,4] = self.T + 0.1*srandu(n)

        for band in xrange(nband):
            if self.scaling=='linear':
                guess[:,5+band] = fluxes[band]*(1.0 + 0.1*srandu(n))
            else:
                guess[:,5+band] = self.log_fluxes[band] + 0.1*srandu(n)

        self._fix_guess(guess, self.prior)

        if n==1:
            guess=guess[0,:]
        return guess
示例#4
0
    def __call__(self, n=None, get_sigmas=False, prior=None, ntry=10):
        """
        center, shape are just distributed around zero
        """

        if n is None:
            n=1
            is_scalar=True
        else:
            is_scalar=False

        pars=self.pars
        widths=self.widths
        npars=pars.size

        guess=numpy.zeros( (n, npars) )

        # bigger than LOWVAL
        for i in xrange(n):

            ok=False
            for itry in xrange(ntry):
                guess[i,0] = pars[0] + widths[0]*srandu()
                guess[i,1] = pars[1] + widths[1]*srandu()

                # prevent from getting too large
                guess_shape=get_shape_guess(pars[2],pars[3],
                                            1,
                                            widths[2:2+2],
                                            max=0.8)
                guess[i,2]=guess_shape[0,0]
                guess[i,3]=guess_shape[0,1]

                for j in xrange(4,npars):
                    guess[i,j] = pars[j] + widths[j]*srandu()

                try:
                    lnp=prior.get_lnprob_scalar(guess[i,:])
                    if lnp != LOWVAL:
                        ok=True
                        break
                except GMixRangeError as err:
                    pass

            if not ok:
                print("    had to sample....")
                guess[j,:] = prior.sample()

        if is_scalar:
            guess=guess[0,:]

        if get_sigmas:
            return guess, self.pars_err
        else:
            return guess
示例#5
0
    def __call__(self, n=None, get_sigmas=False, prior=None):
        """
        center is just distributed around zero
        """

        if n is None:
            n=1
            is_scalar=True
        else:
            is_scalar=False

        pars=self.pars
        npars=pars.size

        width = pars*0 + 0.1

        guess=numpy.zeros( (n, npars) )


        guess[:,0] = width[0]*srandu(n)
        guess[:,1] = width[1]*srandu(n)

        for j in xrange(n):
            itr = 0
            maxitr = 100
            while itr < maxitr:
                for i in xrange(2,npars):
                    if self.scaling=='linear' and i >= 4:
                        if pars[i] <= 0.0:
                            guess[j,:] = width[i]*srandu(1)
                        else:
                            guess[j,i] = pars[i]*(1.0 + width[i]*srandu(1))
                    else:
                        # we add to log pars!
                        guess[j,i] = pars[i] + width[i]*srandu(1)

                if numpy.abs(guess[j,2]) < 1.0 \
                        and numpy.abs(guess[j,3]) < 1.0 \
                        and guess[j,2]*guess[j,2] + guess[j,3]*guess[j,3] < 1.0:
                    break
                itr += 1

        if prior is not None:
            self._fix_guess(guess, prior)

        if is_scalar:
            guess=guess[0,:]

        if get_sigmas:
            return guess, self.pars_err
        else:
            return guess
示例#6
0
    def __call__(self, n=1, prior=None, **keys):
        """
        center, shape are just distributed around zero
        """
        fluxes=self.fluxes
        nband=fluxes.size
        np = 5+nband

        guess=numpy.zeros( (n, np) )
        guess[:,0] = 0.01*srandu(n)
        guess[:,1] = 0.01*srandu(n)
        guess[:,2] = 0.1*srandu(n)
        guess[:,3] = 0.1*srandu(n)

        if self.scaling=='linear':
            guess[:,4] = self.T*(1.0 + 0.1*srandu(n))

            fluxes=self.fluxes
            for band in xrange(nband):
                guess[:,5+band] = fluxes[band]*(1.0 + 0.1*srandu(n))

        else:
            guess[:,4] = self.log_T + 0.1*srandu(n)

            for band in xrange(nband):
                guess[:,5+band] = self.log_fluxes[band] + 0.1*srandu(n)

        if prior is not None:
            self._fix_guess(guess, prior)

        if n==1:
            guess=guess[0,:]
        return guess
示例#7
0
    def __call__(self, n=None, get_sigmas=False, prior=None):
        """
        get a random sample from the best points
        """
        import random

        if n is None:
            is_scalar=True
            n=1
        else:
            is_scalar=False

        # choose randomly from best
        #indices = self.lnp_sort[-n:]
        #guess = self.trials[indices, :]

        trials=self.trials
        np = trials.shape[0]

        rand_int = random.sample(xrange(np), n)
        guess=trials[rand_int, :]

        if prior is not None:
            self._fix_guess(guess, prior)

        w,=numpy.where(guess[:,4] <= 0.0)
        if w.size > 0:
            guess[w,4] = 0.05*srandu(w.size)

        for i in xrange(5, self.npars):
            w,=numpy.where(guess[:,i] <= 0.0)
            if w.size > 0:
                guess[w,i] = (1.0 + 0.1*srandu(w.size))

        #print("guess from mcmc:")
        #for i in xrange(n):
        #    print_pars(guess[i,:], front="%d: " % i)

        if is_scalar:
            guess=guess[0,:]

        if get_sigmas:
            return guess, self.sigmas
        else:
            return guess
示例#8
0
    def __call__old(self, n=None, get_sigmas=False, prior=None):
        """
        center, shape are just distributed around zero
        """

        if n is None:
            n=1
            is_scalar=True
        else:
            is_scalar=False

        pars=self.pars
        widths=self.widths
        npars=pars.size

        guess=numpy.zeros( (n, npars) )

        # srandu() has std of 0.57..
        guess[:,0] = pars[0] + widths[0]*srandu(n)
        guess[:,1] = pars[1] + widths[1]*srandu(n)

        guess_shape=get_shape_guess(pars[2],pars[3],n,widths[2:2+2])
        guess[:,2]=guess_shape[:,0]
        guess[:,3]=guess_shape[:,1]

        for i in xrange(4,npars):
            guess[:,i] = pars[i] + widths[i]*srandu(n)

        if prior is not None:
            self._fix_guess(guess, prior)

        if is_scalar:
            guess=guess[0,:]

        if get_sigmas:
            return guess, self.pars_err
        else:
            return guess
示例#9
0
文件: utils.py 项目: kstory8/egret
def get_image_center(im,wt,rng=None,ntry=10,T0=10.0,**em_pars):
    """
    get the image center via fitting a single Gaussian with EM
    
    lifted from ngmix bootstrappers by E. Sheldon
    """

    if rng is None:
        rng = np.random.RandomState()
    em_pars['maxiter'] = em_pars.get('maxiter',4000)
    em_pars['tol'] = em_pars.get('tol',5e-6)
        
    srandu = lambda : rng.uniform(low=-1.0,high=1.0)
    
    row = im.shape[0]/2.0
    col = im.shape[1]/2.0
    jac = ngmix.Jacobian(0.0,0.0,1.0,0.0,0.0,1.0)
    obs = ngmix.Observation(im,weight=wt,jacobian=jac)
    
    for i in xrange(ntry):
        guess=[1.0*(1.0 + 0.01*srandu()),
               row*(1.0+0.5*srandu()),
               col*(1.0+0.5*srandu()),
               T0/2.0*(1.0 + 0.1*srandu()),
               0.1*ngmix.srandu(),
               T0/2.0*(1.0 + 0.1*srandu())]
        gm_guess = ngmix.GMix(pars=guess)
        fitter = ngmix.em.fit_em(obs,gm_guess,**em_pars)
        res = fitter.get_result()
        
        if res['flags']==0:
            break
        
    if res['flags']==0:
        gm = fitter.get_gmix()
        row,col = gm.get_cen()
    
    return row,col