Ejemplo n.º 1
0
def main():

    args = get_args()
    rng = np.random.RandomState(args.seed)

    obs, gm = make_data(rng=rng, noise=args.noise)

    guess = gm.copy()
    randomize_gmix(rng=rng, gmix=guess, pixel_scale=obs.jacobian.scale)

    res = ngmix.em.run_em(obs=obs, guess=guess)

    gmfit = res.get_gmix()
    print('true gm:')
    print(gm)
    print('fit gm:')
    print(gmfit)

    if args.show:
        try:
            import images
        except ImportError:
            from espy import images

        imfit = res.make_image()

        images.compare_images(obs.image, imfit)

    return gmfit
Ejemplo n.º 2
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
Ejemplo 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
Ejemplo n.º 4
0
    def _fit_psf_model_lm(self, im, ares, model, skysig):
        """
        Returns the fitter object
        """
        if model == "gmix1":
            ngauss = 1
        elif model == "gmix2":
            ngauss = 2
        elif model == "gmix3":
            ngauss = 3
        else:
            raise ValueError("bad psf model: '%s'" % model)

        ntry = 1
        gm = None
        while ntry <= self["psf_lm_ntry"]:
            tmp = gmix_image.gmix_fit.quick_fit_psf_coellip(im, skysig, ngauss, ares=ares)
            if tmp is None:
                continue
            if tmp.flags == 0:
                gm = tmp
                break
            ntry += 1

        if self["make_psf_plots"] and gm is not None:
            import images

            model = gmix_image.render.gmix2image(gm.get_gmix(), im.shape)
            images.compare_images(im, model, label1="image", label2="model")
            key = raw_input("hit a key (q to quit): ")
            if key == "q":
                stop

        return gm
Ejemplo n.º 5
0
def main():

    args = get_args()
    rng = np.random.RandomState(args.seed)

    obs, gm = make_data(rng=rng, noise=args.noise)

    # use a psf guesser
    guesser = ngmix.guessers.GMixPSFGuesser(rng=rng, ngauss=1)
    guess = guesser(obs=obs)
    res = ngmix.em.run_em(obs=obs, guess=guess)

    gmfit = res.get_gmix()

    print('true gm:')
    print(gm)
    print('fit gm:')
    print(gmfit)

    if args.show:
        try:
            import images
        except ImportError:
            from espy import images

        imfit = res.make_image()

        images.compare_images(obs.image, imfit)

    return gmfit
Ejemplo n.º 6
0
    def _fit_psf_model_em(self, im, ares, model, skysig):
        """
        Returns the fitter object
        """
        if model == "em1":
            ngauss = 1
        elif model == "em2":
            ngauss = 2
        elif model == "em3":
            ngauss = 3
        else:
            raise ValueError("bad psf model: '%s'" % model)

        ivar = 1.0 / skysig ** 2
        gm = GMixEMPSF(im, ivar, ngauss, ares=ares, maxtry_em=self["psf_em_ntry"])
        res = gm.get_result()

        if self["make_psf_plots"] and res["flags"] == 0:
            import images

            model = gmix_image.render.gmix2image(gm.get_gmix(), im.shape)
            images.compare_images(im, model, label1="image", label2="model")
            key = raw_input("hit a key (q to quit): ")
            if key == "q":
                stop

        return gm
Ejemplo n.º 7
0
    def get_target_image(self, psf_obj, shear=None):
        """
        get the target image, convolved with the specified psf
        and possibly sheared

        parameters
        ----------
        psf_obj: A galsim object
            psf object by which to convolve.  An interpolated image,
            or surface brightness profile
        shear: ngmix.Shape, optional
            The shear to apply

        returns
        -------
        galsim image object
        """

        imconv = self._get_target_gal_obj(psf_obj, shear=shear)

        # this should carry over the wcs
        #newim = self.image.copy()
        #imconv.drawImage(
        #    image=newim,
        #    method='no_pixel' # pixel is in the PSF
        #)
        ny, nx = self.image.array.shape

        try:
            newim = imconv.drawImage(
                nx=nx,
                ny=ny,
                #scale=0.263,
                wcs=self.image.wcs,
                dtype=numpy.float64,
            )
        except RuntimeError as err:
            # argh, galsim uses generic exceptions
            raise GMixRangeError("galsim error: '%s'" % str(err))

        if False:
            import images
            print()
            print("imconv:", imconv)
            print()
            print(newim.array.shape, newim.array.dtype)
            print("imsum:", newim.array.sum())
            print()
            images.compare_images(
                self.image.array,
                newim.array,
                label1='image',
                label2='reconvolved',
                file='/u/ki/esheldon/public_html/tmp/plots/tmp.png',
            )
            if 'q' == raw_input('hit a key: '):
                stop
        return newim
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
 def _compare_images(self, im1, im2, **keys):
     import images
     keys['width']=1000
     keys['height']=1000
     images.compare_images(im1, im2, 
                           **keys)
     key=raw_input('hit a key: ')
     if key=='q':
         stop
Ejemplo n.º 11
0
 def show(self):
     import images
     if hasattr(self.gm,'popt'):
         pars=self.gm.popt
     else:
         pars=self.gm.get_pars()
     model=gmix2image(pars,self.psf.shape, coellip=True)
     #print((model-self.psf).max())
     #images.multiview(self.psf)
     #images.multiview(model)
     #model *= self['counts']/model.sum()
     images.compare_images(self.psf, model)
Ejemplo n.º 12
0
def test_interpolate_gauss_image(show=False):
    """
    test that our interpolation works decently for a linear
    piece missing from a gaussian image
    """

    noise = 0.001

    sigma = 4.0
    is2 = 1.0 / sigma**2
    dims = 51, 51
    cen = (np.array(dims) - 1.0) / 2.0

    rows, cols = np.mgrid[0:dims[0], 0:dims[1], ]
    rows = rows - cen[0]
    cols = cols - cen[1]

    image_unmasked = np.exp(-0.5 * (rows**2 + cols**2) * is2)
    weight = image_unmasked * 0 + 1.0 / noise**2

    badcol = int(cen[1] - 3)
    bw = 3
    rr = badcol - bw, badcol + bw + 1

    weight[rr[0]:rr[1], badcol] = 0.0
    image_masked = image_unmasked.copy()
    image_masked[rr[0]:rr[1], badcol] = 0.0

    bmask = np.zeros_like(image_unmasked, dtype=np.int32)
    bad_flags = 0

    msk = (weight <= 0) | ((bmask & bad_flags) != 0)
    assert np.any(msk)

    iimage = interp_image_nocheck(image=image_masked, bad_msk=msk)

    maxdiff = np.abs(image_unmasked - iimage).max()

    if show:
        import images
        images.view_mosaic([image_masked, weight])

        images.compare_images(
            image_unmasked,
            iimage,
            width=2000,
            height=int(2000 * 2 / 3),
        )
        print('max diff:', maxdiff)

    assert maxdiff < 0.0015
