Example #1
0
    def __init__(self,
                 ellmin,
                 ellmax,
                 nlev_t,
                 beam_arcmin,
                 px_arcmin,
                 width_deg,
                 cmb1=None,
                 cmb2=None,
                 inkap=None):

        self.cmb1 = cmb1
        self.cmb2 = cmb2
        self.inkap = inkap
        self.ellmin = ellmin
        self.ellmax = ellmax
        self.nlev_t = nlev_t
        self.beam_arcmin = beam_arcmin
        self.px_arcmin = px_arcmin
        self.width_deg = width_deg

        self.shape = self.cmb1.shape
        self.wcs = self.cmb2.wcs

        # npix x npix cutouts
        self.npix = int(width_deg * 60 / self.px_arcmin)
        # total number of tiles
        self.ntiles = int(np.prod(self.shape) / self.npix**2)
        # number of tiles in a row
        self.num_x = int(360 / self.width_deg)

        # Get Cl_TT in theory for symlens, it is same for each cutout
        self.theory = cosmology.default_theory()
Example #2
0
def plot(cents, nls, ncoadd, pols, tag="default"):
    npols = len(pols)
    pl = io.Plotter(xyscale='linlog', ylabel='$C_L$', xlabel='$L$')
    for i in range(npols):
        for j in range(i, npols):
            nl1d = nls[i, j]
            if i != j:
                pl.add(cents, np.abs(nl1d), ls="--",
                       alpha=0.2)  #,label=pols[i]+'x'+pols[j]
            else:
                pl.add(cents, nl1d, alpha=0.6)  #,label=pols[i])

    ells = np.arange(0, 2500, 1)
    theory = cosmology.default_theory()
    clkk = theory.gCl('kk', ells)
    pl.add(ells, clkk, color='k', lw=3)
    pl.legend(loc='upper right')
    pl._ax.set_ylim(1e-10, 1e-4)
    pl.add(cents, ncoadd, color='red', lw=3)  #,label='MV')
    pl.done("nlkk_%s.png" % tag)
    # pl = io.Plotter(xyscale='linlin')
    # pl.add(cents,nls[0,3],label="TT x EB")
    # pl.hline(y=0)
    # pl.done("nltteb_%s.png"%tag)
    io.save_cols("lensing_noise_%s.txt" % tag, (cents, ncoadd))
Example #3
0
def get_cmb(ells):
    theory = cosmology.default_theory()
    lcltt = theory.lCl('TT', ells)
    lclee = theory.lCl('EE', ells)
    lclte = theory.lCl('TE', ells)
    lclbb = theory.lCl('BB', ells)
    clkk = theory.gCl('kk', ells)
    return lcltt, lclee, lclte, lclbb, clkk
Example #4
0
def test_limber():

    zs = np.geomspace(0.1,10.,40)
    ks = np.geomspace(1e-4,10,100)
    ms = np.geomspace(1e8,1e16,30)
    hcos = hmvec.HaloModel(zs,ks,ms,nfw_numeric=False)
    pmm_1h = hcos.get_power_1halo(name="nfw")
    pmm_2h = hcos.get_power_2halo(name="nfw")

    Wk = hcos.lensing_window(zs,zs=1100.)
    
    Pmm = pmm_1h + pmm_2h
    ells = np.linspace(100,1000,20)
    ckk = hcos.C_kk(ells,zs,ks,Pmm,lwindow1=Wk,lwindow2=Wk)
    theory = cosmology.default_theory()
    
    pl = io.Plotter(xyscale='linlog')
    pl.add(ells,ckk)
    pl.add(ells,theory.gCl('kk',ells),ls='--')
    pl.done('ckk_comp.png')

    bias = 2.
    nzs = np.exp(-(zs-1.0)**2./0.3**2.)
    ckg = hcos.C_kg(ells,zs,ks,Pmm*bias,lwindow=Wk,gzs=zs,gdndz=nzs)
    cgg = hcos.C_gg(ells,zs,ks,Pmm*bias**2,gzs=zs,gdndz=nzs)
    lc = cosmology.LimberCosmology(skipCls=True,low_acc=True)
    lc.addNz('g',zs,nzs,bias=bias)
    lc.generateCls(ells)
    ckg2 = lc.getCl('cmb','g')
    cgg2 = lc.getCl('g','g')
    
    pl = io.Plotter(xyscale='linlog')
    pl.add(ells,ckg)
    pl.add(ells,ckg2,ls='--')
    pl.done('ckg_comp.png')

    pl = io.Plotter(xyscale='linlog')
    pl.add(ells,cgg)
    pl.add(ells,cgg2,ls='--')
    pl.done('cgg_comp.png')
