예제 #1
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)
예제 #2
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)
예제 #3
0
    def get_cw_sn(b_a1, b_a2, b_b1, b_b2,
                  n_a1, n_a2, n_b1, n_b2):
        name = "%d%d_%d%d" % (b_a1, b_a2, b_b1, b_b2)
        name += "_%d%d_%d%d" % (n_a1, n_a2, n_b1, n_b2)
        fields_a = [fields_s, fields_n]

        if name in cw_sn:
            pass
        else:
            cw_sn[name] = nmt.NmtCovarianceWorkspace()
            fname_cmcm = prefix_cmcm + name + '_ns%d.fits' % o.nside
            if os.path.isfile(fname_cmcm):
                printflush(" - Reading")
                cw_sn[name].read_from(fname_cmcm)
            else:
                printflush(" - Fields")
                if b_a1 not in fields_a[n_a1]:
                    fields_a[n_a1][b_a1] = get_field(b_a1, mask_sigma=bool(n_a1))
                if b_a2 not in fields_a[n_a2]:
                    fields_a[n_a2][b_a2] = get_field(b_a2, mask_sigma=bool(n_a2))
                if b_b1 not in fields_a[n_b1]:
                    fields_a[n_b1][b_b1] = get_field(b_b1, mask_sigma=bool(n_b1))
                if b_b2 not in fields_a[n_b2]:
                    fields_a[n_b2][b_b2] = get_field(b_b2, mask_sigma=bool(n_b2))
                printflush(" - Computing " + name)
                cw_sn[name].compute_coupling_coefficients(fields_a[n_a1][b_a1],
                                                          fields_a[n_a2][b_a2],
                                                          fields_a[n_b1][b_b1],
                                                          fields_a[n_b2][b_b2])
                cw_sn[name].write_to(fname_cmcm)
예제 #4
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)
예제 #5
0
파일: cov.py 프로젝트: xC-ell/xCell
    def get_covariance_workspace(self):
        mask1, mask2 = self.clA1A2.get_masks_names()
        mask3, mask4 = self.clB1B2.get_masks_names()
        fname = os.path.join(self.outdir,
                             f'cw__{mask1}__{mask2}__{mask3}__{mask4}.fits')
        cw = nmt.NmtCovarianceWorkspace()
        recompute = self.data.data['recompute']['cmcm']
        if recompute or (not os.path.isfile(fname)):
            n_iter = self.data.data['sphere']['n_iter_cmcm']
            l_toeplitz, l_exact, dl_band = self.data.check_toeplitz('cov')
            fA1, fB1 = self.clA1B1.get_nmt_fields()
            fA2, fB2 = self.clA2B2.get_nmt_fields()
            cw.compute_coupling_coefficients(fA1, fA2, fB1, fB2,
                                             n_iter=n_iter,
                                             l_toeplitz=l_toeplitz,
                                             l_exact=l_exact,
                                             dl_band=dl_band)
            # Recheck again in case other process has started writing it
            if (not os.path.isfile(fname)):
                cw.write_to(fname)
            self.recompute_cmcm = False
        else:
            cw.read_from(fname)

        return cw
예제 #6
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]
예제 #7
0
 def get_covariance_coeff(self):
     if self.cw is None:
         cw = nmt.NmtCovarianceWorkspace()
         cw.compute_coupling_coefficients(self.f0,
                                          self.f0,
                                          self.f0,
                                          self.f0,
                                          lmax=self.lmax)
         self.cw = cw
예제 #8
0
def get_cmcm(f1, f2, f3, f4):
    fname = p.get_fname_cmcm(f1, f2, f3, f4)
    cmcm = nmt.NmtCovarianceWorkspace()
    try:
        cmcm.read_from(fname)
    except:
        #        print("  Computing CMCM")
        cmcm.compute_coupling_coefficients(f1.field, f2.field, f3.field,
                                           f4.field)
        cmcm.write_to(fname)
    return cmcm