Ejemplo n.º 13
0
def lucy_richardson_deconvolution(g,h,eps =0, original=None, N=0):
    #n - iterations count
    f=np.copy(g)
    f_prev=np.zeros(f.shape, dtype=float)
    k=images.compare_images(f_prev, f)
    err=[]
    i=0
    while(True):

        f_prev=np.copy(f)

        k1=conv.convolution2(f,h)
        #k1=k1[h.shape[0]//2:f.shape[0]+h.shape[0]//2, h.shape[1]//2:f.shape[1]+h.shape[1]//2]
        k2=g/k1
        h1=np.flipud(np.fliplr(h))
        del k1

        k3=conv.convolution2(k2,h1)
        del k2
        del h1

        #k3 = k3[h.shape[0] // 2:f.shape[0] + h.shape[0] // 2, h.shape[1] // 2:f.shape[1] + h.shape[1] // 2]
        f=f*k3
        k_prev=k
        k=images.compare_images(f_prev, f)
        err.append(images.compare_images(f[h.shape[0]//2:original.shape[0]+h.shape[0]//2,
                                         h.shape[1]//2:original.shape[1] + h.shape[1]//2],
                   original))
        if(eps!=0 and (k<eps or k_prev<k)):
            print 'eps break'
            break
        if(N!=0 and not(i<N)):
            print 'N break'
            break
        if N!=0:
            print i
        if eps!=0:
            print k
        i+=1
        gc.collect()
        #print k
    err=np.array(err)
    plt.figure()
    plt.plot(np.arange(err.size), err)
    plt.xlabel('iteration')
    plt.ylabel('dif')
    plt.title('compare f(i) and original image')
    plt.show()
    return f
Ejemplo n.º 14
0
    def process_image(self, image, ngauss, cen, cov, psf=None,
                      coellip=False, cocenter=False, show=False):
        im=image.copy()

        # need no zero pixels and sky value
        im_min = im.min()
        if im_min <= 0:
            im -= im_min
            sky=0.001*im.max()
            im += sky
        else:
            sky = im_min


        # In the iteration, we can sometimes run into negative determinants.
        # we will retry a few times with different random offsets in that case


        flags = GMIXEM_ERROR_NEGATIVE_DET
        ntry=0
        while flags == GMIXEM_ERROR_NEGATIVE_DET and ntry < self['max_retry']:
            guess = self.get_guess(ngauss, cen, cov)
            gm = gmix_image.GMixEM(im,guess,
                                   sky=sky,
                                   maxiter=self['gmix_maxiter'],
                                   tol=self['gmix_tol'],
                                   coellip=coellip,
                                   cocenter=cocenter,
                                   psf=psf,
                                   verbose=self['verbose'])
            flags = gm.flags
            #stderr.write("gmix niter: %d\n" % gm.numiter)
            ntry += 1
        #stderr.write("ntry: %d " % ntry)
        out={'gmix': gm.pars,
             'flags': gm.flags,
             'numiter':gm.numiter,
             'fdiff':gm.fdiff,
             'ntry':ntry}
        if flags == 0 and show:
            print 'psf'
            pprint(psf)
            print 'gmix'
            pprint(out['gmix'])
            imrec=gmix_image.gmix2image(out['gmix'], image.shape, 
                                        psf=psf,counts=image.sum())
            images.compare_images(image,imrec,
                                  label1='image',label2='%d gauss' % ngauss)
        return out
Ejemplo n.º 15
0
    def make_plots(self):
        import images

        burnp, histp=self.fitter.make_plots(separate=True,
                                            show=False,
                                            fontsize_min=0.2)

        gmix0=self.fitter.get_gmix()
        gmix=gmix0.convolve(self.psf_gmix)
        model_image=gmix.make_image(self.image.shape,
                                    jacobian=self.jacobian)

        residp = images.compare_images(self.image, model_image,
                                       label1='image',
                                       label2='model',
                                       show=False)

        print(self.burn_png)
        burnp.write_img(1800,1000,self.burn_png)

        print(self.hist_png)
        histp.write_img(1800,1000,self.hist_png)

        print(self.resid_png)
        residp.write_img(1800,1000,self.resid_png)
Ejemplo n.º 16
0
    def _compare_psf(self, fitter, model):
        """
        compare psf image to best fit model
        """
        import images
        from ngmix.em import GMixEM

        psf_image=self.psf_obs.image

        if isinstance(fitter,GMixEM):
            model_image = fitter.make_image(counts=psf_image.sum())
        else:
            gmix=fitter.get_gmix()
            model_image=gmix.make_image(psf_image.shape,
                                        jacobian=self.psf_obs.jacobian)

        plt=images.compare_images(psf_image,
                                  model_image,
                                  label1='psf',
                                  label2=self.conf['psf_model'],
                                  show=False)

        pname='psf-resid-%s-%06d.png' % (model, self.index)
        print("          ",pname)
        plt.write_img(1400,800,pname)
Ejemplo n.º 17
0
    def _compare_gal(self, fitter):
        """
        compare psf image to best fit model
        """
        import images

        model = self['fit_model']
        title = '%d %s' % (self.mindex, model)

        gmix = fitter.get_gmix()

        obs = self._get_observation()

        res = self.res
        psf_gmix = res['psf_gmix']
        gmix_conv = gmix.convolve(psf_gmix)

        image = obs.image
        model_image = gmix_conv.make_image(image.shape, jacobian=obs.jacobian)

        plt = images.compare_images(image,
                                    model_image,
                                    label1='galaxy',
                                    label2=model,
                                    show=False)
        plt.title = title
        pname = 'gal-resid-%06d-%s.png' % (self.mindex, model)

        resid_std = (image - model_image).std()
        print("    residual std:", resid_std)
        print("          ", pname)
        plt.write_img(1400, 800, pname)
Ejemplo n.º 18
0
    def _compare_gal(self, fitter):
        """
        compare psf image to best fit model
        """
        import images

        model=self['fit_model']
        title = '%d %s' % (self.mindex, model)

        gmix = fitter.get_gmix()

        obs = self._get_observation()

        res=self.res
        psf_gmix = res['psf_gmix']
        gmix_conv = gmix.convolve(psf_gmix)

        image=obs.image
        model_image = gmix_conv.make_image(image.shape,
                                           jacobian=obs.jacobian)

        plt=images.compare_images(image,
                                  model_image,
                                  label1='galaxy',
                                  label2=model,
                                  show=False)
        plt.title=title
        pname='gal-resid-%06d-%s.png' % (self.mindex,model)

        resid_std = (image-model_image).std()
        print("    residual std:",resid_std)
        print("          ",pname)
        plt.write_img(1400,800,pname)
Ejemplo n.º 19
0
    def compare_gal(self):
        """
        compare psf image to best fit model
        """
        import images

        fitter=self.gal_fitter

        model=self['model_pars']['model']
        title = '%d %s' % (self.mindex, model)

        gmix = fitter.get_gmix()

        obs = self.obs
        res=fitter.get_result()

        psf_gmix = self.psf_obs.get_gmix()
        gmix_conv = gmix.convolve(psf_gmix)

        image=obs.image
        model_image = gmix_conv.make_image(image.shape,
                                           jacobian=obs.get_jacobian())

        plt=images.compare_images(image,
                                  model_image,
                                  label1='galaxy',
                                  label2=model,
                                  show=False)
        plt.title=title
        pname='gal-resid-%06d-%s.png' % (self.mindex,model)

        resid_std = (image-model_image).std()
        print("    residual std:",resid_std)
        print("          ",pname)
        plt.write_img(1400,800,pname)
Ejemplo n.º 20
0
    def compare_psf(self):
        """
        compare psf image to best fit model
        """
        import images

        fitter=self.psf_fitter

        model=self['psf_pars']['model']

        obs=self.psf_obs
        if 'em' in model:
            model_image = fitter.make_image(counts=obs.image.sum())
        else:
            gm=fitter.get_gmix()
            j=obs.get_jacobian()
            model_image = gm.make_image(obs.image.shape,
                                        jacobian=j)

        plt=images.compare_images(obs.image,
                                  model_image,
                                  label1='psf',
                                  label2=model,
                                  show=False)

        pname='psf-resid-%s-%06d.png' % (model, self.mindex)
        print("          ",pname)
        plt.write_img(1400,800,pname)
Ejemplo n.º 21
0
    def compare_gal(self):
        """
        compare psf image to best fit model
        """
        import images

        fitter = self.gal_fitter

        model = self['model_pars']['model']
        title = '%d %s' % (self.mindex, model)

        gmix = fitter.get_gmix()

        obs = self.obs
        res = fitter.get_result()

        psf_gmix = self.psf_obs.get_gmix()
        gmix_conv = gmix.convolve(psf_gmix)

        image = obs.image
        model_image = gmix_conv.make_image(image.shape,
                                           jacobian=obs.get_jacobian())

        plt = images.compare_images(image,
                                    model_image,
                                    label1='galaxy',
                                    label2=model,
                                    show=False)
        plt.title = title
        pname = 'gal-resid-%06d-%s.png' % (self.mindex, model)

        resid_std = (image - model_image).std()
        print("    residual std:", resid_std)
        print("          ", pname)
        plt.write_img(1400, 800, pname)
Ejemplo n.º 22
0
    def compare_psf(self):
        """
        compare psf image to best fit model
        """
        import images

        fitter = self.psf_fitter

        model = self['psf_pars']['model']

        obs = self.psf_obs
        if 'em' in model:
            model_image = fitter.make_image(counts=obs.image.sum())
        else:
            gm = fitter.get_gmix()
            j = obs.get_jacobian()
            model_image = gm.make_image(obs.image.shape, jacobian=j)

        plt = images.compare_images(obs.image,
                                    model_image,
                                    label1='psf',
                                    label2=model,
                                    show=False)

        pname = 'psf-resid-%s-%06d.png' % (model, self.mindex)
        print("          ", pname)
        plt.write_img(1400, 800, pname)
Ejemplo n.º 23
0
    def do_regauss(self):
        self['rgstats'] = None

        if 'imstats' not in self or 'psfstats' not in self:
            raise ValueError("run admom on image and psf first")

        if self['imstats']['whyflag'] != 0:
            if self.verbose:
                print("admom failed, cannot run regauss")
            return

        self.make_f0()
        if self.f0 == None:
            self['rgstats'] = {'f0flags':2**0}
            return

        self.make_epsilon()
        self.make_f0conv()

        self.iprime = self.image - self.f0conv

        if self.debug:
            import images
            images.compare_images(self.image,
                                  self.iprime,
                                  label1='original',
                                  label2='minus f0conv')
            k=raw_input('hit a key (q to quit): ')
            if k=='q':
                stop

        guess = (self['imstats']['Irr'] + self['imstats']['Irr'])/2
        wrow = self['imstats']['wrow']
        wcol = self['imstats']['wcol']
        out = admom.admom(self.iprime,
                          wrow,
                          wcol,
                          sky=0.0,
                          sigsky=self.sigsky,
                          guess=guess)

        out['f0flags'] = 0
        self['rgstats'] = out

        if self.verbose:
            print("rgstats:")
            pprint(self['rgstats'])
Ejemplo n.º 24
0
    def make_epsilon(self):
        """
        make a model image for the fit gaussian and subtract it from the
        psf.
        
        This becomes a convolution kernel on our simplified model for the galaxy
        
        Note psf and the subtracted gaussian are both normalized to 1, and
        epsilon thus integrates to zero
        """
        import fimage

        if 'psfstats' not in self:
            raise ValueError("run admom on psf first")

        pstats=self['psfstats']

        Irr = pstats['Irr']
        Irc = pstats['Irc']
        Icc = pstats['Icc']

        #row = (self.psf.shape[0]-1)/2
        #col = (self.psf.shape[1]-1)/2
        row = pstats['wrow']
        col = pstats['wcol']
        #print('row:',row,'col:',col)
        #print('wrow:',pstats['wrow'],'wcol:',pstats['wcol'])

        gauss = fimage.model_image('gauss',
                                   self.psf.shape,
                                   [row,col],
                                   [Irr,Irc,Icc],
                                   nsub=1,
                                   counts=1)
        
        # need both our gaussian and the psf to be normalized
        tpsf = self.psf/self.psf.sum()

        if self.debug:
            import images
            images.compare_images(tpsf, gauss, label1='psf',label2='gauss')

        epsilon = tpsf - gauss

        self.epsilon = epsilon
Ejemplo n.º 25
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)
Ejemplo n.º 26
0
def test_conv_exp_gauss():
    '''
    Generate hires objects, convolve them with fft, and
    compare to the conv_exp_gauss.  

    Then rebin the hires and compare to conv_exp_gauss created
    at the lower res
    '''

    fac=5

    cov=array([2.0,0.5,1.0])
    psf_cov = array([1.0,0.0,1.0])

    objpars_hires={'model':'exp',   'cov':cov*fac**2}
    psfpars_hires={'model':'gauss', 'cov':psf_cov*fac**2}

    ci_fft = ConvolvedImage(objpars_hires, psfpars_hires, conv='fft')
    ci_int = ConvolvedImage(objpars_hires, psfpars_hires, conv='fconvint', fconvint_nsub=1)

    images.compare_images(ci_fft.image, ci_int.image, label1='fft', label2='int')