Example #5
0
def test_lensing():

    zs = np.geomspace(0.1, 300., 30)
    ks = np.geomspace(1e-4, 20, 100)
    ms = np.geomspace(1e10, 1e17, 20)
    hcos = hmvec.HaloModel(zs, ks, ms, nfw_numeric=False)
    pmm_1h = hcos.get_power_1halo(name="nfw")
    pmm_2h = hcos.get_power_2halo(name="nfw")

    Wk = hcos.lensing_window(zs, zs=1100.)
    pl = io.Plotter(xyscale='loglin')
    pl.add(zs, Wk)
    pl.done()

    Pmm = pmm_1h + pmm_2h
    ells = np.linspace(100, 1000, 20)
    ckk = hcos.C_kk(ells, zs, ks, Pmm, lwindow1=Wk, lwindow2=Wk)
    theory = cosmology.default_theory()

    pl = io.Plotter(xyscale='linlog')
    pl.add(ells, ckk)
    pl.add(ells, theory.gCl('kk', ells), ls='--')
    pl.done()
Example #6
0

for task in my_tasks:

    # Choose a seed. This has to be varied when simulating.
    seed = (0,0,task+sindex)

    # If debugging, get unfiltered maps and plot Cls
    if task==0 and debug_cmb:
        t_alm,e_alm,b_alm = solint.get_kmap(channel,seed,lmin,lmax,filtered=False)
        tcl = hp.alm2cl(t_alm)
        ls = np.arange(tcl.size)
        pl = io.Plotter('Cell')
        pl.add(ls,tcl/w2)
        ls2,nells,nells_P = solint.get_noise_power(channel,beam_deconv=True)
        theory = cosmology.default_theory()
        pl.add(ls,theory.lCl('TT',ls) + maps.interp(ls2,nells)(ls),color='k')
        pl._ax.set_xlim(1,6000)
        pl._ax.set_ylim(1e-6,1e3)
        pl.done(f'{solenspipe.opath}/tcl.png')
        imap = enmap.downgrade(solint.alm2map(np.asarray([t_alm,e_alm,b_alm]),ncomp=3) * maps.binary_mask(mask),2)
        for i in range(3): io.hplot(imap[i],f'{solenspipe.opath}/imap_{i}',mask=0)


    with bench.show("sim"):
        # Get simulated, prepared filtered T, E, B maps, i.e. (1/(C+N) * teb_alm)
        t_alm,e_alm,b_alm = solint.get_kmap(channel,seed,lmin,lmax,filtered=True)
        # Get the reconstructed kappa map alms and filter it with the normalization
        recon_alms = qe.filter_alms(solint.get_mv_kappa(polcomb,t_alm,e_alm,b_alm),maps.interp(Als['L'],Als[polcomb]))
    
    # Subtract a meanfield if necessary