예제 #9
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())
예제 #10
0
def get_cmcm(p, f1, f2, f3, f4):
    fname = p.get_fname_cmcm(f1.mask_id, f2.mask_id, f3.mask_id, f4.mask_id)
    cmcm = nmt.NmtCovarianceWorkspace()
    try:
        cmcm.read_from(fname)
    except:
        cmcm.compute_coupling_coefficients(f1.field, f2.field, f3.field,
                                           f4.field)
        try:
            cmcm.write_to(fname)
        except RuntimeError:
            pass
    return cmcm
예제 #11
0
파일: utils.py 프로젝트: damonge/S8z
 def get_workspace(self, fields):
     fname, found = self._get_workspace_file(self.msks_1, self.msks_2)
     cw = nmt.NmtCovarianceWorkspace()
     if found and (not self.recompute):
         cw.read_from(fname)
     else:
         print("Computing " + fname)
         cw.compute_coupling_coefficients(
             fields[self.tracers_1[0]].get_field(),
             fields[self.tracers_1[1]].get_field(),
             fields[self.tracers_2[0]].get_field(),
             fields[self.tracers_2[1]].get_field())
         cw.write_to(fname)
     return cw
예제 #12
0
파일: clcalc.py 프로젝트: damonge/cmb-lss
 def get_covariance_workspace(self, typ1, typ2):
     tttt = typ1 + typ2
     fname = self.get_clfile_name(tttt, 'cwsp', 'fits')
     cwsp = nmt.NmtCovarianceWorkspace()
     if not os.path.isfile(fname):
         if 'g' in tttt:
             self.get_delta_field()
         if 'k' in tttt:
             self.get_kappa_field()
         fl = {'g': self.f_d, 'k': self.f_k}
         t1, t2, t3, t4 = tttt
         cwsp.compute_coupling_coefficients(fl[t1], fl[t2], fl[t3], fl[t4],
                                            **(self.toe_cov))
         cwsp.write_to(fname)
     else:
         cwsp.read_from(fname)
     return cwsp
예제 #13
0
 def get_cw_nn(b_a1, b_a2, b_b1, b_b2):
     name = "%d%d_%d%d" % (b_a1, b_a2, b_b1, b_b2)
     if name in cw_nn:
         pass
     else:
         cw_nn[name] = nmt.NmtCovarianceWorkspace()
         fname_cmcm = prefix_cmcm + name + '_ns%d.fits' % o.nside
         if os.path.isfile(fname_cmcm):
             printflush(" - Reading")
             cw_nn[name].read_from(fname_cmcm)
         else:
             printflush(" - Fields")
             for b in [b_a1, b_a2, b_b1, b_b2]:
                 fields_n[b] = get_field(b, mask_sigma=True)
             printflush(" - Computing " + name)
             cw_nn[name].compute_coupling_coefficients(fields_n[b_a1], fields_n[b_a2],
                                                       fields_n[b_b1], fields_n[b_b2])
             cw_nn[name].write_to(fname_cmcm)
예제 #14
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
예제 #15
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)
예제 #16
0
def generate_covariance_workspace00(run_path, fa1, fa2, flat=False):
    if flat:
        w00 = nmt.NmtWorkspaceFlat()
        cw00 = nmt.NmtCovarianceWorkspaceFlat()
    else:
        w00 = nmt.NmtWorkspace()
        cw00 = nmt.NmtCovarianceWorkspace()

    w00.read_from(run_path + "_w00.dat")

    cw00_file = run_path + "_cw00.dat"
    if not os.path.isfile(cw00_file):
        if flat:
            raise ValueError('flat not implemented yet')
            # cw00.compute_coupling_coefficients(w00, w00)
        else:
            cw00.compute_coupling_coefficients(fa1, fa2)
        cw00.write_to(cw00_file)
    else:
        cw00.read_from(cw00_file)

    return w00, cw00
