Beispiel #1
0
def test_coadds_bad_max_maskfrac(max_maskfrac):
    rng = np.random.RandomState(55)

    coadd_dim = 101
    psf_dim = 51

    bands = ['i']
    sim_data = _make_sim(
        rng=rng,
        psf_type='gauss',
        bands=bands,
        coadd_dim=coadd_dim,
        psf_dim=psf_dim,
    )

    # coadd each band separately
    exps = sim_data['band_data']['i']

    with pytest.raises(ValueError):
        coadd, exp_info = make_coadd_obs(
            exps=exps,
            coadd_wcs=sim_data['coadd_wcs'],
            coadd_bbox=sim_data['coadd_bbox'],
            psf_dims=sim_data['psf_dims'],
            rng=rng,
            remove_poisson=False,  # no object poisson noise in sims
            max_maskfrac=max_maskfrac,
        )
Beispiel #2
0
def test_coadds_noise(dither, rotate):
    rng = np.random.RandomState(55)

    coadd_dim = 101
    psf_dim = 51

    cen = (coadd_dim - 1) // 2
    pcen = (psf_dim - 1) // 2

    bands = ['r', 'i', 'z']
    sim_data = _make_sim(
        rng=rng,
        psf_type='gauss',
        bands=bands,
        coadd_dim=coadd_dim,
        psf_dim=psf_dim,
        dither=dither,
        rotate=rotate,
    )

    # coadd each band separately
    bdata = sim_data['band_data']
    for band in bands:
        assert band in bdata
        exps = bdata[band]

        coadd, exp_info = make_coadd_obs(
            exps=exps,
            coadd_wcs=sim_data['coadd_wcs'],
            coadd_bbox=sim_data['coadd_bbox'],
            psf_dims=sim_data['psf_dims'],
            rng=rng,
            remove_poisson=False,  # no object poisson noise in sims
        )

        coadd_dims = (coadd_dim, ) * 2
        psf_dims = (psf_dim, ) * 2
        assert coadd.image.shape == coadd_dims
        assert coadd.noise.shape == coadd_dims
        assert coadd.psf.image.shape == psf_dims

        assert np.all(np.isfinite(coadd.psf.image))
        assert np.all(coadd.mfrac == 0)

        emed = coadd.coadd_exp.variance.array[cen, cen]
        pmed = coadd.coadd_psf_exp.variance.array[pcen, pcen]
        nmed = coadd.coadd_noise_exp.variance.array[cen, cen]

        assert abs(pmed / emed - 1) < 1.0e-3
        assert abs(nmed / emed - 1) < 1.0e-3
Beispiel #3
0
def test_cosmics():

    rng = np.random.RandomState(32)

    coadd_dim = 101
    buff = 5
    psf_dim = 51
    galaxy_catalog = make_galaxy_catalog(
        rng=rng,
        gal_type="exp",
        coadd_dim=coadd_dim,
        buff=buff,
        layout="grid",
    )
    psf = make_fixed_psf(psf_type='gauss')

    data = make_sim(
        rng=rng,
        galaxy_catalog=galaxy_catalog,
        coadd_dim=coadd_dim,
        psf_dim=psf_dim,
        g1=0.02,
        g2=0.00,
        psf=psf,
        epochs_per_band=10,  # so we get a CR
        cosmic_rays=True,
    )
    coadd, exp_info = make_coadd_obs(
        exps=data['band_data']['i'],
        coadd_wcs=data['coadd_wcs'],
        coadd_bbox=data['coadd_bbox'],
        psf_dims=data['psf_dims'],
        rng=rng,
        remove_poisson=False,  # no object poisson noise in sims
    )

    assert any(exp_info['maskfrac'] > 0)

    cosmic_flag = coadd.coadd_exp.mask.getPlaneBitMask('CR')

    bmask = coadd.coadd_exp.mask.array
    noise_bmask = coadd.coadd_noise_exp.mask.array

    w = np.where((bmask & cosmic_flag) != 0)
    assert w[0].size != 0

    w = np.where((noise_bmask & cosmic_flag) != 0)
    assert w[0].size != 0
