示例#1
0
 def fit_minuit_1(tau, As, r):
     cls_ana = get_spectrum_camb(lmax=lmax, tau=tau, As=As, r=r,
                                 isDl=False) * clscale
     Cls_ana = lh.cls2Cls(cls_ana)
     lk = n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid)
     print('tau = %e, As = %e, r = %e, lk = %e' % (tau, As, r, lk))
     return lk
示例#2
0
def plot_spec(parname, pars, lmax=1000):
    ell = np.arange(lmax + 1)

    if (parname == 'As'):
        pars *= 1e-9

    plt.figure()
    kwargs = {}
    scaleEE = []
    scaleBB = []

    for par in pars:
        kwargs['As'] = par
        kwargs['tau'] = 0.0522
        kwargs['r'] = 0.01
        dls = sp.get_spectrum_camb(lmax, **kwargs)[:3].T
        scaleEE.append(dls[2, 1])
        scaleBB.append(dls[2, 2])
        print(scaleEE[-1] / scaleBB[-1])
        plt.loglog(ell[2:], dls[2:])

    plt.xlabel('Multipolt moment, $l$')
    plt.ylabel('$D_l (\mu K)$')

    return scaleEE, scaleBB
示例#3
0
def doit():
    nside = 512
    lmax = 3*nside-1
    
    cl = get_spectrum_camb(lmax=2000, isDl=False) 

    # maps to be tested
    m = hp.synfast(cl, nside=nside, new=True) 
    m_top = m_bot = np.zeros(m.shape)
    m_top[:,:int(len(m))] = m[:,:int(len(m))]
    m_bot[:,int(len(m)):] = m[:,int(len(m)):]

    hp.mollview(m[0])
    hp.mollview(m_top[0])
    hp.mollview(m_bot[0])

    plt.show()

    cl_full = hp.anafast(m, lmax=lmax)
示例#4
0
def doit():
    fnmask = [
        "./masks/COM_Mask_CMB-common-Mask-Int_2048_R3.00.fits",
        "./masks/COM_Mask_CMB-common-Mask-Pol_2048_R3.00.fits",
        "./masks/HFI_Mask_GalPlane-apo0_2048_R2.00.fits",
        "./masks/HFI_Mask_GalPlane-apo2_2048_R2.00.fits",
        "./masks/HFI_Mask_GalPlane-apo5_2048_R2.00.fits",
        "./masks/HFI_Mask_PointSrc_2048_R2.00.fits",
        "./masks/hitMap_GB145_1024.fits",
        "./masks/hitMap_GB220_1024.fits",
    ]

    cl = get_spectrum_camb(lmax=2000, r=0.01, isDl=False, CMB_unit='muK')

    mask = hp.read_map(fnmask[1])
    mask_GB = cs.hitmap2mask(hp.read_map(fnmask[-1]))
    hp.mollview(mask)

    rseed = 42
    test_single(cl, mask, rseed, lmin=0, lmax=100, nside=256)
    #test_ensemble(cl, mask, rseed, lmin=30, lmax=100, nside=128, nsample=100)

    plt.show()
示例#5
0
def compare_cmb_noise():
    nside = 1024
    lmax = 3 * nside - 1  #1500#nside*3 - 1
    cl_r01 = spectrum.get_spectrum_camb(lmax, r=0.01)
    cl_r001 = spectrum.get_spectrum_camb(lmax, r=0.001)
    nl_wp10 = spectrum.get_spectrum_whitenoise(
        lmax,
        wp=10,
        bw=30,
    )  # 1.5*60)
    nl_wp2 = spectrum.get_spectrum_whitenoise(
        lmax,
        wp=2,
        bw=30,
    )  #1.5*60)

    print(cl_r01.shape)
    print(cl_r001.shape)
    print(nl_wp10.shape)
    print(nl_wp2.shape)

    ell = np.arange(lmax + 1)
    plt.loglog(ell, cl_r01[:3].T * 1e12)
    plt.loglog(ell, cl_r001[:3].T * 1e12)
    plt.loglog(ell, nl_wp10[:3].T * 1e12)
    plt.loglog(ell, nl_wp2[:3].T * 1e12)

    cls = utils.dl2cl(nl_wp10)

    m = hp.synfast(cls, nside=nside, new=True, fwhm=0.0 * np.pi / 180)
    hp.mollview(m[0])

    std_wnT = np.std(m[0])
    std_wnQ = np.std(m[1])
    std_wnU = np.std(m[2])
    std_wn = np.std(m)

    #mr = np.random.random(len(m[0]))
    mrT = np.random.normal(size=len(m[0]), scale=std_wnT)
    mrQ = np.random.normal(size=len(m[0]), scale=std_wnQ)
    mrU = np.random.normal(size=len(m[0]), scale=std_wnU)
    mr = [mrT, mrQ, mrU]

    std_wnTr = np.std(mr[0])
    std_wnQr = np.std(mr[1])
    std_wnUr = np.std(mr[2])
    std_wnr = np.std(mr)

    print('Standard deviations of synthesized noise maps')
    print(std_wnT, std_wnQ, std_wnU)
    print(std_wn)
    print('Standard deviations of Gaussian random maps')
    print(std_wnTr, std_wnQr, std_wnUr)
    print(std_wnr)

    cls_rec = hp.anafast(m, lmax=lmax)
    cls_ran = hp.anafast(mr, lmax=lmax)

    names = ['TT', 'EE', 'BB']
    for i, name in enumerate(names):
        plt.figure()
        plt.loglog(ell, utils.cl2dl(cls)[i].T)
        plt.loglog(ell,
                   utils.cl2dl(cls_rec)[i].T,
                   '*',
                   label='Synthesized map')
        plt.loglog(ell,
                   utils.cl2dl(cls_ran)[i].T,
                   '+',
                   label='Gaussian random number map')
        plt.title(name)

    plt.show()