Example #7
0
def build_empirical_cov(kdiffs,
                        kcoadds,
                        wins,
                        mask,
                        lmins,
                        lmaxs,
                        anisotropic_pairs,
                        do_radial_fit,
                        save_fn,
                        signal_bin_width=None,
                        signal_interp_order=0,
                        delta_ell=400,
                        rfit_lmaxes=None,
                        rfit_wnoise_width=250,
                        rfit_lmin=300,
                        rfit_bin_width=None,
                        verbose=True,
                        debug_plots_loc=None,
                        separate_masks=False):
    """
    TODO: Add docs for wins and mask, and names and save_fn

    Build an empirical covariance matrix using hybrid radial (signal) and cartesian
    (noise) binning.

    Args:

        kdiffs: list of length narrays of (nsplits,Ny,Nx) fourier transforms of
        maps that have already been inverse noise weighted and tapered. This
        routine will not apply any corrections for the windowing. Alternatively,
        list of strings containing filenames to versions of these on disk.

        kcoadds: list of length narrays of (Ny,Nx) fourier transforms of
        coadd maps that have already been inverse noise weighted and tapered. This
        routine will not apply any corrections for the windowing. Alternatively,
        list of strings containing filenames to versions of these on disk.


        anisotropic_pairs: list of 2-tuples specifying which elements of the covariance
        matrix will be treated under hybrid radial(signal)/cartesian(noise) mode. If
        (i,j) is in the list, (j,i) will be added if it already doesn't exist, since
        the covariance has to be symmetric. For these pairs, an atmospheric 1/f noise 
        will be fitted out before downsampling the noise power.
    

    For each pair of arrays, one of the following modes is chosen:
    1) fully radial
       - this is appropriate for any i,j for an isotropic experiment like Planck
         or for i!=j combinations of independent experiments like ACT/Planck
         or for i!=j combinations of ACT arrays that have independent instrument 
         noise (no correlated atmosphere), e.g. pa2,pa3
       - it is calculated simply by radially binning the total spectrum of
         the coadds ps(i,j). The split info is not used.
    
    2) hybrid radial/cartesian
       - this is appropriate for diagonals i=j of a ground-based experiment like
         ACT, or for i!=j combinations of ACT arrays that have correlated atmosphere
         e.g. pa3-150/pa3-90
       - this routine assumes that such off diagonal pairs of arrays have the same
         number of splits, and calculates the noise power from splits, and the signal from
         the total spectrum minus the noise power.

    """
    # Setup
    narrays = len(kdiffs)
    on_disk = False
    try:
        tshape = kdiffs[0].shape[-2:]
    except:
        assert isinstance(
            kdiffs[0],
            basestring), "List contents are neither enmaps nor filenames."
        on_disk = True

    def get_mask(aind):
        if separate_masks:
            return _load_map(mask[aind])
        else:
            assert mask.ndim == 2
            return mask

    mask = get_mask(0)
    shape, wcs = mask.shape[-2:], mask.wcs

    def _load_map(kitem):
        if not (on_disk):
            return kitem
        else:
            if kitem[:-5] == '.fits': return enmap.read_map(kitem)
            elif kitem[:-4] == '.npy': return enmap.enmap(np.load(kitem), wcs)
            else: raise IOError

    minell = maps.minimum_ell(shape, wcs)
    modlmap = enmap.modlmap(shape, wcs)

    # Defaults
    if rfit_lmaxes is None:
        px_arcmin = np.rad2deg(maps.resolution(shape, wcs)) * 60.
        rfit_lmaxes = [8000 * 0.5 / px_arcmin] * narrays
    if rfit_bin_width is None: rfit_bin_width = minell * 4.
    if signal_bin_width is None: signal_bin_width = minell * 8.

    maxval = -np.inf
    # Loop over unique covmat elements
    for aindex1 in range(narrays):
        for aindex2 in range(aindex1, narrays):

            # !!!!!
            # from orphics import cosmology
            # theory = cosmology.default_theory()
            # # #['p01','p02','p03','p04','p05','p06','p07','p08']
            # # freqs = [30,44,70,100,143,217,353,545]
            # # fwhms = [7*143/freq for freq in freqs]
            # # rmss = [195,226,199,77,33,47,153,1000]
            # # fwhm1 = fwhms[aindex1]
            # # fwhm2 = fwhms[aindex2]
            # rms = 0 if aindex1!=aindex2 else 60 #rmss[aindex1]
            # fwhm1 = fwhm2 = 1.5
            # tcov = theory.lCl('TT',modlmap)*maps.gauss_beam(modlmap,fwhm1)*maps.gauss_beam(modlmap,fwhm2) + rms # !!!!
            # save_fn(tcov,aindex1,aindex2)
            # continue
            # !!!

            if verbose:
                print("Calculating covariance for array ", aindex1, " x ",
                      aindex2, " ...")

            hybrid = ((aindex1, aindex2) in anisotropic_pairs) or (
                (aindex2, aindex1) in anisotropic_pairs)
            kd1 = _load_map(kdiffs[aindex1])
            kd2 = _load_map(kdiffs[aindex2])
            nsplits1 = kd1.shape[0]
            nsplits2 = kd2.shape[0]
            assert (nsplits1 == 2 or nsplits1 == 4) and (nsplits2 == 2
                                                         or nsplits2 == 4)
            kc1 = _load_map(kcoadds[aindex1])
            kc2 = _load_map(kcoadds[aindex2])
            if kc1.ndim > 2:
                assert kc1.shape[0] == 1
                kc1 = kc1[0]
            if kc2.ndim > 2:
                assert kc2.shape[0] == 1
                kc2 = kc2[0]
            m1 = get_mask(aindex1)
            m2 = get_mask(aindex2)
            if hybrid:
                w1 = _load_map(wins[aindex1])
                w2 = _load_map(wins[aindex2])
                ncov = simnoise.noise_power(kd1,
                                            m1,
                                            kmaps2=kd2,
                                            weights2=m2,
                                            coadd_estimator=True)
                ccov = np.real(kc1 * kc2.conj()) / np.mean(m1 * m2)
                scov = ccov - ncov

                from orphics import cosmology
                theory = cosmology.default_theory()
                # # #['p01','p02','p03','p04','p05','p06','p07','p08']
                # # freqs = [30,44,70,100,143,217,353,545]
                # # fwhms = [7*143/freq for freq in freqs]
                # # rmss = [195,226,199,77,33,47,153,1000]
                # # fwhm1 = fwhms[aindex1]
                # # fwhm2 = fwhms[aindex2]
                # rms = 0 if aindex1!=aindex2 else 60 #rmss[aindex1]
                fwhm1 = fwhm2 = 1.5
                scov = theory.lCl('TT', modlmap) * maps.gauss_beam(
                    modlmap, fwhm1) * maps.gauss_beam(modlmap, fwhm2)
                # save_fn(tcov,aindex1,aindex2)

                # import os
                # # newcov = np.real(kd1[aindex1][0]*ksplits[aindex2][1].conj())/np.mean(w1*w2*m1*m2)
                # enmap.write_map(os.environ['WORK']+'/tiling/ncov.fits',ncov)
                # enmap.write_map(os.environ['WORK']+'/tiling/ccov.fits',ccov)
                # enmap.write_map(os.environ['WORK']+'/tiling/scov.fits',scov)
                # sys.exit()

            else:
                scov = np.real(kc1 * kc2.conj()) / np.mean(m1 * m2)
                ncov = None

            dscov = covtools.signal_average(
                scov,
                bin_width=signal_bin_width,
                kind=signal_interp_order,
                lmin=max(lmins[aindex1], lmins[aindex2]),
                dlspace=True
            )  # ((a,inf),(inf,inf))  doesn't allow the first element to be used, so allow for cross-covariance from non informative
            if ncov is not None:
                assert nsplits1 == nsplits2
                nsplits = nsplits1
                dncov, _, _ = covtools.noise_block_average(
                    ncov,
                    nsplits=nsplits,
                    delta_ell=delta_ell,
                    radial_fit=do_radial_fit[aindex1],
                    lmax=max(rfit_lmaxes[aindex1], rfit_lmaxes[aindex2]),
                    wnoise_annulus=rfit_wnoise_width,
                    lmin=rfit_lmin,
                    bin_annulus=rfit_bin_width,
                    fill_lmax=max(lmaxs[aindex1], lmaxs[aindex2]),
                    log=(aindex1 == aindex2))
            else:
                dncov = np.zeros(dscov.shape)

            tcov = dscov + dncov

            assert np.all(np.isfinite(tcov))
            # print(modlmap[np.isinf(tcov)])
            if aindex1 == aindex2:
                tcov[modlmap <= lmins[aindex1]] = 1e90
                tcov[modlmap >= lmaxs[aindex1]] = 1e90

            # try:
            #     print("scov : ",scov[4,959],scov[5,959])
            # except: pass
            # try:
            #     print("ncov : ",ncov[4,959],ncov[5,959])
            # except:
            #     pass
            # print("dscov : ",dscov[4,959],dscov[5,959])
            # print("dncov : ",dncov[4,959],dncov[5,959])

            maxcov = tcov.max()
            if maxcov > maxval: maxval = maxcov
            if np.any(np.isnan(tcov)): raise ValueError
            # save PS
            save_fn(tcov, aindex1, aindex2)
            if debug_plots_loc:
                save_debug_plots(scov,
                                 dscov,
                                 ncov,
                                 dncov,
                                 tcov,
                                 modlmap,
                                 aindex1,
                                 aindex2,
                                 save_loc=debug_plots_loc)
    return maxval