예제 #17
0
    def get_covariance_workspace(self):
        mask1 = os.path.basename(self.data['tracers'][self.trA1]['mask'])
        mask2 = os.path.basename(self.data['tracers'][self.trA2]['mask'])
        mask3 = os.path.basename(self.data['tracers'][self.trB1]['mask'])
        mask4 = os.path.basename(self.data['tracers'][self.trB2]['mask'])
        # Remove the extension
        mask1 = os.path.splitext(mask1)[0]
        mask2 = os.path.splitext(mask2)[0]
        mask3 = os.path.splitext(mask3)[0]
        mask4 = os.path.splitext(mask4)[0]
        fname = os.path.join(
            self.outdir,
            'cw__{}__{}__{}__{}.fits'.format(mask1, mask2, mask3, mask4))
        cw = nmt.NmtCovarianceWorkspace()
        if not os.path.isfile(fname):
            n_iter = self.data['healpy']['n_iter_cmcm']
            if 'toeplizt' in self.data:
                l_toeplitz = self.data['toeplizt']['l_toeplitz']
                l_exact = self.data['toeplizt']['l_exact']
                dl_band = self.data['toeplizt']['dl_band']
            else:
                l_toeplitz = l_exact = dl_band = -1
            fA1, fB1 = self.clA1B1.get_fields()
            fA2, fB2 = self.clA2B2.get_fields()
            cw.compute_coupling_coefficients(fA1.f,
                                             fA2.f,
                                             fB1.f,
                                             fB2.f,
                                             n_iter=n_iter,
                                             l_toeplitz=l_toeplitz,
                                             l_exact=l_exact,
                                             dl_band=dl_band)
            cw.write_to(fname)
        else:
            cw.read_from(fname)

        return cw
예제 #18
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])
예제 #19
0
k = '%d%d' % (o.bin_a1, o.bin_b2)
if k not in clt:
    clt[k] = get_cl(tracers, o.bin_a1, o.bin_b2)
k = '%d%d' % (o.bin_a2, o.bin_b1)
if k not in clt:
    clt[k] = get_cl(tracers, o.bin_a2, o.bin_b1)
k = '%d%d' % (o.bin_a2, o.bin_b2)
if k not in clt:
    clt[k] = get_cl(tracers, o.bin_a2, o.bin_b2)


printflush("CMCM")
fname_cmcm = predir + 'cls_metacal_cmcm_bins_'
fname_cmcm += '%d%d_%d%d_ns%d.fits' % (o.bin_a1, o.bin_a2, o.bin_b1, o.bin_b2, o.nside)

cw = nmt.NmtCovarianceWorkspace()
fields_s = {}
fields_n = {}
if os.path.isfile(fname_cmcm) and not o.recompute_mcm:
    printflush(" - Reading")
    cw.read_from(fname_cmcm)
else:
    printflush(" - Fields")
    if o.bin_a1 not in fields_s:
        fields_s[o.bin_a1] = get_field(o.bin_a1)
    if o.bin_a2 not in fields_s:
        fields_s[o.bin_a2] = get_field(o.bin_a2)
    if o.bin_b1 not in fields_s:
        fields_s[o.bin_b1] = get_field(o.bin_b1)
    if o.bin_b2 not in fields_s:
        fields_s[o.bin_b2] = get_field(o.bin_b2)
예제 #20
0
    w02.read_from(prefix_out + "_w02_02.dat")

w22 = nmt.NmtWorkspace()
if not os.path.isfile(prefix_out + "_w22_22.dat"):  #spin0-spin2
    print("Computing w22")
    w22.compute_coupling_matrix(f2, f2, b, n_iter=o.n_iter)
    w22.write_to(prefix_out + "_w22_22.dat")
else:
    w22.read_from(prefix_out + "_w22_22.dat")

