Exemplo n.º 1
0
    def get_round_obs(self, obs, gm0round):
        """
        get round version of obs, with simulated galaxy image

        set jacobian center to current best center, to simplify
        fitting later
        """

        # a copy
        jacob=obs.get_jacobian()

        psf_round=obs.psf.gmix.make_round()

        gm_round = gm0round.convolve( psf_round )

        im_nonoise=gm_round.make_image(obs.image.shape,
                                       jacobian=jacob)
        
        #noise=1.0/sqrt( median(obs.weight) )
        noise=1.0/sqrt( obs.weight.max() )
        nim = numpy.random.normal(scale=noise, size=im_nonoise.shape)
        im = im_nonoise + nim

        psf_obs=Observation(zeros( (1,1) ), gmix=psf_round)

        # note this is different from roundify in ngmix where
        # the gmix is the unconvolved one
        new_obs=Observation(im,
                            gmix=gm_round,
                            weight=obs.weight.copy(),
                            jacobian=jacob,
                            psf=psf_obs)

        new_obs.im_nonoise=im
        return new_obs
Exemplo n.º 2
0
    def _get_band_observations(self, band, mindex):
        """
        Get an ObsList for the coadd observations in each band
        If psf fitting fails, the ObsList will be zero length
        note we have already checked that we have a coadd and a single epoch
        without flags
        """

        meds=self.meds_list[band]
        ncutout=meds['ncutout'][mindex]

        image_flags=self._get_image_flags(band, mindex)

        coadd_obs_list = ObsList()
        obs_list       = ObsList()

        fake=numpy.zeros((0,0))
        for icut in xrange(ncutout):

            flags=0
            if not self._should_use_obs(band, mindex, icut):
                obs = Observation(fake)
                flags = IMAGE_FLAGS
            else:
                iflags = image_flags[icut]
                if iflags != 0:
                    flags = IMAGE_FLAGS
                    obs = Observation(fake)
                else:
                    obs = self._get_band_observation(band, mindex, icut)
                    if obs is None:
                        flags = IMAGE_FLAGS
                        obs = Observation(fake)

            # fill the meta data
            self._fill_obs_meta_data(obs,band,mindex,icut)

            # set flags
            meta = {'flags':flags}
            obs.update_meta_data(meta)

            if icut==0:
                coadd_obs_list.append(obs)
            else:
                obs_list.append(obs)

        if self.conf['reject_outliers'] and len(obs_list) > 0:
            self._reject_outliers(obs_list)

        obs_list.update_meta_data({'band_num':band})
        coadd_obs_list.update_meta_data({'band_num':band})

        return coadd_obs_list, obs_list
Exemplo n.º 3
0
    def _get_band_observations(self, band, mindex):
        """
        Get an ObsList for the coadd observations in each band
        If psf fitting fails, the ObsList will be zero length
        note we have already checked that we have a coadd and a single epoch
        without flags
        """

        meds = self.meds_list[band]
        ncutout = meds["ncutout"][mindex]

        image_flags = self._get_image_flags(band, mindex)

        coadd_obs_list = ObsList()
        obs_list = ObsList()

        for icut in xrange(ncutout):
            iflags = image_flags[icut]
            if iflags != 0:
                flags = IMAGE_FLAGS
                obs = Observation(numpy.zeros((0, 0)))
            else:
                obs = self._get_band_observation(band, mindex, icut)
                flags = 0

            # fill the meta data
            self._fill_obs_meta_data(obs, band, mindex, icut)

            # set flags
            meta = {"flags": flags}
            obs.update_meta_data(meta)

            if icut == 0:
                coadd_obs_list.append(obs)
            else:
                obs_list.append(obs)

        """
        if ncutout == 0:
            for o in [coadd_obs_list,obs_list]:
                flags = IMAGE_FLAGS
                obs = Observation(numpy.zeros((0,0)))
                meta = {'flags':flags}
                obs.update_meta_data(meta)            
                o.append(obs)
        """

        if self.conf["reject_outliers"] and len(obs_list) > 0:
            self._reject_outliers(obs_list)

        return coadd_obs_list, obs_list
Exemplo n.º 4
0
    def _trim_images(self, mbo_input, censky):
        """
        cen in sky coords, relative to jacobian
        center
        """

        mbo = MultiBandObsList()
        for obslist in mbo_input:

            new_obslist=ObsList()
            for obs in obslist:

                j=obs.jacobian
                scale=j.get_scale()
                cenpix=array( j.get_cen() )

                new_cen = cenpix + censky/scale
                #print("cen0:",cenpix,"newcen:",new_cen)

                new_im=_trim_image(obs.image, new_cen)
                new_wt=_trim_image(obs.weight, new_cen)
                new_j = j.copy()
                new_j.set_cen(row=new_cen[0], col=new_cen[1])

                newobs = Observation(
                    new_im,
                    weight=new_wt,
                    jacobian=new_j,
                    psf=obs.psf,
                )

                new_obslist.append( newobs )
            mbo.append( new_obslist )

        return mbo
Exemplo n.º 5
0
def test_gaussmom_flags():
    """
    test we get flags for very noisy data
    """
    rng = np.random.RandomState(seed=100)

    ntrial = 10
    noise = 100000
    scale = 0.263
    dims = [32] * 2
    weight = np.zeros(dims) + 1.0 / noise**2

    cen = (np.array(dims) - 1) / 2
    jacobian = ngmix.DiagonalJacobian(row=cen[0], col=cen[1], scale=scale)

    flags = np.zeros(ntrial)
    for i in range(ntrial):

        im = rng.normal(scale=noise, size=dims)

        obs = Observation(
            image=im,
            weight=weight,
            jacobian=jacobian,
        )

        fitter = GaussMom(fwhm=1.2)

        res = fitter.go(obs)
        flags[i] = res['flags']

    assert np.any(flags != 0)
