Beispiel #1
0
def test_concatenate_data():
    s1 = sacc.Sacc()

    # Tracer
    z = np.arange(0., 1.0, 0.01)
    nz = (z - 0.5)**2 / 0.1**2
    s1.add_tracer('NZ', 'source_0', z, nz)

    for i in range(20):
        ee = 0.1 * i
        tracers = ('source_0', 'source_0')
        s1.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                          tracers,
                          ee,
                          ell=10.0 * i)

    s2 = sacc.Sacc()

    # Tracer
    z = np.arange(0., 1.0, 0.01)
    nz = (z - 0.5)**2 / 0.1**2
    s2.add_tracer('NZ', 'source_0', z, nz, quantity='galaxy_shear', spin=2)

    for i in range(20):
        ee = 0.1 * i
        tracers = ('source_0', 'source_0')
        s2.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                          tracers,
                          ee,
                          ell=10.0 * i,
                          label='xxx')

    # name clash
    with pytest.raises(ValueError):
        sacc.concatenate_data_sets(s1, s2)

    s3 = sacc.concatenate_data_sets(s1, s2, labels=['1', '2'])
    assert 'source_0_1' in s3.tracers
    assert 'source_0_2' in s3.tracers
    assert len(s3) == len(s1) + len(s2)

    # check data points in right order
    for i in range(20):
        assert s3.data[i].get_tag('ell') == 10.0 * i
        assert s3.data[i + 20].get_tag('ell') == 10.0 * i
        assert s3.data[i].get_tag('label') == '1'
        assert s3.data[i + 20].get_tag('label') == 'xxx_2'
        t1 = s3.data[i].tracers[0]
        t2 = s3.data[i + 20].tracers[0]
        assert t1 == 'source_0_1'
        assert t2 == 'source_0_2'
        s3.get_tracer(t1)
        s3.get_tracer(t2)
Beispiel #2
0
def test_io():
    s = sacc.Sacc()

    # Tracer
    z = np.arange(0., 1.0, 0.01)
    nz = (z - 0.5)**2 / 0.1**2
    s.add_tracer('NZ', 'source_0', z, nz)

    for i in range(20):
        ee = 0.1 * i
        tracers = ('source_0', 'source_0')
        s.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                         tracers,
                         ee,
                         ell=10.0 * i)

    with tempfile.TemporaryDirectory() as tmpdir:
        filename = os.path.join(tmpdir, 'test.sacc')
        s.save_fits(filename)
        s2 = sacc.Sacc.load_fits(filename)

    assert len(s2) == 20
    mu = s2.get_mean(sacc.standard_types.galaxy_shear_cl_ee)
    for i in range(20):
        assert mu[i] == 0.1 * i
Beispiel #3
0
def test_tracers_later():
    s = sacc.Sacc()

    with pytest.raises(ValueError):
        tracers = ('source_0', 'source_0')
        s.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                         tracers,
                         0.0,
                         ell=1)

    s = sacc.Sacc()

    tracers = ('source_0', 'source_0')
    s.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                     tracers,
                     0.0,
                     tracers_later=True,
                     ell=1)
Beispiel #4
0
    def write_output(self, data, meta, results):
        import sacc
        XIP = sacc.standard_types.galaxy_shear_xi_plus
        XIM = sacc.standard_types.galaxy_shear_xi_minus
        GAMMAT = sacc.standard_types.galaxy_shearDensity_xi_t

        S = sacc.Sacc()

        # We include the n(z) data in the output.
        # So here we load it in and add it to the data
        f = self.open_input('photoz_stack')

        # Load the tracer data N(z) from an input file and
        # copy it to the output, for convenience
        for i in data['source_list']:
            z = f['n_of_z/source/z'][:]
            Nz = f[f'n_of_z/source/bin_{i}'][:]
            S.add_tracer('NZ', f'source_{i}', z, Nz)

        # For both source and lens
        for i in data['lens_list']:
            z = f['n_of_z/lens/z'][:]
            Nz = f[f'n_of_z/lens/bin_{i}'][:]
            S.add_tracer('NZ', f'lens_{i}', z, Nz)
        # Closing n(z) file
        f.close()

        # Add the data points that we have one by one, recording which
        # tracer they each require
        for d in results:
            tracer1 = f'source_{d.i}' if d.corr_type in [XIP, XIM, GAMMAT
                                                         ] else f'lens_{d.i}'
            tracer2 = f'source_{d.j}' if d.corr_type in [XIP, XIM
                                                         ] else f'lens_{d.j}'
            # Each of our Measurement objects contains various theta values,
            # and we loop through and add them all
            n = len(d.value)
            for i in range(n):
                S.add_data_point(d.corr_type, (tracer1, tracer2),
                                 d.value[i],
                                 theta=d.theta[i],
                                 error=d.error[i],
                                 npair=d.npair[i],
                                 weight=d.weight[i])

        #self.write_metadata(S, meta)

        # Our data points may currently be in any order depending on which processes
        # ran which calculations.  Re-order them.
        S.to_canonical_order()

        # Finally, save the output to Sacc file
        S.save_fits(self.get_output('twopoint_data'), overwrite=True)
Beispiel #5
0
def test_construct():
    s = sacc.Sacc()

    # Tracer
    z = np.arange(0., 1.0, 0.01)
    nz = (z - 0.5)**2 / 0.1**2
    s.add_tracer('NZ', 'source_0', z, nz, quantity='galaxy_density')

    for i in range(20):
        ee = 0.1 * i
        tracers = ('source_0', 'source_0')
        s.add_data_point(sacc.standard_types.cl_00, tracers, ee, ell=10.0 * i)
