Esempio n. 1
0
    def _maybe_clip(self, efitter, dfitter, pars, fracdev):
        """
        allow the user to send a s/n above which the clip
        is applied.  Default is effectively no clipping,
        since fracdev_s2n_clip_min defaults to 1.0e9
        """
        eres=efitter.get_result()
        dres=dfitter.get_result()

        s2n_max=max( eres['s2n_w'], dres['s2n_w'] )

        clip_min = pars.get('fracdev_s2n_clip_min',1.e9)
        if s2n_max > clip_min:
            print("        clipping")
            frange=pars.get('fracdev_range',[-2.0, 2.0])
            fracdev_clipped = fracdev.clip(min=frange[0],max=frange[1])
        else:
            fracdev_clipped = 0.0 + fracdev

        if False and s2n_max > 50:
            import images
            images.multiview(self.gal_obs.image)
            key=raw_input('hit a key: ')
            if key=='q':
                stop
        return fracdev_clipped
Esempio n. 2
0
    def make_galaxy_image(self, dindex, show=False, get_obj=False):
        """
        Make the galaxy image object
        """

        rindex=self._index_list[dindex]
        gal_obj=self.make_galaxy_obj(rindex)

        # apply center offset in pixel coords
        dx = self._randu() - 0.5
        dy = self._randu() - 0.5

        gal_im_obj=gal_obj.draw(dx=self._pixel_scale, offset=(dx,dy))

        gal_im_obj.addNoise(self._rand_gauss_obj)
        #sky_var=gal_obj.noise.applyWhiteningTo(gal_im_obj)
        #sky_sigma = numpy.sqrt(sky_var)

        sh=gal_im_obj.array.shape
        #wt=numpy.zeros(sh) + 1./sky_sigma**2
        wt=numpy.zeros(sh) + 1./self._sky_sigma0**2
        #print 'sky_sigma0:',self._sky_sigma0,'sky_sigma:',sky_sigma

        if show:
            import images
            print('max image:',gal_im_obj.array.max())
            images.multiview(gal_im_obj.array)

        if get_obj:
            return gal_obj, gal_im_obj, wt
        else:
            return gal_im_obj, wt
Esempio n. 3
0
def _make_symmetrized_image(im_input):
    """
    add a version of itself roated by 90,180,270 degrees
    """
    im = im_input.copy()
    im += numpy.rot90(im_input, k=1)
    im += numpy.rot90(im_input, k=2)
    im += numpy.rot90(im_input, k=3)

    im *= (1.0 / 4.0)

    if False:
        import images
        images.multiview(im)
        images.compare_images(
            im_input,
            im,
            label1='orig',
            label2='symm',
            width=1000,
            height=1000,
        )
        if 'q' == raw_input('hit a key: '):
            stop

    return im
Esempio n. 4
0
def test():
    import images
    dguess=[{'p':0.58,'row':15.,'col':15.,'irr':1.3,'irc':-0.05,'icc':1.16},
            {'p':0.19,'row':15.,'col':15.,'irr':7.7,'irc':-0.23,'icc':6.8}]
    im=gmix_image.gmix2image(dguess, [31,31])
    print 'counts:',im.sum()
    images.multiview(im)
Esempio n. 5
0
    def _set_sigma_weight(self):
        """
        find the largest
        """

        N=4.0
        
        sigma_weight=0.0
        dk = 0.0
        for obslist in self.mb_obs:
            for obs in obslist:

                kreal = obs.deconvolver.psf_kreal
                kmax = util.find_kmax(kreal)

                if False:
                    import images
                    images.multiview(
                        kreal.array,
                        title='psf k real',
                    )

                print("    kmax:",kmax)

                tsigma = math.sqrt(2*N)/kmax
                if tsigma > sigma_weight:
                    sigma_weight = tsigma
                    dk = kreal.scale

        self.sigma_weight = sigma_weight/dk
        print("    sigma weight:",self.sigma_weight)
Esempio n. 6
0
    def _set_sigma_weight(self):
        """
        find the largest
        """

        N = 4.0

        sigma_weight = 0.0
        dk = 0.0
        for obslist in self.mb_obs:
            for obs in obslist:

                kreal = obs.deconvolver.psf_kreal
                kmax = util.find_kmax(kreal)

                if False:
                    import images
                    images.multiview(
                        kreal.array,
                        title='psf k real',
                    )

                print("    kmax:", kmax)

                tsigma = math.sqrt(2 * N) / kmax
                if tsigma > sigma_weight:
                    sigma_weight = tsigma
                    dk = kreal.scale

        self.sigma_weight = sigma_weight / dk
        print("    sigma weight:", self.sigma_weight)
Esempio n. 7
0
def _make_symmetrized_image(im_input):
    """
    add a version of itself roated by 90,180,270 degrees
    """
    im = im_input.copy()
    im += numpy.rot90(im_input, k=1)
    im += numpy.rot90(im_input, k=2)
    im += numpy.rot90(im_input, k=3)

    im *= (1.0/4.0)

    if False:
        import images
        images.multiview(im)
        images.compare_images(
            im_input,
            im,
            label1='orig',
            label2='symm',
            width=1000,
            height=1000,
        )
        if 'q'==raw_input('hit a key: '):
            stop

    return im
