示例#1
0
    def compute_subcovmat(self, spins):

        cw = nmt.NmtCovarianceWorkspace()
        cw.compute_coupling_coefficients(
            self.get_field(self.namap1, spins[0]),
            self.get_field(self.namap2, spins[1]),
            self.get_field(self.namap1, spins[2]),
            self.get_field(self.namap2, spins[3]),
            lmax=self.lmax,
        )

        a1b1, a1b2, a2b1, a2b2 = self.get_cov_input_spectra(spins)
        ordering_a = self.ordering[(spins[0], spins[1])]
        ordering_b = self.ordering[(spins[2], spins[3])]

        covar = nmt.gaussian_covariance(
            cw,
            spins[0],
            spins[1],
            spins[2],
            spins[3],
            a1b1,
            a1b2,
            a2b1,
            a2b2,
            self.mc_12.workspace_dict[(spins[0], spins[1])],
            wb=self.mc_12.workspace_dict[(spins[2], spins[3])],
        ).reshape(
            [self.num_ell,
             len(ordering_a), self.num_ell,
             len(ordering_b)])

        for i, AB in enumerate(ordering_a):
            for j, CD in enumerate(ordering_b):
                self.covmat[AB + CD] = covar[:, i, :, j]
示例#2
0
文件: utils.py 项目: damonge/S8z
    def get_cov(self, cosmo, fields, cls):
        cw = self.get_workspace(fields)
        cla1b1 = self._search_cl(cls, self.tracers_1[0],
                                 self.tracers_2[0]).get_cl_theory(
                                     cosmo, fields, return_decoupled=False)
        cla1b2 = self._search_cl(cls, self.tracers_1[0],
                                 self.tracers_2[1]).get_cl_theory(
                                     cosmo, fields, return_decoupled=False)
        cla2b1 = self._search_cl(cls, self.tracers_1[1],
                                 self.tracers_2[0]).get_cl_theory(
                                     cosmo, fields, return_decoupled=False)
        cla2b2 = self._search_cl(cls, self.tracers_1[1],
                                 self.tracers_2[1]).get_cl_theory(
                                     cosmo, fields, return_decoupled=False)
        cla = self._search_cl(cls, self.tracers_1[0], self.tracers_1[1])
        wa = cla.get_workspace(fields)
        ncla = cla.ncls
        nl = cla.nl
        clb = self._search_cl(cls, self.tracers_2[0], self.tracers_2[1])
        wb = clb.get_workspace(fields)
        nclb = clb.ncls

        cov = nmt.gaussian_covariance(cw, fields[self.tracers_1[0]].get_spin(),
                                      fields[self.tracers_1[1]].get_spin(),
                                      fields[self.tracers_2[0]].get_spin(),
                                      fields[self.tracers_2[1]].get_spin(),
                                      cla1b1, cla1b2, cla2b1, cla2b2, wa, wb)
        cov = cov.reshape([nl, ncla, nl, nclb])
        return cov
示例#3
0
文件: clcalc.py 项目: damonge/cmb-lss
 def get_covariance(self, typ1, typ2):
     tttt = typ1 + typ2
     if self.cov[tttt] is None:
         fname = self.get_clfile_name(tttt, 'cov', 'npz')
         if not os.path.isfile(fname):
             t1, t2, t3, t4 = tttt
             clt = {}
             cwsp = self.get_covariance_workspace(typ1, typ2)
             w1 = self.get_workspace(typ1)
             w2 = self.get_workspace(typ2)
             for tt in [t1 + t3, t1 + t4, t2 + t3, t2 + t4]:
                 clt[tt] = self.get_interpolated_cl(tt)
             self.cov[tttt] = nmt.gaussian_covariance(cwsp,
                                                      0,
                                                      0,
                                                      0,
                                                      0,
                                                      clt[t1 + t3],
                                                      clt[t1 + t4],
                                                      clt[t2 + t3],
                                                      clt[t2 + t4],
                                                      w1,
                                                      wb=w2)
             np.savez(fname, cov=self.cov[tttt])
         else:
             d = np.load(fname)
             self.cov[tttt] = d['cov']
     return self.cov[tttt]
示例#4
0
    def get_covariance_TE_TE(self, cl_te):
        self.get_covariance_coeff()
        w02 = self.w[2]
        n_ell = len(cl_te)
        cl_tt = np.zeros(n_ell)
        cl_bb = np.zeros(n_ell)
        cl_ee = np.zeros(n_ell)
        cl_eb = np.zeros(n_ell)
        cl_tb = np.zeros(n_ell)
        covar_02_02 = nmt.gaussian_covariance(
            self.cw,
            0,
            2,
            0,
            2,  # Spins of the 4 fields
            [cl_tt],  # TT
            [cl_te, cl_tb],  # TE, TB
            [cl_te, cl_tb],  # ET, BT
            [cl_ee, cl_eb, cl_eb, cl_bb],  # EE, EB, BE, BB
            w02,
            wb=w02)
        covar_02_02 = np.reshape(
            covar_02_02, (len(self.ell_binned), 2, len(self.ell_binned), 2))
        covar_TE_TE = covar_02_02[:, 0, :, 0]
        # covar_TE_TB = covar_02_02[:, 0, :, 1]
        # covar_TB_TE = covar_02_02[:, 1, :, 0]
        # covar_TB_TB = covar_02_02[:, 1, :, 1]

        return covar_TE_TE
def compute_covariance(w,clpred,binning,t1,t2,t3,t4,nz1,nz2,cw):
    """Routine to compute the covariance matrix using NaMaster
    needs a NaMaster workspace w, the 4 tracers considered, and an array with the predicted cls
    cl_t1t3, cl_t1t4, cl_t2t3, cl_t2t4.
    """
    
    t1t3 = np.logical_and(binning.binar['T1']==min(t1,t3),binning.binar['T2']==max(t3,t1))
    t1t4 = np.logical_and(binning.binar['T1']==min(t1,t4),binning.binar['T2']==max(t4,t1))
    t2t3 = np.logical_and(binning.binar['T1']==min(t2,t3),binning.binar['T2']==max(t2,t3))
    t2t4 = np.logical_and(binning.binar['T1']==min(t2,t4),binning.binar['T2']==max(t4,t2))

    if t1==t3:
        c13 = clpred[t1t3]+1./nz1
    else:
        c13 = clpred[t1t3]
    if t1==t4:
        c14 = clpred[t1t4]+1./nz1
    else:
        c14 = clpred[t1t4]
    if t2==t3:
        c23 = clpred[t2t3]+1./nz2
    else:
        c23 = clpred[t2t3]
    if t2==t4:
        c24 = clpred[t2t4]+1./nz2
    else:
        c24 = clpred[t2t4]  
    return nmt.gaussian_covariance(cw,c13,c14,c23,c24)
