Пример #1
0
    def setUp(self):
        wcs, msk = read_flat_map("test/benchmarks/msk_flat.fits")
        (ny, nx) = msk.shape
        lx = np.radians(np.fabs(nx * wcs.wcs.cdelt[0]))
        ly = np.radians(np.fabs(ny * wcs.wcs.cdelt[1]))
        mps = np.array([
            read_flat_map("test/benchmarks/mps_flat.fits", i_map=i)[1]
            for i in range(3)
        ])

        d_ell = 20
        lmax = 500.
        ledges = np.arange(int(lmax / d_ell) + 1) * d_ell + 2
        self.b = nmt.NmtBinFlat(ledges[:-1], ledges[1:])
        ledges_half = ledges[:len(ledges) // 2]
        self.b_half = nmt.NmtBinFlat(ledges_half[:-1], ledges_half[1:])
        self.f0 = nmt.NmtFieldFlat(lx, ly, msk, [mps[0]])
        self.f2 = nmt.NmtFieldFlat(lx, ly, msk, [mps[1], mps[2]])
        self.f0_half = nmt.NmtFieldFlat(lx, ly, msk[:ny // 2, :nx // 2],
                                        [mps[0, :ny // 2, :nx // 2]])
        self.w = nmt.NmtWorkspaceFlat()
        self.w.read_from("test/benchmarks/bm_f_nc_np_w00.fits")
        self.w02 = nmt.NmtWorkspaceFlat()
        self.w02.read_from("test/benchmarks/bm_f_nc_np_w02.fits")
        self.w22 = nmt.NmtWorkspaceFlat()
        self.w22.read_from("test/benchmarks/bm_f_nc_np_w22.fits")

        cls = np.loadtxt("test/benchmarks/cls_lss.txt", unpack=True)
        l, cltt, clee, clbb, clte, nltt, nlee, nlbb, nlte = cls
        self.ll = l
        self.cltt = cltt + nltt
        self.clee = clee + nlee
        self.clbb = clbb + nlbb
        self.clte = clte
Пример #2
0
    def __init__(self):
        # This is to avoid showing an ugly warning
        # that has nothing to do with pymaster
        if (sys.version_info > (3,  1)):
            warnings.simplefilter("ignore",  ResourceWarning)

        self.wcs, self.msk = read_flat_map("test/benchmarks/msk_flat.fits")
        (self.ny, self.nx) = self.msk.shape
        self.lx = np.radians(np.fabs(self.nx*self.wcs.wcs.cdelt[0]))
        self.ly = np.radians(np.fabs(self.ny*self.wcs.wcs.cdelt[1]))
        self.mps = np.array([read_flat_map("test/benchmarks/mps_flat.fits",
                                           i_map=i)[1] for i in range(3)])
        self.mps_s1 = np.array([read_flat_map("test/benchmarks/"
                                              "mps_sp1_flat.fits",
                                              i_map=i)[1] for i in range(3)])
        self.tmp = np.array([read_flat_map("test/benchmarks/tmp_flat.fits",
                                           i_map=i)[1] for i in range(3)])
        self.d_ell = 20
        self.lmax = 500.
        ledges = np.arange(int(self.lmax/self.d_ell)+1)*self.d_ell+2
        self.b = nmt.NmtBinFlat(ledges[:-1], ledges[1:])
        self.leff = self.b.get_effective_ells()
        self.f0 = nmt.NmtFieldFlat(self.lx, self.ly, self.msk,
                                   [self.mps[0]])
        self.f0_half = nmt.NmtFieldFlat(self.lx, self.ly,
                                        self.msk[:self.ny//2,
                                                 :self.nx//2],
                                        [self.mps[0,
                                                  :self.ny//2,
                                                  :self.nx//2]])
        ledges_half = ledges[:len(ledges)//2]
        self.b_half = nmt.NmtBinFlat(ledges_half[:-1],
                                     ledges_half[1:])

        dd = np.loadtxt("test/benchmarks/cls_lss.txt",
                        unpack=True)
        l, cltt, clee, clbb, clte, nltt, nlee, nlbb, nlte = dd
        self.ll = l[:]
        self.cltt = cltt[:]
        self.clee = clee[:]
        self.clbb = clbb[:]
        self.clte = clte[:]
        self.nltt = nltt[:]
        self.nlee = nlee[:]
        self.nlbb = nlbb[:]
        self.nlte = nlte[:]
        self.n_good = np.zeros([1, len(l)])
        self.n_bad = np.zeros([2, len(l)])
        self.nb_good = np.zeros([1, self.b.bin.n_bands])
        self.nb_bad = np.zeros([2, self.b.bin.n_bands])
Пример #3
0
    def __init__(self, catfolder):
        """
        Holds all of the information relevant to a given HSC field
        """
        self.name = catfolder.split('_')[-3]
        self.filepath = catfolder

        self.cat = fits.open(self.filepath + 'clean_catalog.fits')[1].data

        self.masked_fraction_fp = self.filepath + 'masked_fraction.fits'
        self.masked_fraction = fits.open(
            self.masked_fraction_fp)[0].data.ravel()
        self.mask_binary = np.ones_like(self.masked_fraction)
        self.mask_binary[self.masked_fraction < 0.5] = 0.

        self.weight = self.masked_fraction * self.mask_binary
        self.goodpix = np.where(self.mask_binary > 0.1)[0]

        self.fsk, _ = read_flat_map(self.masked_fraction_fp)

        self.nmaps, self.nmaps_s1, self.nmaps_s2 = self.get_nmaps_split()

        self.fields = [field(self, thisbin) for thisbin in self.nmaps]
        self.fields_s1 = [field(self, thisbin) for thisbin in self.nmaps_s1]
        self.fields_s2 = [field(self, thisbin) for thisbin in self.nmaps_s2]

        self.ell_bins = nmt.NmtBinFlat(ell_bins[:-1], ell_bins[1:])
        self.ell_bins_uncpld = self.ell_bins.get_effective_ells()

        self.wsp = nmt.NmtWorkspaceFlat()
        self.wsp.compute_coupling_matrix(self.fields[0].field,
                                         self.fields[0].field, self.ell_bins)
Пример #4
0
    def run(self):
        """
        Main function.
        This stage:
        - Produces measurements of the power spectrum with and without contaminant deprojections.
        - Estimates the noise bias
        - Estimates the covariance matrix
        - Estimates the deprojection bias
        """
        self.parse_input()

        # Deal with the fact that SLURM only allows job array indices <= 1000
        if self.config['gt1000remd'] != 'NONE':
            logger.info(
                'SLURM jobID is larger than 1000. Old jobID = {}.'.format(
                    self.config['tracerCombInd']))
            self.config['tracerCombInd'] += int(self.config['gt1000remd'])
            logger.info('New jobID = {}.'.format(self.config['tracerCombInd']))

        logger.info("Reading mask.")
        self.msk_bi, self.mskfrac, self.mp_depth = self.get_masks()

        logger.info("Computing area.")
        self.area_pix = np.radians(self.fsk.dx) * np.radians(self.fsk.dy)
        self.area_patch = np.sum(self.msk_bi * self.mskfrac) * self.area_pix
        self.lmax = int(180. *
                        np.sqrt(1. / self.fsk.dx**2 + 1. / self.fsk.dy**2))

        logger.info("Reading contaminants.")
        temps = self.get_contaminants()

        logger.info("Setting bandpowers.")
        lini = np.array(self.config['ell_bpws'])[:-1]
        lend = np.array(self.config['ell_bpws'])[1:]
        bpws = nmt.NmtBinFlat(lini, lend)
        ell_eff = bpws.get_effective_ells()
        self.nbands = ell_eff.shape[0]
        logger.info('Number of ell bands = {}.'.format(self.nbands))

        tracers_nc, tracers_wc = self.get_all_tracers(temps)

        self.ntracers = len(tracers_nc)
        self.nmaps = self.ntracers_counts + self.ntracers_comptony + self.ntracers_kappa + 2 * self.ntracers_shear
        self.ncross = self.nmaps * (self.nmaps + 1) // 2 + self.ntracers_shear

        # Set up mapping
        self.mapping(tracers_nc)

        cwsp_curr = self.get_covar_mcm(
            tracers_wc, bpws, tracerCombInd=self.config['tracerCombInd'])

        # Permissions on NERSC
        os.system(
            'find /global/cscratch1/sd/damonge/GSKY/ -type d -exec chmod -f 777 {} \;'
        )
        os.system(
            'find /global/cscratch1/sd/damonge/GSKY/ -type f -exec chmod -f 666 {} \;'
        )
Пример #5
0
    def test_field_masked(self):
        wcs, msk = read_flat_map("test/benchmarks/msk_flat.fits")
        ny, nx = msk.shape
        lx = np.radians(np.fabs(nx * wcs.wcs.cdelt[0]))
        ly = np.radians(np.fabs(ny * wcs.wcs.cdelt[1]))
        mps = np.array([
            read_flat_map("test/benchmarks/mps_flat.fits", i_map=i)[1]
            for i in range(3)
        ])
        mps_msk = np.array([m * msk for m in mps])
        d_ell = 20
        lmax = 500.
        ledges = np.arange(int(lmax / d_ell) + 1) * d_ell + 2
        b = nmt.NmtBinFlat(ledges[:-1], ledges[1:])

        f0 = nmt.NmtFieldFlat(lx, ly, msk, [mps[0]])
        f0_msk = nmt.NmtFieldFlat(lx,
                                  ly,
                                  msk, [mps_msk[0]],
                                  masked_on_input=True)
        f2 = nmt.NmtFieldFlat(lx, ly, msk, [mps[1], mps[2]])
        f2_msk = nmt.NmtFieldFlat(lx,
                                  ly,
                                  msk, [mps_msk[1], mps_msk[2]],
                                  masked_on_input=True)
        w00 = nmt.NmtWorkspaceFlat()
        w00.compute_coupling_matrix(f0, f0, b)
        w02 = nmt.NmtWorkspaceFlat()
        w02.compute_coupling_matrix(f0, f2, b)
        w22 = nmt.NmtWorkspaceFlat()
        w22.compute_coupling_matrix(f2, f2, b)

        def mkcl(w, f, g):
            return w.decouple_cell(nmt.compute_coupled_cell_flat(f, g, b))

        c00 = mkcl(w00, f0, f0).flatten()
        c02 = mkcl(w02, f0, f2).flatten()
        c22 = mkcl(w22, f2, f2).flatten()
        c00_msk = mkcl(w00, f0_msk, f0_msk).flatten()
        c02_msk = mkcl(w02, f0_msk, f2_msk).flatten()
        c22_msk = mkcl(w22, f2_msk, f2_msk).flatten()
        self.assertTrue(np.all(np.fabs(c00 - c00_msk) / np.mean(c00) < 1E-10))
        self.assertTrue(np.all(np.fabs(c02 - c02_msk) / np.mean(c02) < 1E-10))
        self.assertTrue(np.all(np.fabs(c22 - c22_msk) / np.mean(c22) < 1E-10))
Пример #6
0
(ny, nx) = msk.shape
lx = np.fabs(nx * wcs.wcs.cdelt[0]) * np.pi / 180
ly = np.fabs(ny * wcs.wcs.cdelt[1]) * np.pi / 180
dt, dq, du = nmt.synfast_flat(
    int(nx), int(ny), lx, ly,
    [cltt + nltt, clte + nlte, 0 * cltt, clee + nlee, 0 * clee, clbb + nlbb],
    [0, 2])
write_flat_map("mps_flat.fits", np.array([dt, dq, du]), wcs, ["T", "Q", "U"])
d_ell = 20
lmax = 500.
ledges = np.arange(int(lmax / d_ell) + 1) * d_ell + 2
_, st = read_flat_map("tmp_flat.fits", 0)
_, sq = read_flat_map("tmp_flat.fits", 1)
_, su = read_flat_map("tmp_flat.fits", 2)

b = nmt.NmtBinFlat(ledges[:-1], ledges[1:])
leff = b.get_effective_ells()

#No contaminants
prefix = 'bm_f_nc_np'
f0 = nmt.NmtFieldFlat(lx, ly, msk, [dt])
f2 = nmt.NmtFieldFlat(lx, ly, msk, [dq, du])
w00 = nmt.NmtWorkspaceFlat()
w00.compute_coupling_matrix(f0, f0, b)
cw00 = nmt.NmtCovarianceWorkspaceFlat()
cw00.compute_coupling_coefficients(w00, w00)
cw00.write_to(prefix + '_cw00.dat')
cov = nmt.gaussian_covariance_flat(cw00, l, cltt + nltt, cltt + nltt,
                                   cltt + nltt, cltt + nltt)
np.savetxt(prefix + "_cov.txt", cov)
clb00 = w00.couple_cell(l, np.array([nltt]))
Пример #7
0
# Write out for reuse later
np.save('../data/mpt.npy', mpt)
np.save('../data/mpt_conv.npy', mpt_conv)
np.save('../data/mpt_conv2.npy', mpt_conv2)
np.save('../data/mpt_conv3.npy', mpt_conv3)

mask = np.ones_like(mpt)
f0 = nmt.NmtFieldFlat(Lx, Ly, mask, [mpt])
f0_conv = nmt.NmtFieldFlat(Lx, Ly, mask, [mpt_conv], beam=[l, beam])
f0_conv2 = nmt.NmtFieldFlat(Lx, Ly, mask, [mpt_conv2], beam=[l, beam])
f0_conv3 = nmt.NmtFieldFlat(Lx, Ly, mask, [mpt_conv3], beam=[l, beam])

l0_bins = np.arange(Nx / 8) * 8 * np.pi / Lx
lf_bins = (np.arange(Nx / 8) + 1) * 8 * np.pi / Lx
b = nmt.NmtBinFlat(l0_bins, lf_bins)
ells_uncoupled = b.get_effective_ells()

w00 = nmt.NmtWorkspaceFlat()
w00.compute_coupling_matrix(f0, f0, b)
w00.write_to("../data/w00_flat.fits")

w00_conv = nmt.NmtWorkspaceFlat()
w00_conv.compute_coupling_matrix(f0_conv, f0_conv, b)
w00_conv.write_to("../data/w00_flat_conv.fits")

w00_conv2 = nmt.NmtWorkspaceFlat()
w00_conv2.compute_coupling_matrix(f0_conv2, f0_conv2, b)
w00_conv2.write_to("../data/w00_flat_conv2.fits")

w00_conv3 = nmt.NmtWorkspaceFlat()
Пример #8
0
    def compute_power_spectrum(self,
                               map1,
                               mask1,
                               map2=None,
                               mask2=None,
                               l_bpw=None,
                               return_bpw=False,
                               wsp=None,
                               return_wsp=False,
                               temp1=None,
                               temp2=None):
        """
        Computes power spectrum between two maps.
        map1 : first map to correlate
        mask1 : mask for the first map
        map2 : second map to correlate. If None map2==map1.
        mask2 : mask for the second map. If None mask2==mask1.
        l_bpw : bandpowers on which to calculate the power spectrum. Should be an [2,N_ell] array, where
                the first and second columns list the edges of each bandpower. If None, the function will
                create bandpowers of its own taylored to the properties of your map.
        return_bpw : if True, the bandpowers will also be returned
        wsp : NmtWorkspaceFlat object to accelerate the calculation. If None, this will be precomputed.
        return_wsp : if True, the workspace will also be returned 
        temp1 : if not None, set of contaminants to remove from map1
        temp2 : if not None, set of contaminants to remove from map2
        """
        same_map = False
        if map2 is None:
            map2 = map1
            same_map = True

        same_mask = False
        if mask2 is None:
            mask2 = mask1
            same_mask = False

        if len(map1) != self.npix:
            raise ValueError("Input map has the wrong size")
        if (len(map1) != len(map2)) or (len(map1) != len(mask1)) or (
                len(map1) != len(mask2)):
            raise ValueError("Sizes of all maps and masks don't match")

        if l_bpw is None:
            ell_min = max(2 * np.pi / self.lx_rad, 2 * np.pi / self.ly_rad)
            ell_max = min(self.nx * np.pi / self.lx_rad,
                          self.ny * np.pi / self.ly_rad)
            d_ell = 2 * ell_min
            n_ell = int((ell_max - ell_min) / d_ell) - 1
            l_bpw = np.zeros([2, n_ell])
            l_bpw[0, :] = ell_min + np.arange(n_ell) * d_ell
            l_bpw[1, :] = l_bpw[0, :] + d_ell
            return_bpw = True

        #Generate binning scheme
        b = nmt.NmtBinFlat(l_bpw[0, :], l_bpw[1, :])

        if temp1 is not None:
            tmp1 = np.array([[t.reshape([self.ny, self.nx])] for t in temp1])
        else:
            tmp1 = None
        if temp2 is not None:
            tmp2 = np.array([[t.reshape([self.ny, self.nx])] for t in temp2])
        else:
            tmp2 = None

        #Generate fields
        f1 = nmt.NmtFieldFlat(self.lx_rad,
                              self.ly_rad,
                              mask1.reshape([self.ny, self.nx]),
                              [map1.reshape([self.ny, self.nx])],
                              templates=tmp1)
        if same_map and same_mask:
            f2 = f1
        else:
            f2 = nmt.NmtFieldFlat(self.lx_rad,
                                  self.ly_rad,
                                  mask2.reshape([self.ny, self.nx]),
                                  [map2.reshape([self.ny, self.nx])],
                                  templates=tmp2)

        #Compute workspace if needed
        if wsp is None:
            wsp = nmt.NmtWorkspaceFlat()
            wsp.compute_coupling_matrix(f1, f2, b)
            return_wsp = True

        #Compute power spectrum
        cl_coupled = nmt.compute_coupled_cell_flat(f1, f2, b)
        cl_uncoupled = wsp.decouple_cell(cl_coupled)[0]

        #Return
        if return_bpw and return_wsp:
            return cl_uncoupled, l_bpw, wsp
        else:
            if return_bpw:
                return cl_uncoupled, l_bpw
            elif return_wsp:
                return cl_uncoupled, wsp
            else:
                return cl_uncoupled
Пример #9
0
    def compute_wsps(self):
        """
        Convenience method for calculating the NaMaster workspaces for all the probes in the simulation.
        :return wsps: wsps list
        """

        self.wsps = [[None for i in range(self.params['nprobes'])]
                     for ii in range(self.params['nprobes'])]

        if self.params['signal']:
            signalmaps = self.simmaps.generate_maps()
        if self.params['noise']:
            # We need to add noise maps to the signal maps
            noisemaps = self.noisemaps.generate_maps()

        if self.params['signal']:
            maps = copy.deepcopy(signalmaps)
        elif self.params['noise']:
            maps = copy.deepcopy(noisemaps)
        else:
            raise RuntimeError(
                'Either signal or noise must be True. Aborting.')

        b = nmt.NmtBinFlat(self.params['l0_bins'], self.params['lf_bins'])

        # Compute workspaces for all the probes
        for j in range(self.params['nprobes']):
            for jj in range(j + 1):

                if j == jj:
                    compute_cls = True
                else:
                    if self.params['signal']:
                        compute_cls = True
                    else:
                        compute_cls = False

                if compute_cls:
                    spin1 = self.params['spins'][j]
                    spin2 = self.params['spins'][jj]

                    logger.info('Spins: spin1 = {}, spin2 = {}.'.format(
                        spin1, spin2))
                    if spin1 == 2 and spin2 == 0:
                        # Define flat sky spin-2 field
                        emaps = [maps[j], maps[j + self.params['nspin2']]]
                        f2_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[j],
                                                emaps,
                                                purify_b=False)
                        # Define flat sky spin-0 field
                        emaps = [maps[jj]]
                        f0_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[jj],
                                                emaps,
                                                purify_b=False)

                        logger.info('Computing workspace element.')
                        wsp = nmt.NmtWorkspaceFlat()
                        wsp.compute_coupling_matrix(f2_1, f0_1, b)
                        self.wsps[j][jj] = wsp
                        if j != jj:
                            self.wsps[jj][j] = wsp

                    elif spin1 == 2 and spin2 == 2:
                        # Define flat sky spin-2 field
                        emaps = [maps[j], maps[j + self.params['nspin2']]]
                        f2_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[j],
                                                emaps,
                                                purify_b=False)
                        # Define flat sky spin-0 field
                        emaps = [maps[jj], maps[jj + self.params['nspin2']]]
                        f2_2 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[jj],
                                                emaps,
                                                purify_b=False)

                        logger.info('Computing workspace element.')
                        wsp = nmt.NmtWorkspaceFlat()
                        wsp.compute_coupling_matrix(f2_1, f2_2, b)
                        self.wsps[j][jj] = wsp
                        if j != jj:
                            self.wsps[jj][j] = wsp

                    else:
                        # Define flat sky spin-0 field
                        emaps = [maps[j]]
                        f0_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[j],
                                                emaps,
                                                purify_b=False)
                        # Define flat sky spin-0 field
                        emaps = [maps[jj]]
                        f0_2 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[jj],
                                                emaps,
                                                purify_b=False)

                        logger.info('Computing workspace element.')
                        wsp = nmt.NmtWorkspaceFlat()
                        wsp.compute_coupling_matrix(f0_1, f0_2, b)
                        self.wsps[j][jj] = wsp
                        if j != jj:
                            self.wsps[jj][j] = wsp

        return self.wsps
Пример #10
0
        from pixell import enmap

        spin1, spin2 = config['spins']
        logger.info('Spins: spin1 = {}, spin2 = {}.'.format(spin1, spin2))

        map1 = enmap.read_fits(config['path2map1'])
        logger.info('Read map1 from {}.'.format(config['path2map1']))
        map2 = enmap.read_fits(config['path2map2'])
        logger.info('Read map2 from {}.'.format(config['path2map2']))

        mask1 = enmap.read_fits(config['path2mask1'])
        logger.info('Read mask1 from {}.'.format(config['path2mask1']))
        mask2 = enmap.read_fits(config['path2mask2'])
        logger.info('Read mask2 from {}.'.format(config['path2mask2']))

        b = nmt.NmtBinFlat(config['l0_bins'], config['lf_bins'])
        # The effective sampling rate for these bandpowers can be obtained calling:
        ells_uncoupled = b.get_effective_ells()

        if spin1 == 2 and spin2 == 0:
            # Define flat sky spin-2 map
            emaps = [map1[0], map1[1]]
            # Define flat sky spin-0 map
            emaps = [map2]

        elif spin1 == 2 and spin2 == 0:
            # Define flat sky spin-2 map
            emaps = [map1[0], map1[1]]
            # Define flat sky spin-0 map
            emaps = [map2]
Пример #11
0
    def guess_spectra_cpld(self,
                           params,
                           saccfile_coadd,
                           noise_saccfile_coadd=None):

        ell_theor = np.arange(self.config['ellmax'])
        theor = GSKYPrediction(saccfile_coadd, ells=ell_theor)

        cl_theor = theor.get_prediction(params)

        masks, fsk = self.get_masks()

        dl_min = int(
            min(2 * np.pi / np.radians(fsk.lx),
                2 * np.pi / np.radians(fsk.ly)))
        ells_hi = np.arange(2, 15800, dl_min * 1.5).astype(int)
        bpws_hi = nmt.NmtBinFlat(ells_hi[:-1], ells_hi[1:])
        leff_hi = bpws_hi.get_effective_ells()

        cl_cpld = []
        trc_combs = saccfile_coadd.get_tracer_combinations()
        for i, (tr_i, tr_j) in enumerate(trc_combs):

            logger.info('Computing wsp for trc_comb = {}.'.format(
                (tr_i, tr_j)))

            tr_i_ind = self.config['tracers'].index(tr_i)
            tr_j_ind = self.config['tracers'].index(tr_j)

            mask_i = masks[tr_i_ind]
            mask_j = masks[tr_j_ind]

            cl_theor_curr = [cl_theor[i]]
            if 'wl' in tr_i:
                field_i = nmt.NmtFieldFlat(
                    np.radians(fsk.lx),
                    np.radians(fsk.ly),
                    mask_i.reshape([fsk.ny, fsk.nx]), [
                        mask_i.reshape([fsk.ny, fsk.nx]),
                        mask_i.reshape([fsk.ny, fsk.nx])
                    ],
                    templates=None)
                cl_theor_curr.append(np.zeros_like(cl_theor[i]))
            else:
                field_i = nmt.NmtFieldFlat(np.radians(fsk.lx),
                                           np.radians(fsk.ly),
                                           mask_i.reshape([fsk.ny, fsk.nx]),
                                           [mask_i.reshape([fsk.ny, fsk.nx])],
                                           templates=None)
            if 'wl' in tr_j:
                field_j = nmt.NmtFieldFlat(
                    np.radians(fsk.lx),
                    np.radians(fsk.ly),
                    mask_j.reshape([fsk.ny, fsk.nx]), [
                        mask_j.reshape([fsk.ny, fsk.nx]),
                        mask_j.reshape([fsk.ny, fsk.nx])
                    ],
                    templates=None)
                cl_theor_curr.append(np.zeros_like(cl_theor[i]))
            else:
                field_j = nmt.NmtFieldFlat(np.radians(fsk.lx),
                                           np.radians(fsk.ly),
                                           mask_j.reshape([fsk.ny, fsk.nx]),
                                           [mask_j.reshape([fsk.ny, fsk.nx])],
                                           templates=None)

            wsp_hi_curr = nmt.NmtWorkspaceFlat()
            wsp_hi_curr.compute_coupling_matrix(field_i, field_j, bpws_hi)

            msk_prod = mask_i * mask_j

            cl_cpld_curr = self.get_cl_cpld(cl_theor_curr, ell_theor, leff_hi,
                                            wsp_hi_curr, msk_prod)

            if noise_saccfile_coadd is not None:
                logger.info('Adding noise.')
                if tr_i == tr_j:
                    if 'wl' in tr_i:
                        datatype = 'cl_ee'
                    else:
                        datatype = 'cl_00'
                    l_curr, nl_curr = noise_saccfile_coadd.get_ell_cl(
                        datatype, tr_i, tr_j, return_cov=False)
                    nl_curr_int = scipy.interpolate.interp1d(
                        l_curr,
                        nl_curr,
                        bounds_error=False,
                        fill_value=(nl_curr[0], nl_curr[-1]))
                    nl_curr_hi = nl_curr_int(ell_theor)
                    cl_cpld_curr += nl_curr_hi

            cl_cpld.append(cl_cpld_curr)

        # Add tracers to sacc
        saccfile_guess_spec = sacc.Sacc()
        for trc_name, trc in saccfile_coadd.tracers.items():
            saccfile_guess_spec.add_tracer_object(trc)

        for i, (tr_i, tr_j) in enumerate(trc_combs):
            if 'wl' not in tr_i and 'wl' not in tr_j:
                saccfile_guess_spec.add_ell_cl('cl_00', tr_i, tr_j, ell_theor,
                                               cl_cpld[i])
            elif ('wl' in tr_i and 'wl' not in tr_j) or ('wl' not in tr_i
                                                         and 'wl' in tr_j):
                saccfile_guess_spec.add_ell_cl('cl_0e', tr_i, tr_j, ell_theor,
                                               cl_cpld[i])
                saccfile_guess_spec.add_ell_cl('cl_0b', tr_i, tr_j, ell_theor,
                                               np.zeros_like(cl_cpld[i]))
            else:
                saccfile_guess_spec.add_ell_cl('cl_ee', tr_i, tr_j, ell_theor,
                                               cl_cpld[i])
                saccfile_guess_spec.add_ell_cl('cl_eb', tr_i, tr_j, ell_theor,
                                               np.zeros_like(cl_cpld[i]))
                saccfile_guess_spec.add_ell_cl('cl_bb', tr_i, tr_j, ell_theor,
                                               np.zeros_like(cl_cpld[i]))

        return saccfile_coadd, noise_saccfile_coadd, saccfile_guess_spec
Пример #12
0
    def __call__(self, realiz):
        """
        Convenience method for calculating the signal and noise cls for
        a given mock realization. This is a function that can be pickled and can be thus
        used when running the mock generation in parallel using multiprocessing pool.
        :param realis: number of the realisation to run
        :param noise: boolean flag indicating if noise is added to the mocks
        noise=True: add noise to the mocks
        noise=False: do not add noise to the mocks
        :param probes: list of desired probes to run the mock for
        :param maskmat: matrix with the relevant masks for the probes
        :param clparams: list of dictionaries with the parameters for calculating the
        power spectra for each probe
        :return cls: 3D array of signal and noise cls for the given realisation,
        0. and 1. axis denote the power spectrum, 2. axis gives the cls belonging
        to this configuration
        :return noisecls: 3D array of noise cls for the given realisation,
        0. and 1. axis denote the power spectrum, 2. axis gives the cls belonging
        to this configuration
        :return tempells: array of the ell range of the power spectra
        """

        logger.info('Running realization : {}.'.format(realiz))

        cls = np.zeros((self.params['nautocls'], self.params['nautocls'],
                        self.params['nell']))
        noisecls = np.zeros_like(cls)

        if self.params['signal']:
            signalmaps = self.simmaps.generate_maps()
        if self.params['noise']:
            # We need to add noise maps to the signal maps
            noisemaps = self.noisemaps.generate_maps()

        if self.params['signal'] and self.params['noise']:
            # We need to add the two lists elementwise
            maps = list(map(add, signalmaps, noisemaps))
        elif self.params['signal'] and not self.params['noise']:
            maps = copy.deepcopy(signalmaps)
        elif not self.params['signal'] and self.params['noise']:
            maps = copy.deepcopy(noisemaps)
        else:
            raise RuntimeError(
                'Either signal or noise must be True. Aborting.')

        b = nmt.NmtBinFlat(self.params['l0_bins'], self.params['lf_bins'])
        # The effective sampling rate for these bandpowers can be obtained calling:
        ells_uncoupled = b.get_effective_ells()

        # First compute the cls of map realization for all the probes
        for j in range(self.params['nprobes']):
            for jj in range(j + 1):

                if j == jj:
                    compute_cls = True
                else:
                    if self.params['signal']:
                        compute_cls = True
                    else:
                        compute_cls = False

                if compute_cls:
                    probe1 = self.params['probes'][j]
                    probe2 = self.params['probes'][jj]
                    spin1 = self.params['spins'][j]
                    spin2 = self.params['spins'][jj]

                    logger.info(
                        'Computing the power spectrum between probe1 = {} and probe2 = {}.'
                        .format(probe1, probe2))
                    logger.info('Spins: spin1 = {}, spin2 = {}.'.format(
                        spin1, spin2))
                    if spin1 == 2 and spin2 == 0:
                        # Define flat sky spin-2 field
                        emaps = [maps[j], maps[j + self.params['nspin2']]]
                        f2_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[j],
                                                emaps,
                                                purify_b=False)
                        # Define flat sky spin-0 field
                        emaps = [maps[jj]]
                        f0_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[jj],
                                                emaps,
                                                purify_b=False)

                        if self.wsps[j][jj] is None:
                            logger.info(
                                'Workspace element for j, jj = {}, {} not set.'
                                .format(j, jj))
                            logger.info('Computing workspace element.')
                            wsp = nmt.NmtWorkspaceFlat()
                            wsp.compute_coupling_matrix(f2_1, f0_1, b)
                            self.wsps[j][jj] = wsp
                            if j != jj:
                                self.wsps[jj][j] = wsp
                        else:
                            logger.info(
                                'Workspace element already set for j, jj = {}, {}.'
                                .format(j, jj))

                        # Compute pseudo-Cls
                        cl_coupled = nmt.compute_coupled_cell_flat(
                            f2_1, f0_1, b)
                        # Uncoupling pseudo-Cls
                        cl_uncoupled = self.wsps[j][jj].decouple_cell(
                            cl_coupled)

                        # For one spin-0 field and one spin-2 field, NaMaster gives: n_cls=2, [C_TE,C_TB]
                        tempclse = cl_uncoupled[0]
                        tempclsb = cl_uncoupled[1]

                        cls[j, jj, :] = tempclse
                        cls[j + self.params['nspin2'], jj, :] = tempclsb

                    elif spin1 == 2 and spin2 == 2:
                        # Define flat sky spin-2 field
                        emaps = [maps[j], maps[j + self.params['nspin2']]]
                        f2_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[j],
                                                emaps,
                                                purify_b=False)
                        # Define flat sky spin-0 field
                        emaps = [maps[jj], maps[jj + self.params['nspin2']]]
                        f2_2 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[jj],
                                                emaps,
                                                purify_b=False)

                        if self.wsps[j][jj] is None:
                            logger.info(
                                'Workspace element for j, jj = {}, {} not set.'
                                .format(j, jj))
                            logger.info('Computing workspace element.')
                            wsp = nmt.NmtWorkspaceFlat()
                            wsp.compute_coupling_matrix(f2_1, f2_2, b)
                            self.wsps[j][jj] = wsp
                            if j != jj:
                                self.wsps[jj][j] = wsp
                        else:
                            logger.info(
                                'Workspace element already set for j, jj = {}, {}.'
                                .format(j, jj))

                        # Compute pseudo-Cls
                        cl_coupled = nmt.compute_coupled_cell_flat(
                            f2_1, f2_2, b)
                        # Uncoupling pseudo-Cls
                        cl_uncoupled = self.wsps[j][jj].decouple_cell(
                            cl_coupled)

                        # For two spin-2 fields, NaMaster gives: n_cls=4, [C_E1E2,C_E1B2,C_E2B1,C_B1B2]
                        tempclse = cl_uncoupled[0]
                        tempclseb = cl_uncoupled[1]
                        tempclsb = cl_uncoupled[3]

                        cls[j, jj, :] = tempclse
                        cls[j + self.params['nspin2'], jj, :] = tempclseb
                        cls[j + self.params['nspin2'],
                            jj + self.params['nspin2'], :] = tempclsb

                    else:
                        # Define flat sky spin-0 field
                        emaps = [maps[j]]
                        f0_1 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[j],
                                                emaps,
                                                purify_b=False)
                        # Define flat sky spin-0 field
                        emaps = [maps[jj]]
                        f0_2 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                                np.radians(self.fsk.ly),
                                                self.masks[jj],
                                                emaps,
                                                purify_b=False)

                        if self.wsps[j][jj] is None:
                            logger.info(
                                'Workspace element for j, jj = {}, {} not set.'
                                .format(j, jj))
                            logger.info('Computing workspace element.')
                            wsp = nmt.NmtWorkspaceFlat()
                            wsp.compute_coupling_matrix(f0_1, f0_2, b)
                            self.wsps[j][jj] = wsp
                            if j != jj:
                                self.wsps[jj][j] = wsp
                        else:
                            logger.info(
                                'Workspace element already set for j, jj = {}, {}.'
                                .format(j, jj))

                        # Compute pseudo-Cls
                        cl_coupled = nmt.compute_coupled_cell_flat(
                            f0_1, f0_2, b)
                        # Uncoupling pseudo-Cls
                        cl_uncoupled = self.wsps[j][jj].decouple_cell(
                            cl_coupled)
                        cls[j, jj, :] = cl_uncoupled

        # If noise is True, then we need to compute the noise from simulations
        # We therefore generate different noise maps for each realisation so that
        # we can then compute the noise power spectrum from these noise realisations
        if self.params['signal'] and self.params['noise']:
            # Determine the noise bias on the auto power spectrum for each realisation
            # For the cosmic shear, we now add the shear from the noisefree signal maps to the
            # data i.e. we simulate how we would do it in real life
            noisemaps = self.noisemaps.generate_maps(signalmaps)
            for j, probe in enumerate(self.params['probes']):
                logger.info(
                    'Computing the noise power spectrum for {}.'.format(probe))
                if self.params['spins'][j] == 2:
                    # Define flat sky spin-2 field
                    emaps = [
                        noisemaps[j], noisemaps[j + self.params['nspin2']]
                    ]
                    f2 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                          np.radians(self.fsk.ly),
                                          self.masks[j],
                                          emaps,
                                          purify_b=False)

                    if self.wsps[j][j] is None:
                        logger.info(
                            'Workspace element for j, j = {}, {} not set.'.
                            format(j, j))
                        logger.info('Computing workspace element.')
                        wsp = nmt.NmtWorkspaceFlat()
                        wsp.compute_coupling_matrix(f2, f2, b)
                        self.wsps[j][j] = wsp
                    else:
                        logger.info(
                            'Workspace element already set for j, j = {}, {}.'.
                            format(j, j))

                    # Compute pseudo-Cls
                    cl_coupled = nmt.compute_coupled_cell_flat(f2, f2, b)
                    # Uncoupling pseudo-Cls
                    cl_uncoupled = self.wsps[j][j].decouple_cell(cl_coupled)

                    # For two spin-2 fields, NaMaster gives: n_cls=4, [C_E1E2,C_E1B2,C_E2B1,C_B1B2]
                    tempclse = cl_uncoupled[0]
                    tempclsb = cl_uncoupled[3]

                    noisecls[j, j, :] = tempclse
                    noisecls[j + self.params['nspin2'],
                             j + self.params['nspin2'], :] = tempclsb
                else:
                    # Define flat sky spin-0 field
                    emaps = [noisemaps[j]]
                    f0 = nmt.NmtFieldFlat(np.radians(self.fsk.lx),
                                          np.radians(self.fsk.ly),
                                          self.masks[j],
                                          emaps,
                                          purify_b=False)

                    if self.wsps[j][j] is None:
                        logger.info(
                            'Workspace element for j, j = {}, {} not set.'.
                            format(j, j))
                        logger.info('Computing workspace element.')
                        wsp = nmt.NmtWorkspaceFlat()
                        wsp.compute_coupling_matrix(f0, f0, b)
                        self.wsps[j][j] = wsp
                    else:
                        logger.info(
                            'Workspace element already set for j, j = {}, {}.'.
                            format(j, j))

                    # Compute pseudo-Cls
                    cl_coupled = nmt.compute_coupled_cell_flat(f0, f0, b)
                    # Uncoupling pseudo-Cls
                    cl_uncoupled = self.wsps[j][j].decouple_cell(cl_coupled)
                    noisecls[j, j, :] = cl_uncoupled

        if not self.params['signal'] and self.params['noise']:
            noisecls = copy.deepcopy(cls)
            cls = np.zeros_like(noisecls)

        return cls, noisecls, ells_uncoupled