Esempio n. 8
0
def test_sub_pixel(ellip,theta):
    """
    Round objects for now
    """
    import fimage
    import images

    sigma_vals = linspace(1.0,3.0,10)
    nsub_vals = array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],dtype='i4')

    data = subpixel_struct(sigma_vals.size, nsub_vals.size)

    ct=0
    amt=0
    for j in xrange(sigma_vals.size):
        sigma = sigma_vals[j]
        data['sigma'][j] = sigma


        Irr,Irc,Icc=util.ellip2mom(2*sigma**2, e=ellip, theta=theta)
        print("sigma: %0.2f Irr: %0.2f Irc: %0.2f Icc: %0.2f" % (sigma,Irr,Irc,Icc))

        d=int( numpy.ceil( 4.5*sigma*2 ) )
        if (d % 2) == 0:
            d += 1
        cen=[(d-1)/2]*2
        dims = [d,d]

        # create image with super good subpixel integration 
        ct0=time.time()
        im=fimage.model_image('gauss',dims,cen,[Irr,Irc,Icc],nsub=64)
        ct += time.time()-ct0

        amt0=time.time()
        for i in xrange(nsub_vals.size):
            nsub = nsub_vals[i]
            res = admom(im, cen[0], cen[1], guess=sigma**2, nsub=nsub)
            if res['whyflag'] != 0:
                print("    ** Failure:'%s'" % res['whystr'])
                images.multiview(im, levels=7)
                #key=raw_input('hit a key: ')
                #if key == 'q': return

            data['Irr'][j,i] = res['Irr']
            data['Irc'][j,i] = res['Irc']
            data['Icc'][j,i] = res['Icc']
            data['a4'][j,i] = res['a4']
            data['nsub'][j,i] = nsub

            #print("  nsub:",nsub)

        amt += time.time()-amt0

    #print("time for creation:",ct)
    #print("time for admom:",amt)

    outf=subpixel_file(ellip,theta,'fits')
    eu.io.write(outf,data,verbose=True,clobber=True)
    plot_sub_pixel(ellip,theta)
Esempio n. 9
0
    def _process_star(self, iobj, icutout):
        from gmix_image import GMixEMBoot
        import admom

        defres=({'whyflag':ZERO_WEIGHT_PIXELS},
                {'flags':ZERO_WEIGHT_PIXELS},
                {'flags':ZERO_WEIGHT_PIXELS},
                numpy.zeros(len(WIDTHS)) - 9999,
                -9999.0)

        im,ivar,cen_guess=self._get_star_data(iobj,icutout)

        if im is None:
            return defres
        
        max_pixel = im.max()

        widths = measure_image_width(im, WIDTHS)

        cen1_guess=cen_guess
        sig1_guess=sqrt(2)

        ares = admom.admom(im,
                           cen_guess[0],
                           cen_guess[1],
                           guess=sig1_guess)



        gm1=GMixEMBoot(im, 1, cen1_guess, sigma_guess=sig1_guess)
        res1=gm1.get_result()
        
        if res1['flags'] == 0:
            #print cen_guess
            #print res1['pars']
            sig2_guess=sqrt( (res1['pars'][3] + res1['pars'][5])/2. )
            cen2_guess=[res1['pars'][1], res1['pars'][2] ]
        else:
            sig2_guess=sig1_guess
            cen2_guess=cen1_guess

        gm2=GMixEMBoot(im, 2, cen2_guess, sigma_guess=sig2_guess)

        res2=gm2.get_result()

        if False and res2['flags'] != 0:
            import images
            images.multiview(im)
            resp=raw_input('hit enter (q to quit): ')
            if resp.lower() == 'q':
                stop


        return ares,res1,res2,widths,max_pixel
Esempio n. 10
0
 def _debug_image(self, im, wt):
     import biggles
     import images
     implt=images.multiview(im,show=False,title='image')
     wtplt=images.multiview(wt,show=False,title='weight')
     arr=biggles.Table(2,1)
     arr[0,0] = implt
     arr[1,0] = wtplt
     arr.show()
     key=raw_input('hit a key (q to quit): ')
     if key.lower() == 'q':
         stop
Esempio n. 11
0
def find_centroid(image, rng, offset=None, maxiter=200, ntry=4):
    """
    use AM to fit a single gaussian
    """


    dims = numpy.array(image.shape)
    row0, col0 = (dims-1)/2.0

    if offset is not None:
        row0 += offset['row_offset']
        col0 += offset['col_offset']


    if False:
        import images
        images.multiview(image, width=800,height=800)
        if 'q'==raw_input('hit a key: '):
            stop



    j=ngmix.UnitJacobian(row=row0, col=col0)

    obs = ngmix.Observation(image, jacobian=j)

    guess_T = 4.0
    for i in xrange(ntry):
        fitter = ngmix.admom.run_admom(obs, guess_T, rng=rng)

        res=fitter.get_result()
        if res['flags']==0:
            break

    if res['flags'] != 0:
        if False:
            import images
            images.view(image, width=800,height=800)
            if 'q'==raw_input('hit a key: '):
                stop

        raise TryAgainError("could not fit 1 gauss to get centroid")

    pars=res['pars']
    row=pars[0]
    col=pars[1]

    row = row0 + row
    col = col0 + col

    return row,col