示例#6
0
def test_workspace_covar_spin0():
    # Compute all coefficients
    cw1 = nmt.NmtCovarianceWorkspace()
    cw1.compute_coupling_coefficients(CT.f0, CT.f2)
    # Write to file
    cw1.write_to("cwsp_test.fits")

    # Only spin-0
    cw2 = nmt.NmtCovarianceWorkspace()
    cw2.compute_coupling_coefficients(CT.f0, CT.f2, spin0_only=True)

    # Read only spin-0 from file
    cw3 = nmt.NmtCovarianceWorkspace()
    cw3.read_from("cwsp_test.fits", force_spin0_only=True)

    # Spin-0 matrices
    c1 = nmt.gaussian_covariance(cw1, 0, 0, 0, 0, [CT.cltt], [CT.cltt],
                                 [CT.cltt], [CT.cltt], CT.w)
    c2 = nmt.gaussian_covariance(cw2, 0, 0, 0, 0, [CT.cltt], [CT.cltt],
                                 [CT.cltt], [CT.cltt], CT.w)
    c3 = nmt.gaussian_covariance(cw3, 0, 0, 0, 0, [CT.cltt], [CT.cltt],
                                 [CT.cltt], [CT.cltt], CT.w)
    assert np.all(c1 == c2)
    assert np.all(c1 == c3)

    # Errors thrown otherwise
    # This should be fine
    c1 = nmt.gaussian_covariance(cw1,
                                 0,
                                 2,
                                 0,
                                 2, [CT.cltt], [CT.clte, 0 * CT.clte],
                                 [CT.clte, 0 * CT.clte],
                                 [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
                                 CT.w02,
                                 wb=CT.w02)
    # These shouldn't
    with pytest.raises(RuntimeError):
        nmt.gaussian_covariance(cw2,
                                0,
                                2,
                                0,
                                2, [CT.cltt], [CT.clte, 0 * CT.clte],
                                [CT.clte, 0 * CT.clte],
                                [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
                                CT.w02,
                                wb=CT.w02)
    with pytest.raises(RuntimeError):
        nmt.gaussian_covariance(cw3,
                                0,
                                2,
                                0,
                                2, [CT.cltt], [CT.clte, 0 * CT.clte],
                                [CT.clte, 0 * CT.clte],
                                [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
                                CT.w02,
                                wb=CT.w02)
示例#7
0
    def test_workspace_covar_benchmark(self):
        cw = nmt.NmtCovarianceWorkspace()
        cw.compute_coupling_coefficients(self.w, self.w)

        covar = nmt.gaussian_covariance(cw, self.cltt, self.cltt, self.cltt,
                                        self.cltt)
        covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov.txt",
                                 unpack=True)
        self.assertTrue(
            (np.fabs(covar - covar_bench) <=
             np.fmin(np.fabs(covar), np.fabs(covar_bench)) * 1E-5).all())
示例#8
0
def test_workspace_covar_errors():
    cw = nmt.NmtCovarianceWorkspace()

    with pytest.raises(ValueError):  # Write uninitialized
        cw.write_to("wsp.fits")

    cw.compute_coupling_coefficients(CT.f0, CT.f0)  # All good
    assert cw.wsp.lmax == CT.w.wsp.lmax
    assert cw.wsp.lmax == CT.w.wsp.lmax

    cw.read_from('test/benchmarks/bm_nc_np_cw00.fits')  # Correct reading
    assert cw.wsp.lmax == CT.w.wsp.lmax
    assert cw.wsp.lmax == CT.w.wsp.lmax

    # gaussian_covariance
    with pytest.raises(ValueError):  # Wrong input cl size
        nmt.gaussian_covariance(cw, 0, 0, 0, 0, [CT.cltt], [CT.cltt],
                                [CT.cltt], [CT.cltt[:15]], CT.w)
    with pytest.raises(ValueError):  # Wrong input cl shapes
        nmt.gaussian_covariance(cw, 0, 0, 0, 0, [CT.cltt], [CT.cltt],
                                [CT.cltt], [CT.cltt, CT.cltt], CT.w)
    with pytest.raises(ValueError):  # Wrong input spins
        nmt.gaussian_covariance(cw, 0, 2, 0, 0, [CT.cltt], [CT.cltt],
                                [CT.cltt], [CT.cltt, CT.cltt], CT.w)

    with pytest.raises(RuntimeError):  # Incorrect reading
        cw.read_from('none')

    with pytest.raises(ValueError):  # Incompatible resolutions
        cw.compute_coupling_coefficients(CT.f0, CT.f0_half)
示例#9
0
    def test_workspace_covar_errors(self):
        cw = nmt.NmtCovarianceWorkspace()

        with self.assertRaises(ValueError):  #Write uninitialized
            cw.write_to("wsp.dat")

        cw.compute_coupling_coefficients(self.w, self.w)  #All good
        self.assertEqual(cw.wsp.cs.n_eq, self.w.wsp.cs.n_eq)
        self.assertEqual(cw.wsp.lmax_a, self.w.wsp.lmax)
        self.assertEqual(cw.wsp.lmax_b, self.w.wsp.lmax)

        with self.assertRaises(RuntimeError):  #Write uninitialized
            cw.write_to("tests/wsp.dat")

        cw.read_from('test/benchmarks/bm_nc_np_cw00.dat')  #Correct reading
        self.assertEqual(cw.wsp.cs.n_eq, self.w.wsp.cs.n_eq)
        self.assertEqual(cw.wsp.lmax_a, self.w.wsp.lmax)
        self.assertEqual(cw.wsp.lmax_b, self.w.wsp.lmax)

        #gaussian_covariance
        with self.assertRaises(ValueError):  #Wrong input power spectra
            nmt.gaussian_covariance(cw, self.cltt, self.cltt, self.cltt,
                                    self.cltt[:15])

        with self.assertRaises(RuntimeError):  #Incorrect reading
            cw.read_from('none')
        w2 = nmt.NmtWorkspace()
        w2.read_from("test/benchmarks/bm_nc_np_w00.dat")
        w2.wsp.cs.n_eq = self.w.wsp.cs.n_eq // 2
        with self.assertRaises(ValueError):  #Incompatible resolutions
            cw.compute_coupling_coefficients(self.w, w2)
        w2.wsp.cs.n_eq = self.w.wsp.cs.n_eq
        w2.wsp.lmax = self.w.wsp.lmax // 2
        with self.assertRaises(RuntimeError):  #Incompatible resolutions
            cw.compute_coupling_coefficients(self.w, w2)
        w2.wsp.lmax = self.w.wsp.lmax

        w2.read_from("test/benchmarks/bm_nc_np_w02.dat")
        with self.assertRaises(ValueError):  #Spin-2
            cw.compute_coupling_coefficients(self.w, w2)
示例#10
0
    def test_workspace_covar_errors(self):
        cw = nmt.NmtCovarianceWorkspace()

        with self.assertRaises(ValueError):  # Write uninitialized
            cw.write_to("wsp.fits")

        cw.compute_coupling_coefficients(self.f0, self.f0)  # All good
        self.assertEqual(cw.wsp.lmax, self.w.wsp.lmax)
        self.assertEqual(cw.wsp.lmax, self.w.wsp.lmax)
        with self.assertRaises(RuntimeError):  # Write uninitialized
            cw.write_to("tests/wsp.fits")

        cw.read_from('test/benchmarks/bm_nc_np_cw00.fits')  # Correct reading
        self.assertEqual(cw.wsp.lmax, self.w.wsp.lmax)
        self.assertEqual(cw.wsp.lmax, self.w.wsp.lmax)

        # gaussian_covariance
        with self.assertRaises(ValueError):  # Wrong input cl size
            nmt.gaussian_covariance(cw, 0, 0, 0, 0, [self.cltt], [self.cltt],
                                    [self.cltt], [self.cltt[:15]], self.w)
        with self.assertRaises(ValueError):  # Wrong input cl shapes
            nmt.gaussian_covariance(cw, 0, 0, 0, 0, [self.cltt], [self.cltt],
                                    [self.cltt], [self.cltt, self.cltt],
                                    self.w)
        with self.assertRaises(ValueError):  # Wrong input spins
            nmt.gaussian_covariance(cw, 0, 2, 0, 0, [self.cltt], [self.cltt],
                                    [self.cltt], [self.cltt, self.cltt],
                                    self.w)

        with self.assertRaises(RuntimeError):  # Incorrect reading
            cw.read_from('none')

        with self.assertRaises(ValueError):  # Incompatible resolutions
            cw.compute_coupling_coefficients(self.f0, self.f0_half)
示例#11
0
 def get_covariance_TT_TT(self, cl_tt):
     self.get_covariance_coeff()
     w00 = self.w[0]
     covar_00_00 = nmt.gaussian_covariance(self.cw,
                                           0, 0, 0, 0,  # Spins of the 4 fields
                                           [cl_tt * 0],  # TT
                                           [cl_tt * 0],  # TT
                                           [cl_tt * 0],  # TT
                                           [cl_tt * 0],  # TT
                                           w00, wb=w00).reshape([len(self.ell_binned), 1,
                                                                 len(self.ell_binned), 1])
     covar_TT_TT = covar_00_00[:, 0, :, 0]
     return covar_TT_TT
示例#12
0
    def get_covariance(self):
        fname = os.path.join(
            self.outdir, 'cov_{}_{}_{}_{}.npz'.format(self.trA1, self.trA2,
                                                      self.trB1, self.trB2))
        if os.path.isfile(fname):
            return np.load(fname)['cov']

        self.load_cls()

        wa1b1 = self.clA1B1.get_workspace()
        wa1b2 = self.clA1B2.get_workspace()
        wa2b1 = self.clA2B1.get_workspace()
        wa2b2 = self.clA2B2.get_workspace()

        # Couple Theory Cls
        cla1b1 = wa1b1.couple_cell(self.clfid_A1B1.cl)
        cla1b2 = wa1b2.couple_cell(self.clfid_A1B2.cl)
        cla2b1 = wa2b1.couple_cell(self.clfid_A2B1.cl)
        cla2b2 = wa2b2.couple_cell(self.clfid_A2B2.cl)
        #####
        nla1b1 = self.clA1B1.nl_cp
        nla1b2 = self.clA1B2.nl_cp
        nla2b1 = self.clA2B1.nl_cp
        nla2b2 = self.clA2B2.nl_cp
        #####
        m_a1, m_b1 = self.clA1B1.get_masks()
        m_a2, m_b2 = self.clA2B2.get_masks()

        ##### Weight the Cls
        cla1b1 = (cla1b1 + nla1b1) / np.mean(m_a1 * m_b1)
        cla1b2 = (cla1b2 + nla1b2) / np.mean(m_a1 * m_b2)
        cla2b1 = (cla2b1 + nla2b1) / np.mean(m_a2 * m_b1)
        cla2b2 = (cla2b2 + nla2b2) / np.mean(m_a2 * m_b2)
        #####
        wa = self.clA1A2.get_workspace()
        wb = self.clB1B2.get_workspace()
        cw = self.get_covariance_workspace()

        s_a1, s_a2 = self.clA1A2.get_spins()
        s_b1, s_b2 = self.clB1B2.get_spins()

        cov = nmt.gaussian_covariance(cw, s_a1, s_a2, s_b1, s_b2, cla1b1,
                                      cla1b2, cla2b1, cla2b2, wa, wb)

        np.savez_compressed(fname, cov=cov)
        return cov
示例#13
0
    def from_fields(Covariance,
                    field_a1,
                    field_a2,
                    field_b1,
                    field_b2,
                    wsp_a,
                    wsp_b,
                    cla1b1,
                    cla1b2,
                    cla2b1,
                    cla2b2,
                    cwsp=None):
        """Creator from a set of fields.

        Args:
            field_a1, field_a2 (:obj:`Field`): the two fields
                contributing to the first power spectrum we want
                the covariance of.
            field_b1, field_b2 (:obj:`Field`): the two fields
                contributing to the second power spectrum we want
                the covariance of.
            wsp_a, wsp_b (:obj:`NmtWorkspace`): mode-coupling matrix
                for the two different power spectra.
            cla1b1 (array): power spectrum between `field_a1` and
                `field_b1`. Must be sampled at all multipoles between
                0 and 3*nside-1.
            cla1b2 (array): same as cla1b1 for field_a1 and field_b2.
            cla2b1 (array): same as cla2b1 for field_a2 and field_b1.
            cla2b2 (array): same as cla2b2 for field_a2 and field_b2.
            cwsp (:obj:`NmtCovarianceWorkspace`): container for the
                mode-coupling coefficients used to compute the
                covariance matrix. If `None`, a new one will be
                generated from the inputs.
        """
        if cwsp is None:  # Generate coupling coefficients if needed.
            cwsp = nmt.NmtCovarianceWorskpace()
            cwsp.compute_coupling_coefficients(field_a1.field, field_a2.field,
                                               field_b1.field, field_b2.field)

        # Compute covariance matrix
        covar = nmt.gaussian_covariance(cwsp, 0, 0, 0, 0, [cla1b1], [cla1b2],
                                        [cla2b1], [cla2b2], wsp_a, wsp_b)
        return Covariance(field_a1.name, field_a2.name, field_b1.name,
                          field_b2.name, covar)
def compute_covariance_full_spin0(nmaps, nbins, maps_bins, maps_spins, w00):

    cl_indices = []
    cl_modes = []
    cl_bins = []
    for i in range(nmaps):
        si = maps_modes[i]
        for j in range(i, nmaps):
            sj = maps_modes[j]
            cl_indices.append([i, j])
            cl_modes.append([si, sj])
            cl_bins.append([maps_bins[i], maps_bins[j]])

    cov_indices = []
    cov_modes = []
    cov_bins = []
    for i, clij in enumerate(cl_indices):
        for j, clkl in enumerate(cl_indices[i:]):
            cov_indices.append(cl_indices[i] + cl_indices[i + j])
            cov_modes.append(cl_modes[i] + cl_modes[i + j])
            cov_bins.append(cl_bins[i] + cl_bins[i + j])

    cov_indices = np.array(cov_indices)
    cov_modes = np.array(cov_modes)
    cov_bins = np.array(cov_bins)

    for i, indices in enumerate(cov_indices):
        fname = out_run_path + '_cov_c{}{}{}{}_{}{}{}{}.npz'.format(
            *cov_modes[i], *cov_bins[i])
        if os.path.isfile(fname):
            continue

        ibin_a1, ibin_a2, ibin_b1, ibin_b2 = indices

        cla1b1 = [clTh[ibin_a1, ibin_b1]]
        cla1b2 = [clTh[ibin_a1, ibin_b2]]
        cla2b1 = [clTh[ibin_a2, ibin_b1]]
        cla2b2 = [clTh[ibin_a2, ibin_b2]]

        cov = nmt.gaussian_covariance(cw, 0, 0, 0, 0, cla1b1, cla1b2, cla2b1,
                                      cla2b2, w00)

        np.savez_compressed(fname, cov)
示例#15
0
def gcov_make(fa1, fa2, fb1, fb2, wa, wb, cla1b1, cla1b2, cla2b1, cla2b2,
              n_ell):
    """Returns the Gaussian covariance matrix fa1fa2_fb1fb2
    """
    cw = nmt.NmtCovarianceWorkspace()
    cw.compute_coupling_coefficients(fa1, fb1, fa2, fb2)
    cov = nmt.gaussian_covariance(
        cw,
        2,
        2,
        2,
        2,  # Spins of the 4 fields
        # EE, EB, BE, BB
        cla1b1,
        cla1b2,
        cla2b1,
        cla2b2,
        wa,
        wb=wb).reshape([n_ell, 4, n_ell, 4])
    return cov
示例#16
0
    def get_covariance_BB_BB(self, cl_bb):
        w22 = self.w[1]
        n_ell = len(cl_bb)
        cl_eb = np.zeros(n_ell)
        cl_ee = np.zeros(n_ell)
        covar_22_22 = nmt.gaussian_covariance(
            self.cw,
            2,
            2,
            2,
            2,  # Spins of the 4 fields
            [cl_ee, cl_eb, cl_eb, cl_bb],  # EE, EB, BE, BB
            [cl_ee, cl_eb, cl_eb, cl_bb],  # EE, EB, BE, BB
            [cl_ee, cl_eb, cl_eb, cl_bb],  # EE, EB, BE, BB
            [cl_ee, cl_eb, cl_eb, cl_bb],  # EE, EB, BE, BB
            w22,
            wb=w22)

        covar_22_22 = np.reshape(
            covar_22_22, (len(self.ell_binned), 4, len(self.ell_binned), 4))

        # covar_EE_EE = covar_22_22[:, 0, :, 0]
        # covar_EE_EB = covar_22_22[:, 0, :, 1]
        # covar_EE_BE = covar_22_22[:, 0, :, 2]
        # covar_EE_BB = covar_22_22[:, 0, :, 3]
        # covar_EB_EE = covar_22_22[:, 1, :, 0]
        # covar_EB_EB = covar_22_22[:, 1, :, 1]
        # covar_EB_BE = covar_22_22[:, 1, :, 2]
        # covar_EB_BB = covar_22_22[:, 1, :, 3]
        # covar_BE_EE = covar_22_22[:, 2, :, 0]
        # covar_BE_EB = covar_22_22[:, 2, :, 1]
        # covar_BE_BE = covar_22_22[:, 2, :, 2]
        # covar_BE_BB = covar_22_22[:, 2, :, 3]
        # covar_BB_EE = covar_22_22[:, 3, :, 0]
        # covar_BB_EB = covar_22_22[:, 3, :, 1]
        # covar_BB_BE = covar_22_22[:, 3, :, 2]
        covar_BB_BB = covar_22_22[:, 3, :, 3]

        return covar_BB_BB
示例#17
0
    def set_covariance_matrices(self):
        correlation_pairs = get_pairs(self.correlation_symbols, join_with='-')
        for correlation_pair in tqdm(correlation_pairs,
                                     desc='covariance matrices'):
            a1 = correlation_pair[0]
            a2 = correlation_pair[1]
            b1 = correlation_pair[3]
            b2 = correlation_pair[4]

            covariance_workspace = nmt.NmtCovarianceWorkspace()
            covariance_workspace.compute_coupling_coefficients(
                self.fields[a1], self.fields[a2], self.fields[b1],
                self.fields[b2])

            self.covariance_matrices[
                correlation_pair] = nmt.gaussian_covariance(
                    covariance_workspace,
                    0,
                    0,
                    0,
                    0,
                    [self.theory_correlations[''.join(sorted([a1, b1]))]],
                    [self.theory_correlations[''.join(sorted([a1, b2]))]],
                    [self.theory_correlations[''.join(sorted([a2, b1]))]],
                    [self.theory_correlations[''.join(sorted([a2, b2]))]],
                    wa=self.workspaces[a1 + a2],
                    wb=self.workspaces[b1 + b2],
                )

            transpose_corr_symbol = b1 + b2 + '-' + a1 + a2
            self.covariance_matrices[transpose_corr_symbol] = np.transpose(
                self.covariance_matrices[correlation_pair])

            if a1 + a2 == b1 + b2:
                self.correlation_matrices[
                    correlation_pair] = get_correlation_matrix(
                        self.covariance_matrices[correlation_pair])
示例#18
0
    def test_workspace_covar_benchmark(self):
        def compare_covars(c, cb):
            # Check first and second diagonals
            for k in [0, 1]:
                d = np.diag(c, k=k)
                db = np.diag(cb, k=k)
                self.assertTrue(
                    (np.fabs(d - db) <=
                     np.fmin(np.fabs(d), np.fabs(db)) * 1E-4).all())

        # Check against a benchmark
        cw = nmt.NmtCovarianceWorkspace()
        cw.compute_coupling_coefficients(self.f0, self.f0)

        # [0,0 ; 0,0]
        covar = nmt.gaussian_covariance(cw, 0, 0, 0, 0, [self.cltt],
                                        [self.cltt], [self.cltt], [self.cltt],
                                        self.w)
        covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov.txt",
                                 unpack=True)
        compare_covars(covar, covar_bench)
        # [0,2 ; 0,2]
        covar = nmt.gaussian_covariance(
            cw,
            0,
            2,
            0,
            2, [self.cltt], [self.clte, 0 * self.clte],
            [self.clte, 0 * self.clte],
            [self.clee, 0 * self.clee, 0 * self.clee, self.clbb],
            self.w02,
            wb=self.w02)
        covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov0202.txt")
        compare_covars(covar, covar_bench)
        # [0,0 ; 0,2]
        covar = nmt.gaussian_covariance(cw,
                                        0,
                                        0,
                                        0,
                                        2, [self.cltt],
                                        [self.clte, 0 * self.clte],
                                        [self.cltt],
                                        [self.clte, 0 * self.clte],
                                        self.w,
                                        wb=self.w02)
        covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov0002.txt")
        compare_covars(covar, covar_bench)
        # [0,0 ; 2,2]
        covar = nmt.gaussian_covariance(cw,
                                        0,
                                        0,
                                        2,
                                        2, [self.clte, 0 * self.clte],
                                        [self.clte, 0 * self.clte],
                                        [self.clte, 0 * self.clte],
                                        [self.clte, 0 * self.clte],
                                        self.w,
                                        wb=self.w22)
        covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov0022.txt")
        compare_covars(covar, covar_bench)
        # [2,2 ; 2,2]
        covar = nmt.gaussian_covariance(
            cw,
            2,
            2,
            2,
            2, [self.clee, 0 * self.clee, 0 * self.clee, self.clbb],
            [self.clee, 0 * self.clee, 0 * self.clee, self.clbb],
            [self.clee, 0 * self.clee, 0 * self.clee, self.clbb],
            [self.clee, 0 * self.clee, 0 * self.clee, self.clbb],
            self.w22,
            wb=self.w22)
        covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov2222.txt")
        compare_covars(covar, covar_bench)
示例#19
0
wa = nmt.NmtWorkspace()
wa.read_from(fname_mcm_a)
if (o.bin_a1 == o.bin_b1) and (o.bin_a2 == o.bin_b2):
    wb = wa
else:
    fname_mcm_b = predir + 'cls_metacal_mcm_bins_'
    fname_mcm_b += '%d%d_ns%d.fits' % (o.bin_b1, o.bin_b2, o.nside)
    wb = nmt.NmtWorkspace()
    wb.read_from(fname_mcm_b)
nbpw = wa.wsp.bin.n_bands


printflush("Covariance")
cov = nmt.gaussian_covariance(cw, 2, 2, 2, 2,
                              clt['%d%d' % (o.bin_a1, o.bin_b1)],
                              clt['%d%d' % (o.bin_a1, o.bin_b2)],
                              clt['%d%d' % (o.bin_a2, o.bin_b1)],
                              clt['%d%d' % (o.bin_a2, o.bin_b2)],
                              wa, wb).reshape([nbpw, 4, nbpw, 4])
if o.full_noise:
    name = '%d%d_%d%d' % (o.bin_a1, o.bin_a2, o.bin_b1, o.bin_b2)
    cl_ones = np.array([cl1, cl0, cl0, cl1])
    cl_zeros = np.array([cl0, cl0, cl0, cl0])
    if o.bin_a1 == o.bin_b1:
        cov += nmt.gaussian_covariance(cw_sn[name + '_10_10'], 2, 2, 2, 2,
                                       cl_ones,
                                       cl_zeros,
                                       cl_zeros,
                                       clt['%d%d' % (o.bin_a2, o.bin_b2)],
                                       wa, wb).reshape([nbpw, 4, nbpw, 4])*pix_area
        if o.bin_a2 == o.bin_b2:
            cov += nmt.gaussian_covariance(cw_nn[name], 2, 2, 2, 2,
示例#20
0
def compute_covariance_full(nmaps, nbins, maps_bins, maps_spins):

    cl_indices = []
    cl_spins = []
    cl_bins = []
    for i in range(nmaps):
        si = maps_spins[i]
        for j in range(i, nmaps):
            sj = maps_spins[j]
            cl_indices.append([i, j])
            cl_spins.append([si, sj])
            cl_bins.append([maps_bins[i], maps_bins[j]])

    cov_indices = []
    cov_spins = []
    cov_bins = []
    for i, clij in enumerate(cl_indices):
        for j, clkl in enumerate(cl_indices[i:]):
            cov_indices.append(cl_indices[i] + cl_indices[i + j])
            cov_spins.append(cl_spins[i] + cl_spins[i + j])
            cov_bins.append(cl_bins[i] + cl_bins[i + j])

    cov_indices = np.array(cov_indices)
    cov_spins = np.array(cov_spins)
    cov_bins = np.array(cov_bins)

    fname_cw_old = ''
    for i, indices in enumerate(cov_indices):
        s_a1, s_a2, s_b1, s_b2 = cov_spins[i]

        na1 = get_nelems_spin(s_a1)
        na2 = get_nelems_spin(s_a2)
        nb1 = get_nelems_spin(s_b1)
        nb2 = get_nelems_spin(s_b2)

        bin_a1, bin_a2, bin_b1, bin_b2 = cov_bins[i]

        fname = out_run_path + '_cov_c{}{}{}{}_{}{}{}{}.npz'.format(
            *cov_spins[i], *cov_bins[i])
        if os.path.isfile(fname):
            continue

        ibin_a1, ibin_a2, ibin_b1, ibin_b2 = indices

        cla1b1 = [clTh[ibin_a1, ibin_b1]]
        cla1b2 = [clTh[ibin_a1, ibin_b2]]
        cla2b1 = [clTh[ibin_a2, ibin_b1]]
        cla2b2 = [clTh[ibin_a2, ibin_b2]]

        wa = get_workspace_from_spins_masks(0, 0, bin_a1, bin_a2)
        wb = get_workspace_from_spins_masks(0, 0, bin_b1, bin_b2)

        print('Computing ', fname)
        print('spins: ', s_a1, s_a2, s_b1, s_b2)
        print('cla1b1', (s_a1, s_b1), ibin_a1)
        print('cla1b2', (s_a1, s_b2), ibin_a1)
        print('cla2b1', (s_a2, s_b1), ibin_a2)
        print('cla2b2', (s_a2, s_b2), ibin_a2)

        fname_cw = out_run_path_NKA + '_cw{}{}{}{}.dat'.format(*cov_bins[i])
        if fname_cw != fname_cw_old:
            cw = nmt.NmtCovarianceWorkspace()
            cw.read_from(fname_cw)
            fname_cw_old = fname_cw

        cov = nmt.gaussian_covariance(cw, 0, 0, 0, 0, cla1b1, cla1b2, cla2b1,
                                      cla2b2, wa, wb)

        np.savez_compressed(fname, cov)
示例#21
0
    if not os.path.isfile(prefix_out + "_cw2222.dat"):  #spin0-spin2
        print("Computing cw2222")
        cw.compute_coupling_coefficients(f2, f2, f2, f2, n_iter=0)
        cw.write_to(prefix_out + "_cw2222.dat")
    else:
        cw.read_from(prefix_out + '_cw2222.dat')

    cla1b1 = cla2b2 = cla1b2 = cla2b1 = [(clee + nlee), cleb, clbe,
                                         clbb + nlbb]

    s_a1 = s_b1 = 2
    s_a2 = s_b2 = 2
    ###
    wa = wb = w22

    C = nmt.gaussian_covariance(cw, int(s_a1), int(s_a2), int(s_b1), int(s_b2),
                                cla1b1, cla1b2, cla2b1, cla2b2, wa, wb)
    fname = prefix_out + '_covTh.npz'
    np.savez_compressed(fname, C)

#Generate theory prediction
if not os.path.isfile(prefix_out + '_cl_th.txt'):
    print("Computing theory prediction")
    cl00_th = w22.decouple_cell(
        w22.couple_cell(np.array([clee, cleb, clbe, clbb])))
    np.savetxt(prefix_out + "_cl_th.txt", np.transpose([lbpw, cl00_th[0]]))

#Compute mean and variance over nsims simulations
cl00_all = []
for i in np.arange(nsims):
    #if i%100==0 :
    print("%d-th sim" % (i + o.isim_ini))
                      [mp_t]), nmt.NmtField(window.data, [mp_q, mp_u])
b = nmt.NmtBin(nside, nlb=nlb)

lb = b.get_effective_ells()

w00 = nmt.NmtWorkspace()
w00.compute_coupling_matrix(f0, f0, b)
cl_00 = compute_master(f0, f0, w00)
n_ell = len(cl_00[0])

cw = nmt.NmtCovarianceWorkspace()
cw.compute_coupling_coefficients(f0, f0, f0, f0)
covar_00_00 = nmt.gaussian_covariance(cw,
                                      0,
                                      0,
                                      0,
                                      0, [cl_tt], [cl_tt], [cl_tt], [cl_tt],
                                      w00,
                                      wb=w00).reshape([n_ell, 1, n_ell, 1])

cov_namaster = covar_00_00[:, 0, :, 0]

corr_pspy = so_cov.cov2corr(cov_pspy)
plt.matshow(corr_pspy, vmin=-0.3, vmax=0.3)
plt.savefig("%s/corr_pspsy.png" % (test_dir))
plt.clf()
plt.close()

corr_namaster = so_cov.cov2corr(cov_namaster)
plt.matshow(corr_namaster, vmin=-0.3, vmax=0.3)
plt.savefig("%s/corr_namaster.png" % (test_dir))
示例#23
0
b = nmt.NmtBin(nside_out, nlb=16)
leff = b.get_effective_ells()
lfull = np.arange(3 * nside_out)

#No contaminants
prefix = 'bm_nc_np'
f0 = nmt.NmtField(mask, [dl])
f2 = nmt.NmtField(mask, [dw_q, dw_u])
w00 = nmt.NmtWorkspace()
w00.compute_coupling_matrix(f0, f0, b)
cw00 = nmt.NmtCovarianceWorkspace()
cw00.compute_coupling_coefficients(w00, w00)
cw00.write_to(prefix + '_cw00.dat')
cov = nmt.gaussian_covariance(cw00, (cltt + nltt)[:3 * nside_out],
                              (cltt + nltt)[:3 * nside_out],
                              (cltt + nltt)[:3 * nside_out],
                              (cltt + nltt)[:3 * nside_out])
np.savetxt(prefix + "_cov.txt", cov)
clb00 = np.array([nltt[:3 * nside_out]])
c00 = w00.decouple_cell(nmt.compute_coupled_cell(f0, f0), cl_bias=clb00)
w00.write_to(prefix + '_w00.dat')
np.savetxt(prefix + "_c00.txt", np.transpose([leff, c00[0]]))
w02 = nmt.NmtWorkspace()
w02.compute_coupling_matrix(f0, f2, b)
clb02 = np.array([nlte[:3 * nside_out], 0 * nlte[:3 * nside_out]])
c02 = w02.decouple_cell(nmt.compute_coupled_cell(f0, f2), cl_bias=clb02)
w02.write_to(prefix + '_w02.dat')
np.savetxt(prefix + "_c02.txt", np.transpose([leff, c02[0], c02[1]]))
w22 = nmt.NmtWorkspace()
w22.compute_coupling_matrix(f2, f2, b)
clb22 = np.array([
示例#24
0
cwsp = nmt.NmtCovarianceWorkspace()
path2cwsp = output + 'cw.fits'
if os.path.isfile(path2cwsp):
    cwsp.read_from(path2cwsp)
else:
    cwsp.compute_coupling_coefficients(f, f)
    cwsp.write_to(path2cwsp)

# Clfid #
clfid = np.load(
    f'/mnt/extraspace/gravityls_3/S8z/Cls_2/4096_asDavid/fiducial/DESgc_DESgc/cl_DESgc{ibin}_DESgc{ibin}.npz'
)['cl']
clfid_cp = wsp.couple_cell(clfid)

cla1b1 = cla1b2 = cla2b1 = cla2b2 = (clfid_cp + nl_cp) / np.mean(mask * mask)
cov = nmt.gaussian_covariance(cwsp,
                              0,
                              0,
                              0,
                              0,
                              cla1b1,
                              cla1b2,
                              cla2b1,
                              cla2b2,
                              wa=wsp,
                              wb=wsp)
np.savez_compressed(
    output +
    f'cov_DESgc{ibin}_DESgc{ibin}_DESgc{ibin}_DESgc{ibin}{corr_suffix}.npz',
    cov)
示例#25
0
#Let's generate one particular sample and its power spectrum.
print("Field")
f0=get_sample_field()
b=nmt.NmtBin(nside,nlb=20) #We will use 20 multipoles per bandpower.
print("Workspace")
w=nmt.NmtWorkspace()
w.compute_coupling_matrix(f0,f0,b)
cl_0=compute_master(f0,f0,w)[0]

#Let's now compute the Gaussian estimate of the covariance!
print("Covariance")
#First we generate a NmtCovarianceWorkspace object to precompute
#and store the necessary coupling coefficients
cw=nmt.NmtCovarianceWorkspace()
cw.compute_coupling_coefficients(w,w) #<- This is the time-consuming operation
covar=nmt.gaussian_covariance(cw,clarr,clarr,clarr,clarr)

#Let's now compute the sample covariance
print("Sample covariance")
nsamp=100
covar_sample=np.zeros([len(cl_0),len(cl_0)])
mean_sample=np.zeros(len(cl_0))
for i in np.arange(nsamp) :
    print(i)
    f=get_sample_field()
    cl=compute_master(f,f,w)[0]
    covar_sample+=cl[None,:]*cl[:,None]
    mean_sample+=cl
mean_sample/=nsamp
covar_sample=covar_sample/nsamp-mean_sample[None,:]*mean_sample[:,None]
示例#26
0
# G-G
prefix = predir + "maps_metacal_bin%d_ns%d" % (o.bin_number, o.nside)
msk = np.load(prefix + '_w.npz')['w']
fsky = np.sum(msk**2) / npix
cl_gg = w.couple_cell([sl, cl0, cl0, cl0]) / fsky + np.array(
    [nl, cl0, cl0, nl])
# PSF-PSF
fname_apsf = predir + "cls_metacal_cls_bins_%d%d_ns%d_apsf.npz" % (
    o.bin_number, o.bin_number, o.nside)
dpsf = np.load(fname_apsf)
cl_pp = dpsf['cls_coupled'] / fsky
# G-PSF
cl_gp = np.array([cl0, cl0, cl0, cl0])

printflush("CMCM")
fname_cmcm = predir + 'cls_metacal_cmcm_bins_'
fname_cmcm += '%d%d_%d%d_ns%d.fits' % (o.bin_number, o.bin_number,
                                       o.bin_number, o.bin_number, o.nside)
cw = nmt.NmtCovarianceWorkspace()
cw.read_from(fname_cmcm)

printflush("Covariance")
nbpw = w.wsp.bin.n_bands
cov = nmt.gaussian_covariance(cw, 2, 2, 2, 2, cl_gg, cl_gp, cl_gp, cl_pp, w,
                              w).reshape([nbpw, 4, nbpw, 4])

printflush("Writing")
fname_cov = predir + 'cls_metacal_covar_xpsf_bin'
fname_cov += '%d_ns%d.npz' % (o.bin_number, o.nside)
np.savez(fname_cov, cov=cov)
示例#27
0
        cl00_cov += nls_gc['cls_raw'][3]
        cl00_cov /= np.mean(mask_gc**2)
        #
        cl02_cov = w02.couple_cell([clte, cltb]) / np.mean(mask_gc * mask_wl)
        #
        cl22_cov = w22.couple_cell([clee, cleb, clbe, clbb])
        cl22_cov += nls_wl['cls_raw'][1].reshape(cl22_cov.shape)
        cl22_cov /= np.mean(mask_wl**2)

    #####
    covar_00_00 = nmt.gaussian_covariance(
        cw00_00,
        0,
        0,
        0,
        0,  # Spins of the 4 fields
        cl00_cov,  # TT
        cl00_cov,  # TT
        cl00_cov,  # TT
        cl00_cov,  # TT
        w00,
        wb=w00).reshape([lbpw.size, 1, lbpw.size, 1])

    covar_00_02 = nmt.gaussian_covariance(
        cw00_02,
        0,
        0,
        0,
        2,  # Spins of the 4 fields
        cl00_cov,  # TT
        cl02_cov,  # TE, TB
        cl00_cov,  # TT
# Let's now compute the Gaussian estimate of the covariance!
print("Covariance")
# First we generate a NmtCovarianceWorkspace object to precompute
# and store the necessary coupling coefficients
# This is the time-consuming operation
# Note that you only need to do this once,
# regardless of spin
cw = nmt.NmtCovarianceWorkspace()
cw.compute_coupling_coefficients(f0_1, f0_2, f0_1, f0_2)
covar_00_00 = nmt.gaussian_covariance(
    cw,
    0,
    0,
    0,
    0,  # Spins of the 4 fields
    [cl_tt + nl_tt],  # TT
    [cl_tt],  # TT
    [cl_tt],  # TT
    [cl_tt + nl_tt],  # TT
    w00,
    wb=w00,
    coupled=True).reshape([n_ell, 1, n_ell, 1])
covar_TT_TT = covar_00_00[:, 0, :, 0]
np.save("covar_TT_TT", covar_TT_TT)

cw = nmt.NmtCovarianceWorkspace()
cw.compute_coupling_coefficients(f0_1, f0_2, f0_1, f2_2)
covar_00_02 = nmt.gaussian_covariance(
    cw,
    0,
    0,
示例#29
0
wt = nmt.NmtWorkspace()
wt.compute_coupling_matrix(f0,
                           f0,
                           b,
                           l_toeplitz=nside,
                           l_exact=nside // 2,
                           dl_band=40)
c_tpltz = wt.get_coupling_matrix() / (2 * ls[None, :] + 1.)
cl_tpltz = wt.decouple_cell(nmt.compute_coupled_cell(f0, f0))

# You can also use the Toeplitz approximation to compute the
# Gaussian covariance matrix. Let's try that here:
# First, the exact calculation
cwe = nmt.NmtCovarianceWorkspace()
cwe.compute_coupling_coefficients(f0, f0)
cov_exact = nmt.gaussian_covariance(cwe, 0, 0, 0, 0, [cl_theory], [cl_theory],
                                    [cl_theory], [cl_theory], we)
# Now using the Toeplitz approximation:
cwt = nmt.NmtCovarianceWorkspace()
cwt.compute_coupling_coefficients(f0,
                                  f0,
                                  l_toeplitz=nside,
                                  l_exact=nside // 2,
                                  dl_band=40)
cov_tpltz = nmt.gaussian_covariance(cwt, 0, 0, 0, 0, [cl_theory], [cl_theory],
                                    [cl_theory], [cl_theory], wt)

# Let's compare the mode-coupling matrices themselves:
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(14, 4))
ax1.set_title(r'Exact MCM ($\log_{10}$)')
ax1.imshow(np.log10(np.fabs(c_exact)), vmax=-1, vmin=-14)
ax1.set_xlabel(r'$\ell_1$', fontsize=15)
示例#30
0
def test_workspace_covar_benchmark():
    def compare_covars(c, cb):
        # Check first and second diagonals
        for k in [0, 1]:
            d = np.diag(c, k=k)
            db = np.diag(cb, k=k)
            assert (np.fabs(d - db) <=
                    np.fmin(np.fabs(d), np.fabs(db)) * 1E-4).all()

    # Check against a benchmark
    cw = nmt.NmtCovarianceWorkspace()
    cw.compute_coupling_coefficients(CT.f0, CT.f0)

    # [0,0 ; 0,0]
    covar = nmt.gaussian_covariance(cw, 0, 0, 0, 0, [CT.cltt], [CT.cltt],
                                    [CT.cltt], [CT.cltt], CT.w)
    covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov.txt", unpack=True)
    compare_covars(covar, covar_bench)
    # Check coupled
    covar_rc = nmt.gaussian_covariance(cw,
                                       0,
                                       0,
                                       0,
                                       0, [CT.cltt], [CT.cltt], [CT.cltt],
                                       [CT.cltt],
                                       CT.w,
                                       coupled=True)  # [nl, nl]
    covar_c = np.array([CT.w.decouple_cell([row])[0]
                        for row in covar_rc])  # [nl, nbpw]
    covar = np.array([CT.w.decouple_cell([col])[0]
                      for col in covar_c.T]).T  # [nbpw, nbpw]
    compare_covars(covar, covar_bench)

    # [0,2 ; 0,2]
    covar = nmt.gaussian_covariance(
        cw,
        0,
        2,
        0,
        2, [CT.cltt], [CT.clte, 0 * CT.clte], [CT.clte, 0 * CT.clte],
        [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
        CT.w02,
        wb=CT.w02)
    covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov0202.txt")
    compare_covars(covar, covar_bench)
    # [0,0 ; 0,2]
    covar = nmt.gaussian_covariance(cw,
                                    0,
                                    0,
                                    0,
                                    2, [CT.cltt], [CT.clte, 0 * CT.clte],
                                    [CT.cltt], [CT.clte, 0 * CT.clte],
                                    CT.w,
                                    wb=CT.w02)
    covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov0002.txt")
    compare_covars(covar, covar_bench)
    # [0,0 ; 2,2]
    covar = nmt.gaussian_covariance(cw,
                                    0,
                                    0,
                                    2,
                                    2, [CT.clte, 0 * CT.clte],
                                    [CT.clte, 0 * CT.clte],
                                    [CT.clte, 0 * CT.clte],
                                    [CT.clte, 0 * CT.clte],
                                    CT.w,
                                    wb=CT.w22)
    covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov0022.txt")
    compare_covars(covar, covar_bench)
    # [2,2 ; 2,2]
    covar = nmt.gaussian_covariance(
        cw,
        2,
        2,
        2,
        2, [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
        [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
        [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
        [CT.clee, 0 * CT.clee, 0 * CT.clee, CT.clbb],
        CT.w22,
        wb=CT.w22)
    covar_bench = np.loadtxt("test/benchmarks/bm_nc_np_cov2222.txt")
    compare_covars(covar, covar_bench)
示例#31
0
        # wl1 - wl1
        fname = '/mnt/extraspace/gravityls_3/S8z/Cls/fiducial/nobaryons/cls_DESwl{}_DESwl{}.npz'.format(
            i, i)
        clwlwl_ee = np.load(fname)['cls'][:3 * nside]
        fname = os.path.join(output_folder,
                             'des_sh_{}_noise_ns{}.npz'.format(wltype, nside))
        nls = np.load(fname)

        cla2b2 = np.array(
            [clwlwl_ee, 0 * clwlwl_ee, 0 * clwlwl_ee, 0 * clwlwl_ee])
        cla2b2 = ws.couple_cell(cla2b2)
        cla2b2 += nls['cls_raw'][i].reshape(cla2b2.shape)
        cla2b2 /= np.mean(masks_gwl[i]**2)

        covar = nmt.gaussian_covariance(
            cw,
            2,
            2,
            2,
            2,  # Spins of the 4 fields
            cla1b1,  # PP -> EE, EB, BE, BB
            cla1b2,  # Pe -> EE, EB, BE, BB
            cla2b1,  # eP -> EE, EB, BE, BB
            cla2b2,  # ee -> EE, EB, BE, BB
            ws,
            wb=ws).reshape([lbpw.size, 4, lbpw.size, 4])

        covar_ar.append(covar)
        np.savez(fname_cov, l=lbpw, cov=covar_ar)