Beispiel #6
0
def make_sacc(tomo_bin, z, what, mu, C):
    import sacc
    # Basic numbers
    nbin = int(tomo_bin.max()) + 1
    tracer_type = get_tracer_type(nbin, what)
    ntot = nbin * 2 if what == '3x2' else nbin

    z_mid, n_of_z = get_n_of_z(tomo_bin, z)
    npair = (ntot * (ntot + 1)) // 2

    # Must be the same as above
    ell, _ = ell_binning()
    n_ell = len(ell)
    # Just EE for now
    EE = sacc.data_types.standard_types.galaxy_shear_cl_ee
    GG = sacc.data_types.standard_types.galaxy_density_cl
    GE = sacc.data_types.standard_types.galaxy_shearDensity_cl_e
    # Start with empty sacc.
    S = sacc.Sacc()

    # Add all the tracers
    for ci in range(ntot):
        i = ci % nbin
        if tracer_type[ci] == 'g':
            S.add_tracer("NZ", f'lens_{i}', z_mid, n_of_z[i])
        elif tracer_type[ci] == 'w':
            S.add_tracer("NZ", f'source_{i}', z_mid, n_of_z[i])
        else:
            raise NotImplemented

    # Now put in all the data points
    n = 0
    for ic in range(ntot):
        i = ic % nbin
        name_i = f'lens_{i}' if tracer_type[ic] == 'g' else f'source_{i}'
        for jc in range(ic, ntot):
            j = jc % nbin
            name_j = f'lens_{j}' if tracer_type[jc] == 'g' else f'source_{j}'
            if tracer_type[ic] + tracer_type[jc] == 'gg':
                S.add_ell_cl(GG, name_i, name_j, ell, mu[n:n + n_ell])
            elif tracer_type[ic] + tracer_type[jc] == 'ww':
                S.add_ell_cl(EE, name_i, name_j, ell, mu[n:n + n_ell])
            else:
                S.add_ell_cl(GE, name_i, name_j, ell, mu[n:n + n_ell])
            n += n_ell

    # And finally add the covmat
    S.add_covariance(C)
    return S
Beispiel #7
0
    def save_cell_to_file(self, cell, tracers, fname, with_windows=False):
        # Create sacc file
        s = sacc.Sacc()

        # Add tracers
        for t in tracers:
            s.add_tracer_object(t)

        # Add each power spectrum
        l_eff = self.bins.get_effective_ells()
        for b1, b2, s1, s2, l1, l2 in self.get_cell_iterator():
            add_BE = not ((b1 == b2) and (s1 == s2))
            if with_windows:
                wname = self.get_workspace_label(b1, b2)
                s.add_ell_cl('cl_ee',
                             l1,
                             l2,
                             l_eff,
                             cell[l1][l2][0],
                             window=self.win[wname]['EE'])  # EE
                s.add_ell_cl('cl_eb',
                             l1,
                             l2,
                             l_eff,
                             cell[l1][l2][1],
                             window=self.win[wname]['EB'])  # EB
                if add_BE:  # Only add B1E2 if 1!=2
                    s.add_ell_cl('cl_be',
                                 l1,
                                 l2,
                                 l_eff,
                                 cell[l1][l2][2],
                                 window=self.win[wname]['BE'])  # BE
                s.add_ell_cl('cl_bb',
                             l1,
                             l2,
                             l_eff,
                             cell[l1][l2][3],
                             window=self.win[wname]['BB'])  # EE
            else:
                s.add_ell_cl('cl_ee', l1, l2, l_eff, cell[l1][l2][0])  # EE
                s.add_ell_cl('cl_eb', l1, l2, l_eff, cell[l1][l2][1])  # EB
                if add_BE:  # Only add B1E2 if 1!=2
                    s.add_ell_cl('cl_be', l1, l2, l_eff, cell[l1][l2][2])  # BE
                s.add_ell_cl('cl_bb', l1, l2, l_eff, cell[l1][l2][3])  # EE

        print("Saving to " + fname)
        s = s.save_fits(fname, overwrite=True)
Beispiel #8
0
def sacc_and_tracers():
    # Initializes a sacc file and fills it up with the shear tracers
    s = sacc.Sacc()
    for b in range(nbins):
        z0, zm, zf, nz = np.loadtxt(predir + f'dndz_metacal_bin{b}.txt',
                                    unpack=True)
        s.add_tracer('NZ',
                     f'bin{b}',
                     quantity='galaxy_shear',
                     spin=2,
                     z=zm,
                     nz=nz,
                     extra_columns={
                         'z_i': z0,
                         'z_f': zf
                     })
    return s
Beispiel #9
0
    def write_output_sacc(self, data, meta, results):
        # We write out the results slightly differently here
        # beause they go to a different file and have different
        # tracers and tags.
        import sacc
        dt = "galaxyStarCenters_shearDensity_xi_t"

        S = sacc.Sacc()

        f = self.open_input('photoz_stack')
        z = f['n_of_z/source2d/z'][:]
        Nz = f[f'n_of_z/source2d/bin_0'][:]
        f.close()

        # Add the data points that we have one by one, recording which
        # tracer they each require
        S.add_tracer('misc', 'starcenter')
        S.add_tracer('NZ', 'source2d', z, Nz)

        d = results[0]
        assert len(results) == 1

        # Each of our Measurement objects contains various theta values,
        # and we loop through and add them all
        n = len(d.value)
        for i in range(n):
            S.add_data_point(dt, ('source2d', 'starcenter'),
                             d.value[i],
                             theta=d.theta[i],
                             error=d.error[i],
                             npair=d.npair[i],
                             weight=d.weight[i])

        self.write_metadata(S, meta)

        print(S)
        # Our data points may currently be in any order depending on which processes
        # ran which calculations.  Re-order them.
        S.to_canonical_order()

        # Finally, save the output to Sacc file
        S.save_fits(self.get_output('gammat_dim_stars'), overwrite=True)
Beispiel #10
0
 def __init__(self, datafile, output, use='cls'):
     self.datafile = datafile
     self.data = co.read_data(datafile)
     self.outdir = self.data['output']
     self.use_nl = False
     self.use_fiducial = False
     if use == 'cls':
         pass
     elif use == 'nl':
         self.use_nl = True
     elif use == 'fiducial':
         self.use_fiducial = True
     else:
         raise ValueError('Use must be one of cls, nl or fiducial')
     self.s = sacc.Sacc()
     self.add_tracers()
     self.add_ell_cls()
     self.add_covariance()
     fname = os.path.join(self.outdir, output)
     self.s.save_fits(fname, overwrite=True)
Beispiel #11
0
    def save_power_spectra(self, tracers, nbin_source, nbin_lens):
        import sacc
        from sacc.windows import TopHatWindow
        CEE = sacc.standard_types.galaxy_shear_cl_ee
        CBB = sacc.standard_types.galaxy_shear_cl_bb
        CdE = sacc.standard_types.galaxy_shearDensity_cl_e
        CdB = sacc.standard_types.galaxy_shearDensity_cl_b
        Cdd = sacc.standard_types.galaxy_density_cl

        S = sacc.Sacc()

        for tracer in tracers.values():
            S.add_tracer_object(tracer)

        for d in self.results:
            tracer1 = f'source_{d.i}' if d.corr_type in [CEE, CBB, CdE, CdB
                                                         ] else f'lens_{d.i}'
            tracer2 = f'source_{d.j}' if d.corr_type in [CEE, CBB
                                                         ] else f'lens_{d.j}'

            n = len(d.l)
            for i in range(n):
                win = TopHatWindow(d.win[i][0], d.win[i][1])
                S.add_data_point(d.corr_type, (tracer1, tracer2),
                                 d.value[i],
                                 ell=d.l[i],
                                 window=win,
                                 i=d.i,
                                 j=d.j)

        # Save provenance information
        for key, value in self.gather_provenance().items():
            if isinstance(value, str) and '\n' in value:
                values = value.split("\n")
                for i, v in enumerate(values):
                    S.metadata[f'provenance/{key}_{i}'] = v
            else:
                S.metadata[f'provenance/{key}'] = value

        output_filename = self.get_output("twopoint_data_fourier")
        S.save_fits(output_filename, overwrite=True)