Esempio n. 12
0
    def make_f0conv(self):
        """
        Make f0 convolved with epsilon.  It is on this image we will
        measure new adaptive moments.

        f0 will be expanded if epsilon is larger.  The center will remain
        the same in that expansion

        """
        import scipy.signal
        import images
        if self.f0 is None or self.epsilon is None:
            raise ValueError("Create both f0 and epsilon first")
        if self.f0 is None:
            raise ValueError("f0 is None")

        f0 = self.f0
        ep = self.epsilon

        if self.verbose:
            if ep.shape[0] > f0.shape[0] or ep.shape[1] > f0.shape[0]:
                print("epsilon is larger than image:")
                print("  epsize:",ep.shape)
                print("  f0size:",f0.shape)
        # if f0 is bigger, it is returned unchanged
        f0_expand = images.expand(f0, ep.shape)

        if self.conv == 'fft':
            f0conv = scipy.signal.fftconvolve(f0_expand, ep, mode='same')
        else:
            f0conv = scipy.signal.convolve2d(f0_expand,
                                             ep, 
                                             #old_behavior=False,
                                             mode='same')

        # trim back to the original size
        if (f0conv.shape[0] > self.image.shape[0] 
                or f0conv.shape[1] > self.image.shape[1]):
            f0conv = f0conv[ 0:self.image.shape[0], 0:self.image.shape[1] ]
            if self.debug:
                print("trimming back")

        self.f0conv = f0conv

        if self.debug:
            print("f0.shape",self.f0.shape)
            print("f0conv.shape",self.f0conv.shape)
            images.multiview(self.f0,title='f0')
            images.multiview(self.f0conv, title='f0conv')
Esempio n. 13
0
    def make_psf(self, show=False):
        """
        Make the psf image
        """
        psf_obj_prepix = galsim.Gaussian(fwhm=self._psf_fwhm,
                                         flux=1.0)
        self._psf_obj = galsim.Convolve([psf_obj_prepix, self._pix_obj])

        self._psf_image_obj = self._psf_obj.draw(dx=self._pixel_scale)
        self._psf_image=self._psf_image_obj.array.astype('f8')

        self._psf_cen = [ 0.5*self._psf_image.shape[0] ]*2

        if show:
            import images
            images.multiview(self.psf_image)
Esempio n. 14
0
    def _doplots(self, id, mindex, icut, img_orig, img, bmask, weight):
        import images
        imdiff = img_orig - img

        lbmask = numpy.log10(1.0 + bmask)
        imlist = [img_orig, img, imdiff, lbmask, weight]
        imlist = [t - t.min() for t in imlist]

        titles = ['image', 'imfix', 'image-imfix', 'bmask', 'weight']

        title = '%d-%06d-%02d' % (id, mindex, icut)
        pname = '%s-%d-%06d-%02d.png' % (self.front, id, mindex, icut)
        pname = os.path.join(self.pdir, pname)
        tab = images.view_mosaic(
            imlist,
            titles=titles,
            show=False,
        )
        tab.title = title

        ctab = images.multiview(img, show=False)
        tab[1, 2] = ctab[0, 1]

        print("    writing:", pname)
        tab.write_img(800, 800, pname)
Esempio n. 15
0
def _make_interpolated_image(im, nsub, order=1):
    """
    Make a new image linearly interpolated 
    on a nsubxnsub grid
    """
    # mgrid is inclusive at end when step
    # is complex and indicates number of
    # points
    import scipy.ndimage
    zimage=scipy.ndimage.zoom(im, nsub, order=order)
    if True:
        import images
        images.multiview(im)
        images.multiview(zimage)

    return zimage
Esempio n. 16
0
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)
Esempio n. 17
0
    def _add_fake_uberseg(self, weight):
        """
        for now just a circle
        """

        sconf=self['fake_uberseg']

        cen = (numpy.array(weight.shape)-1.0)/2.0
        rmax = sconf['rmax_frac']*cen[0]
        rsq_max = rmax**2

        rows,cols = numpy.mgrid[
            0:weight.shape[0],
            0:weight.shape[1],
        ]

        rows = numpy.array(rows, dtype='f8')
        cols = numpy.array(cols,dtype='f8')

        if sconf['type']=='circle':
            axis_ratio=1.0
        elif sconf['type']=='ellipse':
            axis_ratio=sconf['axis_ratio']
        else:
            raise ValueError("bad fake useg type: '%s'" % sconf['type'])

        if 'offset' in sconf:
            offset = self.rng.normal(scale=sconf['offset']['sigma'],size=2)
            print("offset:",offset)
            cen += offset

        rm = rows-cen[0]
        cm = cols-cen[1]
        rsq = ( rm*axis_ratio)**2 - 2*rm*cm*0.2 + cm**2

        w=numpy.where(rsq > rsq_max)
        weight[w] = 0.0

        if False:
            import images
            images.multiview(weight)
            if raw_input('hit a key')=='q':
                stop
Esempio n. 18
0
    def _show_coadd(self, obslist):
        import biggles
        import images


        for i,obs in enumerate(obslist):
            if len(obslist) > 1:
                title='image %d' % i
            else:
                title='coadd'

            tab = biggles.Table(2, 1)
            ppsf = images.multiview(obs.psf.image, title='psf',width=1000,height=1000,show=False)
            pim = images.multiview(obs.image, title=title,width=1000,height=1000,show=False)
            tab[0,0] = ppsf
            tab[1,0] = pim
            tab.show()

        if raw_input('hit a key (q to quit): ')=='q':
            stop