Example #8
0
from __future__ import print_function
from orphics import maps, io, cosmology, symcoupling as sc, stats, lensing
from enlib import enmap, bench
import numpy as np
import os, sys

cache = True
hdv = False
deg = 5
px = 1.5
shape, wcs = maps.rect_geometry(width_deg=deg, px_res_arcmin=px)
mc = sc.LensingModeCoupling(shape, wcs)
pols = ['TT', "TE", 'EE', 'EB', 'TB']

theory = cosmology.default_theory(lpad=20000)
noise_t = 10.0
noise_p = 10.0 * np.sqrt(2.)
fwhm = 1.5
kbeam = maps.gauss_beam(fwhm, mc.modlmap)
ells = np.arange(0, 3000, 1)
lbeam = maps.gauss_beam(fwhm, ells)
ntt = np.nan_to_num((noise_t * np.pi / 180. / 60.)**2. / kbeam**2.)
nee = np.nan_to_num((noise_p * np.pi / 180. / 60.)**2. / kbeam**2.)
nbb = np.nan_to_num((noise_p * np.pi / 180. / 60.)**2. / kbeam**2.)
lntt = np.nan_to_num((noise_t * np.pi / 180. / 60.)**2. / lbeam**2.)
lnee = np.nan_to_num((noise_p * np.pi / 180. / 60.)**2. / lbeam**2.)
lnbb = np.nan_to_num((noise_p * np.pi / 180. / 60.)**2. / lbeam**2.)

ellmin = 20
ellmax = 3000
xmask = maps.mask_kspace(shape, wcs, lmin=ellmin, lmax=ellmax)
Example #9
0
def test_pol():
    from orphics import lensing, io, cosmology, maps

    est = "hu_ok"
    pols = ['TT', 'EE', 'TE', 'EB', 'TB']
    # est = "hdv"
    # pols = ['TT','EE','TE','ET','EB','TB']

    deg = 5.
    px = 2.0
    tellmin = 30
    tellmax = 3000
    pellmin = 30
    pellmax = 5000
    kellmin = 10
    kellmax = 5000
    bin_width = 40

    beam_arcmin = 1.5
    noise_uk_arcmin = 10.0

    theory = cosmology.default_theory(lpad=30000)
    shape, wcs = s.rect_geometry(width_deg=deg, px_res_arcmin=px)
    modlmap = enmap.modlmap(shape, wcs)
    kbeam = s.gauss_beam(modlmap, beam_arcmin)
    n2d = (noise_uk_arcmin * np.pi / 180. / 60.)**2. / kbeam**2.
    tmask = s.mask_kspace(shape, wcs, lmin=tellmin, lmax=tellmax)
    pmask = s.mask_kspace(shape, wcs, lmin=pellmin, lmax=pellmax)
    kmask = s.mask_kspace(shape, wcs, lmin=kellmin, lmax=kellmax)
    bin_edges = np.arange(kellmin, kellmax, bin_width)
    binner = s.bin2D(modlmap, bin_edges)

    feed_dict = {}
    cltt = theory.lCl('TT', modlmap)
    clee = theory.lCl('EE', modlmap)
    clbb = theory.lCl('BB', modlmap)
    clte = theory.lCl('TE', modlmap)
    feed_dict['uC_T_T'] = cltt
    feed_dict['tC_T_T'] = (cltt + n2d)
    feed_dict['uC_E_E'] = clee
    feed_dict['tC_E_E'] = (clee + n2d * 2.)
    feed_dict['uC_B_B'] = clbb
    feed_dict['tC_B_B'] = (clbb + n2d * 2.)
    feed_dict['uC_T_E'] = clte
    feed_dict['tC_T_E'] = clte

    ells = np.arange(0, 10000, 1)
    pl = io.Plotter(xyscale='loglog')
    pl.add(ells, theory.gCl('kk', ells))
    imask = {'T': tmask, 'E': pmask, 'B': pmask}
    for pol in pols:
        print(pol)
        X, Y = pol
        cents, Nl = binner.bin(
            s.N_l(shape,
                  wcs,
                  feed_dict,
                  est,
                  pol,
                  xmask=imask[X],
                  ymask=imask[Y]))
        pl.add(cents, Nl, label=pol)
    pl._ax.set_xlim(10, kellmax)
    pl.done("nls.png")