##############################################################################
# Compute covariance (only depends on the masks)
##############################################################################
if not os.path.isfile(prefix_out +
                      '_covTh{}.npz'.format('old' if o.oldcov else '')):
    cw00_00 = nmt.NmtCovarianceWorkspace()
    if not os.path.isfile(prefix_out +
                          "_cw0000.dat"):  # mask0-mask0-mask0-mask0
        print("Computing cw0000")
        cw00_00.compute_coupling_coefficients(f0, f0, f0, f0, n_iter=o.n_iter)
        cw00_00.write_to(prefix_out + "_cw0000.dat")
    else:
        cw00_00.read_from(prefix_out + '_cw0000.dat')

    cw00_02 = nmt.NmtCovarianceWorkspace()
    if not os.path.isfile(prefix_out +
                          "_cw0002.dat"):  # mask0-mask0-mask0-mask2
        print("Computing cw0002")
        cw00_02.compute_coupling_coefficients(f0, f0, f0, f2, n_iter=o.n_iter)
        cw00_02.write_to(prefix_out + "_cw0002.dat")
    else:
예제 #21
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)
예제 #22
0
def test_cls_vs_namaster():
    # cls
    # Get cl from randomnly generated map ("data")
    cl_class = get_cl_class()
    ell, cl_data = cl_class.get_ell_cl()
    b = cl_class.get_NmtBin()
    win = cl_class.get_bandpower_windows()
    # Read output
    clfile = np.load(os.path.join(tmpdir1,
                                  'Dummy_Dummy',
                                  'cl_Dummy__0_Dummy__0.npz'))
    shutil.rmtree(tmpdir1)

    # Compute covariance
    cov_class = get_cov_class()
    cov = cov_class.get_covariance()
    shutil.rmtree(tmpdir1)

    # NaMaster
    config = get_config()
    conf = config['tracers']['Dummy__0']
    conf['nside'] = config['sphere']['nside']
    conf['coords'] = config['sphere']['coords']
    m = MapperDummy(conf)

    # True cl
    cl_m = m.get_cl()
    spin = m.get_spin()
    mask = m.get_mask()
    signal_map = m.get_signal_map()
    n_iter_sht = config['sphere']['n_iter_sht']
    n_iter_mcm = config['sphere']['n_iter_mcm']
    n_iter_cmcm = config['sphere']['n_iter_cmcm']
    # Compute Cl from map
    f = nmt.NmtField(mask, signal_map, spin=spin, n_iter=n_iter_sht)
    wsp = nmt.NmtWorkspace()
    wsp.compute_coupling_matrix(f, f, bins=b, n_iter=n_iter_mcm)
    cl_data_nmt_cp = nmt.compute_coupled_cell(f, f)
    cl_data_nmt = wsp.decouple_cell(cl_data_nmt_cp)

    # Couple true Cl
    cl_m_cp = wsp.couple_cell([cl_m])
    cl_m = wsp.decouple_cell(cl_m_cp)

    # Compute cov with NaMaster
    cwsp = nmt.NmtCovarianceWorkspace()
    cwsp.compute_coupling_coefficients(f, f, n_iter=n_iter_cmcm)
    cl_cov = cl_m_cp / np.mean(mask * mask)
    cov_nmt = nmt.gaussian_covariance(cwsp, spin, spin, spin, spin, cl_cov,
                                      cl_cov, cl_cov, cl_cov, wsp)
    bpwin = wsp.get_bandpower_windows()
    icov_nmt = np.linalg.inv(cov_nmt)

    def compare(cl, cv, wn, tol=1E-5):
        rdev = cl / cl_data_nmt - 1
        assert np.max(np.abs(rdev)) < tol

        # Compare cl and covariance
        icov = np.linalg.inv(cv)
        dCl = (cl - cl_m)[0]
        chi2 = dCl.dot(icov).dot(dCl)
        chi2_m = dCl.dot(icov_nmt).dot(dCl)
        assert np.fabs(chi2/chi2_m-1) < tol

        # Compare bandpower windows
        assert np.all(win == bpwin)

    compare(cl_data, cov, win)
    compare(clfile['cl'], cov, clfile['wins'])
    assert np.allclose(clfile['cl_cp'], cl_data_nmt_cp, atol=0)
    assert np.allclose(clfile['cl_cov_cp'], cl_data_nmt_cp, atol=0)
    assert np.allclose(clfile['cl_cov_11_cp'], cl_data_nmt_cp, atol=0)
    assert np.allclose(clfile['cl_cov_12_cp'], cl_data_nmt_cp, atol=0)
    assert np.allclose(clfile['cl_cov_22_cp'], cl_data_nmt_cp, atol=0)