Beispiel #12
0
def test_io_maps_bpws():
    s = sacc.Sacc()

    n_ell = 10
    d_ell = 100
    n_ell_large = n_ell * d_ell
    ell = np.linspace(2, 1000, n_ell)
    c_ell = 1. / (ell + 1)**3
    beam = np.exp(-0.1 * ell * (ell + 1))
    nu = np.linspace(30., 60., 100)
    bandpass = np.ones(100)
    z = np.arange(0., 1.0, 0.01)
    nz = (z - 0.5)**2 / 0.1**2

    # Tracer
    s.add_tracer('NZ', 'gc', z, nz)
    s.add_tracer('NuMap', 'cmbp', 2, nu, bandpass, ell, beam)
    s.add_tracer('Map', 'sz', 0, ell, beam)

    # Window
    ells_large = np.arange(n_ell_large)
    window_single = np.zeros([n_ell, n_ell_large])
    for i in range(n_ell):
        window_single[i, i * d_ell:(i + 1) * d_ell] = 1.
    wins = sacc.BandpowerWindow(ells_large, window_single.T)

    s.add_ell_cl('cl_00', 'gc', 'gc', ell, c_ell, window=wins)
    s.add_ell_cl('cl_0e', 'gc', 'cmbp', ell, c_ell, window=wins)
    s.add_ell_cl('cl_00', 'gc', 'sz', ell, c_ell, window=wins)

    with tempfile.TemporaryDirectory() as tmpdir:
        filename = os.path.join(tmpdir, 'test.sacc')
        s.save_fits(filename)
        s2 = sacc.Sacc.load_fits(filename)

    assert len(s2) == 30
    l, cl, ind = s2.get_ell_cl('cl_00', 'gc', 'sz', return_ind=True)
    w = s2.get_bandpower_windows(ind)
    assert np.all(cl == c_ell)
    assert w.weight.shape == (n_ell_large, n_ell)
Beispiel #13
0
        id_j = 0
        for j1 in range(n_bins):
            for j2 in range(j1, n_bins):
                cl_i1j1 = c_ells_binned[i1, j1, :]+n_ells_binned[i1, j1, :]
                cl_i1j2 = c_ells_binned[i1, j2, :]+n_ells_binned[i1, j2, :]
                cl_i2j1 = c_ells_binned[i2, j1, :]+n_ells_binned[i2, j1, :]
                cl_i2j2 = c_ells_binned[i2, j2, :]+n_ells_binned[i2, j2, :]
                # Knox formula
                cov = (cl_i1j1 * cl_i2j2 + cl_i1j2 * cl_i2j1) / (d_ell * fsky * (2 * l_eff + 1))
                covar[id_i, :, id_j, :] = np.diag(cov)
                id_j += 1
        id_i += 1
covar = covar.reshape([n_cross * n_bpw, n_cross * n_bpw])

# Open sacc
s = sacc.Sacc()
# Add tracers
for i, n in enumerate(nzs):
    s.add_tracer('NZ', 'wl_%d' % i,  # Name
                 quantity='galaxy_shear',  # Quantity
                 spin=2,  # Spin
                 z=z,  # z
                 nz=n,  # nz
                 sigma_g=0.28)  # You can add any extra information as **kwargs
# Add C_ells
wins = sacc.BandpowerWindow(ells, window_single.T)
for i1 in range(n_bins):
    for i2 in range(i1, n_bins):
        s.add_ell_cl('cl_ee', 'wl_%d' % i1, 'wl_%d' % i2, l_eff, c_ells_binned[i1, i2, :],
                     window=wins)
# Add covariance
Beispiel #14
0
n_x = (nmaps * (nmaps + 1)) // 2
n_cls = n_ells * n_x

cov_full = np.zeros([n_x, n_x, n_ells, n_ells])
for ix1, (e1a, f1a, e1b, f1b, p1a, p1b) in enumerate(get_x_iterator()):
    for ix2, (e2a, f2a, e2b, f2b, p2a, p2b) in enumerate(get_x_iterator()):
        cv = get_ell_covar(e1a, f1a, p1a, e1b, f1b, p1b, e2a, f2a, p2a, e2b,
                           f2b, p2b, covar_in, n_ells, id_cov_order)
        cov_full[ix1, ix2, :, :] = cv
cov_full = np.transpose(cov_full, axes=[0, 2, 1, 3]).reshape([n_cls, n_cls])