示例#6
0
def test_n2logLf_TEB():
    K2uK = 1e12
    clscale = K2uK * 1.0
    cls_fid = get_spectrum_camb(lmax, isDl=False) * clscale

    cls_syn = get_spectrum_camb(
        lmax, tau=0.0522, As=2.092e-9, r=0.01, isDl=False) * clscale
    map_syn = hp.synfast(cls_syn, nside=nside, new=True)
    cls_est = hp.anafast(map_syn, lmax=lmax)

    cls_ana = get_spectrum_camb(lmax, isDl=False) * clscale

    Cls_ana = lh.cls2Cls(cls_ana)
    Cls_est = lh.cls2Cls(cls_est)
    inv_Cls_fid, det_Cls_fid = lh.invdet_fid(cls_fid)

    n2logLf = n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid)

    print('Likelihood for scale %e = %e' % (clscale, n2logLf))

    def fit_minuit_1(tau, As, r):
        cls_ana = get_spectrum_camb(lmax=lmax, tau=tau, As=As, r=r,
                                    isDl=False) * clscale
        Cls_ana = lh.cls2Cls(cls_ana)
        lk = n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid)
        print('tau = %e, As = %e, r = %e, lk = %e' % (tau, As, r, lk))
        return lk

    tau0 = 0.0522
    tau_limit = (0.02, 0.08)
    As0 = 2.1e-9
    As_limit = (1.5e-9, 2.5e-9)
    r0 = 0.01
    r_limit = (0.0, 0.4)
    m = Minuit(fit_minuit_1,
               tau=tau0,
               As=As0,
               r=r0,
               limit_tau=tau_limit,
               limit_As=As_limit,
               limit_r=r_limit)

    st = time.time()
    res = m.migrad()
    print('Elapsed time for migrad: %fs' % (time.time() - st))

    st = time.time()
    res = m.hesse()
    print('Elapsed time for hesse: %fs' % (time.time() - st))

    st = time.time()
    #res = m.minos()
    print('Elapsed time for minos: %fs' % (time.time() - st))

    plt.figure()
    m.draw_profile('tau')
    plt.figure()
    m.draw_profile('As')
    plt.figure()
    m.draw_profile('r')

    tau_min = m.values['tau']
    tau_err = m.errors['tau']
    cls_min = get_spectrum_camb(lmax=lmax, tau=tau_min, isDl=False) * clscale
    cls_upp = get_spectrum_camb(lmax=lmax, tau=tau_min + tau_err,
                                isDl=False) * clscale
    cls_low = get_spectrum_camb(lmax=lmax, tau=tau_min - tau_err,
                                isDl=False) * clscale

    ell = np.arange(len(cls_est[0]))
    plt.figure()
    plt.loglog(ell, cl2dl(cls_est[:3].T), '*')
    plt.loglog(ell, cl2dl(cls_syn[:3].T), '--', linewidth=1.0)
    plt.loglog(ell, cl2dl(cls_min[:3].T), '-', linewidth=2.0)
    plt.loglog(ell, cl2dl(cls_upp[:3].T), '--', linewidth=0.5)
    plt.loglog(ell, cl2dl(cls_low[:3].T), '--', linewidth=0.5)

    pprint(res)
    plt.show()