Ejemplo n.º 27
0
    def show_residual(self, ci, psfmix, objmix=None):
        """
        Show plots of the input compared with the fit gaussian mixtures.
        """
        
        psfmodel = gmix_image.gmix2image(psfmix,ci.psf.shape,
                                         counts=ci.psf.sum()) 
        images.compare_images(ci.psf,psfmodel,
                              label1='psf',label2='gmix')

        if objmix is not None:
            skysig=None
            if ci['skysig'] > 0:
                skysig=ci['skysig']
            model0 = gmix_image.gmix2image(objmix,ci.image0.shape,
                                           counts=ci.image0.sum()) 
            model = gmix_image.gmix2image(objmix,ci.image.shape,
                                          psf=psfmix,
                                          counts=ci.image.sum()) 

            images.compare_images(ci.image0,model0,
                                  label1='object0',label2='gmix',
                                  skysig=skysig)
            images.compare_images(ci.image,model,
                                  label1='object+psf',label2='gmix',
                                  skysig=skysig)
        stop
Ejemplo n.º 28
0
    def doplot(self, show=False):
        import images

        gm=self._fitter.get_gmix()

        model=gm.make_image(self.p.shape, jacobian=self.jacobian)

        plt=images.compare_images(self.p, model,
                                  label1='p(g1,g2)',
                                  label2='model',
                                  show=show)

        return plt
Ejemplo n.º 29
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
Ejemplo n.º 30
0
def compare_gmix_approx(type, s2):
    import fimage
    from .render import gmix2image
    from .gmix import GMixCoellip, GMixDev, GMixExp
    import images
    Tpsf=3.9
    Tobj=Tpsf/s2

    objpars = {'model':type, 'cov':[Tobj/2.,0.0,Tobj/2.]}
    psfpars = {'model':'gauss', 'cov':[Tpsf/2.,0.0,Tpsf/2.]}
    ci0=fimage.convolved.ConvolverGaussFFT(objpars,psfpars)
    ci = fimage.convolved.TrimmedConvolvedImage(ci0, fluxfrac=.9997)

    cen=ci['cen']
    psfcen=ci['cen_psf_uw']
    if type=='dev':
        gmix   = GMixDev(     [cen[0],cen[1], 0., 0., Tobj, 1.] )
    elif type=='exp':
        gmix   = GMixExp(     [cen[0],cen[1], 0., 0., Tobj, 1.] )
    else:
        raise ValueError("bad type: '%s'" % type)

    psf_gauss  = GMixCoellip( [psfcen[0],psfcen[1], 0., 0., Tpsf, 1.] )
    obj = gmix.convolve(psf_gauss)

    psf=gmix2image(psf_gauss,ci.image.shape,nsub=16)
    im=gmix2image(obj,ci.image.shape,nsub=16)

    im *= 1.0/im.sum()
    imdev = ci.image*1.0/ci.image.sum()

    images.compare_images(ci.psf, psf,
                          label1='psf0', label2='psf')

    images.compare_images(imdev, im,
                          label1='%s fft' % type, label2='%s gmix' % type)
Ejemplo n.º 31
0
def _do_plots(im, gm, expname, ccd, pos, offset, png_path):
    model=gm.get_model()
    model *= im.sum()/model.sum()
    plt=images.compare_images(im*100, model*100,
                              label1='image',
                              label2='model',
                              nonlinear=.2,
                              show=False)

    title="%s_%02d row: %.1f col: %.2f offset: %.2f''"
    title=title % (expname,ccd,pos[0],pos[1],offset)
    plt.title=title
    plt.title_style['fontsize']=2

    print >>stderr,'    ',png_path
    plt.write_img(1000,1000,png_path)
Ejemplo n.º 32
0
    def _compare_psf_model(self, im, gm, mindex, i,fname,cenpix):
        """
        Since we work in sky coords, can only generate the
        diff image currently
        """
        import os
        import images

        print fname
        name='%s_%06d_%02d' % (self.psf_model,mindex,i)

        imsum=im.sum()
        model=gm.get_model()
        model *= imsum/model.sum()

        if 'em2' in self.psf_model:
            gmix = gm.get_gmix()
            gl=gmix.get_dlist()
            offset=sqrt( (gl[0]['row']-gl[1]['row'])**2 + 
                         (gl[0]['col']-gl[1]['col'])**2 )
            print >>stderr,'offset:',offset
            for g in gl:
                print >>stderr,'gauss %d:' % (i+1),g['row'],g['col']

        #diff = model-im
        #resid = sqrt( (diff**2).sum() )/imsum

        #title='%s sq resid: %s' % (name,resid)
        #plt=images.view(diff, title=title,show=False)

        bname=os.path.basename(fname)
        bname=bname.replace('_psfcat.psf','')

        title='%s row: %.1f col: %.2f' % (bname,cenpix[0],cenpix[1])
        plt=images.compare_images(im, model,show=False,title=title)

        plt.title_style['fontsize']=2

        d=os.path.join(os.environ['HOME'], 'tmp','test-psf-rec')
        if not os.path.exists(d):
            os.makedirs(d)

        pngname='%s_%s.png' % (bname,name)
        path=os.path.join(d,pngname)
        print >>stderr,'    ',path
        plt.write_img(1000,1000,path)