Example #10
0
def test_shear():
    from orphics import lensing, io, cosmology, maps

    deg = 20.
    px = 2.0
    tellmin = 30
    tellmax = 3500
    kellmin = 10
    kellmax = 3000
    bin_width = 20
    beam_arcmin = 1.4
    noise_uk_arcmin = 7.0

    theory = cosmology.default_theory(lpad=30000)
    shape, wcs = s.rect_geometry(width_deg=deg, px_res_arcmin=px)
    flsims = lensing.FlatLensingSims(shape, wcs, theory, beam_arcmin,
                                     noise_uk_arcmin)
    kbeam = flsims.kbeam
    modlmap = enmap.modlmap(shape, wcs)
    fc = maps.FourierCalc(shape, wcs)
    n2d = (noise_uk_arcmin * np.pi / 180. / 60.)**2. / flsims.kbeam**2.
    tmask = s.mask_kspace(shape, wcs, lmin=tellmin, lmax=tellmax)
    kmask = s.mask_kspace(shape, wcs, lmin=kellmin, lmax=kellmax)
    bin_edges = np.arange(kellmin, kellmax, bin_width)
    binner = s.bin2D(modlmap, bin_edges)
    i = 0
    unlensed, kappa, lensed, beamed, noise_map, observed = flsims.get_sim(
        seed_cmb=(i, 1),
        seed_kappa=(i, 2),
        seed_noise=(i, 3),
        lens_order=5,
        return_intermediate=True)
    _, kmap, _ = fc.power2d(observed)
    pii2d, kinput, _ = fc.power2d(kappa)

    feed_dict = {}
    cltt = theory.lCl('TT', modlmap)
    feed_dict['uC_T_T'] = theory.lCl('TT', modlmap)
    feed_dict['tC_T_T'] = (cltt + n2d)
    feed_dict['X'] = kmap / kbeam
    feed_dict['Y'] = kmap / kbeam

    ells = np.arange(0, 10000, 1)
    ucltt = theory.lCl('TT', ells)
    feed_dict['duC_T_T'] = s.interp(ells,
                                    np.gradient(np.log(ucltt),
                                                np.log(ells)))(modlmap)
    sAl = s.A_l(shape, wcs, feed_dict, "shear", "TT", xmask=tmask, ymask=tmask)
    sNl = s.N_l(shape,
                wcs,
                feed_dict,
                "shear",
                "TT",
                xmask=tmask,
                ymask=tmask,
                Al=sAl)
    sukappa = s.unnormalized_quadratic_estimator(shape,
                                                 wcs,
                                                 feed_dict,
                                                 "shear",
                                                 "TT",
                                                 xmask=tmask,
                                                 ymask=tmask)
    snkappa = sAl * sukappa

    pir2d3 = fc.f2power(snkappa, kinput)
    cents, pir1d3 = binner.bin(pir2d3)
    cents, pii1d = binner.bin(pii2d)
    cents, prr1d = binner.bin(fc.f2power(snkappa, snkappa))

    cents, Nlkk3 = binner.bin(sNl)

    pl = io.Plotter(xyscale='loglog')
    pl.add(ells, theory.gCl('kk', ells))
    pl.add(cents, pii1d, color='k', lw=3)
    pl.add(cents, pir1d3, label='shear')
    pl.add(cents, prr1d)
    pl.add(cents, Nlkk3, ls=":")
    pl._ax.set_xlim(10, 3500)
    pl.done("ncomp.png")