Exemplo n.º 6
0
def test_gmix_loglike_fdiff(start):
    """
    test that the approx fdiff calculation agrees well enough with that
    calculated with the exp() value

    restrict comparisons to the valid range, which is +/- 5 sigma
    """

    rtol = 4.0e-5

    row, col, g1, g2, T, flux = -0.5, -0.4, 0, 0, 2.8, 5
    sigma = np.sqrt(T / 2)
    pars = np.array([row, col, g1, g2, T, flux])
    gm = make_gmix_model(pars, 'gauss')

    jacob = DiagonalJacobian(row=6.5, col=7.1, scale=0.25)
    area = jacob.area

    rng = np.random.RandomState(seed=10)
    dims = (13, 15)
    obs = Observation(
        image=rng.normal(size=dims),
        weight=np.exp(rng.normal(size=dims)),
        jacobian=jacob,
    )
    fdiff = np.zeros(dims[0] * dims[1] + start, dtype=np.float64)
    gm.fill_fdiff(obs, fdiff, start=start)

    cen = (np.array(dims) - 1) / 2

    row_range = int(cen[0] - 2 * sigma), int(cen[0] + 2 * sigma) + 1
    col_range = int(cen[1] - 2 * sigma), int(cen[1] + 2 * sigma) + 1

    loc = start

    pnorm = 5 / 2.0 / np.pi / sigma**2
    for r in range(row_range[0], row_range[1]):
        for c in range(col_range[0], col_range[1]):
            loc = start + r * dims[1] + c

            v, u = obs.jacobian(r, c)
            chi2 = ((u - col)**2 + (v - row)**2) / sigma**2
            model = pnorm * np.exp(-0.5 * chi2) * area
            _fdiff = (model - obs.image[r, c]) * np.sqrt(obs.weight[r, c])
            print(_fdiff, fdiff[loc])
            assert np.allclose(_fdiff, fdiff[loc], rtol=rtol)
Exemplo n.º 7
0
    def _get_band_observations(self, band, mindex):
        """
        Get an ObsList for the coadd observations in each band
        If psf fitting fails, the ObsList will be zero length
        note we have already checked that we have a coadd and a single epoch
        without flags
        """

        meds = self.meds_list[band]
        ncutout = meds['ncutout'][mindex]

        image_flags = self._get_image_flags(band, mindex)

        coadd_obs_list = ObsList()
        obs_list = ObsList()

        fake = numpy.zeros((0, 0))
        for icut in xrange(ncutout):

            flags = 0
            if not self._should_use_obs(band, mindex, icut):
                obs = Observation(fake)
                flags = IMAGE_FLAGS
            else:
                iflags = image_flags[icut]
                if iflags != 0:
                    flags = IMAGE_FLAGS
                    obs = Observation(fake)
                else:
                    obs = self._get_band_observation(band, mindex, icut)
                    if obs is None:
                        flags = IMAGE_FLAGS
                        obs = Observation(fake)

            # fill the meta data
            self._fill_obs_meta_data(obs, band, mindex, icut)

            # set flags
            meta = {'flags': flags}
            obs.update_meta_data(meta)

            if icut == 0:
                coadd_obs_list.append(obs)
            else:
                obs_list.append(obs)

        if self.conf['reject_outliers'] and len(obs_list) > 0:
            self._reject_outliers(obs_list)

        obs_list.update_meta_data({'band_num': band})
        coadd_obs_list.update_meta_data({'band_num': band})

        return coadd_obs_list, obs_list
Exemplo n.º 8
0
    def _get_band_observation(self, band, mindex, icut):
        """
        Get an Observation for a single band.
        """
        meds = self.meds_list[band]

        fname = self._get_meds_orig_filename(meds, mindex, icut)
        im = self._get_meds_image(meds, mindex, icut)
        wt, wt_us, seg = self._get_meds_weight(meds, mindex, icut)
        jacob = self._get_jacobian(meds, mindex, icut)

        # for the psf fitting code
        wt = wt.clip(min=0.0)

        psf_obs = self._get_psf_observation(band, mindex, icut, jacob)

        obs = Observation(im, weight=wt.copy(), jacobian=jacob, psf=psf_obs)
        if wt_us is not None:
            obs.weight_us = wt_us.copy()
        else:
            obs.weight_us = None
        obs.weight_raw = wt.copy()
        obs.seg = seg
        obs.filename = fname

        return obs
Exemplo n.º 9
0
def mbobs():
    mbobs = MultiBandObsList()
    for i in range(3):
        ol = ObsList()
        for j in range(4):
            o = Observation(image=np.ones((32, 32)) * (j + 1),
                            weight=np.ones((32, 32)) * (j + 1),
                            jacobian=ngmix.DiagonalJacobian(scale=0.25,
                                                            row=0,
                                                            col=0))
            ol.append(o)
        mbobs.append(ol)
    return mbobs
Exemplo n.º 10
0
def get_psf_obs(*, rng, T=TPSF, model="turb", noise=1.0e-6):
    dims = [25, 25]
    cen = (np.array(dims) - 1.0) / 2.0

    jacob = DiagonalJacobian(scale=PIXEL_SCALE, row=cen[0], col=cen[1])

    gm = GMixModel([0.0, 0.0, 0.0, 0.0, T, 1.0], model)
    im = gm.make_image(dims, jacobian=jacob)

    im += rng.normal(scale=noise, size=im.shape)
    weight = im*0 + 1.0/noise**2
    return {
        'obs': Observation(im, weight=weight, jacobian=jacob),
        'gmix': gm,
    }
