win["Pa"] = so_map.read_map("%s/window_%s.fits" % (window_dir, na))
    win["Pb"] = so_map.read_map("%s/window_%s.fits" % (window_dir, nb))
    win["Pc"] = so_map.read_map("%s/window_%s.fits" % (window_dir, nc))
    win["Pd"] = so_map.read_map("%s/window_%s.fits" % (window_dir, nd))

    coupling = so_cov.cov_coupling_spin0and2_simple(win, lmax, niter=niter)

    analytic_cov = np.zeros((4 * nbins, 4 * nbins))

    # TaTbTcTd
    M_00 = coupling["TaTcTbTd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all,
                                             nl_all, "TTTT")
    M_00 += coupling["TaTdTbTc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all,
                                              nl_all, "TTTT")
    analytic_cov[0 * nbins:1 * nbins,
                 0 * nbins:1 * nbins] = so_cov.bin_mat(M_00, binning_file,
                                                       lmax)

    # TaEbTcEd
    M_11 = coupling["TaTcPbPd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all,
                                             nl_all, "TTEE")
    M_11 += coupling["TaPdPbTc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all,
                                              nl_all, "TEET")
    analytic_cov[1 * nbins:2 * nbins,
                 1 * nbins:2 * nbins] = so_cov.bin_mat(M_11, binning_file,
                                                       lmax)

    # EaTbEcTd
    M_22 = coupling["PaPcTbTd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all,
                                             nl_all, "EETT")
    M_22 += coupling["PaTdTbPc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all,
                                              nl_all, "ETTE")