Example #11
0
def test_hdv_huok_planck():
    from orphics import lensing, io, cosmology, maps

    shape, wcs = enmap.geometry(shape=(512, 512),
                                res=2.0 * putils.arcmin,
                                pos=(0, 0))
    modlmap = enmap.modlmap(shape, wcs)
    theory = cosmology.default_theory()
    ells = np.arange(0, 3000, 1)
    ctt = theory.lCl('TT', ells)
    # ps,_ = powspec.read_camb_scalar("tests/Aug6_highAcc_CDM_scalCls.dat")
    # ells = range(ps.shape[-1])

    ## Build HuOk TT estimator
    f = s.Ldl1 * s.e('uC_T_T_l1') + s.Ldl2 * s.e('uC_T_T_l2')
    F = f / 2 / s.e('tC_T_T_l1') / s.e('tC_T_T_l2')
    expr1 = f * F
    feed_dict = {}
    feed_dict['uC_T_T'] = s.interp(ells, ctt)(modlmap)
    feed_dict['tC_T_T'] = s.interp(ells, ctt)(modlmap) + (
        33. * np.pi / 180. / 60.)**2. / s.gauss_beam(modlmap, 7.0)**2.
    tellmin = 10
    tellmax = 3000
    xmask = s.mask_kspace(shape, wcs, lmin=tellmin, lmax=tellmax)
    integral = s.integrate(shape,
                           wcs,
                           feed_dict,
                           expr1,
                           xmask=xmask,
                           ymask=xmask).real
    Nl = modlmap**4. / integral / 4.
    bin_edges = np.arange(10, 3000, 40)
    binner = s.bin2D(modlmap, bin_edges)
    cents, nl1d = binner.bin(Nl)

    ## Build HDV TT estimator
    F = s.Ldl1 * s.e('uC_T_T_l1') / s.e('tC_T_T_l1') / s.e('tC_T_T_l2')
    expr1 = f * F
    integral = s.integrate(shape,
                           wcs,
                           feed_dict,
                           expr1,
                           xmask=xmask,
                           ymask=xmask).real
    Nl = modlmap**4. / integral / 4.

    cents, nl1d2 = binner.bin(Nl)

    cents, nl1d3 = binner.bin(
        s.N_l_cross(shape,
                    wcs,
                    feed_dict,
                    "hu_ok",
                    "TT",
                    "hu_ok",
                    "TT",
                    xmask=xmask,
                    ymask=xmask))
    cents, nl1d4 = binner.bin(
        s.N_l_cross(shape,
                    wcs,
                    feed_dict,
                    "hdv",
                    "TT",
                    "hdv",
                    "TT",
                    xmask=xmask,
                    ymask=xmask))
    cents, nl1d5 = binner.bin(
        s.N_l(shape, wcs, feed_dict, "hu_ok", "TT", xmask=xmask, ymask=xmask))
    cents, nl1d6 = binner.bin(
        s.N_l(shape, wcs, feed_dict, "hdv", "TT", xmask=xmask, ymask=xmask))

    clkk = theory.gCl('kk', ells)
    pl = io.Plotter(xyscale='linlog')
    pl.add(cents, nl1d)
    pl.add(cents, nl1d2)
    # pl.add(cents,nl1d3)
    pl.add(cents, nl1d4)
    pl.add(cents, nl1d5)
    pl.add(cents, nl1d6)
    pl.add(ells, clkk)
    pl.done("plcomp.png")
Example #12
0
def test_lens_recon():
    from orphics import lensing, io, cosmology, maps
    from enlib import bench

    deg = 10.
    px = 2.0
    tellmin = 100
    tellmax = 3000
    kellmin = 40
    kellmax = 3000
    grad_cut = None
    bin_width = 80
    beam_arcmin = 0.01
    noise_uk_arcmin = 0.01

    theory = cosmology.default_theory(lpad=30000)
    shape, wcs = s.rect_geometry(width_deg=deg, px_res_arcmin=px)
    flsims = lensing.FlatLensingSims(shape, wcs, theory, beam_arcmin,
                                     noise_uk_arcmin)
    kbeam = flsims.kbeam
    modlmap = enmap.modlmap(shape, wcs)
    fc = maps.FourierCalc(shape, wcs)
    n2d = (noise_uk_arcmin * np.pi / 180. / 60.)**2. / flsims.kbeam**2.
    tmask = s.mask_kspace(shape, wcs, lmin=tellmin, lmax=tellmax)
    kmask = s.mask_kspace(shape, wcs, lmin=kellmin, lmax=kellmax)
    with bench.show("orphics init"):
        qest = lensing.qest(shape,
                            wcs,
                            theory,
                            noise2d=n2d,
                            kmask=tmask,
                            kmask_K=kmask,
                            pol=False,
                            grad_cut=grad_cut,
                            unlensed_equals_lensed=True,
                            bigell=30000)
    bin_edges = np.arange(kellmin, kellmax, bin_width)
    binner = s.bin2D(modlmap, bin_edges)
    i = 0
    unlensed, kappa, lensed, beamed, noise_map, observed = flsims.get_sim(
        seed_cmb=(i, 1),
        seed_kappa=(i, 2),
        seed_noise=(i, 3),
        lens_order=5,
        return_intermediate=True)

    kmap = enmap.fft(observed, normalize="phys")
    # _,kmap,_ = fc.power2d(observed)
    with bench.show("orphics"):
        kkappa = qest.kappa_from_map("TT",
                                     kmap / kbeam,
                                     alreadyFTed=True,
                                     returnFt=True)
    pir2d, kinput = fc.f1power(kappa, kkappa)
    pii2d = fc.f2power(kinput, kinput)
    prr2d = fc.f2power(kkappa, kkappa)
    cents, pir1d = binner.bin(pir2d)
    cents, pii1d = binner.bin(pii2d)
    cents, prr1d = binner.bin(prr2d)

    feed_dict = {}
    cltt = theory.lCl('TT', modlmap)
    feed_dict['uC_T_T'] = theory.lCl('TT', modlmap)
    feed_dict['tC_T_T'] = cltt + n2d
    feed_dict['X'] = kmap / kbeam
    feed_dict['Y'] = kmap / kbeam

    with bench.show("symlens init"):
        Al = s.A_l(shape,
                   wcs,
                   feed_dict,
                   "hdv",
                   "TT",
                   xmask=tmask,
                   ymask=tmask)
    Nl = s.N_l_from_A_l_optimal(shape, wcs, Al)
    with bench.show("symlens"):
        ukappa = s.unnormalized_quadratic_estimator(shape,
                                                    wcs,
                                                    feed_dict,
                                                    "hdv",
                                                    "TT",
                                                    xmask=tmask,
                                                    ymask=tmask)
    nkappa = Al * ukappa

    pir2d2 = fc.f2power(nkappa, kinput)
    cents, pir1d2 = binner.bin(pir2d2)

    cents, Nlkk = binner.bin(qest.N.Nlkk['TT'])
    cents, Nlkk2 = binner.bin(Nl)

    pl = io.Plotter(xyscale='linlog')
    pl.add(cents, pii1d, color='k', lw=3)
    pl.add(cents, pir1d, label='orphics')
    pl.add(cents, pir1d2, label='hdv symlens')
    pl.add(cents, Nlkk, ls="--", label='orphics')
    pl.add(cents, Nlkk2, ls="-.", label='symlens')
    pl.done("ncomp.png")