Ejemplo n.º 33
0
    def _compare_gal(self):
        """
        compare psf image to best fit model
        """
        import images

        res=self.res
        gmix = res['fitter'].get_gmix()

        psf_gmix = self._psf_gmix
        gmix_conv = gmix.convolve(psf_gmix)

        model_image = gmix_conv.make_image(self.gal_image.shape,
                                           jacobian=res['jacob'])

        plt=images.compare_images(self.gal_image,
                                  model_image,
                                  label1='galaxy',
                                  label2=self._model,
                                  show=False)
        pname='gal-resid-%06d-%s.png' % (self.rindex,self._model)
        print("          ",pname)
        plt.write_img(1400,800,pname)
Ejemplo n.º 34
0
    def _compare_gal(self, model, fitter):
        """
        compare psf image to best fit model
        """
        import images

        gmix = fitter.get_gmix()

        res=self.res
        psf_gmix = res['psf_gmix']
        gmix_conv = gmix.convolve(psf_gmix)

        gal_image=self.gal_obs.image
        model_image = gmix_conv.make_image(gal_image.shape,
                                           jacobian=self.gal_obs.jacobian)

        plt=images.compare_images(gal_image,
                                  model_image,
                                  label1='galaxy',
                                  label2=model,
                                  show=False)
        pname='gal-resid-%06d-%s.png' % (self.index,model)
        print("          ",pname)
        plt.write_img(1400,800,pname)
Ejemplo n.º 35
0
def lucy_richardson_blind_deconvolution0_1(g, n, m, original):
    #init h
    print 'First error',images.compare_images(g, original)
    plt.imsave(fname='l_r_blind/g.bmp', arr=np.uint8(images.make0to255(g)), cmap='gray')
    #h1=conv.random_psf(3,3)
    # h1[1,:3]=1/3.
    ##h=np.zeros(g.shape, dtype=float)
    #h[255:258, 255:258]=h1
    h = (1. / np.sum(g) ** 2) * conv.correlation2(g, g)
    h/=np.sum(h)
    #h=conv.random_psf(g.shape[0], g.shape[1])
    #h/=np.sum(h)
    #h = conv.correlation2(g, g)
   # print np.sum(h)
    #h/=np.sum(h)
    plt.imsave(fname='l_r_blind/init_h.bmp', arr=np.uint8(images.make0to255(h)), cmap='gray')
    print 'h sum=', np.sum(h)
    #h[255:258, 255:258]=h1
    f=np.copy(g)
    print 'blind l-r'
    print '0  %'
    errors=[]
    for i in range(n):
        f_prev=np.copy(f)

        #print 'h:'
        #print '-- 0 %'
        for k in range(m):
            p = g / (conv.convolution2(f, h))
            flr=np.fliplr(np.flipud(f))
            h=conv.convolution2(p,flr)*h
            h/=np.sum(f)
            #h/=np.sum(h)
            #h = (1. / np.sum(f)) * (h * conv.correlation2(f, p))
            # p=g/(sg.convolve2d(f,h, mode='same'))
            # h=(1./np.sum(f))*(h*sg.correlate2d(f,p,mode='same'))
            #print '--', float(k+1) / m * 100, '%'

            # f=(1./np.sum(h))*(f*sg.correlate2d(h,p,mode='same'))
            # print 'f:'
            # print '-- 0 %'
            h /= np.sum(h)
            for k in range(m):
                p = g / (conv.convolution2(f, h))
                hlr = np.fliplr(np.flipud(h))
                f = conv.convolution2(p, hlr) * f
                # f = (1. / np.sum(h)) * (f * conv.correlation2(h, p))
                # p=g/(sg.convolve2d(f,h, mode='same'))
                # print '--', float(k+1) / m * 100, '%'
            #images.check_image(f)
        print (float(i+1) / n) * 100, ' %'
        name='l_r_blind/new_lena'+str(i)+'.bmp'
        h_name='l_r_blind/h_'+str(i)+'.bmp'
        plt.imsave(fname=name, arr=np.uint8(images.make0to255(f)), cmap='gray')
        #print 't and f comp', images.compare_images(temp, f)
        plt.imsave(fname=h_name, arr=np.uint8(images.make0to255(h)), cmap='gray')
        images.check_image(images.correct_image(f))
        error = images.compare_images(images.correct_image(f), original)
        errors.append(error)
        print 'err =',error
    errors=np.array(errors)
    plt.figure()
    plt.plot(np.arange(errors.size), errors)
    plt.xlabel('Steps')
    plt.ylabel('Dif btw original and f_k')
    plt.show()
    return f,h
Ejemplo n.º 36
0
def main():
    # Initialization - argument parsing, logging and stage preparations
    logging.basicConfig(format='%(asctime)s %(message)s',
                        filename='logs.log',
                        level=logging.INFO)
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

    args = parse_args()
    out_folder = args.out_folder if args.out_folder.endswith(
        '/') else args.out_folder + '/'
    if not os.path.exists(out_folder):
        os.makedirs(out_folder)

    # Training
    logging.info("Preparing images for training...")
    _, gray_img, _, samples, labels = prepare_for_training(
        args.training_image, kernels)
    logging.info("Starting training process...")
    weights = train(samples,
                    labels,
                    num_of_epochs=args.num_of_epochs,
                    batch_size=args.batch_size)
    logging.info("Training done.")

    # Training results
    results = np.round(weights[-1, :], 2)
    logging.info("Results: ")
    for result, kernel, name in zip(results, kernels, kernel_names):
        logging.info(f"Original {name} filter: \n{kernel}")
        logging.info(f"Trained {name} filter: \n{result}")

        out_image = out_folder + f"{name}/training_image_compare.jpg"
        logging.info(f"Saving training comparition to {out_image}\n")
        compare_images(convolution(gray_img, result),
                       convolution(gray_img, kernel),
                       out_image=out_image)

    # Results on validation image
    _, _, validation_image = load_normalize_and_grayscale(
        args.validation_image)
    logging.info(
        f"Acceptance error is set to {args.acceptance_error}% of distance between highest and lowest possible value"
    )
    for result, kernel, name in zip(results, kernels, kernel_names):
        out_image = out_folder + f"{name}/validation_image_compare.jpg"
        logging.info(f"Saving validation comparition to {out_image}")
        perc_of_similarity = compare_images(
            convolution(validation_image, result),
            convolution(validation_image, kernel),
            out_image=out_image,
            acceptance_error=args.acceptance_error)
        logging.info(
            f"Similarity between validation image filtered with trained and original {name} kernel is {perc_of_similarity}%"
        )

    # Animated matrix
    if args.create_gifs:
        for i, name in zip(range(weights.shape[1]), kernel_names):
            out_image = out_folder + f"{name}/animated_training_matrix.gif"
            logging.info(f"Creating matrix gif for {name}...")
            create_matrix_gif(weights[:, i],
                              save_folder=out_folder + "gif_images/",
                              out_image=out_folder +
                              f"{name}/animated_training_matrix.gif")
    else:
        logging.info("Skipping gif creation.")
Ejemplo n.º 37
0
    def compare_model(self):
        import images

        model = self.get_model()
        dof = self.image.size - self.ngauss * 6
        images.compare_images(self.image, model, label1="image", label2="model", skysig=sqrt(1 / self.ivar), dof=dof)