Пример #13
0
    dum, [fgp[0, 0, :],
          fgp[0, 1, :]] = fm.read_flat_map("data/cont_wl_psf_flat.fits",
                                           i_map=-1)  #PSF
    dum, [fgp[1, 0, :],
          fgp[1, 1, :]] = fm.read_flat_map("data/cont_wl_ss_flat.fits",
                                           i_map=-1)  #Small-scales

#Binning scheme
ell_min = max(2 * np.pi / fmi.lx_rad, 2 * np.pi / fmi.ly_rad)
ell_max = min(fmi.nx * np.pi / fmi.lx_rad, fmi.ny * np.pi / fmi.ly_rad)
d_ell = 2 * ell_min
n_ell = int((ell_max - ell_min) / d_ell) - 1
l_bpw = np.zeros([2, n_ell])
l_bpw[0, :] = ell_min + np.arange(n_ell) * d_ell
l_bpw[1, :] = l_bpw[0, :] + d_ell
b = nmt.NmtBinFlat(l_bpw[0, :], l_bpw[1, :])

#Generate some initial fields
print(" - Res(x): %.3lf arcmin. Res(y): %.3lf arcmin." %
      (fmi.lx * 60 / fmi.nx, fmi.ly * 60 / fmi.ny))
print(" - lmax = %d, lmin = %d" % (int(ell_max), int(ell_min)))