Example #13
0
                     "/new_mr3f/coadd_srcfree_filtered_%s" % fname,
                     grid=True)
            rmap = maps.filter_map(splits * mask, mfilter)
            io.hplot(rmap,
                     os.environ['WORK'] +
                     "/new_mr3f/splits_srcfree_filtered_%s" % fname,
                     grid=True)

sys.exit()

out_dir = "/scratch/r/rbond/msyriac/data/depot/actsims/inpainted/"

plot_img = lambda x, y, **kwargs: io.plot_img(
    x, os.environ['WORK'] + "/new_mr3f/" + y, cmap='gray', **kwargs)
noise_pix = 60
cmb_theory_fn = lambda s, l: cosmology.default_theory().lCl(s, l)
hole_radius = 9.
res = np.deg2rad(0.5 / 60.)


def plot_cutout(nsplits, cutout, pcutout, ivars, tag="", skip_plots=False):
    pols = ['I', 'Q', 'U']
    retvars = ivars.copy()
    for s in range(nsplits):
        if np.std(cutout[s]) < 1e-3:
            print("Skipping split %d as it seems empty" % s)
            continue
        iname = "%s%s_%s_%s_split_%d_%d" % (tag, season, patch, array, s, sid)
        if not (skip_plots): plot_img(ivars[s, 0], "ivars_%s.png" % iname)
        for p in range(3):
            pol = pols[p]
Example #14
0
def inpaint_map_white(imap,ivar,fn_beam,union_sources_version=None,noise_pix = 40,hole_radius = 6.,plots=False,cache_name=None,verbose=True):
    """

    Inpaints a map under the assumption of inhomogenous but white uncorrelated instrument noise.
    Pros: no products needed other than map-maker outputs of map and ivar, good inpainting despite crappy
    noise model.
    Cons: noise model has to be built for each source, so this is actually quite slow.

    imap -- (npol,Ny,Nx)
    ivar -- (Ny,Nx)
    fn_beam -- lambda ells: beam(ells)
    cache_name -- a unique string identifying the catalog+map/array/frequency/split combination to/from which the geometries are cached
    """
   
    if cache_name is not None:
        cache_name = cache_name + "_catversion_%s" % union_sources_version
        try:
            ras,decs,gtags,pcoords,gdicts = load_cached_inpaint_geometries(cache_name)
            do_geoms = False
            if verbose: print("actsims.inpaint: loaded cached geometries for ", cache_name)
        except:
            if verbose: print("actsims.inpaint: no cached geometries found for ", cache_name, ". Generating and saving...")
            do_geoms = True
    else:
        do_geoms = True

    if do_geoms:
        ras,decs = sints.get_act_mr3f_union_sources(version=union_sources_version)
        cmb_theory_fn = lambda s,l: cosmology.default_theory().lCl(s,l)
        gtags = []
        gdicts = {}
        pcoords = []
        for i,(ra,dec) in enumerate(zip(ras,decs)):
            sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True)
            if sel is None: continue
            civar = ivar[sel]
            if np.any(civar<=0): continue
            modrmap = civar.modrmap()
            modlmap = civar.modlmap()
            res = maps.resolution(civar.shape,civar.wcs)
            cimap = imap[sel]
            if verbose: print("actsims.inpaint: built noise model for source ",i," / ",len(ras))
            scov = pixcov.scov_from_theory(modlmap,cmb_theory_fn,fn_beam,iau=False)
            ncov = pixcov.ncov_from_ivar(civar)
            pcov = scov + ncov
            gdicts[i] = pixcov.make_geometry(hole_radius=np.deg2rad(hole_radius/60.),n=noise_pix,deproject=True,iau=False,pcov=pcov,res=res)
            pcoords.append(np.array((dec,ra)))
            gtags.append(i)
        if len(gtags)>0: 
            pcoords = np.stack(pcoords).swapaxes(0,1)
        if cache_name is not None:
            save_cached_inpaint_geometries(cache_name,ras,decs,gtags,pcoords,gdicts)
            if verbose: print("actsims.inpaint: cached geometries for ",cache_name)

    if len(gtags)>0: result = pixcov.inpaint(imap,pcoords,deproject=True,iau=False,geometry_tags=gtags,geometry_dicts=gdicts,verbose=verbose)

    if plots:
        for i,(ra,dec) in enumerate(zip(ras,decs)):
            sel = reproject.cutout(ivar, ra=np.deg2rad(ra), dec=np.deg2rad(dec), pad=1, corner=False,npix=noise_pix,return_slice=True)
            if sel is None: continue
            civar = ivar[sel]
            if np.any(civar<=0): continue
            modrmap = civar.modrmap()
            modlmap = civar.modlmap()
            res = maps.resolution(civar.shape,civar.wcs)
            cimap = imap[sel]
            for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/cimap_%d_%s" % (p,str(i).zfill(2)))
            mimap = cimap.copy()
            mimap[...,modrmap<np.deg2rad(hole_radius/60.)] = np.nan
            for p in range(3): io.plot_img(mimap[p],os.environ['WORK']+"/masked_cimap_%d_%s" % (p,str(i).zfill(2)))
            cimap = result[sel]
            for p in range(3): io.plot_img(cimap[p],os.environ['WORK']+"/inpainted_cimap_%d_%s" % (p,str(i).zfill(2)))
            
    return result