Ejemplo n.º 38
0
    def compare_images(self, type, nrand=None):
        """
        compare images of the specified type

        parameters
        ----------
        type: string
            Type of images to compare, e.g. 'image','weight' etc.
        nrand: integer, optional
            Compare at most this many images.  A random subset will be
            drawn
        """

        if nrand is not None:
            nrand = min(nrand, self.ind1.size)
            ids = self._get_random_subset(nrand)
        else:
            ids = numpy.arange(self.ind1.size)

        means = []

        ntot = ids.size
        for icount, index in enumerate(ids):
            print("    %d/%d %d" % (icount + 1, ntot, index))

            i1 = self.ind1[index]
            i2 = self.ind2[index]
            ncut = self._check_ncutout(i1, i2)

            for icut in range(ncut):
                im2 = self.m2.get_cutout(i2, icut, type=type)
                im1 = self.m1.get_cutout(i1, icut, type=type)
                #if type=='image':
                #    wt=self.m2.get_cutout(i2, icut, type='weight')
                #    err=numpy.sqrt(1.0/wt.max())
                #    print("        compare to noise:",err)

                if type == 'image':
                    ok, mean, err, std = self._compare_images(im1, im2)
                    means.append(mean)
                elif type == 'weight':
                    self._compare_weights(im1, im2)
                else:
                    self._compare_images_exact(im1, im2)

            if type == 'image' and self.png_prefix is not None:
                import images
                fname = self.png_prefix + '-imdiff-%06d-%03d.png' % (index,
                                                                     icut)
                print("writing:", fname)
                if ok:
                    oks = 'OK'
                else:
                    oks = 'Not OK'

                images.compare_images(
                    im1,
                    im2,
                    dims=(1500, 1500),
                    #width=1500,
                    #height=1500,
                    title='rms: %.3g mean: %.3g +/- %.3g %s' %
                    (std, mean, err, oks),
                    file=fname,
                )
                #if 'q'==raw_input("hit a key: (q to quit)"):
                #    stop

        if type == 'image':
            import esutil as eu
            print(len(means))
            means = numpy.array(means)
            mdiff = means.mean()
            mdiff_err = means.std() / numpy.sqrt(means.size)
            print("average mean diff: %g +/- %g" % (mdiff, mdiff_err))
            mdiff, mdiff_std, mdiff_err = eu.stat.sigma_clip(means,
                                                             get_err=True)
            print("clipped average mean diff: %g +/- %g" % (mdiff, mdiff_err))

            if self.png_prefix is not None:
                import biggles
                binsize = mdiff_std * 0.1
                fname = self.png_prefix + '-imdiff-means.png'
                biggles.plot_hist(
                    means,
                    binsize=binsize,
                    visible=False,
                    file=fname,
                )
Ejemplo n.º 39
0
def lucy_richardson_blind_deconvolution_pir(g, n, m,d, max_psf_size=0, init_h_mode='gaussian', original=None):
    # g - blurred image
    # m, n - count of interation in RL-method
    # max_psf_size - How long must be PSF
    # init_h_mode - How initialize PSF:
    #   'gaussian' - gaussian PSF size = 3x3, sigma =
    #   'horizontal' - motion blur size 3x3, ang=0
    #   'vertical' - motion blir size 3x3 ang = 90
    #   'wow'      - init with g
    plt.imsave(fname='l_r_blind/g.bmp', arr=np.uint8(g), cmap='gray')
    if max_psf_size==0:
        max_psf_size = min(g.shape[0], g.shape[1])

    #
    # init h:
    #
    if init_h_mode=='gaussian':
        h = conv.gaussian(1,3,3)
    elif init_h_mode == 'horizontal':
        h = np.array([[0,0,0],
                   [1,1,1],
                   [0,0,0]], dtype=float)/3.
    elif init_h_mode == 'vertical':
        h  =np.array([[0,1,0],
                   [0,1,0],
                   [0,1,0]], dtype=float)/3.
    elif init_h_mode == 'wow':
        mini_g=smisc.imresize(g, (3,3))
        h = (1. / np.sum(mini_g) ** 2) * conv.correlation2(mini_g, mini_g)
        h /= np.sum(h)
    else:
        h = conv.random_psf(3,3)
    print h
    s=3
    err=[]
    while (s<=max_psf_size):
        print 'size =',s
        mini_g=smisc.imresize(g, (s,s))
        f = lucy_richardson_deconvolution(mini_g, h, 20000)
        if (s!=3):
            h=smisc.imresize(h, (s,s))
        for k in range (d):
            if(k!=0):
                f = lucy_richardson_deconvolution(mini_g, h, 20000)
            for i in range(n):
                #f_prev = np.copy(f)

                #Correct h
                h_prev=np.copy(h)
                for k in range(m):
                    p = mini_g / (conv.convolution2(f, h))
                    flr = np.fliplr(np.flipud(f))
                    h = conv.convolution2(p, flr)*h
                    #h /= np.sum(h)
                    #print 'h =',h

                for k in range(m):
                    p = mini_g / (conv.convolution2(h_prev, f))
                    hlr = np.fliplr(np.flipud(h_prev))
                    f = conv.convolution2(p, hlr) * f
                print (float(i + 1) / n) * 100, ' %'
                name = 'l_r_blind/new_lena' +str(s)+'_'+ str(i) + '.bmp'
                h_name = 'l_r_blind/h_'+str(s)+'_' + str(i) + '.bmp'
                plt.imsave(fname=name, arr=np.uint8(images.correct_image(f)), cmap='gray')
                plt.imsave(fname=h_name, arr=np.uint8(images.correct_image(h*255)), cmap='gray')
        s=int(s*math.sqrt(2))
        #
        if(original!=None):
            good_f = lucy_richardson_deconvolution(g, h, 20000)
            print good_f.shape
            dx = (good_f.shape[0]-original.shape[0])//2
            dy = (good_f.shape[1]-original.shape[1])//2
            err.append(images.compare_images(good_f[dx:original.shape[0]+dx,
                                                    dy:original.shape[1] + dy],
                                             original))
    err=np.array(err)
    plt.figure()
    plt.plot(np.arange(err.size), err)
    #plt.imshow(h,cmap='gray')
    plt.show()
    f = lucy_richardson_deconvolution(g, h, 5000)
    return f, h