def get_fields():
    st, sq, su = nmt.synfast_flat(int(fmi.nx), int(fmi.ny), fmi.lx_rad,
                                  fmi.ly_rad, [
                                      cltt + nltt, clte + nlte, 0 * cltt,
                                      clee + nlee, 0 * clee, clbb + nlbb
                                  ], [0, 2])
    st = st.flatten()
    sq = sq.flatten()
Пример #14
0
    def run(self):
        """
        Main function.
        This stage:
        - Produces measurements of the power spectrum with and without contaminant deprojections.
        - Estimates the noise bias
        - Estimates the covariance matrix
        - Estimates the deprojection bias
        """
        self.parse_input()

        print("Reading mask")
        self.msk_bi, self.mskfrac, self.mp_depth = self.get_masks()

        print("Computing area")
        self.area_pix = np.radians(self.fsk.dx) * np.radians(self.fsk.dy)
        self.area_patch = np.sum(self.msk_bi * self.mskfrac) * self.area_pix
        self.lmax = int(180. *
                        np.sqrt(1. / self.fsk.dx**2 + 1. / self.fsk.dy**2))

        print("Reading contaminants")
        temps = self.get_contaminants()

        print("Setting bandpowers")
        lini = np.array(self.config['ell_bpws'])[:-1]
        lend = np.array(self.config['ell_bpws'])[1:]
        bpws = nmt.NmtBinFlat(lini, lend)
        ell_eff = bpws.get_effective_ells()

        print("Generating tracers")
        tracers_nc, tracers_wc = self.get_tracers(temps)
        self.nbins = len(tracers_nc)

        print("Translating into SACC tracers")
        tracers_sacc = self.get_sacc_tracers(tracers_nc)

        self.ordering = np.zeros([self.nbins, self.nbins], dtype=int)
        ix = 0
        for i in range(self.nbins):
            for j in range(i, self.nbins):
                self.ordering[i, j] = ix
                if j != i:
                    self.ordering[j, i] = ix
                ix += 1

        print("Getting MCM")
        wsp = self.get_mcm(tracers_nc, bpws)

        print("Computing window function")
        windows = self.get_sacc_windows(wsp)

        print("Computing SACC binning")
        #No windows
        binning_nw = self.get_sacc_binning(ell_eff, lini, lend, windows=None)
        #With windows
        binning_ww = self.get_sacc_binning(ell_eff,
                                           lini,
                                           lend,
                                           windows=windows)

        print("Computing power spectra")
        print(" No deprojections")
        cls_wodpj, _ = self.get_power_spectra(tracers_nc, wsp, bpws)
        print(" W. deprojections")
        cls_wdpj, cls_wdpj_coupled = self.get_power_spectra(
            tracers_wc, wsp, bpws)
        self.ncross, self.nell = cls_wodpj.shape

        print("Getting guess power spectra")
        lth, clth = self.get_cl_guess(ell_eff, cls_wdpj)

        print("Computing deprojection bias")
        cls_wdpj, cls_deproj = self.get_dpj_bias(tracers_wc, lth, clth,
                                                 cls_wdpj_coupled, wsp, bpws)

        print("Computing covariance")
        cov_wodpj = self.get_covar(lth, clth, bpws, tracers_wc, wsp, None,
                                   None)
        if self.config['gaus_covar_type'] == 'analytic':
            cov_wdpj = cov_wodpj.copy()
        else:
            cov_wdpj = self.get_covar(lth, clth, bpws, tracers_wc, wsp, temps,
                                      cls_deproj)

        print("Computing noise bias")
        nls = self.get_noise(tracers_nc, wsp, bpws)

        print("Writing output")
        print(self.get_output_fname('noi_bias', ext='sacc'))
        self.write_vector_to_sacc(self.get_output_fname('noi_bias',
                                                        ext='sacc'),
                                  tracers_sacc,
                                  binning_nw,
                                  nls,
                                  verbose=False)
        self.write_vector_to_sacc(self.get_output_fname('dpj_bias',
                                                        ext='sacc'),
                                  tracers_sacc,
                                  binning_nw,
                                  cls_deproj,
                                  verbose=False)
        self.write_vector_to_sacc(self.get_output_fname('power_spectra_wodpj',
                                                        ext='sacc'),
                                  tracers_sacc,
                                  binning_ww,
                                  cls_wodpj,
                                  covar=cov_wodpj,
                                  verbose=False)
        self.write_vector_to_sacc(self.get_output_fname('power_spectra_wdpj',
                                                        ext='sacc'),
                                  tracers_sacc,
                                  binning_ww,
                                  cls_wdpj,
                                  covar=cov_wdpj,
                                  verbose=True)