Esempio n. 19
0
    def _make_obs(self, im, psf_im):
        """
        Make new Observation objects for the image and psf.
        Copy out the weight maps and jacobians from the original
        Observation.

        parameters
        ----------
        im: Galsim Image
        psf_im: Galsim Image

        returns
        -------
        A new Observation
        """

        obs = self.obs

        psf_obs = self._make_psf_obs(psf_im)

        newobs = Observation(
            im.array,
            jacobian=obs.jacobian.copy(),
            weight=obs.weight.copy(),
            psf=psf_obs,
        )
        if False:
            import images
            print("orig psf im sum:", self.obs.psf.image.sum())
            print("new psf im sum:", psf_im.array.sum())
            images.multiview(
                psf_im.array,
                title='psf',
                file='/u/ki/esheldon/public_html/tmp/plots/tmp.png',
            )
            if 'q' == raw_input('hit a key: '):
                stop

        return newobs
Esempio n. 20
0
    def _read_data(self):
        import fitsio
        import images
        path=files.get_output_path(ftype='shear-stack',
                                   run=self.run,
                                   psfnum=self.psfnum,
                                   shnum=self.shnum)
        print path
        with fitsio.FITS(path) as fits:
            #res=fits[1].read()
            self.psf_stack=fits[2].read()

            self.psf_h=fits[2].read_header()
            self.psf_skyvar=self.psf_h['skyvar']

            self.im_stacks=fits[3].read()

        return 
        subtract_median=False

        images.multiview(self.psf_stack,title='psf')

        edg=3

        if subtract_median:
            psf=self.psf_stack
            pix2med = tuple( [psf[:,0:edg].ravel(), psf[:, -edg:-1].ravel(), psf[0:edg, :].ravel(), psf[-edg:-1, :].ravel() ])
            pix2med=numpy.concatenate(pix2med)
            self.psf_stack -= numpy.median(pix2med)

            images.multiview(self.psf_stack,title='psf after')

        for i in xrange(self.im_stacks.size):

            images.multiview(self.im_stacks['images'][i,:,:],title='%d before' % (i+1))
            
            if subtract_median:

                im=self.im_stacks['images'][i,:,:]
                pix2med = tuple( [im[:,0:edg].ravel(), im[:, -edg:-1].ravel(), im[0:edg, :].ravel(), im[-edg:-1, :].ravel() ])
                pix2med=numpy.concatenate(pix2med)

                self.im_stacks['images'][i,:,:] -= numpy.median(pix2med)

                images.multiview(self.im_stacks['images'][i,:,:],title='%d after' % (i+1))
Esempio n. 21
0
    def _doplots(self, id, mindex, icut, img_orig, img, bmask, weight):
        import images

        imdiff = img_orig - img

        lbmask = numpy.log10(1.0 + bmask)
        imlist = [img_orig, img, imdiff, lbmask, weight]
        imlist = [t - t.min() for t in imlist]

        titles = ["image", "imfix", "image-imfix", "bmask", "weight"]

        title = "%d-%06d-%02d" % (id, mindex, icut)
        pname = "%s-%d-%06d-%02d.png" % (self.front, id, mindex, icut)
        pname = os.path.join(self.pdir, pname)
        tab = images.view_mosaic(imlist, titles=titles, show=False)
        tab.title = title

        ctab = images.multiview(img, show=False)
        tab[1, 2] = ctab[0, 1]

        print("    writing:", pname)
        tab.write_img(800, 800, pname)