def test_coadd_image_correct(crazy_wcs, crazy_obj):

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

    n_coadd = 10
    psf_dim = 51
    coadd_dim = 53

    coadd_cen = (coadd_dim + 1) / 2

    se_dim = int(np.ceil(coadd_dim * np.sqrt(2)))
    if se_dim % 2 == 0:
        se_dim += 1

    se_cen = (se_dim + 1) / 2
    scale = 0.2
    noise_std = 0.1
    world_origin = galsim.CelestialCoord(0 * galsim.degrees,
                                         0 * galsim.degrees)

    aff = galsim.PixelScale(scale).affine()
    aff = aff.withOrigin(galsim.PositionD(coadd_cen, coadd_cen),
                         galsim.PositionD(0, 0))
    coadd_wcs = galsim.TanWCS(
        aff,
        world_origin,
    )

    def _gen_psf_func(wcs, fwhm):
        def _psf_func(*args, **kargs):
            return galsim.Gaussian(fwhm=fwhm).drawImage(
                nx=101, ny=101,
                wcs=wcs.local(world_pos=world_origin)), galsim.PositionD(0, 0)

        return _psf_func

    wgts = []
    objs = []
    psf_objs = []
    exps = []
    for _ in range(n_coadd):
        if crazy_obj:
            _fwhm = 2.9 * (1.0 + rng.normal() * 0.1)
            _g1 = rng.normal() * 0.3
            _g2 = rng.normal() * 0.3
            obj = galsim.Gaussian(fwhm=_fwhm).shear(g1=_g1, g2=_g2)
        else:
            obj = galsim.Gaussian(fwhm=2.9).shear(g1=-0.1, g2=0.3)

        objs.append(obj)

        if crazy_wcs:
            shear = galsim.Shear(g1=rng.normal() * 0.01,
                                 g2=rng.normal() * 0.01)
            aff = galsim.ShearWCS(scale, shear).affine()
            aff = aff.withOrigin(galsim.PositionD(se_cen, se_cen),
                                 galsim.PositionD(0, 0))
            wcs = galsim.TanWCS(
                aff,
                world_origin,
            )
        else:
            aff = galsim.PixelScale(scale).affine()
            aff = aff.withOrigin(galsim.PositionD(se_cen, se_cen),
                                 galsim.PositionD(0, 0))
            wcs = galsim.TanWCS(
                aff,
                world_origin,
            )

        _noise = noise_std * (1 + (rng.uniform() - 0.5) * 2 * 0.05)
        wgts.append(1.0 / _noise**2)

        bmsk = galsim.ImageI(np.zeros((se_dim, se_dim)))

        img = obj.drawImage(
            nx=se_dim,
            ny=se_dim,
            wcs=wcs.local(world_pos=world_origin),
        )

        if crazy_obj:
            _psf_fwhm = 1.0 * (1.0 + rng.normal() * 0.1)
        else:
            _psf_fwhm = 1.0

        psf = galsim.Gaussian(fwhm=_psf_fwhm)
        psf_objs.append(psf)

        exp = make_exp(
            gsimage=img,
            bmask=bmsk,
            noise=_noise,
            galsim_wcs=wcs,
            galsim_psf=psf,
            psf_dim=psf_dim,
        )
        exps.append(exp)

    coadd_bbox = geom.Box2I(
        geom.IntervalI(min=0, max=coadd_dim - 1),
        geom.IntervalI(min=0, max=coadd_dim - 1),
    )
    coadd, exp_info = make_coadd_obs(
        exps=exps,
        coadd_wcs=make_dm_wcs(coadd_wcs),
        coadd_bbox=coadd_bbox,
        psf_dims=(psf_dim, ) * 2,
        rng=rng,
        remove_poisson=False,
    )

    coadd_img = coadd.image
    coadd_psf = coadd.psf.image

    wgts = np.array(wgts) / np.sum(wgts)
    true_coadd_img = galsim.Sum([
        obj.withFlux(wgt) for obj, wgt in zip(objs, wgts)
    ]).drawImage(nx=coadd_dim,
                 ny=coadd_dim,
                 wcs=coadd_wcs.local(world_pos=world_origin)).array

    true_coadd_psf = galsim.Sum([
        obj.withFlux(wgt) for obj, wgt in zip(psf_objs, wgts)
    ]).drawImage(nx=psf_dim,
                 ny=psf_dim,
                 wcs=coadd_wcs.local(world_pos=world_origin)).array

    if not crazy_wcs:
        rtol = 0
        atol = 5e-7
    else:
        rtol = 0
        atol = 5e-5

    coadd_img_err = np.max(np.abs(coadd_img - true_coadd_img))
    coadd_psf_err = np.max(np.abs(coadd_psf - true_coadd_psf))
    print("image max abs error:", coadd_img_err)
    print("psf max abs error:", coadd_psf_err)

    if not np.allclose(coadd_img, true_coadd_img, rtol=rtol, atol=atol):
        _plot_cmp(coadd_img, true_coadd_img, rtol, atol, crazy_obj, crazy_wcs,
                  "img")

    if not np.allclose(coadd_psf, true_coadd_psf, rtol=rtol, atol=atol):
        _plot_cmp(coadd_psf, true_coadd_psf, rtol, atol, crazy_obj, crazy_wcs,
                  "psf")

    assert np.allclose(coadd_img, true_coadd_img, rtol=rtol, atol=atol)
    assert np.allclose(coadd_psf, true_coadd_psf, rtol=rtol, atol=atol)
    assert np.all(np.isfinite(coadd.noise))
