Beispiel #1
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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