Esempio n. 22
0
    def _generate_ring_stacks(self):

        sdata={}
        gvals=self._get_gvals()
        
        i=0
        while i < self.nimage:
            if (((i+1) % 100) == 0):
                print '%d/%d' % (i+1,self.nimage)

            g=gvals[i]
            imd1,imd2=self._make_pair(g)
            if imd1['flags'] != 0 or imd2['flags'] != 0:
                print 'bad'
                continue

            if i==0:
                sdata['image_stack']  = numpy.zeros(imd1['image'].shape)
                sdata['psf_stack']    = numpy.zeros(imd1['psf_image'].shape)
                sdata['image_skyvar'] = 0.0
                sdata['psf_skyvar']   = 0.0

                sdata['imsum']=0.0
                """
                sdata['rowcen2_sum'] = 0.0
                sdata['rowcolcen_sum'] = 0.0
                sdata['colcen2_sum'] = 0.0
                """

                sdata['row2_sum'] = 0.0
                sdata['rowcol_sum'] = 0.0
                sdata['col2_sum'] = 0.0

                sdata['psf_imsum']=0.0
                """
                sdata['psf_rowcen2_sum'] = 0.0
                sdata['psf_rowcolcen_sum'] = 0.0
                sdata['psf_colcen2_sum'] = 0.0
                """

                sdata['psf_row2_sum'] = 0.0
                sdata['psf_rowcol_sum'] = 0.0
                sdata['psf_col2_sum'] = 0.0



            sdata['image_stack'] += imd1['image']
            sdata['image_skyvar'] += imd1['image_skyvar']
            sdata['psf_stack'] += imd1['psf_image']
            sdata['psf_skyvar'] += imd1['psf_skyvar']


            sdata['imsum'] += imd1['imsum']
            """
            sdata['rowcen2_sum'] += imd1['rowcen2_sum']
            sdata['rowcolcen_sum'] += imd1['rowcolcen_sum']
            sdata['colcen2_sum'] += imd1['colcen2_sum']
            """

            sdata['row2_sum'] += imd1['row2_sum']
            sdata['rowcol_sum'] += imd1['rowcol_sum']
            sdata['col2_sum'] += imd1['col2_sum']

            sdata['psf_imsum'] += imd1['psf_imsum']
            """
            sdata['psf_rowcen2_sum'] += imd1['psf_rowcen2_sum']
            sdata['psf_rowcolcen_sum'] += imd1['psf_rowcolcen_sum']
            sdata['psf_colcen2_sum'] += imd1['psf_colcen2_sum']
            """

            sdata['psf_row2_sum'] += imd1['psf_row2_sum']
            sdata['psf_rowcol_sum'] += imd1['psf_rowcol_sum']
            sdata['psf_col2_sum'] += imd1['psf_col2_sum']



            sdata['image_stack'] += imd2['image']
            sdata['image_skyvar'] += imd2['image_skyvar']
            sdata['psf_stack'] += imd2['psf_image']
            sdata['psf_skyvar'] += imd2['psf_skyvar']

            sdata['imsum'] += imd2['imsum']
            """
            sdata['rowcen2_sum'] += imd2['rowcen2_sum']
            sdata['rowcolcen_sum'] += imd2['rowcolcen_sum']
            sdata['colcen2_sum'] += imd2['colcen2_sum']
            """

            sdata['row2_sum'] += imd2['row2_sum']
            sdata['rowcol_sum'] += imd2['rowcol_sum']
            sdata['col2_sum'] += imd2['col2_sum']
 
            sdata['psf_imsum'] += imd2['psf_imsum']
            """
            sdata['psf_rowcen2_sum'] += imd2['psf_rowcen2_sum']
            sdata['psf_rowcolcen_sum'] += imd2['psf_rowcolcen_sum']
            sdata['psf_colcen2_sum'] += imd2['psf_colcen2_sum']
            """

            sdata['psf_row2_sum'] += imd2['psf_row2_sum']
            sdata['psf_rowcol_sum'] += imd2['psf_rowcol_sum']
            sdata['psf_col2_sum'] += imd2['psf_col2_sum']

            i += 1
 
        
        self._stack_data=sdata

        if False:
            import images
            images.multiview(image_stack)
Esempio n. 23
0
    def _measure_gals_admom(self):
        import admom
        nbin=self.im_stacks.size

        Tpsf=self.psf_ares['Irr'] + self.psf_ares['Icc']
        e1p=self.psf_ares['e1']
        e2p=self.psf_ares['e2']
        a4p=self.psf_ares['a4']

        st=zeros(nbin, dtype=[('s2n','f8'),
                              ('e1','f8'),
                              ('e1err','f8'),
                              ('e2','f8'),
                              ('e2err','f8'),
                              ('T','f8'),
                              ('R','f8')])
        self._ares_dicts=[]
        for i in xrange(nbin):
            print '-'*70
            s2n_min=self.im_stacks['s2n_min'][i]
            s2n_max=self.im_stacks['s2n_max'][i]

            print 's2n: [%.2f,%.2f]' % (s2n_min,s2n_max)


            im=self.im_stacks['images'][i,:,:]
            skysig=sqrt(self.im_stacks['skyvar'][i])

            images.multiview(im)

            cen=array(im.shape)/2.
            ares = admom.admom(im, cen[0], cen[1],
                               sigsky=skysig,
                               guess=4.0,
                               nsub=self.nsub)
            T=ares['Irr']+ares['Icc']

            corr=admom.correct(T, ares['e1'], ares['e2'],ares['a4'],
                               Tpsf, e1p, e2p, a4p)
            
            e1,e2,R,flags=corr

            sh1=0.5*e1/self.Rshear
            sh2=0.5*e2/self.Rshear

            uncer=ares['uncer']/R

            err=0.16/sqrt(self.im_stacks['nstack'][i])

            mess='sh1: %s +/- %s sh2: %s +/- %s R: %s  flags: %s'
            mess=mess % (sh1,err,sh2,err,R,flags)
            print mess

            st['s2n'][i] = (s2n_min+s2n_max)/2.
            st['e1'][i] = e1
            st['e1err'][i] = err
            st['e2'][i] = e2
            st['e2err'][i] = err
            st['R'][i] = R
            st['T'][i] = T

            self._ares_dicts.append(ares)

        self._admom_shear = st
Esempio n. 24
0
 def show_psf_image(self):
     import images
     images.multiview(self.psf_image,title='psf')