Exemplo n.º 11
0
    def _get_psf_observation(self, band, mindex, icut, image_jacobian):
        """
        Get an Observation representing the PSF and the "sigma"
        from the psf model
        """
        im, cen, sigma_pix, fname = self._get_psf_image(band, mindex, icut)

        psf_jacobian = image_jacobian.copy()
        psf_jacobian.set_cen(row=cen[0], col=cen[1])

        psf_obs = Observation(im, jacobian=psf_jacobian)
        psf_obs.filename = fname

        # convert to sky coords
        sigma_sky = sigma_pix * psf_jacobian.get_scale()

        psf_obs.update_meta_data({'sigma_sky': sigma_sky})
        psf_obs.update_meta_data({'Tguess': sigma_sky * sigma_sky})
        psf_obs.update_meta_data({'psf_norm': im.sum()})

        return psf_obs
Exemplo n.º 12
0
    def _get_band_observation(self, band, mindex, icut):
        """
        Get an Observation for a single band.
        """

        meds = self.meds_list[band]

        bmask, skip = self._get_meds_bmask(band, meds, mindex, icut)
        if skip:
            return None

        wt, wt_us, wt_raw, seg, skip = self._get_meds_weight(
            band, meds, mindex, icut, bmask)
        if skip:
            return None

        im = self._get_meds_image(meds, mindex, icut)

        jacob = self._get_jacobian(band, mindex, icut)
        if jacob is None:
            return None

        if self.conf['ignore_zero_images'] and 0.0 == im.sum():
            print("    image all zero, skipping")
            return None

        psf_obs = self._get_psf_observation(band, mindex, icut, jacob)

        obs = Observation(im,
                          weight=wt,
                          bmask=bmask,
                          jacobian=jacob,
                          psf=psf_obs)
        if wt_us is not None:
            obs.weight_us = wt_us
        else:
            obs.weight_us = None

        obs.weight_raw = wt_raw
        obs.seg = seg

        nimage = self._get_meds_noise(meds, mindex, icut)
        if nimage is not None:
            obs.noise = nimage

        if 'trim_image' in self.conf:
            self._trim_obs(obs)

        return obs
Exemplo n.º 13
0
    def _get_psf_observation(self, band, mindex, icut, image_jacobian):
        """
        Get an Observation representing the PSF and the "sigma"
        from the psf model
        """
        im, cen, sigma_pix, fname = self._get_psf_image(band, mindex, icut)

        psf_jacobian = image_jacobian.copy()
        psf_jacobian.set_cen(cen[0], cen[1])

        psf_obs = Observation(im, jacobian=psf_jacobian)
        psf_obs.filename = fname

        # convert to sky coords
        sigma_sky = sigma_pix * psf_jacobian.get_scale()

        psf_obs.update_meta_data({"sigma_sky": sigma_sky})
        psf_obs.update_meta_data({"Tguess": sigma_sky * sigma_sky})
        psf_obs.update_meta_data({"psf_norm": im.sum()})

        return psf_obs
Exemplo n.º 14
0
    def _get_band_observation(self, band, mindex, icut):
        """
        Get an Observation for a single band.
        """

        meds=self.meds_list[band]

        bmask,skip = self._get_meds_bmask(meds, mindex, icut)
        if skip:
            return None


        wt,wt_us,wt_raw,seg,skip = self._get_meds_weight(meds, mindex, icut)
        if skip:
            return None

        im = self._get_meds_image(meds, mindex, icut)

        jacob = self._get_jacobian(meds, mindex, icut)


        if self.conf['ignore_zero_images'] and 0.0==im.sum():
            print("    image all zero, skipping")
            return None

        psf_obs = self._get_psf_observation(band, mindex, icut, jacob)

        obs=Observation(im,
                        weight=wt,
                        bmask=bmask,
                        jacobian=jacob,
                        psf=psf_obs)
        if wt_us is not None:
            obs.weight_us = wt_us
        else:
            obs.weight_us = None

        obs.weight_raw = wt_raw
        obs.seg = seg

        fname = self._get_meds_orig_filename(meds, mindex, icut)
        obs.filename=fname

        if 'trim_image' in self.conf:
            self._trim_obs(obs)

        return obs
Exemplo n.º 15
0
    def _make_mbobs(im, cen):
        dx = cen[0] - int(cen[0] + 0.5)
        dy = cen[1] - int(cen[1] + 0.5)
        xlow = int(int(cen[0] + 0.5) - nstamp_cen)
        ylow = int(int(cen[1] + 0.5) - nstamp_cen)
        _im = im[ylow:ylow + nstamp, xlow:xlow + nstamp]
        jac = DiagonalJacobian(
            scale=wcs.scale,
            x=nstamp_cen + dx,
            y=nstamp_cen + dy,
        )

        obs = Observation(image=_im,
                          jacobian=jac,
                          psf=psf_obs,
                          weight=np.ones_like(_im),
                          noise=rng.normal(size=_im.shape) * noise)
        mbobs = MultiBandObsList()
        obsl = ObsList()
        obsl.append(obs)
        mbobs.append(obsl)
        return mbobs
Exemplo n.º 16
0
def make_ngmix_mbobslist(im, psf, wcs, cen1, cen2, noise, rng, nstamp=33):
    nstamp_cen = (nstamp - 1) / 2

    psf_im = psf.drawImage(nx=nstamp, ny=nstamp, wcs=wcs)
    jac = DiagonalJacobian(scale=wcs.scale, x=nstamp_cen, y=nstamp_cen)
    psf_obs = Observation(
        image=psf_im.array,
        jacobian=jac,
        weight=np.ones_like(psf_im.array),
    )

    def _make_mbobs(im, cen):
        dx = cen[0] - int(cen[0] + 0.5)
        dy = cen[1] - int(cen[1] + 0.5)
        xlow = int(int(cen[0] + 0.5) - nstamp_cen)
        ylow = int(int(cen[1] + 0.5) - nstamp_cen)
        _im = im[ylow:ylow + nstamp, xlow:xlow + nstamp]
        jac = DiagonalJacobian(
            scale=wcs.scale,
            x=nstamp_cen + dx,
            y=nstamp_cen + dy,
        )

        obs = Observation(image=_im,
                          jacobian=jac,
                          psf=psf_obs,
                          weight=np.ones_like(_im),
                          noise=rng.normal(size=_im.shape) * noise)
        mbobs = MultiBandObsList()
        obsl = ObsList()
        obsl.append(obs)
        mbobs.append(obsl)
        return mbobs

    return [
        _make_mbobs(im, cen1),
        _make_mbobs(im, cen2),
    ]