Ejemplo n.º 40
0
def test():
    import images
    import fitsio
    import os
    #import mchuff

    dir='./mcal-tests'
    if not os.path.exists(dir):
        os.makedirs(dir)

    step=0.01
    shears=[Shape(step,0.0), Shape(0.0,step)]

    for i,shear in enumerate(shears):
        for type in ['gal','psf']:

            obs, obs_sheared_dilated = _get_sim_obs(shear.g1,shear.g2,
                                                    r50=2.0, r50_psf=1.5)

            m=Metacal(obs)



            if type=='gal':
                obs_mcal = m.get_obs_galshear(shear)
            else:
                obs_mcal = m.get_obs_psfshear(shear)

            '''
            s, us, tpsf = mchuff.metaCalibrate(galsim.Image(obs.image, scale=1.0),
                                               galsim.Image(obs.psf.image,scale=1.0),
                                               g1=shear.g1, g2=shear.g2,
                                               gal_shear=type=='gal')

            print("psf:",numpy.abs(tpsf.array - obs_mcal.psf.image).max()/obs_mcal.psf.image.max())
            print("im:",numpy.abs(s.array - obs_mcal.image).max()/obs_mcal.image.max())
            '''

            images.compare_images(obs_sheared_dilated.image,
                                  obs_mcal.image,
                                  label1='shear/dilate',
                                  label2='metacal',
                                  width=1000,
                                  height=1000)

            if i==0 and type=='gal':
                imfile='test-image.fits'
                imfile=os.path.join(dir, imfile)
                print("writing image:",imfile)
                fitsio.write(imfile, obs.image, clobber=True)

                psffile='test-psf.fits'
                psffile=os.path.join(dir, psffile)
                print("writing psf:",psffile)
                fitsio.write(psffile, obs.psf.image, clobber=True)

            mcalfile='test-image-mcal-%sshear-%.2f-%.2f.fits' % (type,shear.g1,shear.g2)
            mcalfile=os.path.join(dir,mcalfile)
            print("writing metacaled imag:",mcalfile)
            fitsio.write(mcalfile, obs_mcal.image, clobber=True)

            mcal_psf_file='test-psf-mcal-%sshear-%.2f-%.2f.fits' % (type,shear.g1,shear.g2)
            mcal_psf_file=os.path.join(dir,mcal_psf_file)
            print("writing metacaled psf image:",mcal_psf_file)
            fitsio.write(mcal_psf_file, obs_mcal.psf.image, clobber=True)


    readme=os.path.join(dir, 'README')
    with open(readme,'w') as fobj:
        fobj.write("""metacal test files

original images:
----------------
test-image.fits
test-psf.fits

metacaled images:
-----------------

# galaxy sheared
test-image-mcal-galshear-0.01-0.00.fits
test-psf-mcal-galshear-0.01-0.00.fits

test-image-mcal-galshear-0.00-0.01.fits
test-psf-mcal-galshear-0.00-0.01.fits

# psf sheared
test-image-mcal-psfshear-0.01-0.00.fits
test-psf-mcal-psfshear-0.01-0.00.fits

test-image-mcal-psfshear-0.00-0.01.fits
test-psf-mcal-psfshear-0.00-0.01.fits
"""     )
Ejemplo n.º 41
0
def do_meta_mof_full_withsim(sim, fit_conf, fitter, show=False):
    """
    not finding groups, just fitting everything.  This means
    it won't work on bigger images with lots of empty space
    """
    import galsim
    import mof

    assert fit_conf['fofs']['find_fofs'] == False

    tm0 = time.time()
    mofc = fit_conf['mof']

    # create metacal versions of image
    metacal_pars = fit_conf['metacal']['metacal_pars']
    if metacal_pars.get('symmetrize_psf', False):
        fitters._fit_all_psfs([sim.obs], fit_conf['mof']['psf'])

    # create the catalog based on original images
    # this will just run sx and create seg and
    # cat
    medser = sim.get_medsifier()
    if medser.cat.size == 0:
        return {}, 0, 0.0
    cat = medser.cat

    mof_fitter, data = fitter.go(
        sim.obs,
        cat,
        ntry=mofc['ntry'],
        get_fitter=True,
    )

    tobs = sim.obs[0][0]
    if show:
        gmix = mof_fitter.get_convolved_gmix()
        _plot_compare_model(gmix, tobs)

    sim_mbobs_1p = ngmix.MultiBandObsList()
    sim_mbobs_1m = ngmix.MultiBandObsList()

    for band, obslist in enumerate(sim.obs):
        sim_obslist_1p = ngmix.ObsList()
        sim_obslist_1m = ngmix.ObsList()
        band_gmix0 = mof_fitter.get_gmix(band=band, )

        theta = fitter.rng.uniform(low=0.0, high=np.pi)
        #gmix0 = gmix0.get_rotated(theta)
        for obsnum, obs in enumerate(obslist):

            jac = obs.jacobian
            scale = jac.scale

            gmix0 = band_gmix0.copy()

            gmix0.set_flux(gmix0.get_flux() / scale**2)

            # cheating on psf for now

            ny, nx = obs.image.shape

            # galsim does everything relative to the canonical center, but
            # for the mof fitter we had the origin a 0,0.  Shift over by
            # the cen
            ccen = (np.array(obs.image.shape) - 1.0) / 2.0

            gs0 = gmix0.make_galsim_object()

            gs0 = gs0.shift(dx=-ccen[1] * scale, dy=-ccen[1] * scale)

            if show and obsnum == 0:
                import images
                #gs = gmix0.make_galsim_object(psf=sim.psf)
                #gs = gs.shift(dx=-ccen[1]*scale, dy=-ccen[1]*scale)
                gs = galsim.Convolve(gs0, sim.psf)
                tim = gs.drawImage(nx=nx, ny=ny,
                                   scale=sim['pixel_scale']).array
                images.compare_images(sim.obs[0][0].image, tim)
                if 'q' == input('hit a key (q to quit): '):
                    stop

            gs0_1p = gs0.shear(g1=0.01, g2=0.0)
            gs0_1m = gs0.shear(g1=-0.01, g2=0.0)

            gs_1p = galsim.Convolve(gs0_1p, sim.psf)
            gs_1m = galsim.Convolve(gs0_1m, sim.psf)

            im_1p = gs_1p.drawImage(nx=nx, ny=ny,
                                    scale=sim['pixel_scale']).array
            im_1m = gs_1m.drawImage(nx=nx, ny=ny,
                                    scale=sim['pixel_scale']).array

            # adding same noise to both

            noise_image = ngmix.simobs.get_noise_image(obs.weight,
                                                       rng=fitter.rng)
            im_1p += noise_image
            im_1m += noise_image

            # this one will be used for fixnoise
            noise_image2 = ngmix.simobs.get_noise_image(obs.weight,
                                                        rng=fitter.rng)

            sobs_1p = ngmix.Observation(
                im_1p,
                weight=obs.weight.copy(),
                jacobian=jac,
                psf=obs.psf.copy(),
                noise=noise_image2.copy(),
            )
            sobs_1m = ngmix.Observation(
                im_1m,
                weight=obs.weight.copy(),
                jacobian=jac,
                psf=obs.psf.copy(),
                noise=noise_image2.copy(),
            )

            sim_obslist_1p.append(sobs_1p)
            sim_obslist_1m.append(sobs_1m)

        sim_mbobs_1p.append(sim_obslist_1p)
        sim_mbobs_1m.append(sim_obslist_1m)

    # for the measurement on real data
    odict = ngmix.metacal.get_all_metacal(sim.obs,
                                          rng=fitter.rng,
                                          **metacal_pars)

    sim_metacal_pars = {}
    sim_metacal_pars.update(metacal_pars)
    sim_metacal_pars['use_noise_image'] = True

    sim_odict_1p = ngmix.metacal.get_all_metacal(
        sim_mbobs_1p,
        rng=fitter.rng,  # not needed
        **sim_metacal_pars)
    sim_odict_1m = ngmix.metacal.get_all_metacal(
        sim_mbobs_1m,
        rng=fitter.rng,  # not needed
        **sim_metacal_pars)

    reslists = {}
    reslists.update(_process_one_full_mof_metacal(mofc, odict, cat, fitter))
    reslists.update(
        _process_one_full_mof_metacal(mofc,
                                      sim_odict_1p,
                                      cat,
                                      fitter,
                                      prefix='sim1p'))
    reslists.update(
        _process_one_full_mof_metacal(mofc,
                                      sim_odict_1m,
                                      cat,
                                      fitter,
                                      prefix='sim1m'))

    nobj = cat.size
    tm_fit = time.time() - tm0

    return reslists, nobj, tm_fit
Ejemplo n.º 42
0
def test():
    import images
    import fitsio
    import os
    #import mchuff

    dir = './mcal-tests'
    if not os.path.exists(dir):
        os.makedirs(dir)

    step = 0.01
    shears = [Shape(step, 0.0), Shape(0.0, step)]

    for i, shear in enumerate(shears):
        for type in ['gal', 'psf']:

            obs, obs_sheared_dilated = _get_sim_obs(shear.g1,
                                                    shear.g2,
                                                    r50=2.0,
                                                    r50_psf=1.5)

            m = Metacal(obs)

            if type == 'gal':
                obs_mcal = m.get_obs_galshear(shear)
            else:
                obs_mcal = m.get_obs_psfshear(shear)

            images.compare_images(obs_sheared_dilated.image,
                                  obs_mcal.image,
                                  label1='shear/dilate',
                                  label2='metacal',
                                  width=1000,
                                  height=1000)

            if i == 0 and type == 'gal':
                imfile = 'test-image.fits'
                imfile = os.path.join(dir, imfile)
                print("writing image:", imfile)
                fitsio.write(imfile, obs.image, clobber=True)

                psffile = 'test-psf.fits'
                psffile = os.path.join(dir, psffile)
                print("writing psf:", psffile)
                fitsio.write(psffile, obs.psf.image, clobber=True)

            mcalfile = 'test-image-mcal-%sshear-%.2f-%.2f.fits' % (
                type, shear.g1, shear.g2)
            mcalfile = os.path.join(dir, mcalfile)
            print("writing metacaled imag:", mcalfile)
            fitsio.write(mcalfile, obs_mcal.image, clobber=True)

            mcal_psf_file = 'test-psf-mcal-%sshear-%.2f-%.2f.fits' % (
                type, shear.g1, shear.g2)
            mcal_psf_file = os.path.join(dir, mcal_psf_file)
            print("writing metacaled psf image:", mcal_psf_file)
            fitsio.write(mcal_psf_file, obs_mcal.psf.image, clobber=True)

    readme = os.path.join(dir, 'README')
    with open(readme, 'w') as fobj:
        fobj.write("""metacal test files

original images:
----------------
test-image.fits
test-psf.fits

metacaled images:
-----------------

# galaxy sheared
test-image-mcal-galshear-0.01-0.00.fits
test-psf-mcal-galshear-0.01-0.00.fits

test-image-mcal-galshear-0.00-0.01.fits
test-psf-mcal-galshear-0.00-0.01.fits

# psf sheared
test-image-mcal-psfshear-0.01-0.00.fits
test-psf-mcal-psfshear-0.01-0.00.fits

test-image-mcal-psfshear-0.00-0.01.fits
test-psf-mcal-psfshear-0.00-0.01.fits
""")
Ejemplo n.º 43
0
def test_2gauss(counts=100.0, noise=0.0, maxiter=100,show=False):
    import time
    dims=[25,25]

    cen=(numpy.array(dims)-1.0)/2.0
    jacob=UnitJacobian(row=cen[0], col=cen[1])

    cen1=[ -3.25, -3.25]
    cen2=[ 3.0, 0.5]

    e1_1=0.1
    e2_1=0.05
    T_1=8.0
    counts_1=0.4*counts
    irr_1 = T_1/2.*(1-e1_1)
    irc_1 = T_1/2.*e2_1
    icc_1 = T_1/2.*(1+e1_1)

    e1_2=-0.2
    e2_2=-0.1
    T_2=4.0
    counts_2=0.6*counts
    irr_2 = T_2/2.*(1-e1_2)
    irc_2 = T_2/2.*e2_2
    icc_2 = T_2/2.*(1+e1_2)

    pars = [counts_1, cen1[0],cen1[1], irr_1, irc_1, icc_1,
            counts_2, cen2[0],cen2[1], irr_2, irc_2, icc_2]

    gm=gmix.GMix(pars=pars)
    print('gmix true:')
    print(gm)

    im0=gm.make_image(dims, jacobian=jacob)
    im = im0 + noise*numpy.random.randn(im0.size).reshape(dims)

    imsky,sky = prep_image(im)

    obs=Observation(imsky, jacobian=jacob)

    gm_guess=gm.copy()
    gm_guess._data['p']=[0.5,0.5]
    gm_guess._data['row'] += 4*srandu(2)
    gm_guess._data['col'] += 4*srandu(2)
    gm_guess._data['irr'] += 0.5*srandu(2)
    gm_guess._data['irc'] += 0.5*srandu(2)
    gm_guess._data['icc'] += 0.5*srandu(2)

    print('guess:')
    print(gm_guess)

    for i in xrange(2):
        tm0=time.time()
        em=GMixEM(obs)
        em.go(gm_guess, sky, maxiter=maxiter)
        tm=time.time()-tm0
    print('time:',tm,'seconds')

    gmfit=em.get_gmix()
    res=em.get_result()
    print('best fit:')
    print(gmfit)
    print('results')
    print(res)

    if show:
        import images
        imfit=gmfit.make_image(im.shape)
        imfit *= (im0.sum()/imfit.sum())

        images.compare_images(im, imfit)

    return tm