Beispiel #5
0
def test_coadd_psf(show=False):
    """
    test that a coadded high s/n star and the coadded psf are consistent to
    high precision.  Also check that both are well centered on the odd dims psf
    of coadd/psf codad

    This is a crucial test of the conventions used for the wcs, bounding
    boxes, and the dithers

    huge props to Eli R. for suggesting this test!
    """
    rng = np.random.RandomState(995)

    ntrial = 10

    for itrial in range(ntrial):
        sim_data, cat = _make_one_star_sim(rng=rng)

        bdata = sim_data['band_data']
        for band, exps in bdata.items():
            exps = bdata[band]

            survey = get_survey(gal_type='fixed', band=band)
            star_flux = survey.get_flux(cat.mag)

            coadd, exp_info = make_coadd_obs(
                exps=exps,
                coadd_wcs=sim_data['coadd_wcs'],
                coadd_bbox=sim_data['coadd_bbox'],
                psf_dims=sim_data['psf_dims'],
                rng=rng,
                remove_poisson=False,  # no object poisson noise in sims
            )

            assert coadd.image.shape == coadd.psf.image.shape

            star_row, star_col = get_centroid(coadd.image)
            psf_row, psf_col = get_centroid(coadd.psf.image)

            cen = (coadd.image.shape[0] - 1) // 2
            tol = 0.015
            assert abs(star_row - cen) < tol
            assert abs(star_col - cen) < tol
            assert abs(psf_row - cen) < tol
            assert abs(psf_col - cen) < tol

            print(f'star cen: {star_row:g} {star_col:g}')
            print(f'psf cen:  {psf_row:g} {psf_col:g}')

            psf_image_rescaled = (coadd.psf.image * star_flux /
                                  coadd.psf.image.sum())

            abs_diff_im = np.abs(coadd.image - psf_image_rescaled)
            max_diff = abs_diff_im.max()
            im_max = coadd.image.max()
            assert max_diff / im_max < 0.0005

            if show:
                compare_images(
                    coadd_image=coadd.image,
                    psf_image_rescaled=psf_image_rescaled,
                )
Beispiel #6
0
def test_coadds_sat(dither, rotate):
    """
    run trials with stars and make sure we get some SAT
    in the coadd mask
    """
    rng = np.random.RandomState(85)

    coadd_dim = 101
    psf_dim = 51
    band = 'i'
    epochs_per_band = 1

    ntrial = 100

    somesat = False
    for i in range(ntrial):
        sim_data = _make_sim(
            rng=rng,
            psf_type='gauss',
            bands=[band],
            epochs_per_band=epochs_per_band,
            coadd_dim=coadd_dim,
            psf_dim=psf_dim,
            dither=dither,
            rotate=rotate,
            stars=True,
        )

        exps = sim_data['band_data'][band]

        coadd, exp_info = make_coadd_obs(
            exps=exps,
            coadd_wcs=sim_data['coadd_wcs'],
            coadd_bbox=sim_data['coadd_bbox'],
            psf_dims=sim_data['psf_dims'],
            rng=rng,
            remove_poisson=False,  # no object poisson noise in sims
        )

        if False:
            import lsst.afw.display as afw_display

            display = afw_display.getDisplay(backend='ds9')
            display.mtv(coadd.coadd_exp)
            display.scale('log', 'minmax')

        mask = coadd.coadd_exp.mask
        sat_flag = mask.getPlaneBitMask('SAT')
        interp_flag = mask.getPlaneBitMask('INTRP')

        wsat = np.where(mask.array & sat_flag != 0)
        wint = np.where(mask.array & interp_flag != 0)

        if wsat[0].size > 0:
            assert wint[0].size > 0, 'expected sat to be interpolated'
            assert np.all(mask.array[wsat] & interp_flag != 0)
            somesat = True
            break

    print('i:', i)
    assert somesat
Beispiel #7
0
def test_coadds_mfrac(dither, rotate):
    rng = np.random.RandomState(55)
    ntrial = 100

    coadd_dim = 101
    psf_dim = 51

    bands = ['r', 'i', 'z']

    for itrial in range(ntrial):
        sim_data = _make_sim(
            rng=rng,
            psf_type='gauss',
            bands=bands,
            coadd_dim=coadd_dim,
            psf_dim=psf_dim,
            dither=dither,
            rotate=rotate,
            bad_columns=True,
        )

        # coadd each band separately
        bdata = sim_data['band_data']
        for band in bands:
            assert band in bdata
            exps = bdata[band]

            coadd, exp_info = make_coadd_obs(
                exps=exps,
                coadd_wcs=sim_data['coadd_wcs'],
                coadd_bbox=sim_data['coadd_bbox'],
                psf_dims=sim_data['psf_dims'],
                rng=rng,
                remove_poisson=False,  # no object poisson noise in sims
            )

            coadd_dims = (coadd_dim, ) * 2
            psf_dims = (psf_dim, ) * 2
            assert coadd.image.shape == coadd_dims
            assert coadd.psf.image.shape == psf_dims

            assert np.all(np.isfinite(coadd.psf.image))
            assert np.all(coadd.mfrac >= 0)
            assert np.all(coadd.mfrac <= 1)

            # This depends on the realization, try until we get one
            if (np.any(coadd.mfrac != 0) and np.max(coadd.mfrac) > 0.1
                    and np.mean(coadd.mfrac) < 0.05):
                ok = True
                break

            if False:
                import matplotlib.pyplot as plt
                plt.figure()
                plt.imshow(coadd.mfrac)
                import pdb
                pdb.set_trace()
        if ok:
            break

    assert ok, 'mfrac not set properly'