for isim in range(iStart, iStop):
    print(isim)

    spec_sacc = sacc.Sacc()
    if isim == 0:
        cov_sacc = sacc.Sacc()

    sim_suffix = "%05d" % isim

    for exp in experiments:
        freqs = d["freqs_%s" % exp]
        for f in freqs:
            # dummies file: not in used
            data_bandpasses = {"nu": [149, 150], "b_nu": [0.5, 0.5]}
            data_beams = {"l": np.arange(10000), "bl": np.ones(10000)}

            spec_sacc.add_tracer("NuMap",
                                 "%s_%s_s0" % (exp, f),
                                 "temperature",
Beispiel #15
0
bin_lo, bin_hi, lb, bin_size = pspy_utils.read_binning_file(binning_file, lmax)
n_bins = len(bin_hi)

# let's get a list of all frequencies we plan to study
surveys = d["surveys"]
frequencies = []
for sv in surveys:
    arrays = d["arrays_%s" % sv]
    for ar in arrays:
        frequencies += [int(d["nu_eff_%s_%s" % (sv, ar)])]
frequencies = np.sort(list(dict.fromkeys(frequencies)))

for iii in range(iStart, iStop):
    # Saving into sacc format
    act_sacc = sacc.Sacc()
    for freq in frequencies:
        for spin, quantity in zip([0, 2], ["temperature", "polarization"]):
            # dummies file: not in used
            data_beams = {"l": np.arange(10000), "bl": np.ones(10000)}

            act_sacc.add_tracer(
                "NuMap",
                f"ACT_{freq}_s{spin}",
                quantity=f"cmb_{quantity}",
                spin=spin,
                nu=[freq],
                bandpass=[1.0],
                ell=data_beams.get("l"),
                beam=data_beams.get("bl"),
            )
Beispiel #16
0
def test_data_type_warning():
    s = sacc.Sacc()
    s.add_tracer('Misc', 'source_0')
    with pytest.warns(UserWarning):
        s.add_data_point('cl_wrong', ('source_0', 'source_0'), 0.1, ell=10.)
Beispiel #17
0
def test_quantity_warning():
    s = sacc.Sacc()
    with pytest.warns(UserWarning):
        s.add_tracer('Misc', 'source_0', quantity='dummy')
Beispiel #18
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
Beispiel #19
0
    def get_everything(self):
        if self.sacc is None:
            fname = os.path.join(self.config['output_dir'], f'cls_all.fits')
            fname_n = os.path.join(self.config['output_dir'],
                                   f'cls_noise.fits')
            if not os.path.isfile(fname):
                self.get_cls()
                self.get_covariances()
                s = sacc.Sacc()
                sn = sacc.Sacc()
                # Add tracers
                s.add_tracer('NZ',
                             'LoTSS',
                             quantity='galaxy_density',
                             spin=0,
                             z=np.linspace(0, 5, 512),
                             nz=np.ones(512))  # We will need a better N(z)
                s.add_tracer('Map',
                             'Planck18',
                             quantity='cmb_convergence',
                             spin=0,
                             ell=np.arange(3 * self.nside),
                             beam=np.ones(3 * self.nside))
                sn.add_tracer('NZ',
                              'LoTSS',
                              quantity='galaxy_density',
                              spin=0,
                              z=np.linspace(0, 5, 512),
                              nz=np.ones(512))  # We will need a better N(z)
                sn.add_tracer('Map',
                              'Planck18',
                              quantity='cmb_convergence',
                              spin=0,
                              ell=np.arange(3 * self.nside),
                              beam=np.ones(3 * self.nside))

                order = []
                nls = []
                ells = np.arange(3 * self.nside)
                if 'gg' in self.cls_compute:
                    d = self.cls['gg']
                    wins = sacc.BandpowerWindow(ells, d['win'][0, :, 0, :].T)
                    s.add_ell_cl('cl_00',
                                 'LoTSS',
                                 'LoTSS',
                                 d['ls'],
                                 d['cl'][0] - d['nl'][0],
                                 window=wins)
                    sn.add_ell_cl('cl_00', 'LoTSS', 'LoTSS', d['ls'],
                                  d['nl'][0])
                    order.append('gg')
                    nls.append(len(d['ls']))
                if 'gk' in self.cls_compute:
                    d = self.cls['gk']
                    wins = sacc.BandpowerWindow(ells, d['win'][0, :, 0, :].T)
                    s.add_ell_cl('cl_00',
                                 'LoTSS',
                                 'Planck18',
                                 d['ls'],
                                 d['cl'][0] - d['nl'][0],
                                 window=wins)
                    sn.add_ell_cl('cl_00', 'LoTSS', 'Planck18', d['ls'],
                                  d['nl'][0])
                    order.append('gk')
                    nls.append(len(d['ls']))
                if 'kk' in self.cls_compute:
                    d = self.cls['kk']
                    wins = sacc.BandpowerWindow(ells, d['win'][0, :, 0, :].T)
                    s.add_ell_cl('cl_00',
                                 'Planck18',
                                 'Planck18',
                                 d['ls'],
                                 d['cl'][0] - d['nl'][0],
                                 window=wins)
                    sn.add_ell_cl('cl_00', 'Planck18', 'Planck18', d['ls'],
                                  d['nl'][0])
                    order.append('kk')
                    nls.append(len(d['ls']))
                ncls = len(nls)
                cov = np.zeros([ncls, nls[0], ncls, nls[0]])
                for i1, t1 in enumerate(order):
                    for i2, t2 in enumerate(order):
                        if t1 + t2 in self.cov:
                            cv = self.cov[t1 + t2]
                        else:
                            cv = self.cov[t2 + t1].T
                        cov[i1, :, i2, :] = cv
                cov = cov.reshape([ncls * nls[0], ncls * nls[0]])
                s.add_covariance(cov)
                s.save_fits(fname)
                sn.save_fits(fname_n)
                self.sacc = s
                self.sacc_noise = sn
            else:
                self.sacc = sacc.Sacc.load_fits(fname)
                self.sacc_noise = sacc.Sacc.load_fits(fname_n)
        return self.sacc, self.sacc_noise
_, nell[:, 2:], _ = nc.Simons_Observatory_V3_SA_noise(sens, knee, ylf, fsky,
                                                      lmax + 1, 1)
n_bpw = np.sum(nell[:, None, :] * windows[None, :, :], axis=2)
bpw_freq_noi = np.zeros_like(bpw_freq_sig)
for ib, n in enumerate(n_bpw):
    bpw_freq_noi[ib, 0, ib, 0, :] = n_bpw[ib, :]
    bpw_freq_noi[ib, 1, ib, 1, :] = n_bpw[ib, :]

# Add to signal
bpw_freq_tot = bpw_freq_sig + bpw_freq_noi
bpw_freq_tot = bpw_freq_tot.reshape([nfreqs * 2, nfreqs * 2, nbands])
bpw_freq_sig = bpw_freq_sig.reshape([nfreqs * 2, nfreqs * 2, nbands])
bpw_freq_noi = bpw_freq_noi.reshape([nfreqs * 2, nfreqs * 2, nbands])

# Creating Sacc files
s_d = sacc.Sacc()
s_f = sacc.Sacc()
s_n = sacc.Sacc()

# Adding tracers
print("Adding tracers")
for ib, n in enumerate(band_names):
    bandpass = bpss[n]
    beam = beams[n]
    for s in [s_d, s_f, s_n]:
        s.add_tracer('NuMap',
                     'band%d' % (ib + 1),
                     quantity='cmb_polarization',
                     spin=2,
                     nu=bandpass.nu,
                     bandpass=bandpass.bnu,
Beispiel #21
0
def test_keep_remove():
    s = sacc.Sacc()

    # Tracer
    z = np.arange(0., 1.0, 0.01)
    nz = (z - 0.5)**2 / 0.1**2
    s.add_tracer('NZ', 'source_0', z, nz)
    s.add_tracer('NZ', 'source_1', z, nz, quantity='galaxy_shear', spin=2)
    s.add_tracer('NZ', 'source_2', z, nz, quantity='cluster_density')

    for i in range(20):
        ee = 0.1 * i
        tracers = ('source_0', 'source_0')
        s.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                         tracers,
                         ee,
                         ell=10.0 * i)
    for i in range(20):
        bb = 0.1 * i
        tracers = ('source_1', 'source_1')
        s.add_data_point(sacc.standard_types.galaxy_shear_cl_bb,
                         tracers,
                         bb,
                         ell=10.0 * i)
    for i in range(20):
        ee = 0.1 * i
        tracers = ('source_2', 'source_2')
        s.add_data_point(sacc.standard_types.galaxy_shear_cl_ee,
                         tracers,
                         ee,
                         ell=10.0 * i)

    # Select by data type
    s2 = s.copy()
    s2.keep_selection(data_type=sacc.standard_types.galaxy_shear_cl_bb)
    assert all(d.data_type == sacc.standard_types.galaxy_shear_cl_bb
               for d in s2.data)
    assert len(s2) == 20

    # From multiple tracers
    s2 = s.copy()
    s2.keep_selection(data_type=sacc.standard_types.galaxy_shear_cl_ee)
    assert all(d.data_type == sacc.standard_types.galaxy_shear_cl_ee
               for d in s2.data)
    assert len(s2) == 40

    # Test removing a single tracer
    s2 = s.copy()
    s2.remove_selection(tracers=('source_1', 'source_1'))
    for i, d in enumerate(s2.data):
        if i < 20:
            assert d.tracers == ('source_0', 'source_0')
        else:
            assert d.tracers == ('source_2', 'source_2')
    assert all(d.data_type == sacc.standard_types.galaxy_shear_cl_ee
               for d in s2.data)
    assert len(s2) == 40

    # Test selecting by tag
    s2 = s.copy()
    s2.remove_selection(ell__lt=55)
    ell = s2.get_tag('ell')
    for e in ell:
        assert e > 55
    s2 = s.copy()
    s2.keep_selection(ell__lt=55)
    ell = s2.get_tag('ell')
    for e in ell:
        assert e < 55

    # Cutting just by index
    s2 = s.copy()
    ind = s2.indices(tracers=('source_1', 'source_1'))
    assert (ind == np.arange(20, 40)).all()

    # multiple selections
    s2 = s.copy()
    ind = s2.indices(tracers=('source_2', 'source_2'), ell__lt=45)
    assert len(ind) == 5
Beispiel #22
0
def two_point_template(config):
    """Creates a template SACC object with tracers and statistics

    Parameters:
    ----------
    config : dict
        The dictinary containt the relevant two_point section of the config

    Returns:
    -------
    sacc : sacc object

       Sacc objects with appropriate tracers and measurement slots
       (i.e. data vectors with associated correlation functions,
       angles, etc), but with zeros for measurement values
    """

    S = sacc.Sacc()
    verbose = config["verbose"]
    cosmo = firecrown.get_ccl_cosmology(config["parameters"])
    #  I assume that kmax will be passed in the default CCL units, i.e., Mpc^-1
    kmax = config["kmax"] if "kmax" in config.keys() else None
    zmean = {}
    if verbose:
        print("Generating tracers: ", end="")
    for src, tcfg in config["sources"].items():
        if verbose:
            print(src, " ", end="")
        if "Nz_type" not in tcfg:
            print("Missing Nz_type in %s. Quitting." % src)
            raise RuntimeError
        if tcfg["Nz_type"] == "LensSRD2018":
            mu, wi = tcfg["Nz_center"], tcfg["Nz_width"]
            alpha, z0 = tcfg["Nz_alpha"], tcfg["Nz_z0"]
            sig_z = tcfg["Nz_sigmaz"]
            zall = np.linspace(0, 4, 1500)
            mask = (zall > mu - wi / 2) & (zall < mu + wi / 2)
            dndz_bin = np.zeros_like(zall)
            dndz_bin[mask] = srd_dndz(zall[mask], z0, alpha)
            dz = zall[1] - zall[0]
            # Convolve the SRD N(z) with a Gaussian with the required smearing
            Nz = gaussian_filter(dndz_bin, 0.05 * (1 + mu) / dz)
            S.add_tracer("NZ", src, zall, Nz)
            zmean[src] = np.average(zall, weights=Nz / np.sum(Nz))
        elif tcfg["Nz_type"] == "Gaussian":
            mu, wi = tcfg["Nz_center"], tcfg["Nz_width"]
            zall = np.linspace(0, 4, 1500)
            mask = (zall > mu - wi / 2) & (zall < mu + wi / 2)
            Nz = np.exp(-0.5 * (zall - mu)**2 / wi**2)
            S.add_tracer("NZ", src, zall, Nz)
            zmean[src] = np.average(zall, weights=Nz / np.sum(Nz))
        elif tcfg["Nz_type"] == "TopHat":
            mu, wi = tcfg["Nz_center"], tcfg["Nz_width"]
            alpha, z0 = tcfg["Nz_alpha"], tcfg["Nz_z0"]
            zar = np.linspace(max(0, mu - wi / 2), mu + wi / 2, 5)
            Nz = np.ones(5)
            S.add_tracer("NZ", src, zar, Nz)
            zmean[src] = np.average(zar, weights=Nz / np.sum(Nz))
        elif tcfg["Nz_type"] == 'SourceSRD2018':
            ibin, nbins = tcfg["Nz_bin"], tcfg['Nz_nbins']
            alpha, z0 = tcfg["Nz_alpha"], tcfg["Nz_z0"]
            sig_z = tcfg["Nz_sigmaz"]
            zall = np.linspace(0, 4, 1500)
            tile_hi = 1.0 / nbins * (ibin + 1)
            tile_low = 1.0 / nbins * ibin
            nz_sum = np.cumsum(srd_dndz(zall, z0, alpha)) / np.sum(
                srd_dndz(zall, z0, alpha))
            zlow = zall[np.argmin(np.fabs(nz_sum - tile_low))]
            zhi = zall[np.argmin(np.fabs(nz_sum - tile_hi))]
            zcent = 0.5 * (zlow + zhi)
            dz = zall[1] - zall[0]
            mask = (zall > zlow) & (zall < zhi)
            dndz_bin = np.zeros_like(zall)
            dndz_bin[mask] = srd_dndz(zall[mask], z0, alpha)
            # Convolve the SRD N(z) with a Gaussian with the required smearing
            Nz = gaussian_filter(dndz_bin,
                                 sig_z * (1 + zcent) /
                                 dz)  # sigma should be in units of step
            S.add_tracer("NZ", src, zall, Nz)
            zmean[src] = np.average(zall, weights=Nz / np.sum(Nz))
        else:
            print("Bad Nz_type %s in %s. Quitting." % (tcfg["Nz_type"], src))
            raise RuntimeError
    if verbose:
        print("\nGenerating data slots: ", end="")
    for name, scfg in config["statistics"].items():
        if verbose:
            print(name, " ", end="")
        dt = scfg["sacc_data_type"]
        src1, src2 = scfg["sources"]
        zmean1 = zmean[src1]
        zmean2 = zmean[src2]
        a12 = np.array([1. / (1 + zmean1), 1. / (1 + zmean2)])
        if kmax is not None:
            ell_max = (kmax * ccl.comoving_radial_distance(cosmo, a12) -
                       0.5).astype(np.int)
            ell_max = np.min(ell_max)  # we get the minimum ell_max
        else:
            ell_max = None
        if "cl" in dt:
            ell_edges = scfg["ell_edges"]
            if isinstance(ell_edges, str):
                ell_edges = eval(ell_edges)
            else:
                ell_edges = np.array(ell_edges)
            ell_edges = np.sort(ell_edges)
            # Here I choose to cut the last bin to ell_max (we could drop it)
            if (ell_max is not None) & ("shear_cl_ee" not in dt):
                ell_edges = ell_edges[ell_edges <= ell_max]
            scfg["ell_edges"] = ell_edges
            ells = 0.5 * (ell_edges[:-1] + ell_edges[1:])
            for ell in ells:
                S.add_data_point(dt, (src1, src2), 0.0, ell=ell, error=1e30)
        elif "xi" in dt:
            theta_edges = np.array(scfg["theta_edges"])
            thetas = 0.5 * (theta_edges[:-1] + theta_edges[1:])
            for theta in thetas:
                S.add_data_point(dt, (src1, src2),
                                 0.0,
                                 theta=theta,
                                 error=1e30)
        else:
            print("Cannot process %s. Quitting." % dt)
            raise NotImplementedError

    if verbose:
        print()
    config["sacc_data"] = S
Beispiel #23
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 {} \;'
        )