Example #2
0
def covariance_element(coupling, id_element, ns, ps_all, nl_all, binning_file, mbb_inv_ab, mbb_inv_cd):
    
    na, nb, nc, nd = id_element
    
    lmax = coupling["TaTcTbTd"].shape[0]
    bin_lo, bin_hi, bin_c, bin_size = pspy_utils.read_binning_file(binning_file, lmax)
    nbins = len(bin_hi)
    analytic_cov = np.zeros((4*nbins, 4*nbins))

    # TaTbTcTd
    M_00 = coupling["TaTcTbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TTTT")
    M_00 += coupling["TaTdTbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TTTT")
    analytic_cov[0*nbins:1*nbins, 0*nbins:1*nbins] = so_cov.bin_mat(M_00, binning_file, lmax)

    # TaEbTcEd
    M_11 = coupling["TaTcPbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TTEE")
    M_11 += coupling["TaPdPbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TEET")
    analytic_cov[1*nbins:2*nbins, 1*nbins:2*nbins] = so_cov.bin_mat(M_11, binning_file, lmax)

    # EaTbEcTd
    M_22 = coupling["PaPcTbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "EETT")
    M_22 += coupling["PaTdTbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "ETTE")
    analytic_cov[2*nbins:3*nbins, 2*nbins:3*nbins] = so_cov.bin_mat(M_22, binning_file, lmax)

    # EaEbEcEd
    M_33 = coupling["PaPcPbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "EEEE")
    M_33 += coupling["PaPdPbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "EEEE")
    analytic_cov[3*nbins:4*nbins, 3*nbins:4*nbins] = so_cov.bin_mat(M_33, binning_file, lmax)

    # TaTbTcEd
    M_01 = coupling["TaTcTbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TTTE")
    M_01 += coupling["TaPdTbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TETT")
    analytic_cov[0*nbins:1*nbins, 1*nbins:2*nbins] = so_cov.bin_mat(M_01, binning_file, lmax)

    # TaTbEcTd
    M_02 = coupling["TaPcTbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TETT")
    M_02 += coupling["TaTdTbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TTTE")
    analytic_cov[0*nbins:1*nbins, 2*nbins:3*nbins] = so_cov.bin_mat(M_02, binning_file, lmax)

    # TaTbEcEd
    M_03 = coupling["TaPcTbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TETE")
    M_03 += coupling["TaPdTbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TETE")
    analytic_cov[0*nbins:1*nbins, 3*nbins:4*nbins] = so_cov.bin_mat(M_03, binning_file, lmax)

    # TaEbEcTd
    M_12 = coupling["TaPcPbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TEET")
    M_12 += coupling["TaTdPbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TTEE")
    analytic_cov[1*nbins:2*nbins, 2*nbins:3*nbins] = so_cov.bin_mat(M_12, binning_file, lmax)

    # TaEbEcEd
    M_13 = coupling["TaPcPbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TEEE")
    M_13 += coupling["TaPdPbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TEEE")
    analytic_cov[1*nbins:2*nbins, 3*nbins:4*nbins] = so_cov.bin_mat(M_13, binning_file, lmax)

    # EaTbEcEd
    M_23 = coupling["PaPcTbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "EETE")
    M_23 += coupling["PaPdTbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "EETE")
    analytic_cov[2*nbins:3*nbins, 3*nbins:4*nbins] = so_cov.bin_mat(M_23, binning_file, lmax)

    # TaEbTcTd
    M_10 = coupling["TaTcPbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "TTET")
    M_10 += coupling["TaTdPbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "TTET")
    analytic_cov[1*nbins:2*nbins, 0*nbins:1*nbins] = so_cov.bin_mat(M_10, binning_file, lmax)

    # EaTbTcTd
    M_20 = coupling["PaTcTbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "ETTT")
    M_20 += coupling["PaTdTbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "ETTT")
    analytic_cov[2*nbins:3*nbins, 0*nbins:1*nbins] = so_cov.bin_mat(M_20, binning_file, lmax)

    # EaEbTcTd
    M_30 = coupling["PaTcPbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "ETET")
    M_30 += coupling["PaTdPbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "ETET")
    analytic_cov[3*nbins:4*nbins, 0*nbins:1*nbins] = so_cov.bin_mat(M_30, binning_file, lmax)

    # EaTbTcEd
    M_21 = coupling["PaTcTbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "ETTE")
    M_21 += coupling["PaPdTbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "EETT")
    analytic_cov[2*nbins:3*nbins, 1*nbins:2*nbins] = so_cov.bin_mat(M_21, binning_file, lmax)

    # EaEbTcEd
    M_31 = coupling["PaTcPbPd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "ETEE")
    M_31 += coupling["PaPdPbTc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "EEET")
    analytic_cov[3*nbins:4*nbins, 1*nbins:2*nbins] = so_cov.bin_mat(M_31, binning_file, lmax)

    # EaEbEcTd
    M_32 = coupling["PaPcPbTd"] * chi(na, nc, nb, nd, ns, ps_all, nl_all, "EEET")
    M_32 += coupling["PaTdPbPc"] * chi(na, nd, nb, nc, ns, ps_all, nl_all, "ETEE")
    analytic_cov[3*nbins:4*nbins, 2*nbins:3*nbins] = so_cov.bin_mat(M_32, binning_file, lmax)

    mbb_inv_ab = so_cov.extract_TTTEEE_mbb(mbb_inv_ab)
    mbb_inv_cd = so_cov.extract_TTTEEE_mbb(mbb_inv_cd)

    analytic_cov = np.dot(np.dot(mbb_inv_ab, analytic_cov), mbb_inv_cd.T)
    
    return analytic_cov
        for j, (Y, Z) in enumerate(speclist):

            id0 = W + "a" + Y + "c"
            id1 = X + "b" + Z + "d"
            id2 = W + "a" + Z + "d"
            id3 = X + "b" + Y + "c"

            M = coupling[id0.replace("E", "P") +
                         id1.replace("E", "P")] * so_cov.chi(
                             na, nc, nb, nd, ns, ps_all, nl_all, W + Y + X + Z)
            M += coupling[id2.replace("E", "P") +
                          id3.replace("E", "P")] * so_cov.chi(
                              na, nd, nb, nc, ns, ps_all, nl_all,
                              W + Z + X + Y)
            analytic_cov[i * nbins:(i + 1) * nbins,
                         j * nbins:(j + 1) * nbins] = so_cov.bin_mat(
                             M, binning_file, lmax)

    mbb_inv_ab, Bbl_ab = so_mcm.read_coupling(prefix="%s/%sx%s" %
                                              (mcm_dir, na, nb),
                                              spin_pairs=spin_pairs)
    mbb_inv_ab = so_cov.extract_TTTEEE_mbb(mbb_inv_ab)

    mbb_inv_cd, Bbl_cd = so_mcm.read_coupling(prefix="%s/%sx%s" %
                                              (mcm_dir, nc, nd),
                                              spin_pairs=spin_pairs)
    mbb_inv_cd = so_cov.extract_TTTEEE_mbb(mbb_inv_cd)

    #transpose = analytic_cov.copy().T
    #transpose[analytic_cov != 0] = 0
    #analytic_cov += transpose
Example #4
0
def get_covariance(window,
                   lmax,
                   spec_name_list,
                   ps_dict,
                   binning_file,
                   error_method="master",
                   spectra=None,
                   l_thres=None,
                   l_toep=None,
                   mbb_inv=None,
                   compute_T_only=False):
    """Compute the covariance matrix of the power spectrum in the patch

    Parameters
    ----------

    window: so_map
      the window function of the patch
    lmax: integer
      the maximum multipole to consider for the spectra computation
    spec_name_list:  list
      the list of  power spectra
      For example : [split0xsplit0,split0xsplit1,split1xsplit1]
      note that for computing the error on PS(split0xsplit1) we need PS(split0xsplit0), PS(split0xsplit1), PS(split1xsplit1)
    ps_dict: dict
      a dict containing all power spectra
    binning_file: text file
      a binning file with three columns bin low, bin high, bin mean
      note that either binning_file or bin_size should be provided
    error_method: string
      the method for the computation of error
      can be "master" or "knox" for now
   approx_coupling: dict
   mbb_inv: 2d array
     the inverse mode coupling matrix, not in use for 2dflat
   compute_T_only: boolean
     True to compute only T spectra

    """

    bin_lo, bin_hi, bin_c, bin_size = pspy_utils.read_binning_file(
        binning_file, lmax)
    n_bins = len(bin_hi)

    fsky = enmap.area(window.data.shape, window.data.wcs) / 4. / np.pi
    fsky *= np.mean(window.data)

    cov_dict = {}

    if error_method == "Knox":
        for name in spec_name_list:
            m1, m2 = name.split("x")
            cov_dict[name] = {}
            for spec in spectra:
                X, Y = spec
                prefac = 1 / ((2 * bin_c + 1) * fsky * bin_size)
                cov_dict[name][X + Y] = np.diag(
                    prefac *
                    (ps_dict["%sx%s" %
                             (m1, m1)][X + X] * ps_dict["%sx%s" %
                                                        (m2, m2)][Y + Y] +
                     ps_dict["%sx%s" % (m1, m2)][X + Y]**2))

    elif error_method == "master":
        print("compute master error")
        if mbb_inv is None:
            raise ValueError("Missing 'mbb_inv' argument")
        if not compute_T_only:
            mbb_inv = mbb_inv["spin0xspin0"]
        coupling_dict = so_cov.cov_coupling_spin0(window,
                                                  lmax,
                                                  niter=0,
                                                  l_thres=l_thres,
                                                  l_toep=l_toep)
        coupling = so_cov.bin_mat(coupling_dict["TaTcTbTd"], binning_file,
                                  lmax)

        for name in spec_name_list:
            m1, m2 = name.split("x")
            cov_dict[name] = {}
            for spec in spectra:
                X, Y = spec
                cov_dict[name][X + Y] = so_cov.symmetrize(
                    ps_dict["%sx%s" % (m1, m1)][X + X]) * so_cov.symmetrize(
                        ps_dict["%sx%s" % (m2, m2)][Y + Y])
                cov_dict[name][X + Y] += so_cov.symmetrize(
                    ps_dict["%sx%s" % (m1, m2)][X + Y]**2)
                cov_dict[name][X + Y] *= coupling
                cov_dict[name][X + Y] = np.dot(
                    np.dot(mbb_inv, cov_dict[name][X + Y]), mbb_inv.T)

    else:
        cov_dict = None

    return cov_dict
Example #5
0
    win["Ta"] = so_map.read_map("%s/window_T_%s-hm1.fits"%(windows_dir, na))
    win["Tb"] = so_map.read_map("%s/window_T_%s-hm2.fits"%(windows_dir, nb))
    win["Tc"] = so_map.read_map("%s/window_T_%s-hm1.fits"%(windows_dir, nc))
    win["Td"] = so_map.read_map("%s/window_T_%s-hm2.fits"%(windows_dir, nd))
    win["Pa"] = so_map.read_map("%s/window_P_%s-hm1.fits"%(windows_dir, na))
    win["Pb"] = so_map.read_map("%s/window_P_%s-hm2.fits"%(windows_dir, nb))
    win["Pc"] = so_map.read_map("%s/window_P_%s-hm1.fits"%(windows_dir, nc))
    win["Pd"] = so_map.read_map("%s/window_P_%s-hm2.fits"%(windows_dir, nd))

    coupling = so_cov.cov_coupling_spin0and2_simple(win, lmax, niter=niter, planck=True)
    analytic_cov = np.zeros((4*nbins, 4*nbins))

    # EaEbEcEd
    M_00 = coupling["PaPcPbPd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all, nl_all, "EEEE")
    M_00 += coupling["PaPdPbPc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all, nl_all, "EEEE")
    analytic_cov[0*nbins:1*nbins, 0*nbins:1*nbins] = so_cov.bin_mat(M_00, binning_file, lmax)
    
    # EaBbEcBd
    M_11 = coupling["PaPcPbPd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all, nl_all, "EEBB")
    M_11 += coupling["PaPdPbPc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all, nl_all, "EBBE")
    analytic_cov[1*nbins:2*nbins, 1*nbins:2*nbins] = so_cov.bin_mat(M_11, binning_file, lmax)
    
    # BaEbBcEd
    M_22 = coupling["PaPcPbPd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all, nl_all, "BBEE")
    M_22 += coupling["PaPdPbPc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all, nl_all, "BEEB")
    analytic_cov[2*nbins:3*nbins, 2*nbins:3*nbins] = so_cov.bin_mat(M_22, binning_file, lmax)
    
    # BaBbBcBd
    M_33 = coupling["PaPcPbPd"] * so_cov.chi(na, nc, nb, nd, ns, ps_all, nl_all, "BBBB")
    M_33 += coupling["PaPdPbPc"] * so_cov.chi(na, nd, nb, nc, ns, ps_all, nl_all, "BBBB")
    analytic_cov[3*nbins:4*nbins, 3*nbins:4*nbins] = so_cov.bin_mat(M_33, binning_file, lmax)
Example #6
0
def covariance_element(coupling, id_element, ns, ps_all, nl_all, binning_file,
                       mbb_inv_ab, mbb_inv_cd):
    """
    This routine deserves some explanation
    We want to compute the covariance between two power spectra
    C1 = Wa * Xb, C2 =  Yc * Zd
    Here W, X, Y, Z can be either T or E and a,b,c,d will be an index
    corresponding to the survey and array we consider so for example a = s17_pa5_150 or a = dr6_pa4_090
    The formula for the analytic covariance of C1, C2 is given by
    Cov( Wa * Xb,  Yc * Zd) = < Wa Yc> <Xb Zd>  + < Wa Zd> <Xb Yc> (this is just from the wick theorem)
    In practice we need to include the effect of the mask (so we have to introduce the coupling dict D)
    and we need to take into account that we use only the cross power spectra, that is why we use the chi function
    Cov( Wa * Xb,  Yc * Zd) = D(Wa*Yc,Xb Zd) chi(Wa,Yc,Xb Zd) +  D(Wa*Zd,Xb*Yc) chi(Wa,Zd,Xb,Yc)
    
    Parameters
    ----------
    coupling : dictionnary
      a dictionnary that countains the coupling terms arising from the window functions
    id_element : list
      a list of the form [a,b,c,d] where a = dr6_pa4_090, etc, this identify which pair of power spectrum we want the covariance of
    ns: dict
      this dictionnary contains the number of split we consider for each of the survey
    ps_all: dict
      this dict contains the theoretical best power spectra, convolve with the beam for example
      ps["dr6&pa5_150", "dr6&pa4_150", "TT"] = bl_dr6_pa5_150 * bl_dr6_pa4_150 * (Dl^{CMB}_TT + fg_TT)
    nl_all: dict
      this dict contains the estimated noise power spectra, note that it correspond to the noise power spectrum per split
      e.g nl["dr6&pa5_150", "dr6&pa4_150", "TT"]
    binning_file:
      a binning file with three columns bin low, bin high, bin mean
    mbb_inv_ab and mbb_inv_cd:
      the inverse mode coupling matrices corresponding to the C1 = Wa * Xb and C2 =  Yc * Zd power spectra
    """

    na, nb, nc, nd = id_element

    lmax = coupling["TaTcTbTd"].shape[0]
    bin_lo, bin_hi, bin_c, bin_size = pspy_utils.read_binning_file(
        binning_file, lmax)
    nbins = len(bin_hi)

    speclist = ["TT", "TE", "ET", "EE"]
    nspec = len(speclist)
    analytic_cov = np.zeros((nspec * nbins, nspec * nbins))
    for i, (W, X) in enumerate(speclist):
        for j, (Y, Z) in enumerate(speclist):

            id0 = W + "a" + Y + "c"
            id1 = X + "b" + Z + "d"
            id2 = W + "a" + Z + "d"
            id3 = X + "b" + Y + "c"

            M = coupling[id0.replace("E", "P") + id1.replace("E", "P")] * chi(
                na, nc, nb, nd, ns, ps_all, nl_all, W + Y + X + Z)
            M += coupling[id2.replace("E", "P") + id3.replace("E", "P")] * chi(
                na, nd, nb, nc, ns, ps_all, nl_all, W + Z + X + Y)
            analytic_cov[i * nbins:(i + 1) * nbins,
                         j * nbins:(j + 1) * nbins] = so_cov.bin_mat(
                             M, binning_file, lmax)

    mbb_inv_ab = so_cov.extract_TTTEEE_mbb(mbb_inv_ab)
    mbb_inv_cd = so_cov.extract_TTTEEE_mbb(mbb_inv_cd)

    analytic_cov = np.dot(np.dot(mbb_inv_ab, analytic_cov), mbb_inv_cd.T)

    return analytic_cov
Example #7
0
def get_covariance(
    window,
    lmax,
    spec_name_list,
    ps_dict,
    binning_file,
    error_method="master",
    spectra=None,
    l_exact=None,
    l_band=None,
    l_toep=None,
    mbb_inv=None,
    compute_T_only=False,
    transfer_function=None,
):
    """Compute the covariance matrix of the power spectrum in the patch

     Parameters
     ----------

     window: so_map
       the window function of the patch
     lmax: integer
       the maximum multipole to consider for the spectra computation
     spec_name_list:  list
       the list of  power spectra
       For example : [split0xsplit0,split0xsplit1,split1xsplit1]
       note that for computing the error on PS(split0xsplit1) we need PS(split0xsplit0), PS(split0xsplit1), PS(split1xsplit1)
     ps_dict: dict
       a dict containing all power spectra
     binning_file: text file
       a binning file with three columns bin low, bin high, bin mean
       note that either binning_file or bin_size should be provided
     error_method: string
       the method for the computation of error
       can be "master" or "knox" for now
    approx_coupling: dict
    mbb_inv: 2d array
      the inverse mode coupling matrix, not in use for 2dflat
    compute_T_only: boolean
      True to compute only T spectra
    transfer_function: str
      the path to the transfer function
    """
    timer.start("Compute {} error...".format(error_method))

    bin_lo, bin_hi, bin_c, bin_size = pspy_utils.read_binning_file(
        binning_file, lmax)

    fsky = enmap.area(window.data.shape, window.data.wcs) / 4.0 / np.pi
    fsky *= np.mean(window.data)

    cov_dict = {}

    if error_method == "knox":
        for name in spec_name_list:
            m1, m2 = name.split("x")
            cov_dict[name] = {}
            for spec in spectra:
                X, Y = spec
                prefac = 1 / ((2 * bin_c + 1) * fsky * bin_size)
                # fmt: off
                cov_dict[name][X + Y] = np.diag(
                    prefac *
                    (ps_dict["%sx%s" %
                             (m1, m1)][X + X] * ps_dict["%sx%s" %
                                                        (m2, m2)][Y + Y] +
                     ps_dict["%sx%s" % (m1, m2)][X + Y]**2))
                # fmt: on

    elif error_method == "master":

        if not compute_T_only:
            mbb_inv = mbb_inv["spin0xspin0"]

        coupling_dict = so_cov.cov_coupling_spin0(window,
                                                  lmax,
                                                  niter=0,
                                                  l_band=l_band,
                                                  l_toep=l_toep,
                                                  l_exact=l_exact)
        coupling = so_cov.bin_mat(coupling_dict["TaTcTbTd"], binning_file,
                                  lmax)

        for name in spec_name_list:
            m1, m2 = name.split("x")
            cov_dict[name] = {}
            for spec in spectra:
                X, Y = spec
                cov_dict[name][X + Y] = so_cov.symmetrize(
                    ps_dict["%sx%s" % (m1, m1)][X + X]) * so_cov.symmetrize(
                        ps_dict["%sx%s" % (m2, m2)][Y + Y])
                cov_dict[name][X + Y] += so_cov.symmetrize(
                    ps_dict["%sx%s" % (m1, m2)][X + Y]**2)
                cov_dict[name][X + Y] *= coupling
                cov_dict[name][X + Y] = np.dot(
                    np.dot(mbb_inv, cov_dict[name][X + Y]), mbb_inv.T)
                if transfer_function is not None:
                    _, _, tf, _ = np.loadtxt(transfer_function, unpack=True)
                    tf = tf[:len(bin_c)]
                    cov_dict[name][X + Y] /= np.outer(np.sqrt(tf), np.sqrt(tf))

    else:
        cov_dict = None

    timer.stop()
    return cov_dict