Пример #15
0
def xcorr_flatsky(modedecomp=False,
                  simkey="512_alfven3_0002_a_z",
                  Imapkey="",
                  deglen=10,
                  apotype="C2",
                  aposcale=0.5,
                  Epure=True,
                  Bpure=True):

    TQU = SimsTQU(fn=simkey, modedecomp=modedecomp, ikey=Imapkey)

    # Define flat-sky field
    #  - Lx and Ly: the size of the patch in the x and y dimensions (in radians)
    Lx = deglen * np.pi / 180.  # arbitrarily set this to a deglen x deglen deg box
    Ly = deglen * np.pi / 180.
    #  - Nx and Ny: the number of pixels in the x and y dimensions
    Nx = 512
    Ny = 512

    # Define mask
    mask = np.ones_like(TQU.T).flatten()
    xarr = np.ones(Ny)[:, None] * np.arange(Nx)[None, :] * Lx / Nx
    yarr = np.ones(Nx)[None, :] * np.arange(Ny)[:, None] * Ly / Ny
    #Let's also trim the edges
    mask[np.where(xarr.flatten() < Lx / 16.)] = 0
    mask[np.where(xarr.flatten() > 15 * Lx / 16.)] = 0
    mask[np.where(yarr.flatten() < Ly / 16.)] = 0
    mask[np.where(yarr.flatten() > 15 * Ly / 16.)] = 0
    mask = mask.reshape([Ny, Nx])
    mask = nmt.mask_apodization_flat(mask,
                                     Lx,
                                     Ly,
                                     aposize=aposcale,
                                     apotype=apotype)

    # Fields:
    # Once you have maps it's time to create pymaster fields.
    # Note that, as in the full-sky case, you can also pass
    # contaminant templates and flags for E and B purification
    # (see the documentation for more details)
    f0 = nmt.NmtFieldFlat(Lx, Ly, mask, [TQU.T])
    f2 = nmt.NmtFieldFlat(Lx,
                          Ly,
                          mask, [TQU.Q, TQU.U],
                          purify_b=Bpure,
                          purify_e=Epure)

    # Bins:
    # For flat-sky fields, bandpowers are simply defined as intervals in ell, and
    # pymaster doesn't currently support any weighting scheme within each interval.
    l0_bins = np.arange(Nx / 8) * 8 * np.pi / Lx
    lf_bins = (np.arange(Nx / 8) + 1) * 8 * np.pi / Lx
    b = nmt.NmtBinFlat(l0_bins, lf_bins)
    # The effective sampling rate for these bandpowers can be obtained calling:
    ells_uncoupled = b.get_effective_ells()

    # Workspaces:
    # As in the full-sky case, the computation of the coupling matrix and of
    # the pseudo-CL estimator is mediated by a WorkspaceFlat case, initialized
    # by calling its compute_coupling_matrix method:
    w00 = nmt.NmtWorkspaceFlat()
    w00.compute_coupling_matrix(f0, f0, b)
    w02 = nmt.NmtWorkspaceFlat()
    w02.compute_coupling_matrix(f0, f2, b)
    w22 = nmt.NmtWorkspaceFlat()
    w22.compute_coupling_matrix(f2, f2, b)

    # Computing power spectra:
    # As in the full-sky case, you compute the pseudo-CL estimator by
    # computing the coupled power spectra and then decoupling them by
    # inverting the mode-coupling matrix. This is done in two steps below,
    # but pymaster provides convenience routines to do this
    # through a single function call
    cl00_coupled = nmt.compute_coupled_cell_flat(f0, f0, b)
    cl00_uncoupled = w00.decouple_cell(cl00_coupled)
    cl02_coupled = nmt.compute_coupled_cell_flat(f0, f2, b)
    cl02_uncoupled = w02.decouple_cell(cl02_coupled)
    cl22_coupled = nmt.compute_coupled_cell_flat(f2, f2, b)
    cl22_uncoupled = w22.decouple_cell(cl22_coupled)

    TT = cl00_uncoupled[0]
    TE = cl02_uncoupled[0]
    TB = cl02_uncoupled[1]
    EE = cl22_uncoupled[0]
    EB = cl22_uncoupled[1]
    BE = cl22_uncoupled[2]
    BB = cl22_uncoupled[3]

    if modedecomp:
        outroot = "/data/seclark/BlakesleySims/simdata/ModeDecomp/xcorrdata/"
    else:
        outroot = "/data/seclark/BlakesleySims/xcorrdata/"
    outfn = simkey + "_deglen{}_{}apod{}_EBpure{}{}.h5".format(
        deglen, apotype, aposcale, Epure, Bpure)

    with h5py.File(outroot + outfn, 'w') as f:

        TTdset = f.create_dataset(name='TT', data=TT)
        TEdset = f.create_dataset(name='TE', data=TE)
        TBdset = f.create_dataset(name='TB', data=TB)
        EEdset = f.create_dataset(name='EE', data=EE)
        EBdset = f.create_dataset(name='EB', data=EB)
        BEdset = f.create_dataset(name='BE', data=BE)
        BBdset = f.create_dataset(name='BB', data=BB)
        TTdset.attrs['deglen'] = deglen
        TTdset.attrs['Epure'] = Epure
        TTdset.attrs['Bpure'] = Bpure
        TTdset.attrs['ell_binned'] = ells_uncoupled