Esempio n. 25
0
    def go(self, shear=None, doplots=False):
        """
        measure moments on all images and perform the sum and mean
        """

        fix_noise = self.kw.get('fix_noise', False)

        mb_reslist = []

        dk = self._dk

        if not hasattr(self, 'nx'):
            self.nx, self.ny = None, None
            #self.nx,self.ny=66,66

        for il, obslist in enumerate(self.mb_obs):
            reslist = []
            for obs in obslist:

                ivar = obs.weight

                gs_kimage, gs_ikimage = obs.deconvolver.get_kimage(
                    shear=shear,
                    dk=dk,
                    nx=self.nx,
                    ny=self.ny,
                )

                self.ny, self.nx = gs_kimage.array.shape

                if fix_noise:

                    nshear = shear
                    if nshear is not None:
                        nshear = -nshear

                    ndk = gs_kimage.scale
                    rim, iim = obs.noise_deconvolver.get_kimage(
                        shear=nshear,
                        dk=ndk,
                        nx=self.nx,
                        ny=self.ny,
                    )
                    print(obs.deconvolver.dk, obs.noise_deconvolver.dk)
                    print(gs_kimage.array.shape, rim.array.shape)
                    gs_kimage += rim

                    # adding equal noise doubles the variance
                    ivar = ivar * (1.0 / 2.0)

                print("shape:", gs_kimage.array.shape, "dk:", gs_kimage.scale,
                      "shear:", shear)
                if doplots:
                    import images
                    dims = gs_kimage.array.shape
                    kwt = KSigmaWeight(self.sigma_weight * gs_kimage.scale)
                    cen = util.get_canonical_kcenter(dims)
                    kweight, rows, cols = kwt.get_weight(dims, cen)

                    pim = gs_kimage.array * kweight
                    #pim=gs_kimage.array

                    #off=int(12*(0.1/obs.deconvolver.dk))
                    off = int(18 * (0.1 / obs.deconvolver.dk))
                    pim = pim[cen[0] - off:cen[0] + off + 1,
                              cen[1] - off:cen[1] + off + 1]
                    #images.multiview(obs.image,title='image')
                    #images.multiview(pim,title=str(shear))#,
                    images.multiview(obs.deconvolver.kreal,
                                     title=str(shear))  #,
                    #file='/astro/u/esheldon/www/tmp/plots/tmp.png')

                res = self._measure_moments(gs_kimage, ivar)

                reslist.append(res)

            mb_reslist.append(reslist)

        if self._force_same:
            self._kdims = ny, nx
            self._dk = dk

        self._mb_reslist = mb_reslist
        self._combine_results()
Esempio n. 26
0
 def _show(self, image):
     import images
     return images.multiview(image)
Esempio n. 27
0
    def process_trial(self, is2, is2n, itrial):

        gsum=numpy.zeros(2)
        gsum_means=numpy.zeros(2)
        nsum_means=numpy.zeros(2)

        wsum=numpy.zeros(2)
        nsum=0

        npair=self.get_npair(is2n)
        gvals=self.gprior.sample1d(npair)

        gmm_list=[]
        for i,g in enumerate(gvals):
            imd1,imd2=self._make_ring_pair(g, is2, is2n)
            print '%d/%d' % (i+1,npair)

            mcm1=self._run_mcm(imd1)
            mcm2=self._run_mcm(imd2)

            gmm1=mcm1.get_like_mixture(ngauss=3)
            gmm2=mcm2.get_like_mixture(ngauss=3)

            gmm_list += [gmm1,gmm2]

            res1=mcm1.get_result()
            res2=mcm2.get_result()

            gsum_means += res1['g']
            nsum_means += 1

            wsum[0] += (1./res1['gcov'][0,0])
            wsum[1] += (1./res1['gcov'][1,1])

            gsum_means += res2['g']
            nsum_means += 1

            wsum[0] += (1./res2['gcov'][0,0])
            wsum[1] += (1./res2['gcov'][1,1])

            err=sqrt(1./wsum)
            mn=gsum_means/nsum_means
            print '  g1m: %s +/- %s    g2m: %s +/- %s' % (mn[0],err[0],mn[1],err[1])



        self._ng1=200
        self._ng2=200
        g1range=[mn[0]-8.*err[0], mn[0]+8.*err[0]]
        g2range=[mn[1]-8.*err[1], mn[1]+8.*err[1]]
        lnp_surf = self._get_cum_loglike_surf(gmm_list, 
                                              g1range=g1range, 
                                              g2range=g2range)

        self.lnp_surf = lnp_surf - lnp_surf.max()

        g1max,g2max=self._get_maxlike_loc(lnp_surf)
        print 'maxlike:     ',g1max,g2max

        self._test_gmm_mean(gmm_list)
        if False:
            import images
            plt=images.multiview(exp(self.lnp_surf),
                                 xdr=g2range, 
                                 ydr=g1range,
                                 xlabel='g2', ylabel='g1',
                                 show=False)
            plt_log=images.multiview(self.lnp_surf,
                                 xdr=g2range, 
                                 ydr=g1range,
                                 xlabel='g2', ylabel='g1',
                                 show=False)

            fname='/tmp/test.png'
            fname_log='/tmp/test-log.png'
            print fname
            print fname_log
            plt.write_img(1024,1024,fname)
            plt_log.write_img(1024,1024,fname_log)
