Esempio n. 1
0
    def test_workspace_covar_flat_benchmark(self):
        cw = nmt.NmtCovarianceWorkspaceFlat()
        cw.compute_coupling_coefficients(self.w, self.w)

        covar = nmt.gaussian_covariance_flat(cw, self.l, self.cltt, self.cltt,
                                             self.cltt, self.cltt)
        covar_bench = np.loadtxt("test/benchmarks/bm_f_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())
Esempio n. 2
0
    def get_covar_mcm(self, tracers, bpws):
        """
        Get NmtCovarianceWorkspaceFlat for our mask
        """
        cwsp = nmt.NmtCovarianceWorkspaceFlat()
        if not os.path.isfile(self.get_output_fname('cov_mcm', ext='dat')):
            print("Computing covariance MCM")
            cwsp.compute_coupling_coefficients(tracers[0].field,
                                               tracers[0].field, bpws)
            cwsp.write_to(self.get_output_fname('cov_mcm', ext='dat'))
        else:
            print("Reading covariance MCM")
            cwsp.read_from(self.get_output_fname('cov_mcm', ext='dat'))

        return cwsp
Esempio n. 3
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
Esempio n. 4
0
    def test_workspace_covar_flat_errors(self):
        cw = nmt.NmtCovarianceWorkspaceFlat()

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

        cw.compute_coupling_coefficients(self.f0, self.f0, self.b)  # All good
        self.assertEqual(cw.wsp.bin.n_bands, self.w.wsp.bin.n_bands)

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

        cw.read_from('test/benchmarks/bm_f_nc_np_cw00.fits')  # Correct reading
        self.assertEqual(cw.wsp.bin.n_bands, self.w.wsp.bin.n_bands)

        # gaussian_covariance
        with self.assertRaises(ValueError):  # Wrong input power spectra
            nmt.gaussian_covariance_flat(cw, 0, 0, 0, 0, self.ll, [self.cltt],
                                         [self.cltt], [self.cltt],
                                         [self.cltt[:15]], self.w)
        with self.assertRaises(ValueError):  # Wrong input power shapes
            nmt.gaussian_covariance_flat(cw, 0, 0, 0, 0, self.ll,
                                         [self.cltt, self.cltt], [self.cltt],
                                         [self.cltt], [self.cltt[:15]], self.w)
        with self.assertRaises(ValueError):  # Wrong input spins
            nmt.gaussian_covariance_flat(cw, 0, 2, 0, 0, self.ll, [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, self.b)
        with self.assertRaises(RuntimeError):  # Incompatible bandpowers
            cw.compute_coupling_coefficients(self.f0, self.f0, self.b, self.f0,
                                             self.f0, self.b_half)
Esempio n. 5
0
    def test_workspace_covar_flat_errors(self):
        cw = nmt.NmtCovarianceWorkspaceFlat()

        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.bin.n_bands, self.w.wsp.bin.n_bands)

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

        cw.read_from('test/benchmarks/bm_f_nc_np_cw00.dat')  #Correct reading
        self.assertEqual(cw.wsp.bin.n_bands, self.w.wsp.bin.n_bands)

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

        with self.assertRaises(RuntimeError):  #Incorrect reading
            cw.read_from('none')
        w2 = nmt.NmtWorkspaceFlat()
        w2.read_from("test/benchmarks/bm_f_nc_np_w00.dat")
        w2.wsp.fs.nx = self.w.wsp.fs.nx // 2
        with self.assertRaises(ValueError):  #Incompatible resolutions
            cw.compute_coupling_coefficients(self.w, w2)
        w2.wsp.fs.nx = self.w.wsp.fs.nx
        w2.wsp.bin.n_bands = self.w.wsp.bin.n_bands // 2
        with self.assertRaises(RuntimeError):  #Incompatible resolutions
            cw.compute_coupling_coefficients(self.w, w2)
        w2.wsp.bin.n_bands = self.w.wsp.bin.n_bands

        w2.read_from("test/benchmarks/bm_f_nc_np_w02.dat")
        with self.assertRaises(ValueError):  #Spin-2
            cw.compute_coupling_coefficients(self.w, w2)
Esempio n. 6
0
    def test_workspace_covar_flat_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())

        cw = nmt.NmtCovarianceWorkspaceFlat()
        cw.compute_coupling_coefficients(self.f0, self.f0, self.b)

        # [0,0 ; 0,0]
        covar = nmt.gaussian_covariance_flat(cw, 0, 0, 0, 0, self.ll,
                                             [self.cltt], [self.cltt],
                                             [self.cltt], [self.cltt], self.w)
        covar_bench = np.loadtxt("test/benchmarks/bm_f_nc_np_cov.txt",
                                 unpack=True)
        compare_covars(covar, covar_bench)
        # [0,2 ; 0,2]
        covar = nmt.gaussian_covariance_flat(
            cw,
            0,
            2,
            0,
            2,
            self.ll, [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_f_nc_np_cov0202.txt")
        compare_covars(covar, covar_bench)
        # [0,0 ; 0,2]
        covar = nmt.gaussian_covariance_flat(cw,
                                             0,
                                             0,
                                             0,
                                             2,
                                             self.ll, [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_f_nc_np_cov0002.txt")
        compare_covars(covar, covar_bench)
        # [0,0 ; 2,2]
        covar = nmt.gaussian_covariance_flat(cw,
                                             0,
                                             0,
                                             2,
                                             2,
                                             self.ll,
                                             [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_f_nc_np_cov0022.txt")
        compare_covars(covar, covar_bench)
        # [2,2 ; 2,2]
        covar = nmt.gaussian_covariance_flat(
            cw,
            2,
            2,
            2,
            2,
            self.ll, [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_f_nc_np_cov2222.txt")
        compare_covars(covar, covar_bench)
Esempio n. 7
0
)

w00 = nmt.NmtWorkspaceFlat()
w00.compute_coupling_matrix(f0, f0, b)
#Coupling matrix used to estimate angular spectrum
cl00_coupled = nmt.compute_coupled_cell_flat(f0, f0, b)
cl00_uncoupled = w00.decouple_cell(cl00_coupled)[0]
print(cl00_uncoupled)

amp = abs((ells_uncoupled**2) * cl00_uncoupled / (2 * np.pi))**(1 / 2)

print(
    "\n*************************  Covariance matrix  *************************************\n"
)

cw = nmt.NmtCovarianceWorkspaceFlat()
cw.compute_coupling_coefficients(f0, f0, b)
covar = nmt.gaussian_covariance_flat(cw, 0, 0, 0, 0, ells_uncoupled,
                                     [cl00_uncoupled], [cl00_uncoupled],
                                     [cl00_uncoupled], [cl00_uncoupled], w00)

print(covar)
std_power = (np.diag(covar))
std_amp = np.sqrt(abs((ells_uncoupled**2) * std_power / (2 * np.pi))**(1 / 2))

"--------------------------------- Fitting a power law -------------------------------------"

# def power_law(x,a,p):
#     return a*np.power(x,p)

# a_fit, p_fit = curve_fit(power_law, lambdas_inv, amp, p0 = [1e-2,5/3])[0]
Esempio n. 8
0
    def get_covar_mcm(self, tracers, bpws, tracerCombInd=None):
        """
        Get NmtCovarianceWorkspaceFlat for our mask
        """

        logger.info("Computing covariance MCM.")

        cwsp = [[[[0 for i in range(self.ntracers)]
                  for ii in range(self.ntracers)]
                 for j in range(self.ntracers)] for jj in range(self.ntracers)]

        tracer_combs = []
        for i1 in range(self.ntracers):
            for j1 in range(i1, self.ntracers):
                tracer_combs.append((i1, j1))

        tracer_type_arr = [tr.type for tr in tracers]

        tracerCombInd_curr = 0
        for k1, tup1 in enumerate(tracer_combs):
            tr_i1, tr_j1 = tup1
            for tr_i2, tr_j2 in tracer_combs[k1:]:
                # Check if we need to compute this cwsp
                if tracerCombInd is not None:
                    if tracerCombInd_curr == tracerCombInd:
                        logger.info(
                            'tracerCombInd = {}.'.format(tracerCombInd))
                        logger.info(
                            'Computing cwsp for tracers = {}, {}.'.format(
                                (tr_i1, tr_j1), (tr_i2, tr_j2)))
                        do_cwsp = True
                    else:
                        do_cwsp = False
                else:
                    do_cwsp = True

                if do_cwsp:
                    # File does not exist
                    if not os.path.isfile(
                            self.get_output_fname('cov_mcm') +
                            '_{}{}{}{}'.format(tr_i1, tr_j1, tr_i2, tr_j2) +
                            '.dat'):
                        tr_types_cur = np.array([
                            tracers[tr_i1].type, tracers[tr_j1].type,
                            tracers[tr_i2].type, tracers[tr_j2].type
                        ])

                        # All galaxy maps
                        if set(tr_types_cur) == {
                                'galaxy_density', 'galaxy_density',
                                'galaxy_density', 'galaxy_density'
                        }:
                            if not hasattr(self, 'cwsp_counts'):
                                counts_indx = tracer_type_arr.index(
                                    'galaxy_density')
                                if not os.path.isfile(
                                        self.get_output_fname('cov_mcm') +
                                        '_{}{}{}{}'.format(
                                            counts_indx, counts_indx,
                                            counts_indx, counts_indx) +
                                        '.dat'):
                                    # Compute wsp for counts (is always the same as mask is the same)
                                    self.cwsp_counts = nmt.NmtCovarianceWorkspaceFlat(
                                    )
                                    logger.info(
                                        "Computing covariance MCM for counts.")
                                    self.cwsp_counts.compute_coupling_coefficients(
                                        tracers[0].field, tracers[0].field,
                                        bpws)
                                    self.cwsp_counts.write_to(
                                        self.get_output_fname('cov_mcm') +
                                        '_{}{}{}{}'.format(
                                            counts_indx, counts_indx,
                                            counts_indx, counts_indx) + '.dat')
                                    logger.info(
                                        "Covariance MCM written to {}.".format(
                                            self.get_output_fname('cov_mcm') +
                                            '_{}{}{}{}'.format(
                                                counts_indx, counts_indx,
                                                counts_indx, counts_indx) +
                                            '.dat'))
                                else:
                                    logger.info(
                                        "Reading covariance MCM for counts.")
                                    self.cwsp_counts = nmt.NmtCovarianceWorkspaceFlat(
                                    )
                                    self.cwsp_counts.read_from(
                                        self.get_output_fname('cov_mcm') +
                                        '_{}{}{}{}'.format(
                                            counts_indx, counts_indx,
                                            counts_indx, counts_indx) + '.dat')
                                    logger.info(
                                        "Covariance MCM read from {}.".format(
                                            self.get_output_fname('cov_mcm') +
                                            '_{}{}{}{}'.format(
                                                counts_indx, counts_indx,
                                                counts_indx, counts_indx) +
                                            '.dat'))

                            cwsp_curr = self.cwsp_counts

                        # At least one galaxy map
                        elif 'galaxy_density' in tr_types_cur:
                            counts_indx = tracer_type_arr.index(
                                'galaxy_density')
                            i1_curr = tr_i1
                            j1_curr = tr_j1
                            i2_curr = tr_i2
                            j2_curr = tr_j2
                            if tracers[tr_i1].type == 'galaxy_density':
                                i1_curr = counts_indx
                            if tracers[tr_j1].type == 'galaxy_density':
                                j1_curr = counts_indx
                            if tracers[tr_i2].type == 'galaxy_density':
                                i2_curr = counts_indx
                            if tracers[tr_j2].type == 'galaxy_density':
                                j2_curr = counts_indx
                            cwsp_curr = nmt.NmtCovarianceWorkspaceFlat()
                            if not os.path.isfile(
                                    self.get_output_fname('cov_mcm') +
                                    '_{}{}{}{}'.format(i1_curr, j1_curr,
                                                       i2_curr, j2_curr) +
                                    '.dat'):
                                # Compute wsp for counts (is always the same as mask is the same)
                                logger.info(
                                    "Computing covariance MCM for counts xcorr."
                                )
                                cwsp_curr.compute_coupling_coefficients(
                                    tracers[i1_curr].field,
                                    tracers[j1_curr].field, bpws,
                                    tracers[i2_curr].field,
                                    tracers[j2_curr].field, bpws)
                                cwsp_curr.write_to(
                                    self.get_output_fname('cov_mcm') +
                                    '_{}{}{}{}'.format(i1_curr, j1_curr,
                                                       i2_curr, j2_curr) +
                                    '.dat')
                                logger.info(
                                    "Covariance MCM written to {}.".format(
                                        self.get_output_fname('cov_mcm') +
                                        '_{}{}{}{}'.format(
                                            i1_curr, j1_curr, i2_curr,
                                            j2_curr) + '.dat'))
                            else:
                                logger.info(
                                    "Reading covariance MCM for counts xcorr.")
                                cwsp_curr.read_from(
                                    self.get_output_fname('cov_mcm') +
                                    '_{}{}{}{}'.format(i1_curr, j1_curr,
                                                       i2_curr, j2_curr) +
                                    '.dat')
                                logger.info(
                                    "Covariance MCM read from {}.".format(
                                        self.get_output_fname('cov_mcm') +
                                        '_{}{}{}{}'.format(
                                            i1_curr, j1_curr, i2_curr,
                                            j2_curr) + '.dat'))

                        # No galaxy maps
                        else:
                            logger.info(
                                "Computing covariance MCM for {}.".format(
                                    self.get_output_fname('cov_mcm') +
                                    '_{}{}{}{}'.format(tr_i1, tr_j1, tr_i2,
                                                       tr_j2) + '.dat'))
                            cwsp_curr = nmt.NmtCovarianceWorkspaceFlat()
                            cwsp_curr.compute_coupling_coefficients(
                                tracers[tr_i1].field, tracers[tr_j1].field,
                                bpws, tracers[tr_i2].field,
                                tracers[tr_j2].field, bpws)
                            # Write to file
                            cwsp_curr.write_to(
                                self.get_output_fname('cov_mcm') +
                                '_{}{}{}{}'.format(tr_i1, tr_j1, tr_i2,
                                                   tr_j2) + '.dat')

                    # File exists
                    else:
                        logger.info("Reading covariance MCM for {}.".format(
                            self.get_output_fname('cov_mcm') +
                            '_{}{}{}{}'.format(tr_i1, tr_j1, tr_i2, tr_j2) +
                            '.dat'))
                        cwsp_curr = nmt.NmtCovarianceWorkspaceFlat()
                        cwsp_curr.read_from(
                            self.get_output_fname('cov_mcm') +
                            '_{}{}{}{}'.format(tr_i1, tr_j1, tr_i2, tr_j2) +
                            '.dat')

                    cwsp[tr_i1][tr_j1][tr_i2][tr_j2] = cwsp_curr

                tracerCombInd_curr += 1

        return cwsp