Ejemplo n.º 44
0
def test_1gauss_jacob(counts_sky=100.0,
                      noise_sky=0.0,
                      maxiter=100,
                      show=False):
    import time
    #import images
    dims=[25,25]
    cen=[dims[0]/2., dims[1]/2.]

    #j1,j2,j3,j4=0.26,0.02,-0.03,0.23
    dvdrow,dvdcol,dudrow,dudcol=-0.04,-0.915,1.10,0.12
    j=Jacobian(row=cen[0],
               col=cen[1],
               dvdrow=dvdrow,
               dvdcol=dvdcol,
               dudrow=dudrow,
               dudcol=dudcol)

    jfac=j.get_scale()

    g1=0.1
    g2=0.05

    Tsky=8.0*jfac**2
    noise_pix=noise_sky/jfac**2

    pars = [0.0, 0.0, g1, g2, Tsky, counts_sky]
    gm=gmix.GMixModel(pars, "gauss")
    print('gmix true:')
    print(gm)

    im0=gm.make_image(dims, jacobian=j)

    im = im0 + noise_pix*numpy.random.randn(im0.size).reshape(dims)

    imsky,sky = prep_image(im)

    obs=Observation(imsky, jacobian=j)

    gm_guess=gm.copy()
    gm_guess._data['p']=1.0
    gm_guess._data['row'] += srandu()
    gm_guess._data['col'] += srandu()
    gm_guess._data['irr'] += srandu()
    gm_guess._data['irc'] += srandu()
    gm_guess._data['icc'] += srandu()

    print('guess:')
    print(gm_guess)

    tm0=time.time()
    em=GMixEM(obs)
    em.go(gm_guess, sky, maxiter=maxiter)
    tm=time.time()-tm0
    print('time:',tm,'seconds')

    gmfit=em.get_gmix()
    res=em.get_result()
    print('best fit:')
    print(gmfit)
    print('results')
    print(res)

    if show:
        import images
        imfit=gmfit.make_image(im.shape, jacobian=j)
        imfit *= (im0.sum()/imfit.sum())

        images.compare_images(im, imfit)

    return gmfit
Ejemplo n.º 45
0
def test_1gauss(counts=1.0,
                noise=0.0,
                T=4.0,
                maxiter=4000,
                g1=0.0,
                g2=0.0,
                show=False,
                pad=False,
                verbose=True,
                seed=31415):
    import time

    rng=numpy.random.RandomState(seed)

    sigma=numpy.sqrt(T/2)
    dim=int(2*5*sigma)
    dims=[dim]*2
    cen=[dims[0]/2., dims[1]/2.]

    jacob=UnitJacobian(row=cen[0], col=cen[1])

    pars = [0.0, 0.0, g1, g2, T, counts]
    gm=gmix.GMixModel(pars, "gauss")

    im0=gm.make_image(dims, jacobian=jacob)

    im = im0 + rng.normal(size=im0.shape, scale=noise)

    imsky,sky = prep_image(im)

    obs=Observation(imsky, jacobian=jacob)

    guess_pars = [
        srandu(rng=rng),
        srandu(rng=rng),
        0.05*srandu(rng=rng),
        0.05*srandu(rng=rng),
        T*(1.0 + 0.1*srandu(rng=rng)),
        counts*(1.0 + 0.1*srandu(rng=rng)),
    ]
    gm_guess= gmix.GMixModel(guess_pars, "gauss")

    print("gm:",gm)
    print("gm_guess:",gm_guess)

    # twice, first time numba compiles the code
    for i in xrange(2):
        tm0=time.time()
        em=GMixEM(obs)
        em.go(gm_guess, sky, maxiter=maxiter)
        tm=time.time()-tm0

    gmfit=em.get_gmix()
    res=em.get_result()

    if verbose:
        print("dims:",dims)
        print("cen:",cen)
        print('guess:')
        print(gm_guess)

        print('time:',tm,'seconds')
        print()

        print()
        print('results')
        print(res)

        print()
        print('gmix true:')
        print(gm)
        print('best fit:')
        print(gmfit)

    if show:
        import images
        imfit=gmfit.make_image(im.shape)
        imfit *= (im0.sum()/imfit.sum())

        images.compare_images(im, imfit)

    return gmfit
Ejemplo n.º 46
0
Archivo: em.py Proyecto: esheldon/ngmix
def test_2gauss(counts=100.0, noise=0.0, maxiter=100,show=False):
    import time
    dims=[25,25]

    cen=(numpy.array(dims)-1.0)/2.0
    jacob=UnitJacobian(row=cen[0], col=cen[1])

    cen1=[ -3.25, -3.25]
    cen2=[ 3.0, 0.5]

    e1_1=0.1
    e2_1=0.05
    T_1=8.0
    counts_1=0.4*counts
    irr_1 = T_1/2.*(1-e1_1)
    irc_1 = T_1/2.*e2_1
    icc_1 = T_1/2.*(1+e1_1)

    e1_2=-0.2
    e2_2=-0.1
    T_2=4.0
    counts_2=0.6*counts
    irr_2 = T_2/2.*(1-e1_2)
    irc_2 = T_2/2.*e2_2
    icc_2 = T_2/2.*(1+e1_2)

    pars = [counts_1, cen1[0],cen1[1], irr_1, irc_1, icc_1,
            counts_2, cen2[0],cen2[1], irr_2, irc_2, icc_2]

    gm=gmix.GMix(pars=pars)
    print('gmix true:')
    print(gm)

    im0=gm.make_image(dims, jacobian=jacob)
    im = im0 + noise*numpy.random.randn(im0.size).reshape(dims)

    imsky,sky = prep_image(im)

    obs=Observation(imsky, jacobian=jacob)

    gm_guess=gm.copy()
    gm_guess._data['p']=[0.5,0.5]
    gm_guess._data['row'] += 4*srandu(2)
    gm_guess._data['col'] += 4*srandu(2)
    gm_guess._data['irr'] += 0.5*srandu(2)
    gm_guess._data['irc'] += 0.5*srandu(2)
    gm_guess._data['icc'] += 0.5*srandu(2)

    print('guess:')
    print(gm_guess)

    for i in xrange(2):
        tm0=time.time()
        em=GMixEM(obs)
        em.go(gm_guess, sky, maxiter=maxiter)
        tm=time.time()-tm0
    print('time:',tm,'seconds')

    gmfit=em.get_gmix()
    res=em.get_result()
    print('best fit:')
    print(gmfit)
    print('results')
    print(res)

    if show:
        import images
        imfit=gmfit.make_image(im.shape)
        imfit *= (im0.sum()/imfit.sum())

        images.compare_images(im, imfit)

    return tm