Esempio n. 28
0
def T_rat_from_fix_rhalf_rat(sigma_psf=1.414, rhalf_exp=4.0, show=False):
    """
    Generate models with r_{1/2}^{dev} = 0.6 r_{1/2}^{exp} and
    measure the T ratio

    parameters
    ----------
    rhalf: float, optional
        Half light radius of the exponential component

    dependencies
    ------------
    galsim
    """
    import galsim 
    import ngmix
    from numpy.random import randn
    from esutil.random import srandu
    import pprint

    flux=100.0
    s2n=1.0e6

    shape=(40,40)
    size=shape[0]*shape[1]
    pixel_scale=1.0
    j=ngmix.jacobian.UnitJacobian(0.5*shape[0], 0.5*shape[1])

    rhalf_dev = 0.6*rhalf_exp

    pix = galsim.Pixel(pixel_scale)
    psf0=galsim.Gaussian(flux=1.0, sigma=sigma_psf)

    exp0=galsim.Exponential(flux=1.0, half_light_radius=rhalf_exp)
    dev0=galsim.DeVaucouleurs(flux=1.0, half_light_radius=rhalf_dev)
    gal0 = galsim.Add([exp0, dev0])
    gal0.setFlux(flux)

    gal = galsim.Convolve([gal0, psf0, pix])
    psf = galsim.Convolve([psf0, pix])

    galsim_image     = galsim.ImageD(shape[0], shape[1])
    galsim_psf_image = galsim.ImageD(shape[0], shape[1])

    gal.draw(galsim_image, dx=pixel_scale)
    psf.draw(galsim_psf_image, dx=pixel_scale)

    image_nonoise=galsim_image.array
    psf_image=galsim_psf_image.array

    skysig2 = (image_nonoise**2).sum()/s2n**2
    skysig = numpy.sqrt(skysig2)

    image = image_nonoise + skysig*randn(size).reshape(shape)

    if show:
        import images
        images.multiview(psf_image,title='psf')
        images.multiview(image,title='image')

    imsky,sky=ngmix.em.prep_image(psf_image)
    em=ngmix.em.GMixEM(imsky, jacobian=j)
    Tpsf_guess=2*sigma_psf**2
    psf_guess=ngmix.gmix.GMixModel([0.0, 0.0, 0.0, 0.0, Tpsf_guess, 1.0],'gauss')
    em.go(psf_guess, sky)

    psf_gmix = em.get_gmix()

    nwalkers=1000
    guess=numpy.zeros( (nwalkers, 8) )
    guess[:,0] = 0.1*srandu(nwalkers)
    guess[:,1] = 0.1*srandu(nwalkers)
    guess[:,2] = 0.1*srandu(nwalkers)
    guess[:,3] = 0.1*srandu(nwalkers)
    guess[:,4] = 40*(1.0 + 0.1*srandu(nwalkers))
    guess[:,5] = 30*(1.0 + 0.1*srandu(nwalkers))
    guess[:,6] = flux*(1.0 + 0.1*srandu(nwalkers))
    guess[:,7] = flux*(1.0 + 0.1*srandu(nwalkers))

    T_prior = ngmix.priors.FlatPrior(0.001, 200)
    counts_prior = ngmix.priors.FlatPrior(0.001, 1000)

    wt=0*image + 1.0/skysig2
    fitter=ngmix.fitting.MCMCBDC(image, wt, j, 'bdc',
                                 psf=psf_gmix,
                                 full_guess=guess,
                                 T_b_prior=T_prior,
                                 T_d_prior=T_prior,
                                 counts_b_prior=counts_prior,
                                 counts_d_prior=counts_prior,
                                 nwalkers=nwalkers,
                                 burnin=800,
                                 nstep=100,
                                 mca_a=3.0,
                                 min_arate=0.3)
    fitter.go()
    if show:
        fitter.make_plots(show=show)
    res=fitter.get_result()

    pars=res['pars']
    perr=res['pars_err']
    Tb = pars[4] 
    Td = pars[5]
    Tb_err = perr[4]
    Td_err = perr[5]

    Trat = Tb/Td
    Trat_err = Trat*numpy.sqrt( (Tb_err/Tb)**2 + (Td_err/Td)**2 )

    pprint.pprint(res)
    print
    print '%s +/- %s' % (Trat, Trat_err)
Esempio n. 29
0
    def go(self, shear=None, doplots=False):
        """
        measure moments on all images and perform the sum and mean
        """

        fix_noise=self.kw.get('fix_noise',False)

        mb_reslist=[]

        dk = self._dk

        if not hasattr(self,'nx'):
            self.nx,self.ny=None,None
            #self.nx,self.ny=66,66

        for il,obslist in enumerate(self.mb_obs):
            reslist=[]
            for obs in obslist:

                ivar=obs.weight

                gs_kimage,gs_ikimage = obs.deconvolver.get_kimage(
                    shear=shear,
                    dk=dk,
                    nx=self.nx,
                    ny=self.ny,
                )

                self.ny,self.nx=gs_kimage.array.shape

                if fix_noise:

                    nshear=shear
                    if nshear is not None:
                        nshear = -nshear

                    ndk = gs_kimage.scale
                    rim,iim = obs.noise_deconvolver.get_kimage(
                        shear=nshear,
                        dk=ndk,
                        nx=self.nx,
                        ny=self.ny,
                    )
                    print(obs.deconvolver.dk,obs.noise_deconvolver.dk)
                    print(gs_kimage.array.shape, rim.array.shape)
                    gs_kimage += rim

                    # adding equal noise doubles the variance
                    ivar = ivar * (1.0/2.0)

                print("shape:",gs_kimage.array.shape,"dk:",gs_kimage.scale,"shear:",shear)
                if doplots:
                    import images
                    dims=gs_kimage.array.shape
                    kwt = KSigmaWeight(self.sigma_weight*gs_kimage.scale)
                    cen=util.get_canonical_kcenter(dims)
                    kweight, rows, cols = kwt.get_weight(dims, cen)

                    pim=gs_kimage.array*kweight
                    #pim=gs_kimage.array

                    #off=int(12*(0.1/obs.deconvolver.dk))
                    off=int(18*(0.1/obs.deconvolver.dk))
                    pim = pim[cen[0]-off:cen[0]+off+1,
                              cen[1]-off:cen[1]+off+1]
                    #images.multiview(obs.image,title='image')
                    #images.multiview(pim,title=str(shear))#,
                    images.multiview(obs.deconvolver.kreal,title=str(shear))#,
                                     #file='/astro/u/esheldon/www/tmp/plots/tmp.png')

                res=self._measure_moments(gs_kimage, ivar)

                reslist.append(res)

            mb_reslist.append( reslist )

        if self._force_same:
            self._kdims=ny,nx
            self._dk = dk

        self._mb_reslist=mb_reslist
        self._combine_results()