Exemplo n.º 17
0
def test_ml_fitting_exp_obj_gauss_psf_smoke(g1_true, g2_true, wcs_g1, wcs_g2):
    rng = np.random.RandomState(seed=10)

    image_size = 33
    cen = (image_size - 1) / 2
    gs_wcs = galsim.ShearWCS(0.25, galsim.Shear(g1=wcs_g1,
                                                g2=wcs_g2)).jacobian()
    scale = np.sqrt(gs_wcs.pixelArea())

    g_prior = ngmix.priors.GPriorBA(0.1)
    cen_prior = ngmix.priors.CenPrior(0, 0, scale, scale)
    T_prior = ngmix.priors.FlatPrior(0.01, 2)
    F_prior = ngmix.priors.FlatPrior(1e-4, 1e9)
    prior = ngmix.joint_prior.PriorSimpleSep(cen_prior, g_prior, T_prior,
                                             F_prior)

    gal = galsim.Exponential(half_light_radius=0.5).shear(
        g1=g1_true, g2=g2_true).withFlux(400)
    obj = galsim.Convolve([gal, galsim.Gaussian(fwhm=0.5)])

    psf_im = galsim.Gaussian(fwhm=0.5).drawImage(nx=33,
                                                 ny=33,
                                                 wcs=gs_wcs,
                                                 method='no_pixel').array
    psf_gmix = ngmix.gmix.make_gmix_model(
        [0, 0, 0, 0, fwhm_to_T(0.5), 1], "gauss")
    psf_obs = Observation(image=psf_im, gmix=psf_gmix)

    im = obj.drawImage(nx=image_size,
                       ny=image_size,
                       wcs=gs_wcs,
                       method='no_pixel').array
    noise = np.sqrt(np.sum(im**2)) / 1e16

    wgt = np.ones_like(im) / noise**2

    guess = np.ones(6) * 0.1
    guess[0] = 0
    guess[1] = 0
    guess[2] = g1_true
    guess[3] = g2_true
    guess[4] = fwhm_to_T(0.5)
    guess[5] = 400

    g1arr = []
    g2arr = []
    farr = []
    xarr = []
    yarr = []
    for _ in range(50):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(dx=shift[0],
                       dy=shift[1]).drawImage(nx=image_size,
                                              ny=image_size,
                                              wcs=gs_wcs,
                                              method='no_pixel',
                                              dtype=np.float64).array

        jac = Jacobian(y=cen + xy.y,
                       x=cen + xy.x,
                       dudx=gs_wcs.dudx,
                       dudy=gs_wcs.dudy,
                       dvdx=gs_wcs.dvdx,
                       dvdy=gs_wcs.dvdy)

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = Observation(image=_im, weight=wgt, jacobian=jac, psf=psf_obs)
        fitter = LMSimple(obs, 'exp', prior=prior)
        fitter.go(guess + rng.normal(size=6) * 0.01)
        res = fitter.get_result()
        if res['flags'] == 0:
            _g1, _g2, _ = res['g'][0], res['g'][1], res['pars'][4]
            g1arr.append(_g1)
            g2arr.append(_g2)
            farr.append(res['pars'][5])
            xarr.append(res['pars'][1])
            yarr.append(res['pars'][0])

    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)
    gtol = 1e-5
    assert np.abs(g1 - g1_true) < gtol
    assert np.abs(g2 - g2_true) < gtol

    xerr = np.std(xarr) / np.sqrt(len(xarr))
    assert np.abs(np.mean(xarr)) < xerr * 5
    yerr = np.std(yarr) / np.sqrt(len(yarr))
    assert np.abs(np.mean(yarr)) < yerr * 5
Exemplo n.º 18
0
def test_admom_smoke(g1_true, g2_true, wcs_g1, wcs_g2, weight_fac):
    rng = np.random.RandomState(seed=100)

    fwhm = 0.9
    image_size = 107
    cen = (image_size - 1) / 2
    gs_wcs = galsim.ShearWCS(0.125, galsim.Shear(g1=wcs_g1,
                                                 g2=wcs_g2)).jacobian()

    obj = galsim.Gaussian(fwhm=fwhm).shear(g1=g1_true,
                                           g2=g2_true).withFlux(400)
    im = obj.drawImage(nx=image_size,
                       ny=image_size,
                       wcs=gs_wcs,
                       method='no_pixel').array
    noise = np.sqrt(np.sum(im**2)) / 1e18
    wgt = np.ones_like(im) / noise**2
    scale = np.sqrt(gs_wcs.pixelArea())

    g1arr = []
    g2arr = []
    Tarr = []
    for _ in range(50):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(dx=shift[0],
                       dy=shift[1]).drawImage(nx=image_size,
                                              ny=image_size,
                                              wcs=gs_wcs,
                                              method='no_pixel',
                                              dtype=np.float64).array

        jac = Jacobian(y=cen + xy.y,
                       x=cen + xy.x,
                       dudx=gs_wcs.dudx,
                       dudy=gs_wcs.dudy,
                       dvdx=gs_wcs.dvdx,
                       dvdy=gs_wcs.dvdy)

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = Observation(image=_im, weight=wgt, jacobian=jac)
        # use a huge weight so that we get the raw moments back out
        fitter = GaussMom(obs, fwhm * weight_fac, rng=rng)
        try:
            fitter.go()
            res = fitter.get_result()
            if res['flags'] == 0:
                if weight_fac > 1:
                    _g1, _g2 = e1e2_to_g1g2(res['e'][0], res['e'][1])
                else:
                    _g1, _g2 = res['e'][0], res['e'][1]
                g1arr.append(_g1)
                g2arr.append(_g2)
                Tarr.append(res['pars'][4])
        except GMixRangeError:
            pass

    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)

    gtol = 1e-9
    assert np.abs(g1 - g1_true) < gtol, (g1,
                                         np.std(g1arr) / np.sqrt(len(g1arr)))
    assert np.abs(g2 - g2_true) < gtol, (g2,
                                         np.std(g2arr) / np.sqrt(len(g2arr)))

    # T test should only pass when the weight function is constant so
    # weight_fac needs to be rally big
    if g1_true == 0 and g2_true == 0 and weight_fac > 1:
        T = np.mean(Tarr)
        assert np.abs(T - fwhm_to_T(fwhm)) < 1e-6