예제 #23
0
def compute_covariance_full(clTh, nls_all, nbpw, nbins, maps_bins, maps_spins,
                            maps_masks, masks):

    nmaps = len(maps_bins)
    fname_cw_old = ''

    cl_indices = []
    cl_spins = []
    cl_bins = []
    cl_masks = []
    for i in range(nmaps):
        si = maps_spins[i]
        for j in range(i, nmaps):
            if (maps_masks[i] == 0) and (maps_masks[j] == 0) and (
                    i != j):  # Don't compute gcX-gcY for X!=Y
                print('Skipping', i, j)
                continue
            sj = maps_spins[j]
            cl_indices.append([i, j])
            cl_spins.append([si, sj])
            cl_bins.append([maps_bins[i], maps_bins[j]])
            cl_masks.append([maps_masks[i], maps_masks[j]])

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

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

    old_workspaces = None
    old_masks = None
    for i, indices in enumerate(cov_indices):
        s_a1, s_a2, s_b1, s_b2 = cov_spins[i]
        m_a1, m_a2, m_b1, m_b2 = cov_masks[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 = os.path.join(
            outdir,
            'cov_s{}{}{}{}_b{}{}{}{}.npz'.format(*cov_spins[i], *cov_bins[i]))
        if os.path.isfile(fname):
            continue

        ibin_a1 = np.where(maps_bins == bin_a1)[0][0]
        ibin_a2 = np.where(maps_bins == bin_a2)[0][0]
        ibin_b1 = np.where(maps_bins == bin_b1)[0][0]
        ibin_b2 = np.where(maps_bins == bin_b2)[0][0]

        cla1b1 = np.concatenate(clTh[ibin_a1:ibin_a1 + na1,
                                     ibin_b1:ibin_b1 + nb1])
        cla1b2 = np.concatenate(clTh[ibin_a1:ibin_a1 + na1,
                                     ibin_b2:ibin_b2 + nb2])
        cla2b1 = np.concatenate(clTh[ibin_a2:ibin_a2 + na2,
                                     ibin_b1:ibin_b1 + nb1])
        cla2b2 = np.concatenate(clTh[ibin_a2:ibin_a2 + na2,
                                     ibin_b2:ibin_b2 + nb2])

        nla1b1 = np.concatenate(nls_all[ibin_a1:ibin_a1 + na1,
                                        ibin_b1:ibin_b1 + nb1])
        nla1b2 = np.concatenate(nls_all[ibin_a1:ibin_a1 + na1,
                                        ibin_b2:ibin_b2 + nb2])
        nla2b1 = np.concatenate(nls_all[ibin_a2:ibin_a2 + na2,
                                        ibin_b1:ibin_b1 + nb1])
        nla2b2 = np.concatenate(nls_all[ibin_a2:ibin_a2 + na2,
                                        ibin_b2:ibin_b2 + nb2])

        ##### Couple Cl
        wa1b1, wa1b2, wa2b1, wa2b2, wa, wb = get_workspaces_waXbY(
            m_a1, m_a2, m_b1, m_b2, old_masks, old_workspaces)
        # Free space!!
        old_masks = {}
        old_workspaces = {}
        #
        cla1b1 = wa1b1.couple_cell(cla1b1)
        cla1b2 = wa1b2.couple_cell(cla1b2)
        cla2b1 = wa2b1.couple_cell(cla2b1)
        cla2b2 = wa2b2.couple_cell(cla2b2)
        #####

        #### Weight the Cls
        cla1b1 = (cla1b1 + nla1b1) / np.mean(masks[m_a1] * masks[m_b1])
        cla1b2 = (cla1b2 + nla1b2) / np.mean(masks[m_a1] * masks[m_b2])
        cla2b1 = (cla2b1 + nla2b1) / np.mean(masks[m_a2] * masks[m_b1])
        cla2b2 = (cla2b2 + nla2b2) / np.mean(masks[m_a2] * masks[m_b2])
        ####

        fname_cw = os.path.join(obsdir, 'cw{}{}{}{}.dat'.format(*cov_masks[i]))
        if fname_cw != fname_cw_old:
            cw = nmt.NmtCovarianceWorkspace()
            cw.read_from(fname_cw)
            fname_cw_old = fname_cw

        # cla1b1_label = np.concatenate(Cls[ibin_a1 : ibin_a1 + na1, ibin_b1 : ibin_b1 + nb1])
        # cla1b2_label = np.concatenate(Cls[ibin_a1 : ibin_a1 + na1, ibin_b2 : ibin_b2 + nb2])
        # cla2b1_label = np.concatenate(Cls[ibin_a2 : ibin_a2 + na2, ibin_b1 : ibin_b1 + nb1])
        # cla2b2_label = np.concatenate(Cls[ibin_a2 : ibin_a2 + na2, ibin_b2 : ibin_b2 + nb2])

        # print(np.concatenate(cla1b1))
        # print(np.concatenate(cla1b2))
        # print(np.concatenate(cla2b1))
        # print(np.concatenate(cla2b2))

        print('Computing {}'.format(fname))
        # print('spins: ', s_a1, s_a2, s_b1, s_b2)
        # print('cla1b1', (s_a1, s_b1), cla1b1.shape, ibin_a1, ibin_a1 + na1, ibin_b1, ibin_b1 + nb1, cla1b1_label)
        # print('cla1b2', (s_a1, s_b2), cla1b2.shape, ibin_a1, ibin_a1 + na1, ibin_b2, ibin_b2 + nb2, cla1b2_label)
        # print('cla2b1', (s_a2, s_b1), cla2b1.shape, ibin_a2, ibin_a2 + na2, ibin_b1, ibin_b1 + nb1, cla2b1_label)
        # print('cla2b2', (s_a2, s_b2), cla2b2.shape, ibin_a2, ibin_a2 + na2, ibin_b2, ibin_b2 + nb2, cla2b2_label)

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

        np.savez_compressed(fname, cov)

        tracer_names = [co.get_tracer_name(ibin) for ibin in cov_bins[i]]
        fname_new = os.path.join(outdir,
                                 'cov_{}_{}_{}_{}.npz'.format(*tracer_names))
        #  TT -> 0; TE -> 0;  EE -> 0
        np.savez_compressed(
            fname_new,
            cov.reshape((nbpw, na1 * na2, nbpw, nb1 * nb2))[:, 0, :, 0])
        print('Computed {}'.format(fname_new))

        old_masks = {
            'a1': m_a1,
            'a_2': m_a2,
            'b1': m_b1,
            'b2': m_b2,
            'a1a2': [m_a1, m_a2],
            'b1b2': [m_b1, m_b2],
            'a1b1': [m_a1, m_b1],
            'a1b2': [m_a1, m_b2],
            'a2b1': [m_a2, m_b1],
            'a2b2': [m_a2, m_b2]
        }
        old_workspaces = {
            'a1b1': wa1b1,
            'a1b2': wa1b2,
            'a2b1': wa2b1,
            'a2b2': wa2b2,
            'a1a2': wa,
            'b1b2': wb,
            'wa': wa,
            'wb': wb
        }
예제 #24
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)
예제 #25
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)
예제 #26
0
# of l_toeplitz, l_exact and dl_band are arbitrary, and should not
# be understood as a rule of thumb.
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))