Пример #16
0
        temp = read_map_bands(o.prefix_in + '_oc_' + c + '.fits',
                              o.cont_oc_bands)
        for t in temp:
            temps.append(t)
if temps == []:
    temps = None
else:
    print(" - Will marginalize over a total of %d contaminant templates" %
          (len(temps)))
    #Remove mean
    for i_t, t in enumerate(temps):
        temps[i_t] -= np.sum(msk_t * mskfrac * t) / np.sum(msk_t * mskfrac)

#Set binning scheme
lini, lend = np.loadtxt(o.fname_ellbins, unpack=True)
bpws = nmt.NmtBinFlat(lini, lend)
ell_eff = bpws.get_effective_ells()

#Generate tracers
print("Generating tracers")


class Tracer(object):
    def __init__(self,
                 hdu_list,
                 i_bin,
                 fsk,
                 mask_binary,
                 masked_fraction,
                 contaminants=None):
Пример #17
0
    def run(self):
        """
        Main function.
        This stage:
        - Produces measurements of the power spectrum with and without contaminant deprojections.
        - Estimates the noise bias
        - Estimates the covariance matrix
        - Estimates the deprojection bias
        """
        self.parse_input()

        logger.info("Reading mask.")
        self.msk_bi, self.mskfrac, self.mp_depth = self.get_masks()

        logger.info("Computing area.")
        self.area_pix = np.radians(self.fsk.dx) * np.radians(self.fsk.dy)
        self.area_patch = np.sum(self.msk_bi * self.mskfrac) * self.area_pix
        self.lmax = int(180. *
                        np.sqrt(1. / self.fsk.dx**2 + 1. / self.fsk.dy**2))

        logger.info("Reading contaminants.")
        temps = self.get_contaminants()

        logger.info("Setting bandpowers.")
        lini = np.array(self.config['ell_bpws'])[:-1]
        lend = np.array(self.config['ell_bpws'])[1:]
        bpws = nmt.NmtBinFlat(lini, lend)
        ell_eff = bpws.get_effective_ells()
        self.nbands = ell_eff.shape[0]
        logger.info('Number of ell bands = {}.'.format(self.nbands))

        tracers_nc, tracers_wc = self.get_all_tracers(temps)

        self.ntracers = len(tracers_nc)
        self.nmaps = self.ntracers_counts + self.ntracers_comptony + self.ntracers_kappa + 2 * self.ntracers_shear

        logger.info("Translating into SACC tracers.")
        tracers_sacc = self.get_sacc_tracers(tracers_wc)

        # Set up mapping
        self.mapping(tracers_nc)

        logger.info("Getting MCM.")
        wsp = self.get_mcm(tracers_nc, bpws)

        logger.info("Computing window functions.")
        windows = self.get_windows(tracers_nc, wsp)

        self.ncross = self.nmaps * (self.nmaps + 1) // 2 + self.ntracers_shear
        if self.config['gaus_covar_type'] == 'analytic':
            logger.info("Computing analytic covariance.")
            if not os.path.isfile(
                    self.get_output_fname('power_spectra_wdpj', ext='sacc')):
                logger.info("Computing deprojected power spectra.")
                logger.info(" W. deprojections.")
                cls_wdpj, _ = self.get_power_spectra(tracers_wc, wsp, bpws)
            else:
                logger.info("Reading deprojected power spectra.")
                sacc_cls_wdpj = sacc.Sacc.load_fits(
                    self.get_output_fname('power_spectra_wdpj', ext='sacc'))
                cls_wdpj = self.convert_sacc_to_clarr(sacc_cls_wdpj,
                                                      tracers_sacc)

            if not os.path.isfile(
                    self.get_output_fname('power_spectra_wodpj', ext='sacc')):
                logger.info("Computing non-deprojected power spectra.")
                logger.info(" No deprojections.")
                cls_wodpj, _ = self.get_power_spectra(tracers_nc, wsp, bpws)
            else:
                logger.info("Reading non-deprojected power spectra.")
                sacc_cls_wodpj = sacc.Sacc.load_fits(
                    self.get_output_fname('power_spectra_wodpj', ext='sacc'))
                cls_wodpj = self.convert_sacc_to_clarr(sacc_cls_wodpj,
                                                       tracers_sacc)

            logger.info("Getting guess power spectra.")
            lth, clth = self.get_cl_guess(ell_eff, cls_wdpj, tracers_sacc)
            cov_wodpj = self.get_covar(lth, clth, bpws, tracers_wc, wsp, None,
                                       None)
            cov_wdpj = cov_wodpj.copy()

        else:
            logger.info("Computing simulated covariance.")
            if not os.path.isfile(
                    self.get_output_fname('power_spectra_wdpj', ext='sacc')):
                logger.info("Computing deprojected power spectra.")
                logger.info(" W. deprojections.")
                cls_wdpj, cls_wdpj_coupled = self.get_power_spectra(
                    tracers_wc, wsp, bpws)
            else:
                logger.info("Reading deprojected power spectra.")
                sacc_cls_wdpj = sacc.Sacc.load_fits(
                    self.get_output_fname('power_spectra_wdpj', ext='sacc'))
                cls_wdpj = self.convert_sacc_to_clarr(sacc_cls_wdpj,
                                                      tracers_sacc)
                logger.info("Reading deprojected coupled power spectra.")
                sacc_cls_wdpj_coupled = sacc.Sacc.load_fits(
                    self.get_output_fname('power_spectra_wdpj_coupled',
                                          ext='sacc'))
                cls_wdpj_coupled = self.convert_sacc_to_clarr(
                    sacc_cls_wdpj_coupled, tracers_sacc)

            logger.info("Getting guess power spectra.")
            lth, clth = self.get_cl_guess(ell_eff, cls_wdpj, tracers_sacc)

            if os.path.isfile(self.get_output_fname('dpj_bias', ext='sacc')):
                sacc_cl_deproj_bias = sacc.Sacc.load_fits(
                    self.get_output_fname('dpj_bias', ext='sacc'))
                cl_deproj_bias = self.convert_sacc_to_clarr(
                    sacc_cl_deproj_bias, tracers_sacc)
            else:
                logger.info("Computing deprojection bias.")
                _, cl_deproj_bias = self.get_dpj_bias(tracers_wc, lth, clth,
                                                      cls_wdpj_coupled, wsp,
                                                      bpws)

            cov_wodpj = self.get_covar(lth, clth, bpws, tracers_wc, wsp, None,
                                       None)
            cov_wdpj = self.get_covar(lth, clth, bpws, tracers_wc, wsp, temps,
                                      cl_deproj_bias)

        # Write covariances into existing sacc
        if os.path.isfile(
                self.get_output_fname('power_spectra_wodpj', ext='sacc')):
            logger.info('{} provided.'.format(
                self.get_output_fname('power_spectra_wodpj', ext='sacc')))
            logger.info('Adding deprojected covariance matrix to {}.'.format(
                self.get_output_fname('power_spectra_wodpj', ext='sacc')))
            self.write_vector_to_sacc(self.get_output_fname(
                'power_spectra_wodpj', ext='sacc'),
                                      tracers_sacc,
                                      cls_wodpj,
                                      ell_eff,
                                      windows,
                                      covar=cov_wodpj)
            logger.info('Written deprojected covariance matrix.')
        else:
            logger.info('{} not provided.'.format(
                self.get_output_fname('power_spectra_wodpj', ext='sacc')))
            logger.info('Writing deprojected covariance matrix to {}.'.format(
                self.get_output_fname('cov_wodpj', ext='sacc')))
            s_wodpj = sacc.Sacc()
            s_wodpj.add_covariance(cov_wodpj)
            s_wodpj.save_fits(self.get_output_fname('cov_wodpj', ext='sacc'),
                              overwrite=True)
            logger.info('Written deprojected covariance matrix.')

        if os.path.isfile(
                self.get_output_fname('power_spectra_wdpj', ext='sacc')):
            logger.info('{} provided.'.format(
                self.get_output_fname('power_spectra_wdpj', ext='sacc')))
            logger.info('Adding deprojected covariance matrix to {}.'.format(
                self.get_output_fname('power_spectra_wdpj', ext='sacc')))
            self.write_vector_to_sacc(self.get_output_fname(
                'power_spectra_wdpj', ext='sacc'),
                                      tracers_sacc,
                                      cls_wdpj,
                                      ell_eff,
                                      windows,
                                      covar=cov_wdpj)
            logger.info('Written deprojected covariance matrix.')
        else:
            logger.info('{} not provided.'.format(
                self.get_output_fname('power_spectra_wdpj', ext='sacc')))
            logger.info(
                'Writing non deprojected covariance matrix to {}.'.format(
                    self.get_output_fname('cov_wdpj', ext='sacc')))
            s_wdpj = sacc.Sacc()
            s_wdpj.add_covariance(cov_wdpj)
            s_wdpj.save_fits(self.get_output_fname('cov_wdpj', ext='sacc'),
                             overwrite=True)
            logger.info('Written deprojected covariance matrix.')

        # Permissions on NERSC
        os.system(
            'find /global/cscratch1/sd/damonge/GSKY/ -type d -exec chmod -f 777 {} \;'
        )
        os.system(
            'find /global/cscratch1/sd/damonge/GSKY/ -type f -exec chmod -f 666 {} \;'
        )