Exemplo n.º 19
0
def test_ml_fitting_exp_obj_gauss_psf_smoke(
        g1_true, g2_true, wcs_g1, wcs_g2, fit_model):

    rng = np.random.RandomState(seed=10)

    image_size = 33
    cen = (image_size - 1)/2
    gs_wcs = galsim.ShearWCS(
        0.25, galsim.Shear(g1=wcs_g1, g2=wcs_g2)).jacobian()
    scale = np.sqrt(gs_wcs.pixelArea())

    gal = galsim.Exponential(
        half_light_radius=0.5
    ).shear(
        g1=g1_true, g2=g2_true
    ).withFlux(
        400,
    )

    obj = galsim.Convolve([gal, galsim.Gaussian(fwhm=0.5)])

    psf_im = galsim.Gaussian(fwhm=0.5).drawImage(
        nx=33, ny=33, wcs=gs_wcs, method='no_pixel').array
    psf_gmix = ngmix.gmix.make_gmix_model(
        [0, 0, 0, 0, fwhm_to_T(0.5), 1], "gauss")
    psf_obs = Observation(
        image=psf_im,
        gmix=psf_gmix
    )

    im = obj.drawImage(
        nx=image_size,
        ny=image_size,
        wcs=gs_wcs,
        method='no_pixel',
    ).array
    noise = np.sqrt(np.sum(im**2)) / 1e16

    wgt = np.ones_like(im) / noise**2

    prior = get_prior(fit_model=fit_model, rng=rng, scale=scale)
    guess = prior.sample()
    guess[0] = 0
    guess[1] = 0
    guess[2] = g1_true
    guess[3] = g2_true
    guess[4] = fwhm_to_T(0.5)

    if fit_model == 'bd':
        guess[5] = 1.0
        guess[6] = 0.5
        guess[7] = 400
    elif fit_model == 'bdf':
        guess[6] = 400
    else:
        guess[5] = 400

    g1arr = []
    g2arr = []
    farr = []
    xarr = []
    yarr = []

    fitter = Fitter(model=fit_model, prior=prior)

    for _ in range(50):
        shift = rng.uniform(low=-scale/2, high=scale/2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(
            dx=shift[0], dy=shift[1]
        ).drawImage(
            nx=image_size,
            ny=image_size,
            wcs=gs_wcs,
            method='no_pixel',
            dtype=np.float64).array

        jac = Jacobian(
            y=cen + xy.y, x=cen + xy.x,
            dudx=gs_wcs.dudx, dudy=gs_wcs.dudy,
            dvdx=gs_wcs.dvdx, dvdy=gs_wcs.dvdy)

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = Observation(
            image=_im,
            weight=wgt,
            jacobian=jac,
            psf=psf_obs)

        res = fitter.go(
            obs=obs, guess=guess + rng.normal(size=guess.size) * 0.01,
        )
        if res['flags'] == 0:
            _g1, _g2, _ = res['g'][0], res['g'][1], res['pars'][4]
            g1arr.append(_g1)
            g2arr.append(_g2)
            farr.append(res['pars'][5])
            xarr.append(res['pars'][1])
            yarr.append(res['pars'][0])

    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)

    if fit_model == 'bd':
        # bd fitting is highly degenerate, we don't recover the
        # ellipticity with as much accuracy
        gtol = 0.002
    else:
        gtol = 1.0e-5

    assert np.abs(g1 - g1_true) < gtol
    assert np.abs(g2 - g2_true) < gtol

    xerr = np.std(xarr) / np.sqrt(len(xarr))
    assert np.abs(np.mean(xarr)) < xerr * 5
    yerr = np.std(yarr) / np.sqrt(len(yarr))
    assert np.abs(np.mean(yarr)) < yerr * 5