Example #15
0
def ilc_power(beams,
              noises,
              freqs,
              flux_limits_mJy,
              inv_noise_weighting=False,
              total=False,
              include_fg=True):
    from szar import foregrounds as fg
    beams = np.asarray(beams)
    noises = (np.asarray(noises) * np.pi / 180. / 60.)**2.
    freqs = np.asarray(freqs)
    flux_limits_mJy = np.asarray(flux_limits_mJy)

    ellmax = 25000
    ells = np.arange(0, ellmax, 1)

    def flim(nu):
        return flux_limits_mJy[np.argmin(np.abs(freqs - nu))]

    fdict = {}
    fdict['tsz'] = lambda ells, nu1, nu2: fg.power_tsz(
        ells, nu1, nu2, fill_type="extrapolate")
    fdict['cibc'] = lambda ells, nu1, nu2: fg.power_cibc(ells, nu1, nu2)
    fdict['cibp'] = lambda ells, nu1, nu2: fg.power_cibp(ells, nu1, nu2)
    fdict['radps'] = lambda ells, nu1, nu2: get_radio_power(
        flim(nu1),
        nu1,
        flux_limit_mJy_2=flim(nu2),
        freq_ghz_2=nu2,
        flux_min_mJy=1.6e-2,
        num_flux=10000,
        prefit=True,
        units_Jy_sr=False) + ells * 0

    fdict['ksz'] = lambda ells, nu1, nu2: fg.power_ksz_reion(
        ells, fill_type="extrapolate") + fg.power_ksz_late(
            ells, fill_type="extrapolate")

    kbeams = []
    for beam in beams:
        kbeams.append(maps.gauss_beam(ells, beam))

    theory = cosmology.default_theory(lpad=ellmax)
    cltt = theory.lCl("TT", ells)

    components = ['cibc', 'tsz', 'ksz', 'radps', 'cibp'] if include_fg else []

    cov = np.rollaxis(
        maps.ilc_cov(ells,
                     cltt,
                     kbeams,
                     freqs,
                     noises,
                     components,
                     fdict=fdict,
                     ellmaxes=None,
                     data=False,
                     fgmax=None,
                     narray=None,
                     noise_only=False), 2, 0)
    if inv_noise_weighting:
        ncov = np.rollaxis(
            maps.ilc_cov(ells,
                         cltt,
                         kbeams,
                         freqs,
                         noises,
                         components,
                         fdict=fdict,
                         ellmaxes=None,
                         data=False,
                         fgmax=None,
                         narray=None,
                         noise_only=True), 2, 0)
        ninv = np.linalg.inv(ncov)
        ntot = np.sum(ninv, axis=(-2, -1))
        nout = np.sum(np.einsum('lij,ljk->lik',
                                np.einsum('lij,ljk->lik', ninv, cov), ninv),
                      axis=(-2, -1)) / ntot**2
    else:
        cinv = np.rollaxis(np.linalg.inv(cov), 0, 3)
        nout = maps.silc_noise(cinv, response=None)
    csub = 0 if total else cltt
    nell = np.nan_to_num(nout - csub)
    nell[ells < 2] = 0

    return ells, nell