示例#7
0
def test_n2logLf_EB(As_in=2.092e-9,
                    tau_in=0.0522,
                    r_in=0.01,
                    fix_As=False,
                    fix_tau=False,
                    fix_r=False):
    K2uK = 1e12
    clscale = K2uK * 1.0
    cls_fid = get_spectrum_camb(lmax, tau=0.05, As=2e-9, r=0.05,
                                isDl=False) * clscale

    cls_syn = get_spectrum_camb(lmax, tau=tau_in, As=As_in, r=r_in,
                                isDl=False) * clscale
    map_syn = hp.synfast(cls_syn, nside=nside, new=True)
    cls_est = hp.anafast(map_syn, lmax=lmax)

    cls_ana = get_spectrum_camb(lmax, isDl=False) * clscale

    Cls_ana = lh.cls2Cls(cls_ana, T=False)
    Cls_est = lh.cls2Cls(cls_est, T=False)
    Cls_syn = lh.cls2Cls(cls_syn, T=False)
    inv_Cls_fid, det_Cls_fid = lh.invdet_fid(cls_fid, T=False)

    n2logLf = lh.n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid)

    print('Likelihood for scale %e = %e' % (clscale, n2logLf))

    def fit_minuit_1(tau, As, r):
        cls_ana = get_spectrum_camb(lmax=lmax, tau=tau, As=As, r=r,
                                    isDl=False) * clscale
        Cls_ana = lh.cls2Cls(cls_ana, T=False)
        lk = lh.n2logL_approx_TEB(Cls_ana, Cls_est, inv_Cls_fid, det_Cls_fid)
        print('tau = %e, As = %e, r = %e, lk = %e' % (tau, As, r, lk))
        return lk

    tau0 = tau_in * 0.99
    tau_sig = 2e-3
    tau_limit = tau0 + 5 * np.array((-tau_sig, tau_sig))

    As0 = As_in * 0.99
    As_sig = 1e-10
    As_limit = As0 + 5 * np.array((-As_sig, As_sig))

    r0 = r_in * 0.99
    r_sig = 1.5e-3
    r_limit = r0 + 5 * np.array((-r_sig, r_sig))

    if fix_tau:
        tau0 = tau_in

    if fix_As:
        As0 = As_in

    if fix_r:
        r0 = r_in

    # fit fnc check
    print(fit_minuit_1(tau0, As0, r0))

    m = Minuit(fit_minuit_1,
               tau=tau0,
               As=As0,
               r=r0,
               limit_tau=tau_limit,
               limit_As=As_limit,
               limit_r=r_limit,
               fix_tau=fix_tau,
               fix_As=fix_As,
               fix_r=fix_r)

    st = time.time()
    res = m.migrad()
    print('Elapsed time for migrad: %fs' % (time.time() - st))

    st = time.time()
    res_h = m.hesse()
    print('Elapsed time for hesse: %fs' % (time.time() - st))

    st = time.time()
    res_m = m.minos()
    print('Elapsed time for minos: %fs' % (time.time() - st))
    ''' 
    plt.figure()
    m.draw_profile('tau')
    plt.figure()
    m.draw_profile('As')
    plt.figure()
    m.draw_profile('r')

    tau_min = m.values['tau']
    tau_err = m.errors['tau']
    cls_min = get_spectrum_camb(lmax=lmax, tau=tau_min, isDl=False) * clscale
    cls_upp = get_spectrum_camb(lmax=lmax, tau=tau_min + tau_err, isDl=False) * clscale
    cls_low = get_spectrum_camb(lmax=lmax, tau=tau_min - tau_err, isDl=False) * clscale

    ell = np.arange(len(cls_est[0]))
    plt.figure()
    plt.loglog(ell, cl2dl(cls_est[:3].T), '*')
    plt.loglog(ell, cl2dl(cls_syn[:3].T), '--', linewidth=1.0)
    plt.loglog(ell, cl2dl(cls_min[:3].T), '-', linewidth=2.0)
    plt.loglog(ell, cl2dl(cls_upp[:3].T), '--', linewidth=0.5)
    plt.loglog(ell, cl2dl(cls_low[:3].T), '--', linewidth=0.5)
    '''

    #pprint(res)
    #plt.show()
    return res, res_h, res_m