Beispiel #24
0
    def parse_splits_sacc_file(self, s, get_saccs=False, with_windows=False):
        """
        Transform a SACC file containing splits into 4 SACC vectors:
        1 that contains the coadded power spectra.
        1 that contains coadded power spectra for cross-split only.
        1 that contains an estimate of the noise power spectrum.
        1 that contains all null tests
        """

        # Check we have the right number of bands, splits,
        # cross-correlations and power spectra
        self.check_sacc_consistency(s)

        # Now read power spectra into an array of form
        # [nsplits,nsplits,nbands,nbands,2,2,n_ell]
        # This duplicates the number of elements, but
        # simplifies bookkeeping significantly.

        # Put it in shape [nsplits,nsplits,nbands,2,nbands,2,nl]
        spectra = np.transpose(s.mean[self.inds].reshape([self.nsplits,
                                                          self.nbands, 2,
                                                          self.nsplits,
                                                          self.nbands, 2,
                                                          self.n_bpws]),
                               axes=[0, 3, 1, 2, 4, 5, 6])

        # Coadding (assuming flat coadding)
        # Total coadding (including diagonal)
        weights_total = np.ones(self.nsplits, dtype=float)/self.nsplits
        spectra_total = np.einsum('i,ijklmno,j',
                                  weights_total,
                                  spectra,
                                  weights_total)

        # Off-diagonal coadding
        triu_mean = np.mean(spectra[np.triu_indices(self.nsplits, 1)], axis=0)
        tril_mean = np.mean(spectra[np.tril_indices(self.nsplits, -1)], axis=0)
        spectra_xcorr = 0.5*(tril_mean+triu_mean)

        # Noise power spectra
        spectra_noise = spectra_total - spectra_xcorr

        # Nulls
        spectra_nulls = np.zeros([self.n_nulls,
                                  self.nbands, 2,
                                  self.nbands, 2,
                                  self.n_bpws])
        for i_null, (i, j, k, l) in enumerate(self.pairings):
            spectra_nulls[i_null] = (spectra[i, k]-spectra[i, l] -
                                     spectra[j, k]+spectra[j, l])

        ret = {}
        if get_saccs:

            s_total = sacc.Sacc()
            s_xcorr = sacc.Sacc()
            s_noise = sacc.Sacc()
            s_nulls = sacc.Sacc()
            for t in self.t_coadd:
                s_total.add_tracer_object(t)
                s_xcorr.add_tracer_object(t)
                s_noise.add_tracer_object(t)
            for t in self.t_nulls:
                s_nulls.add_tracer_object(t)

            itr = self.bands_pol_iterator(half=True,
                                          with_windows=with_windows)
            for b1, ip1, b2, ip2, l1, l2, x, win in itr:
                s_total.add_ell_cl('cl_' + x, l1, l2,
                                   self.ells,
                                   spectra_total[b1, ip1, b2, ip2],
                                   window=win)
                s_xcorr.add_ell_cl('cl_' + x, l1, l2,
                                   self.ells,
                                   spectra_xcorr[b1, ip1, b2, ip2],
                                   window=win)
                s_noise.add_ell_cl('cl_' + x, l1, l2,
                                   self.ells,
                                   spectra_noise[b1, ip1, b2, ip2],
                                   window=win)
            for i_null, (i, j, k, l) in enumerate(self.pairings):
                itrb = self.bands_pol_iterator(half=False,
                                               with_windows=with_windows)
                for b1, ip1, b2, ip2, l1, l2, x, win in itrb:
                    l1s = l1 + '_null%dm%d' % (i+1, j+1)
                    l2s = l2 + '_null%dm%d' % (k+1, l+1)
                    s_nulls.add_ell_cl('cl_' + x, l1s, l2s,
                                       self.ells,
                                       spectra_nulls[i_null, b1, ip1, b2, ip2],
                                       window=win)
            ret['saccs'] = [s_total, s_xcorr, s_noise, s_nulls]

        spectra_total = spectra_total.reshape([2*self.nbands, 2*self.nbands, self.n_bpws])[np.triu_indices(2*self.nbands)].flatten()
        spectra_xcorr = spectra_xcorr.reshape([2*self.nbands, 2*self.nbands, self.n_bpws])[np.triu_indices(2*self.nbands)].flatten()
        spectra_noise = spectra_noise.reshape([2*self.nbands, 2*self.nbands, self.n_bpws])[np.triu_indices(2*self.nbands)].flatten()
        spectra_nulls = spectra_nulls.reshape([-1, self.n_bpws]).flatten()
        ret['spectra'] = [spectra_total, spectra_xcorr, spectra_noise, spectra_nulls]

        return ret