Пример #18
0
 def setUp(self):
     self.nlb = 5
     self.nbands = 399
     larr = np.arange(self.nbands + 1) * self.nlb + 2
     self.b = nmt.NmtBinFlat(larr[:-1], larr[1:])
Пример #19
0
    def compute_wsps(self):
        """
        Convenience method for calculating the NaMaster workspaces for all the probes in the simulation.
        :return wsps: wsps list
        """

        wsps = [[None for i in range(self.params['nprobes'])]
                for ii in range(self.params['nprobes'])]

        maps = self.simmaps.generate_maps()

        b = nmt.NmtBinFlat(self.params['l0_bins'], self.params['lf_bins'])

        # Compute workspaces for all the probes
        for j in range(self.params['nprobes']):
            for jj in range(j + 1):
                spin1 = self.params['spins'][j]
                spin2 = self.params['spins'][jj]

                logger.info('Spins: spin1 = {}, spin2 = {}.'.format(
                    spin1, spin2))
                if spin1 == 2 and spin2 == 0:
                    # Define flat sky spin-2 field
                    emaps = [maps[j], maps[j + self.params['nspin2']]]
                    f2_1 = nmt.NmtFieldFlat(self.params['Lx'],
                                            self.params['Ly'],
                                            self.maskmat[j, jj],
                                            emaps,
                                            purify_b=False)
                    # Define flat sky spin-0 field
                    emaps = [maps[jj]]
                    f0_1 = nmt.NmtFieldFlat(self.params['Lx'],
                                            self.params['Ly'],
                                            self.maskmat[j, jj],
                                            emaps,
                                            purify_b=False)

                    logger.info('Computing workspace element.')
                    wsp = nmt.NmtWorkspaceFlat()
                    wsp.compute_coupling_matrix(f2_1, f0_1, b)
                    wsps[j][jj] = wsp
                    if j != jj:
                        wsps[jj][j] = wsp

                elif spin1 == 2 and spin2 == 2:
                    # Define flat sky spin-2 field
                    emaps = [maps[j], maps[j + self.params['nspin2']]]
                    f2_1 = nmt.NmtFieldFlat(self.params['Lx'],
                                            self.params['Ly'],
                                            self.maskmat[j, jj],
                                            emaps,
                                            purify_b=False)
                    # Define flat sky spin-0 field
                    emaps = [maps[jj], maps[jj + self.params['nspin2']]]
                    f2_2 = nmt.NmtFieldFlat(self.params['Lx'],
                                            self.params['Ly'],
                                            self.maskmat[j, jj],
                                            emaps,
                                            purify_b=False)

                    logger.info('Computing workspace element.')
                    wsp = nmt.NmtWorkspaceFlat()
                    wsp.compute_coupling_matrix(f2_1, f2_2, b)
                    wsps[j][jj] = wsp
                    if j != jj:
                        wsps[jj][j] = wsp

                else:
                    # Define flat sky spin-0 field
                    emaps = [maps[j]]
                    f0_1 = nmt.NmtFieldFlat(self.params['Lx'],
                                            self.params['Ly'],
                                            self.maskmat[j, jj],
                                            emaps,
                                            purify_b=False)
                    # Define flat sky spin-0 field
                    emaps = [maps[jj]]
                    f0_2 = nmt.NmtFieldFlat(self.params['Lx'],
                                            self.params['Ly'],
                                            self.maskmat[j, jj],
                                            emaps,
                                            purify_b=False)

                    logger.info('Computing workspace element.')
                    wsp = nmt.NmtWorkspaceFlat()
                    wsp.compute_coupling_matrix(f0_1, f0_2, b)
                    wsps[j][jj] = wsp
                    if j != jj:
                        wsps[jj][j] = wsp

        return wsps