示例#8
0
def main():
    st = time.time()
    maskGalactic = hp.read_map('./masks/HFI_Mask_GalPlane-apo0_2048_R2.00.fits', field=3)
    maskPoint = hp.read_map('./masks/HFI_Mask_PointSrc_2048_R2.00.fits')
    hitGB = hp.read_map('./masks/hitMap_GB220_1024.fits')
    print ('time for reading masks', time.time() - st)

    st = time.time()
    maskGB_equ = hitmap2mask(hitGB)
    maskGB_gal = EQU2GAL(maskGB_equ)
    maskTr_equ = np.full(maskGB_equ.shape, 1)
    maskGalactic = hp.ud_grade(maskGalactic, 1024)
    print ('time for manipulating masks', time.time() - st)


    m_comb = maskGB_gal * maskGalactic
    m_comb_equ = GAL2EQU(m_comb)

    """
    hp.mollview(maskGalactic)
    hp.mollview(maskPoint)
    hp.mollview(maskGB_equ)
    hp.mollview(maskGB_gal)
    hp.mollview(m_comb)
    hp.mollview(m_comb_equ)
    """

    mask = maskGalactic
    mask = maskGB_equ

    st = time.time()
    Wl = hp.anafast(mask)
    print ('time for anafast Wl', time.time() - st)

    st = time.time()
    Wl_Tr = hp.anafast(maskTr_equ)
    print ('time for anafast Wl_Tr', time.time() - st)

    #
    Cl_camb = get_spectrum_camb(lmax=1000, isDl=False)

    np.random.seed(42)
    m = hp.synfast(Cl_camb, nside=1024, new=True)
    #
    Cl_full = hp.anafast(m)[:4]

    mm = hp.ma(m)
    mm.mask = np.logical_not(mask)

    #
    Cl_gbcut = hp.anafast(mm.filled())

    
    st = time.time()
    Mll = coupling_matrix(Wl)
    print ('time to calculate coupling matrix', time.time() - st)

    st = time.time()
    Mll_Tr = coupling_matrix(Wl_Tr)
    print ('time to calculate coupling matrix trivial', time.time() - st)

    plt.matshow(np.log10(Mll))
    plt.matshow(np.log10(Mll_Tr))
    
    st = time.time()
    Mlli = np.linalg.pinv(Mll)
    print ('time to calculate inverting coupling matrix', time.time() - st)
    plt.matshow(np.log10(Mlli))

    # 
    Cl_ana = Cl_camb[:,:100]
    Cl_ana = np.concatenate(Cl_ana, 0)
    Cl_ana_gbcut = np.dot(Mll, Cl_ana)
    Cl_ana_gbcut_inv = np.dot(Mlli, Cl_ana_gbcut)
    Cl_ana_gbcut = Cl_ana_gbcut.reshape(4, 100) #len(Cls_gbcut)/4)
    Cl_ana_gbcut_inv = Cl_ana_gbcut_inv.reshape(4, 100)
    Cl_ana_Tr = np.dot(Mll_Tr, Cl_ana)
    Cl_ana_Tr = Cl_ana_Tr.reshape(4, 100) #len(Cls_gbcut)/4)

    Cl_gbcut_tmp = Cl_gbcut[:4,:100]
    Cl_gbcut_tmp = np.concatenate(Cl_gbcut_tmp, 0)
    Cl_gbcut_inv = np.dot(Mlli, Cl_gbcut_tmp)
    Cl_gbcut_inv = Cl_gbcut_inv.reshape(4, 100)

    ell = np.arange(2, 100)

    plt.figure()
    plt.loglog(ell, cl2dl(Cl_gbcut)[:3,2:100].T, '*')
    plt.loglog(ell, cl2dl(Cl_ana_gbcut)[:3, 2:100].T)
    plt.xlabel('Multipole moment, $l$')
    plt.ylabel('$\l(l+1)/2\pi C_l (K^2)$')

    plt.figure()
    plt.loglog(ell, cl2dl(Cl_full)[:3,2:100].T, '*')
    plt.loglog(ell, cl2dl(Cl_camb)[:3,2:100].T) 
    plt.loglog(ell, cl2dl(Cl_ana_Tr)[:3,2:100].T) 
    plt.xlabel('Multipole moment, $l$')
    plt.ylabel('$\l(l+1)/2\pi C_l (K^2)$')

    plt.figure()
    plt.loglog(ell, cl2dl(Cl_gbcut_inv)[:3, 2:100].T, '*')
    plt.loglog(ell, cl2dl(Cl_ana_gbcut_inv)[:3, 2:100].T)
    plt.xlabel('Multipole moment, $l$')
    plt.ylabel('$\l(l+1)/2\pi C_l (K^2)$')

    plt.show()