Exemplo n.º 20
0
def test_ml_max_fitting_gauss_smoke(g1_true, g2_true, wcs_g1, wcs_g2):
    rng = np.random.RandomState(seed=42)

    # allow some small relative bias due to approximate exp function
    tol = 1.0e-5

    flux = 400  # in galsim units
    image_size = 33
    cen = (image_size - 1) / 2
    gs_wcs = galsim.ShearWCS(0.25, galsim.Shear(g1=wcs_g1,
                                                g2=wcs_g2)).jacobian()
    scale = np.sqrt(gs_wcs.pixelArea())

    g_prior = ngmix.priors.GPriorBA(sigma=0.2, rng=rng)
    cen_prior = ngmix.priors.CenPrior(
        cen1=0,
        cen2=0,
        sigma1=scale,
        sigma2=scale,
        rng=rng,
    )
    T_prior = ngmix.priors.FlatPrior(minval=0.1, maxval=2, rng=rng)
    F_prior = ngmix.priors.FlatPrior(minval=1e-4, maxval=1e9, rng=rng)
    prior = ngmix.joint_prior.PriorSimpleSep(
        cen_prior=cen_prior,
        g_prior=g_prior,
        T_prior=T_prior,
        F_prior=F_prior,
    )

    obj = galsim.Gaussian(fwhm=0.9).shear(g1=g1_true,
                                          g2=g2_true).withFlux(flux, )
    im = obj.drawImage(nx=image_size,
                       ny=image_size,
                       wcs=gs_wcs,
                       method='no_pixel').array

    noise = np.sqrt(np.sum(im**2)) / 1.e6
    wgt = np.ones_like(im) / noise**2

    g1arr = []
    g2arr = []
    Tarr = []
    farr = []
    xarr = []
    yarr = []
    for _ in range(10):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(dx=shift[0],
                       dy=shift[1]).drawImage(nx=image_size,
                                              ny=image_size,
                                              wcs=gs_wcs,
                                              method='no_pixel',
                                              dtype=np.float64).array

        jac = Jacobian(
            y=cen + xy.y,
            x=cen + xy.x,
            dudx=gs_wcs.dudx,
            dudy=gs_wcs.dudy,
            dvdx=gs_wcs.dvdx,
            dvdy=gs_wcs.dvdy,
        )

        _im = im + rng.normal(size=im.shape, scale=noise)
        obs = Observation(
            image=_im,
            weight=wgt,
            jacobian=jac,
        )

        prior = None
        fitter = Fitter(model='gauss', prior=prior)

        guess = np.zeros(6)
        guess[0:2] = rng.uniform(low=-0.1 * scale, high=0.1 * scale, size=2)
        guess[2] = g1_true + rng.uniform(low=-0.01, high=0.01)
        guess[3] = g2_true + rng.uniform(low=-0.01, high=0.01)
        guess[4] = fwhm_to_T(0.9) * rng.uniform(low=0.99, high=1.01)
        guess[5] = flux * rng.uniform(low=0.99, high=1.01)

        res = fitter.go(obs=obs, guess=guess)

        if res['flags'] == 0:
            _g1, _g2, _T = res['g'][0], res['g'][1], res['pars'][4]
            g1arr.append(_g1)
            g2arr.append(_g2)
            Tarr.append(_T)
            farr.append(res['pars'][5])
            xarr.append(res['pars'][1])
            yarr.append(res['pars'][0])

    assert len(g1arr) > 0
    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)
    assert np.abs(g1 - g1_true) < tol
    assert np.abs(g2 - g2_true) < tol

    if g1_true == 0 and g2_true == 0:
        T = np.mean(Tarr)
        Ttrue = fwhm_to_T(0.9)
        assert T / Ttrue - 1 < tol

    fmn = np.mean(farr)

    assert np.abs(fmn / flux - 1) < tol

    xerr = np.std(xarr) / np.sqrt(len(xarr))
    assert np.abs(np.mean(xarr)) < xerr * 5
    yerr = np.std(yarr) / np.sqrt(len(yarr))
    assert np.abs(np.mean(yarr)) < yerr * 5
Exemplo n.º 21
0
def get_ngauss_obs(*, rng, ngauss, noise=0.0, with_psf=False, psf_model='turb'):

    counts = 100.0
    dims = [25, 25]
    cen = (np.array(dims) - 1.0) / 2.0
    jacob = DiagonalJacobian(scale=PIXEL_SCALE, row=cen[0], col=cen[1])

    T_1 = 0.55  # arcsec**2
    if with_psf:
        T_1 = T_1 - TPSF

    e1_1 = 0.1
    e2_1 = 0.05

    irr_1 = T_1 / 2.0 * (1 - e1_1)
    irc_1 = T_1 / 2.0 * e2_1
    icc_1 = T_1 / 2.0 * (1 + e1_1)

    cen1 = [-3.25*PIXEL_SCALE, -3.25*PIXEL_SCALE]

    if ngauss == 2:

        frac1 = 0.4
        frac2 = 0.6

        cen2 = [3.0*PIXEL_SCALE, 0.5*PIXEL_SCALE]

        T_2 = T_1/2

        e1_1 = 0.1
        e2_1 = 0.05
        e1_2 = -0.2
        e2_2 = -0.1

        counts_1 = frac1 * counts
        counts_2 = frac2 * counts

        irr_1 = T_1 / 2.0 * (1 - e1_1)
        irc_1 = T_1 / 2.0 * e2_1
        icc_1 = T_1 / 2.0 * (1 + e1_1)

        irr_2 = T_2 / 2.0 * (1 - e1_2)
        irc_2 = T_2 / 2.0 * e2_2
        icc_2 = T_2 / 2.0 * (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,
        ]

    elif ngauss == 1:

        pars = [
            counts,
            cen1[0],
            cen1[1],
            irr_1,
            irc_1,
            icc_1,
        ]

    gm = GMix(pars=pars)

    if with_psf:
        psf_ret = get_psf_obs(rng=rng, model=psf_model)
        gmconv = gm.convolve(psf_ret['gmix'])

        im0 = gmconv.make_image(dims, jacobian=jacob)
        psf_obs = psf_ret['obs']
    else:
        im0 = gm.make_image(dims, jacobian=jacob)
        psf_obs = None

    im = im0 + rng.normal(size=im0.shape, scale=noise)
    obs = Observation(im, jacobian=jacob, psf=psf_obs)

    ret = {
        'obs': obs,
        'gmix': gm,
    }

    if with_psf:
        ret['psf_gmix'] = psf_ret['gmix']

    return ret