Beispiel #25
0
def saveps(sk):
    prefix_out = f'data/sim0{sk}/'

    if residuals:
        sname = 'residual'
        if masked:
            fnames = glob.glob(f'{prefix_out}masked_residualmaps*.fits')
        else:
            fnames = glob.glob(f'{prefix_out}full_residualmaps*.fits')
    else:
        sname = 'baseline'
        fdir = f'/mnt/zfsusers/mabitbol/simdata/sims_gauss_fullsky_ns256_csd_std0.{sk}_gm3/'
        fnames = glob.glob(f'{fdir}s*/maps_sky_signal.fits')
    fnames.sort()

    if masked:
        sname += '_masked'
        sat_mask = hp.ud_grade(hp.read_map('mask_apodized.fits'), nside)
    else:
        sat_mask = np.ones(npix)

    # Precompute coupling matrix for namaster
    b = nmt.NmtBin.from_nside_linear(nside, dell, is_Dell=True)
    purify_b = False
    if masked:
        purify_b = True
    empty_field = nmt.NmtField(sat_mask, maps=None, spin=2, purify_b=purify_b)
    w_yp = nmt.NmtWorkspace()
    w_yp.compute_coupling_matrix(empty_field, empty_field, b)

    for kn in range(nsims):
        x = hp.read_map(fnames[kn],
                        field=np.arange(nfreqs * npol),
                        verbose=False).reshape((nfreqs, npol, -1))
        x[:, :, sat_mask == 0] = 0.

        f2_x = []
        for i in range(nfreqs):
            f2_x.append(nmt.NmtField(sat_mask, x[i], purify_b=purify_b))

        bpw_freq_sig = np.zeros((nfreqs, npol, nfreqs, npol, nbands))
        for i in range(nfreqs):
            for j in range(nfreqs):
                cl_coupled = nmt.compute_coupled_cell(f2_x[i], f2_x[j])
                cl_decoupled = w_yp.decouple_cell(cl_coupled)
                bpw_freq_sig[i, 0, j, 0] = cl_decoupled[0]
                bpw_freq_sig[i, 0, j, 1] = cl_decoupled[1]
                bpw_freq_sig[i, 1, j, 0] = cl_decoupled[2]
                bpw_freq_sig[i, 1, j, 1] = cl_decoupled[3]

        # Add to signal
        bpw_freq_tot = bpw_freq_sig + bpw_freq_noi
        bpw_freq_tot = bpw_freq_tot.reshape(
            [nfreqs * npol, nfreqs * npol, nbands])
        bpw_freq_sig = bpw_freq_sig.reshape(
            [nfreqs * npol, nfreqs * npol, nbands])

        # Creating Sacc files
        s_d = sacc.Sacc()
        s_f = sacc.Sacc()
        s_n = sacc.Sacc()

        # Adding tracers
        for ib, n in enumerate(band_names):
            bandpass = bpss[n]
            beam = beams[n]
            for s in [s_d, s_f, s_n]:
                s.add_tracer('NuMap',
                             'band%d' % (ib + 1),
                             quantity='cmb_polarization',
                             spin=2,
                             nu=bandpass.nu,
                             bandpass=bandpass.bnu,
                             ell=larr_all,
                             beam=beam,
                             nu_unit='GHz',
                             map_unit='uK_CMB')

        # Adding power spectra
        nmaps = npol * nfreqs
        ncross = (nmaps * (nmaps + 1)) // 2
        indices_tr = np.triu_indices(nmaps)
        map_names = []
        for ib, n in enumerate(band_names):
            map_names.append('band%d' % (ib + 1) + '_E')
            map_names.append('band%d' % (ib + 1) + '_B')
        for ii, (i1, i2) in enumerate(zip(indices_tr[0], indices_tr[1])):
            n1 = map_names[i1][:-2]
            n2 = map_names[i2][:-2]
            p1 = map_names[i1][-1].lower()
            p2 = map_names[i2][-1].lower()
            cl_type = f'cl_{p1}{p2}'
            s_d.add_ell_cl(cl_type,
                           n1,
                           n2,
                           leff,
                           bpw_freq_sig[i1, i2, :],
                           window=s_wins)
            s_f.add_ell_cl(cl_type,
                           n1,
                           n2,
                           leff,
                           bpw_freq_sig[i1, i2, :],
                           window=s_wins)
            s_n.add_ell_cl(cl_type,
                           n1,
                           n2,
                           leff,
                           bpw_freq_noi_re[i1, i2, :],
                           window=s_wins)

        # Add covariance
        cov_bpw = np.zeros([ncross, nbands, ncross, nbands])
        factor_modecount = 1. / ((2 * leff + 1) * dell * fsky)
        for ii, (i1, i2) in enumerate(zip(indices_tr[0], indices_tr[1])):
            for jj, (j1, j2) in enumerate(zip(indices_tr[0], indices_tr[1])):
                covar = (bpw_freq_tot[i1, j1, :] * bpw_freq_tot[i2, j2, :] +
                         bpw_freq_tot[i1, j2, :] *
                         bpw_freq_tot[i2, j1, :]) * factor_modecount
                cov_bpw[ii, :, jj, :] = np.diag(covar)
        cov_bpw = cov_bpw.reshape([ncross * nbands, ncross * nbands])
        s_d.add_covariance(cov_bpw)

        # Write output
        skn = str(kn).zfill(4)
        s_d.save_fits(f'{prefix_out}cls_coadd_{sname}_{skn}.fits',
                      overwrite=True)
        s_f.save_fits(f'{prefix_out}cls_fid_{sname}_{skn}.fits',
                      overwrite=True)
        s_n.save_fits(f'{prefix_out}cls_noise_{sname}_{skn}.fits',
                      overwrite=True)
    return