Esempio n. 30
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)
Esempio n. 31
0
 def show_image(self):
     import images
     images.multiview(self.image,title='object')
Esempio n. 32
0
def test_regauss(gal_model='gauss',
                 gal_T=16.0,
                 gal_e1=0.0,
                 gal_e2=0.0,
                 psf_T=4.0,
                 psf_e1=0.0,
                 psf_e2=0.0,
                 show=True):

    from pprint import pprint
    import fimage
    import scipy.signal

    nsigma=5

    psf_sigma=numpy.sqrt(psf_T/2.)
    psf_imsize = int( round(2*nsigma*psf_sigma) )
    if (psf_imsize % 2) == 0:
        psf_imsize+=1

    psf_dims=[psf_imsize]*2
    psf_cen=[(psf_imsize-1)/2.]*2
    psf_moms = admom.ellip2mom(psf_T,e1=psf_e1,e2=psf_e2)
    psf = fimage.model_image('gauss', 
                             psf_dims,
                             psf_cen,
                             psf_moms,
                             nsub=16)

    gal_moms = admom.ellip2mom(gal_T,e1=gal_e1,e2=gal_e2)


    # for the image size, make it big enough to easily fit
    # the *convolved* galaxy
    gal_convolved_sigma = admom.mom2sigma(gal_T+psf_T)
    gal_imsize = int( round(2*nsigma*gal_convolved_sigma) )
    if gal_imsize < psf_imsize:
        gal_imsize = psf_imsize

    gal_cen  = [ (gal_imsize-1)/2. ]*2
    gal_dims = [int(gal_imsize)]*2

    gal = fimage.model_image(gal_model,
                             gal_dims,
                             gal_cen,
                             gal_moms,
                             nsub=16)

    levels=7

    # now convolve with the PSF
    imconv = scipy.signal.fftconvolve(gal, psf, mode='same')

    if show:
        import images
        images.multiview(psf, levels=levels)
        images.multiview(gal, levels=levels)
        images.multiview(imconv, levels=levels)

    rg = ReGauss(imconv, gal_cen[0], gal_cen[1], psf, guess=gal_T/2, guess_psf=psf_T/2)

    rg.do_admom()
    print("admom stats")
    pprint(rg['imstats'])
    rg.do_psf_admom()
    print("psf admom stats")
    pprint(rg['psfstats'])
    rg.do_basic_corr()
    print("admom corrected stats")
    pprint(rg['corrstats'])
    rg.do_regauss()
    print("rg stats")
    pprint(rg['rgstats'])
    rg.do_rg_corr()
    print("rg corrected stats")
    pprint(rg['rgcorrstats'])
Esempio n. 33
0
    def _make_plots(self, obs):
        import images

        images.multiview(obs.image)
        if raw_input('hit a key: ')=='q':
            stop
Esempio n. 34
0
    def _measure_gals_admom(self):
        import admom
        nbin=self.im_stacks.size

        Tpsf=self.psf_ares['Irr'] + self.psf_ares['Icc']
        e1p=self.psf_ares['e1']
        e2p=self.psf_ares['e2']
        a4p=self.psf_ares['a4']

        st=zeros(nbin, dtype=[('s2n','f8'),
                              ('e1','f8'),
                              ('e1err','f8'),
                              ('e2','f8'),
                              ('e2err','f8'),
                              ('T','f8'),
                              ('R','f8')])
        for i in xrange(nbin):
            print '-'*70
            s2n_min=self.im_stacks['s2n_min'][i]
            s2n_max=self.im_stacks['s2n_max'][i]

            print 's2n: [%.2f,%.2f]' % (s2n_min,s2n_max)


            im_real=self.im_stacks['images_real'][i,:,:]
            im_imag=self.im_stacks['images_imag'][i,:,:]

            imc = self._make_complex_image(im_real, im_imag)
            im=self._make_cspace_image(imc)
            if False and i==3:
                import images
                images.multiview(im)

            skysig=sqrt(self.im_stacks['skyvar'][i])

            cen=array(im.shape)/2.
            ares = admom.admom(im, cen[0], cen[1],
                               sigsky=skysig,
                               guess=4.0,
                               nsub=self.nsub)
            T=ares['Irr']+ares['Icc']

            corr=admom.correct(T, ares['e1'], ares['e2'],ares['a4'],
                               Tpsf, e1p, e2p, a4p)
            
            e1,e2,R,flags=corr
            uncer=ares['uncer']/R

            err2=( 0.32**2 + uncer**2)/self.im_stacks['nstack'][i]
            err=sqrt(err2)

            mess='e1: %s +/- %s e2: %s +/- %s R: %s  flags: %s'
            mess=mess % (e1,err,e2,err,R,flags)
            print mess

            st['s2n'][i] = (s2n_min+s2n_max)/2.
            st['e1'][i] = e1
            st['e1err'][i] = err
            st['e2'][i] = e2
            st['e2err'][i] = err
            st['R'][i] = R
            st['T'][i] = T

        self._admom_shear = st