Exemplo n.º 22
0
def get_model_obs(
    *, rng, model,
    noise=0.0,
    psf_model='turb',
    psf_noise=1.0e-6,
    set_psf_gmix=False,
    set_templates=False,
    set_psf_templates=False,
    nepoch=None, nband=None,
    star=False,
):

    if nband is not None:
        do_mbobs = True
        if nepoch is None:
            nepoch = 1
    else:
        do_mbobs = False
        nband = 1

    if nepoch is not None:
        do_obslist = True
    else:
        do_obslist = False
        nepoch = 1

    if star:
        T = 0.0
    else:
        T = 0.27

    g1 = 0.1
    g2 = 0.05
    flux = 100.0
    off = 0.5

    # not offset from the jacobian center
    pars = [0.0, 0.0, g1, g2, T] + [flux]*nband
    gm = GMixModel(pars[0:6], model)

    mbobs = MultiBandObsList()
    for iband in range(nband):
        obslist = ObsList()
        for i in range(nepoch):

            off1_pix, off2_pix = rng.uniform(low=-off, high=off, size=2)
            dims = [32, 32]
            jcen = (np.array(dims) - 1.0) / 2.0
            jacob = DiagonalJacobian(
                scale=PIXEL_SCALE,
                row=jcen[0] + off1_pix,
                col=jcen[1] + off2_pix,
            )

            psf_ret = get_psf_obs(rng=rng, model=psf_model, noise=psf_noise)
            if set_psf_gmix:
                psf_ret['obs'].set_gmix(psf_ret['gmix'])

            if set_psf_templates:
                psf_ret['obs'].template = psf_ret['gmix'].make_image(
                    dims,
                    jacobian=jacob,
                )

            gmconv = gm.convolve(psf_ret['gmix'])

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

            im = im0 + rng.normal(size=im0.shape, scale=noise)
            if noise == 0.0:
                weight = im*0 + 1.0/1.0e-12
            else:
                weight = im*0 + 1.0/noise**2

            obs = Observation(
                im,
                weight=weight,
                jacobian=jacob,
                psf=psf_ret['obs'],
            )

            if set_templates:
                obs.template = im0

            obslist.append(obs)

        mbobs.append(obslist)

    ret = {
        'gmix': gm,
        'pars': pars,
        'psf_data': psf_ret,
    }

    if do_mbobs:
        ret['obs'] = mbobs
    else:
        obslist = mbobs[0]
        if not do_obslist:
            obs = obslist[0]
            ret['obs'] = obs
        else:
            ret['obs'] = obslist

    return ret
Exemplo n.º 23
0
def test_admom_smoke(g1_true, g2_true, wcs_g1, wcs_g2):
    rng = np.random.RandomState(seed=100)

    fwhm = 0.9
    image_size = 107
    cen = (image_size - 1) / 2
    gs_wcs = galsim.ShearWCS(0.125, galsim.Shear(g1=wcs_g1,
                                                 g2=wcs_g2)).jacobian()

    obj = galsim.Gaussian(fwhm=fwhm).shear(g1=g1_true,
                                           g2=g2_true).withFlux(400)
    im = obj.drawImage(nx=image_size,
                       ny=image_size,
                       wcs=gs_wcs,
                       method='no_pixel').array
    noise = np.sqrt(np.sum(im**2)) / 1e18
    wgt = np.ones_like(im) / noise**2
    scale = np.sqrt(gs_wcs.pixelArea())

    g1arr = []
    g2arr = []
    Tarr = []
    for _ in range(50):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(dx=shift[0],
                       dy=shift[1]).drawImage(nx=image_size,
                                              ny=image_size,
                                              wcs=gs_wcs,
                                              method='no_pixel',
                                              dtype=np.float64).array

        jac = Jacobian(y=cen + xy.y,
                       x=cen + xy.x,
                       dudx=gs_wcs.dudx,
                       dudy=gs_wcs.dudy,
                       dvdx=gs_wcs.dvdx,
                       dvdy=gs_wcs.dvdy)

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = Observation(image=_im, weight=wgt, jacobian=jac)
        fitter = Admom(obs, rng=rng)
        try:
            fitter.go(fwhm_to_T(fwhm) + rng.normal() * 0.01)
            res = fitter.get_result()
            if res['flags'] == 0:
                gm = fitter.get_gmix()
                _g1, _g2, _T = gm.get_g1g2T()

                g1arr.append(_g1)
                g2arr.append(_g2)
                Tarr.append(_T)
        except GMixRangeError:
            pass

    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)
    gtol = 1e-6
    assert np.abs(g1 - g1_true) < gtol, (g1,
                                         np.std(g1arr) / np.sqrt(len(g1arr)))
    assert np.abs(g2 - g2_true) < gtol, (g2,
                                         np.std(g2arr) / np.sqrt(len(g2arr)))

    if g1_true == 0 and g2_true == 0:
        T = np.mean(Tarr)
        assert np.abs(T - fwhm_to_T(fwhm)) < 1e-6