Beispiel #26
0
def coadd_saccs(saccfiles, tracers, ell_max_dict=None, trim_sacc=True):
    logger.info('Coadding all saccfiles weighted by inverse variance.')

    for saccfile in saccfiles:
        logger.info('Initial size of saccfile = {}.'.format(
            saccfile.mean.size))
        logger.info('Removing B-modes.')
        saccfile.remove_selection(data_type='cl_eb')
        saccfile.remove_selection(data_type='cl_be')
        saccfile.remove_selection(data_type='cl_bb')
        saccfile.remove_selection(data_type='cl_0b')
        logger.info('Removing yxy.')
        saccfile.remove_selection(data_type='cl_00', tracers=('y_0', 'y_0'))
        logger.info('Removing kappaxkappa.')
        saccfile.remove_selection(data_type='cl_00',
                                  tracers=('kappa_0', 'kappa_0'))
        logger.info('Removing kappaxy.')
        saccfile.remove_selection(data_type='cl_00',
                                  tracers=('kappa_0', 'y_0'))
        saccfile.remove_selection(data_type='cl_00',
                                  tracers=('y_0', 'kappa_0'))
        logger.info('Size of saccfile after cuts = {}.'.format(
            saccfile.mean.size))

        if ell_max_dict is not None:
            logger.info('Size of saccfile before ell cuts {}.'.format(
                saccfile.mean.size))
            for tr_i, tr_j in saccfile.get_tracer_combinations():
                if tr_i in tracers and tr_j in tracers:
                    ell_max_curr = min(ell_max_dict[tr_i], ell_max_dict[tr_j])
                    logger.info('Removing ells > {} for {}, {}.'.format(
                        ell_max_curr, tr_i, tr_j))
                    saccfile.remove_selection(tracers=(tr_i, tr_j),
                                              ell__gt=ell_max_curr)
                else:
                    saccfile.remove_selection(tracers=(tr_i, tr_j))
            logger.info('Size of saccfile after ell cuts {}.'.format(
                saccfile.mean.size))

    ntracers_arr = np.array([len(saccfile.tracers) for saccfile in saccfiles])
    ntracers_unique = np.unique(ntracers_arr)[::-1]

    saccs_list = [[] for i in range(ntracers_unique.shape[0])]
    for i in range(ntracers_unique.shape[0]):
        for saccfile in saccfiles:
            if len(saccfile.tracers) == ntracers_unique[i]:
                saccs_list[i].append(saccfile)

    sacc_coadds = [0 for i in range(ntracers_unique.shape[0])]
    for i in range(ntracers_unique.shape[0]):
        len_curr = ntracers_unique[i]
        nsacc_curr = len(saccs_list[i])
        logger.info('Found {} saccfiles of length {}.'.format(
            nsacc_curr, len_curr))
        for j, saccfile in enumerate(saccs_list[i]):
            if j == 0:
                coadd_mean = saccfile.mean
                coadd_cov = saccfile.covariance.covmat
            else:
                coadd_mean += saccfile.mean
                coadd_cov += saccfile.covariance.covmat

        coadd_mean /= nsacc_curr
        coadd_cov /= nsacc_curr**2

        # Copy sacc
        saccfile_coadd = saccfile.copy()
        # Set mean of new saccfile to coadded mean
        saccfile_coadd.mean = coadd_mean
        saccfile_coadd.add_covariance(coadd_cov)
        sacc_coadds[i] = saccfile_coadd

    tempsacc = sacc_coadds[0]
    tempsacc_tracers = tempsacc.tracers.keys()
    datatypes = tempsacc.get_data_types()
    invcov_coadd = np.linalg.inv(tempsacc.covariance.covmat)
    mean_coadd = np.dot(invcov_coadd, tempsacc.mean)

    assert set(tracers) <= set(tempsacc_tracers), 'Larger tracer set requested than present in largest ' \
                                                                 'saccfile. Aborting.'

    for i, saccfile in enumerate(sacc_coadds[1:]):
        sacc_tracers = saccfile.tracers.keys()
        missing_tracers = list(set(tracers) - set(sacc_tracers))
        logger.info('Found missing tracers {} in saccfile {}.'.format(
            missing_tracers, i))

        invcov_small_curr = np.linalg.inv(saccfile.covariance.covmat)

        mean_big_curr = np.zeros_like(tempsacc.mean)
        invcov_big_curr = np.zeros_like(tempsacc.covariance.covmat)

        for datatype in datatypes:
            tracer_combs = tempsacc.get_tracer_combinations(data_type=datatype)
            for tr_i1, tr_j1 in tracer_combs:
                _, cl = saccfile.get_ell_cl(datatype,
                                            tr_i1,
                                            tr_j1,
                                            return_cov=False)

                ind_here = saccfile.indices(data_type=datatype,
                                            tracers=(tr_i1, tr_j1))
                ind_tempsacc = tempsacc.indices(data_type=datatype,
                                                tracers=(tr_i1, tr_j1))
                if not ind_here.size == 0:
                    mean_big_curr[ind_tempsacc] = cl
                for tr_i2, tr_j2 in tracer_combs:
                    ind_i1j1_curr = saccfile.indices(data_type=datatype,
                                                     tracers=(tr_i1, tr_j1))
                    ind_i2j2_curr = saccfile.indices(data_type=datatype,
                                                     tracers=(tr_i2, tr_j2))

                    subinvcov_curr = invcov_small_curr[np.ix_(
                        ind_i1j1_curr, ind_i2j2_curr)]

                    ind_i1j1_tempsacc = tempsacc.indices(data_type=datatype,
                                                         tracers=(tr_i1,
                                                                  tr_j1))
                    ind_i2j2_tempsacc = tempsacc.indices(data_type=datatype,
                                                         tracers=(tr_i2,
                                                                  tr_j2))

                    if ind_i1j1_curr.size != 0 and ind_i2j2_curr.size != 0:
                        invcov_big_curr[np.ix_(
                            ind_i1j1_tempsacc,
                            ind_i2j2_tempsacc)] = subinvcov_curr

        mean_coadd += np.dot(invcov_big_curr, mean_big_curr)
        invcov_coadd += invcov_big_curr

    # Copy sacc
    saccfile_coadd = tempsacc.copy()
    # Set mean of new saccfile to coadded mean
    cov_coadd = np.linalg.inv(invcov_coadd)
    saccfile_coadd.mean = np.dot(cov_coadd, mean_coadd)
    saccfile_coadd.add_covariance(cov_coadd)

    if trim_sacc:
        logger.info('Trimming sacc - removing windows and covariance.')
        saccfile_coadd_trimmed = sacc.Sacc()
        for trc_name in saccfile_coadd.tracers.keys():
            saccfile_coadd_trimmed.add_tracer_object(
                saccfile_coadd.tracers[trc_name])
        datatypes = saccfile_coadd.get_data_types()
        for datatype in datatypes:
            tracer_combs = saccfile_coadd.get_tracer_combinations(
                data_type=datatype)
            for tr_i1, tr_j1 in tracer_combs:
                ell, cl = saccfile_coadd.get_ell_cl(datatype,
                                                    tr_i1,
                                                    tr_j1,
                                                    return_cov=False)

                saccfile_coadd_trimmed.add_ell_cl(datatype, tr_i1, tr_j1, ell,
                                                  cl)
        assert np.all(
            saccfile_coadd.mean == saccfile_coadd_trimmed.mean
        ), 'Error while trimming sacc, means not equal. Aborting.'
        saccfile_coadd_trimmed.add_covariance(cov_coadd)
        saccfile_coadd = saccfile_coadd_trimmed

    return saccfile_coadd