Ejemplo n.º 47
0
Archivo: em.py Proyecto: esheldon/ngmix
def test_1gauss_jacob(counts_sky=100.0,
                      noise_sky=0.0,
                      maxiter=100,
                      show=False):
    import time
    #import images
    dims=[25,25]
    cen=[dims[0]/2., dims[1]/2.]

    #j1,j2,j3,j4=0.26,0.02,-0.03,0.23
    dvdrow,dvdcol,dudrow,dudcol=-0.04,-0.915,1.10,0.12
    j=Jacobian(row=cen[0],
               col=cen[1],
               dvdrow=dvdrow,
               dvdcol=dvdcol,
               dudrow=dudrow,
               dudcol=dudcol)

    jfac=j.get_scale()

    g1=0.1
    g2=0.05

    Tsky=8.0*jfac**2
    noise_pix=noise_sky/jfac**2

    pars = [0.0, 0.0, g1, g2, Tsky, counts_sky]
    gm=gmix.GMixModel(pars, "gauss")
    print('gmix true:')
    print(gm)

    im0=gm.make_image(dims, jacobian=j)

    im = im0 + noise_pix*numpy.random.randn(im0.size).reshape(dims)

    imsky,sky = prep_image(im)

    obs=Observation(imsky, jacobian=j)

    gm_guess=gm.copy()
    gm_guess._data['p']=1.0
    gm_guess._data['row'] += srandu()
    gm_guess._data['col'] += srandu()
    gm_guess._data['irr'] += srandu()
    gm_guess._data['irc'] += srandu()
    gm_guess._data['icc'] += srandu()

    print('guess:')
    print(gm_guess)

    tm0=time.time()
    em=GMixEM(obs)
    em.go(gm_guess, sky, maxiter=maxiter)
    tm=time.time()-tm0
    print('time:',tm,'seconds')

    gmfit=em.get_gmix()
    res=em.get_result()
    print('best fit:')
    print(gmfit)
    print('results')
    print(res)

    if show:
        import images
        imfit=gmfit.make_image(im.shape, jacobian=j)
        imfit *= (im0.sum()/imfit.sum())

        images.compare_images(im, imfit)

    return gmfit
Ejemplo n.º 48
0
def main():

    args = get_args()
    rng = np.random.RandomState(args.seed)

    obs, obj_pars = make_data(rng=rng, noise=args.noise)

    # fit the object to an exponential disk
    prior = get_prior(rng=rng, scale=obs.jacobian.scale)
    # fit using the levenberg marquards algorithm
    fitter = ngmix.fitting.Fitter(model='exp', prior=prior)
    # make parameter guesses based on a psf flux and a rough T
    guesser = ngmix.guessers.TPSFFluxAndPriorGuesser(
        rng=rng,
        T=0.25,
        prior=prior,
    )

    # psf fitting with coelliptical gaussians
    psf_ngauss = 5
    psf_fitter = ngmix.fitting.CoellipFitter(ngauss=psf_ngauss)
    # special guesser for coelliptical gaussians
    psf_guesser = ngmix.guessers.CoellipPSFGuesser(rng=rng, ngauss=psf_ngauss)

    # this runs the fitter. We set ntry=2 to retry the fit if it fails
    psf_runner = ngmix.runners.PSFRunner(
        fitter=psf_fitter, guesser=psf_guesser,
        ntry=2,
    )
    runner = ngmix.runners.Runner(
        fitter=fitter, guesser=guesser,
        ntry=2,
    )

    # this bootstraps the process, first fitting psfs then the object
    boot = ngmix.bootstrap.Bootstrapper(
        runner=runner,
        psf_runner=psf_runner,
    )

    res = boot.go(obs)

    print()
    print('S/N:', res['s2n'])
    print('true flux: %g meas flux: %g +/- %g (99.7%% conf)' % (
        obj_pars['flux'], res['flux'], res['flux_err']*3,
    ))
    print('true g1: %g meas g1: %g +/- %g (99.7%% conf)' % (
        obj_pars['g1'], res['g'][0], res['g_err'][0]*3,
    ))
    print('true g2: %g meas g2: %g +/- %g (99.7%% conf)' % (
        obj_pars['g2'], res['g'][1], res['g_err'][1]*3,
    ))

    if args.show:
        try:
            import images
        except ImportError:
            from espy import images

        imfit = res.make_image()

        images.compare_images(obs.image, imfit)
Ejemplo n.º 49
0
Archivo: em.py Proyecto: esheldon/ngmix
def test_1gauss(counts=1.0,
                noise=0.0,
                T=4.0,
                maxiter=4000,
                g1=0.0,
                g2=0.0,
                show=False,
                pad=False,
                verbose=True,
                seed=31415):
    import time

    rng=numpy.random.RandomState(seed)

    sigma=numpy.sqrt(T/2)
    dim=int(2*5*sigma)
    dims=[dim]*2
    cen=[dims[0]/2., dims[1]/2.]

    jacob=UnitJacobian(row=cen[0], col=cen[1])

    pars = [0.0, 0.0, g1, g2, T, counts]
    gm=gmix.GMixModel(pars, "gauss")

    im0=gm.make_image(dims, jacobian=jacob)

    im = im0 + rng.normal(size=im0.shape, scale=noise)

    imsky,sky = prep_image(im)

    obs=Observation(imsky, jacobian=jacob)

    guess_pars = [
        srandu(rng=rng),
        srandu(rng=rng),
        0.05*srandu(rng=rng),
        0.05*srandu(rng=rng),
        T*(1.0 + 0.1*srandu(rng=rng)),
        counts*(1.0 + 0.1*srandu(rng=rng)),
    ]
    gm_guess= gmix.GMixModel(guess_pars, "gauss")

    print("gm:",gm)
    print("gm_guess:",gm_guess)

    # twice, first time numba compiles the code
    for i in xrange(2):
        tm0=time.time()
        em=GMixEM(obs)
        em.go(gm_guess, sky, maxiter=maxiter)
        tm=time.time()-tm0

    gmfit=em.get_gmix()
    res=em.get_result()

    if verbose:
        print("dims:",dims)
        print("cen:",cen)
        print('guess:')
        print(gm_guess)

        print('time:',tm,'seconds')
        print()

        print()
        print('results')
        print(res)

        print()
        print('gmix true:')
        print(gm)
        print('best fit:')
        print(gmfit)

    if show:
        import images
        imfit=gmfit.make_image(im.shape)
        imfit *= (im0.sum()/imfit.sum())

        images.compare_images(im, imfit)

    return gmfit
Ejemplo n.º 50
0
def test_1gauss(counts=1.0,
                noise=0.0,
                T=4.0,
                maxiter=4000,
                g1=0.0,
                g2=0.0,
                show=False,
                pad=False,
                verbose=True):
    import time

    sigma = numpy.sqrt(T / 2)
    dim = int(2 * 5 * sigma)
    dims = [dim] * 2
    cen = [dims[0] / 2., dims[1] / 2.]

    pars = [cen[0], cen[1], g1, g2, T, counts]
    gm = gmix.GMixModel(pars, "gauss")

    im0 = gm.make_image(dims)

    im = im0 + noise * numpy.random.randn(im0.size).reshape(dims)

    imsky, sky = prep_image(im)

    obs = Observation(imsky)

    gm_guess = gm.copy()
    gm_guess._data['p'] = 1.0
    gm_guess._data['row'] += 1 * srandu()
    gm_guess._data['col'] += 1 * srandu()
    gm_guess._data['irr'] += 0.5 * srandu()
    gm_guess._data['irc'] += 0.5 * srandu()
    gm_guess._data['icc'] += 0.5 * srandu()

    tm0 = time.time()
    em = GMixEM(obs)
    em.run_em(gm_guess, sky, maxiter=maxiter)
    tm = time.time() - tm0

    gmfit = em.get_gmix()
    res = em.get_result()

    if verbose:
        print("dims:", dims)
        print("cen:", cen)
        print('guess:')
        print(gm_guess)

        print('time:', tm, 'seconds')
        print()

        print()
        print('results')
        print(res)

        print()
        print('gmix true:')
        print(gm)
        print('best fit:')
        print(gmfit)

    if show:
        import images
        imfit = gmfit.make_image(im.shape)
        imfit *= (im0.sum() / imfit.sum())

        images.compare_images(im, imfit)

    return gmfit