Exemplo n.º 24
0
def test_admom_smoke(g1_true, g2_true, wcs_g1, wcs_g2):
    rng = np.random.RandomState(seed=100)

    fwhm = 0.9
    image_size = 107
    cen = (image_size - 1) / 2
    gs_wcs = galsim.ShearWCS(0.125, galsim.Shear(g1=wcs_g1,
                                                 g2=wcs_g2)).jacobian()

    obj = galsim.Gaussian(fwhm=fwhm).shear(g1=g1_true,
                                           g2=g2_true).withFlux(400)
    im = obj.drawImage(nx=image_size,
                       ny=image_size,
                       wcs=gs_wcs,
                       method='no_pixel').array
    noise = np.sqrt(np.sum(im**2)) / 1e18
    wgt = np.ones_like(im) / noise**2
    scale = np.sqrt(gs_wcs.pixelArea())

    g1arr = []
    g2arr = []
    Tarr = []
    for _ in range(50):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(dx=shift[0], dy=shift[1]).drawImage(
            nx=image_size,
            ny=image_size,
            wcs=gs_wcs,
            method='no_pixel',
            dtype=np.float64,
        ).array

        jac = Jacobian(y=cen + xy.y,
                       x=cen + xy.x,
                       dudx=gs_wcs.dudx,
                       dudy=gs_wcs.dudy,
                       dvdx=gs_wcs.dvdx,
                       dvdy=gs_wcs.dvdy)

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = Observation(image=_im, weight=wgt, jacobian=jac)

        Tguess = fwhm_to_T(fwhm) + rng.normal() * 0.01
        res = run_admom(obs=obs, guess=Tguess)

        if res['flags'] == 0:
            gm = res.get_gmix()
            _g1, _g2, _T = gm.get_g1g2T()

            g1arr.append(_g1)
            g2arr.append(_g2)
            Tarr.append(_T)

            fim = res.make_image()
            assert fim.shape == im.shape

        res['flags'] = 5
        with pytest.raises(RuntimeError):
            res.make_image()
        with pytest.raises(RuntimeError):
            res.get_gmix()

    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)
    gtol = 1.5e-6
    assert np.abs(g1 - g1_true) < gtol, (g1,
                                         np.std(g1arr) / np.sqrt(len(g1arr)))
    assert np.abs(g2 - g2_true) < gtol, (g2,
                                         np.std(g2arr) / np.sqrt(len(g2arr)))

    if g1_true == 0 and g2_true == 0:
        T = np.mean(Tarr)
        assert np.abs(T - fwhm_to_T(fwhm)) < 1e-6

    with pytest.raises(ValueError):
        _ = run_admom(None, None)

    # cover some branches
    tres = copy.deepcopy(res)

    tres['flags'] = 0
    tres['sums_cov'][:, :] = np.nan
    tres = ngmix.admom.admom.get_result(tres)
    assert tres['e1err'] == 9999.0

    tres = copy.deepcopy(res)
    tres['flags'] = 0
    tres['pars'][4] = -1
    tres = ngmix.admom.admom.get_result(tres)
    assert tres['flags'] == 0x8
Exemplo n.º 25
0
def test_ml_fitting_gauss_smoke(g1_true, g2_true, wcs_g1, wcs_g2):
    rng = np.random.RandomState(seed=42)

    # allow some small relative bias due to approximate exp function
    tol = 1.0e-5

    image_size = 33
    cen = (image_size - 1) / 2
    gs_wcs = galsim.ShearWCS(0.25, galsim.Shear(g1=wcs_g1,
                                                g2=wcs_g2)).jacobian()
    scale = np.sqrt(gs_wcs.pixelArea())

    g_prior = ngmix.priors.GPriorBA(0.2)
    cen_prior = ngmix.priors.CenPrior(0, 0, scale, scale)
    T_prior = ngmix.priors.FlatPrior(0.1, 2)
    F_prior = ngmix.priors.FlatPrior(1e-4, 1e9)
    prior = ngmix.joint_prior.PriorSimpleSep(cen_prior, g_prior, T_prior,
                                             F_prior)

    obj = galsim.Gaussian(fwhm=0.9).shear(g1=g1_true, g2=g2_true).withFlux(400)
    im = obj.drawImage(nx=image_size,
                       ny=image_size,
                       wcs=gs_wcs,
                       method='no_pixel').array
    noise = np.sqrt(np.sum(im**2)) / 1e16
    wgt = np.ones_like(im) / noise**2

    g1arr = []
    g2arr = []
    Tarr = []
    farr = []
    xarr = []
    yarr = []
    for _ in range(100):
        shift = rng.uniform(low=-scale / 2, high=scale / 2, size=2)
        xy = gs_wcs.toImage(galsim.PositionD(shift))

        im = obj.shift(dx=shift[0],
                       dy=shift[1]).drawImage(nx=image_size,
                                              ny=image_size,
                                              wcs=gs_wcs,
                                              method='no_pixel',
                                              dtype=np.float64).array

        jac = Jacobian(y=cen + xy.y,
                       x=cen + xy.x,
                       dudx=gs_wcs.dudx,
                       dudy=gs_wcs.dudy,
                       dvdx=gs_wcs.dvdx,
                       dvdy=gs_wcs.dvdy)

        _im = im + (rng.normal(size=im.shape) * noise)
        obs = Observation(image=_im, weight=wgt, jacobian=jac)
        fitter = LMSimple(obs, 'gauss', prior=prior)

        guess = np.ones(6) * 0.1
        guess[0] = 0
        guess[1] = 0
        guess[2] = g1_true
        guess[3] = g2_true
        guess[4] = fwhm_to_T(0.9)
        guess[5] = 400 * scale * scale

        fitter.go(guess + rng.normal(size=6) * 0.01)
        res = fitter.get_result()

        if res['flags'] == 0:
            _g1, _g2, _T = res['g'][0], res['g'][1], res['pars'][4]
            g1arr.append(_g1)
            g2arr.append(_g2)
            Tarr.append(_T)
            farr.append(res['pars'][5])
            xarr.append(res['pars'][1])
            yarr.append(res['pars'][0])

    g1 = np.mean(g1arr)
    g2 = np.mean(g2arr)
    assert np.abs(g1 - g1_true) < tol
    assert np.abs(g2 - g2_true) < tol

    if g1_true == 0 and g2_true == 0:
        T = np.mean(Tarr)
        Ttrue = fwhm_to_T(0.9)
        assert T / Ttrue - 1 < tol

    fmn = np.mean(farr) / scale / scale

    assert np.abs(fmn / 400 - 1) < tol

    xerr = np.std(xarr) / np.sqrt(len(xarr))
    assert np.abs(np.mean(xarr)) < xerr * 5
    yerr = np.std(yarr) / np.sqrt(len(yarr))
    assert np.abs(np.mean(yarr)) < yerr * 5