Exemple #1
0
window.plot(file_name="%s/window" % (test_dir), ticks_spacing_car=4)

#for spin0 and 2 the window need to be a tuple made of two objects
#the window used for spin0 and the one used for spin 2
window = (window, window)

# Compute spin 0 spin 2 spectra a la pspy
print("Compute spin0 and spin2 power spectra a la pspy")

t0 = time.time()
mbb_inv, Bbl = so_mcm.mcm_and_bbl_spin0and2(window,
                                            binning_file,
                                            lmax=lmax,
                                            type=type,
                                            niter=niter)
alms = sph_tools.get_alms(split, window, niter, lmax)
l, ps = so_spectra.get_spectra(alms, spectra=spectra)
lb_py, Cb_pspy = so_spectra.bin_spectra(l,
                                        ps,
                                        binning_file,
                                        lmax,
                                        type=type,
                                        mbb_inv=mbb_inv,
                                        spectra=spectra)

print("pspy run in %.2f s" % (time.time() - t0))

print("Compute spin0 and spin2 power spectra a la namaster")

# Compute spin 0 spin 2 spectra a la namaster
t0 = time.time()
    else:
        map_T = d['map_T_%s' % ar]
        map_Q = d['map_Q_%s' % ar]
        map_U = d['map_U_%s' % ar]

    print("compute harmonic transform ...")
    count = 0
    for T, Q, U in zip(map_T, map_Q, map_U):
        map = so_map.from_components(T, Q, U)
        map = so_map.get_submap_car(map, box, mode='floor')

        if d['use_filtered_maps'] == False:
            map = so_map_preprocessing.get_map_kx_ky_filtered_pyfftw(
                map, apo, d['filter_dict'])

        almList += [sph_tools.get_alms(map, window_tuple, niter, lmax)]
        nameList += ['split_%d_%s' % (count, ar)]
        count += 1

    print("get spectra ...")

    Db_dict = {}
    Db_dict_auto = {}
    Db_dict_cross = {}
    for s1 in spectra:
        Db_dict_auto[s1] = []
        Db_dict_cross[s1] = []

    spec_name_list = []
    for name1, alm1, c1 in zip(nameList, almList, np.arange(count)):
        for name2, alm2, c2 in zip(nameList, almList, np.arange(count)):
Exemple #3
0
                if d["use_kspace_filter"]:
                    print("apply kspace filter on %s" %map)
                    binary = so_map.read_map("%s/binary_%s_%s.fits" % (window_dir, sv, ar))
                    split = data_analysis_utils.get_filtered_map(
                        split, binary, vk_mask=d["vk_mask"], hk_mask=d["hk_mask"], normalize=False)

            elif win_T.pixel == "HEALPIX":
                split = so_map.read_map(map)
                
            split.data *= cal
            if d["remove_mean"] == True:
                split = data_analysis_utils.remove_mean(split, window_tuple, ncomp)
                
            #split.plot(file_name="%s/split_%d_%s_%s" % (plot_dir, k, sv, ar), color_range=[250, 100, 100])

            master_alms[sv, ar, k] = sph_tools.get_alms(split, window_tuple, niter, lmax)
            if d["use_kspace_filter"]:
                # there is an extra normalisation for the FFT/IFFT bit
                # note that we apply it here rather than at the FFT level because correcting the alm is faster than correcting the maps
                master_alms[sv, ar, k] /= (split.data.shape[1]*split.data.shape[2])
                
        print(time.time()- t)

ps_dict = {}
_, _, lb, _ = pspy_utils.read_binning_file(binning_file, lmax)

for id_sv1, sv1 in enumerate(surveys):
    arrays_1 = d["arrays_%s" % sv1]
    nsplits_1 = nsplit[sv1]
    
    if d["tf_%s" % sv1] is not None:
            for k in range(nsplits):
                noisy_alms = alms_beamed.copy()

                noisy_alms[0] += nlms["T", k][fid]
                noisy_alms[1] += nlms["E", k][fid]
                noisy_alms[2] += nlms["B", k][fid]

                split = sph_tools.alm2map(noisy_alms, template)

                # Now that we have generated a split of data of experiment exp
                # and frequency freq, we take its harmonic transform
                split = maps_to_params_utils.remove_mean(
                    split, window_tuple, ncomp)
                master_alms[exp, freq,
                            k] = sph_tools.get_alms(split, window_tuple, niter,
                                                    lmax)

    # We now form auto and cross power spectra from the alms
    ps_dict = {}

    for id_exp1, exp1 in enumerate(experiments):
        freqs1 = d["freqs_%s" % exp1]
        nsplits1 = d["nsplits_%s" % exp1]

        for id_f1, f1 in enumerate(freqs1):

            for id_exp2, exp2 in enumerate(experiments):
                freqs2 = d["freqs_%s" % exp2]
                nsplits2 = d["nsplits_%s" % exp2]

                for id_f2, f2 in enumerate(freqs2):
Exemple #5
0
                        norm, split = data_analysis_utils.get_filtered_map(
                            split,
                            binary,
                            filter[sv],
                            weighted_filter=ks_f["weighted"])

                        del binary

                if d["remove_mean"] == True:
                    split = data_analysis_utils.remove_mean(
                        split, window_tuple, ncomp)

                master_alms[sv, ar,
                            k] = sph_tools.get_alms(split,
                                                    window_tuple,
                                                    niter,
                                                    lmax,
                                                    dtype=sim_alm_dtype)
                print("m_alms_dtype", master_alms[sv, ar, k].dtype)
                if ks_f["apply"]:
                    # there is an extra normalisation for the FFT/IFFT bit
                    # note that we apply it here rather than at the FFT level because correcting the alm is faster than correcting the maps
                    master_alms[sv, ar, k] /= (split.data.shape[1] *
                                               split.data.shape[2])**norm

            print(time.time() - t1)

    ps_dict = {}
    _, _, lb, _ = pspy_utils.read_binning_file(binning_file, lmax)

    for id_sv1, sv1 in enumerate(surveys):
                alms_beamed = alms.copy()
                alms_beamed[0] += fglms[id_freq[d["nu_eff_%s_%s" % (sv, ar)]]]

                # we convolve signal + foreground with the beam of the array
                l, bl = pspy_utils.read_beam_file(d["beam_%s_%s" % (sv, ar)])
                alms_beamed = curvedsky.almxfl(alms_beamed, bl)

                if scenario == "noE": alms_beamed[1] *= 0
                if scenario == "noB": alms_beamed[2] *= 0

                # generate our signal only sim
                split = sph_tools.alm2map(alms_beamed, template[sv])

                # compute the alms of the sim

                master_alms[sv, ar, "nofilter"] = sph_tools.get_alms(
                    split, window_tuple, niter, lmax, dtype=sim_alm_dtype)

                # apply the k-space filter

                binary = so_map.read_map("%s/binary_%s_%s.fits" %
                                         (window_dir, sv, ar))

                norm, split = data_analysis_utils.get_filtered_map(
                    split,
                    binary,
                    filter[sv],
                    weighted_filter=ks_f["weighted"])

                # compute the alms of the filtered sim

                master_alms[sv, ar,
Exemple #7
0
models = {k: {} for k in presets}

from itertools import product
for preset, freq in product(presets, frequencies):
    print("Computing {} model @ {} GHz".format(preset, freq))

    # Get emission map
    sky = pysm.Sky(nside=nside, preset_strings=[preset], output_unit=u.uK_CMB)
    emission = sky.get_emission(freq * u.GHz)

    # Compute alm
    from pspy import sph_tools
    tmpl = so_map.healpix_template(ncomp=3, nside=nside)
    tmpl.data = emission.value
    alms = sph_tools.get_alms(tmpl, window, niter=niter, lmax=lmax)

    # Compute spectra
    from pspy import so_spectra
    spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"]
    db = so_spectra.bin_spectra(*so_spectra.get_spectra(alms, spectra=spectra),
                                binning_file,
                                lmax=lmax,
                                type="Dl",
                                mbb_inv=mbb_inv,
                                spectra=spectra)
    models[preset][freq] = {"spectra": db}

    if store_map:
        models[preset][freq].update({"map": emission})
    if store_alms:
        pl_map.data *= 10**6
        cov_map = so_map.read_map('%s' % map, fields_healpix=4)
        badpix = (cov_map.data == hp.pixelfunc.UNSEEN)
        for i in range(3):
            pl_map.data[i][badpix] = 0.0

        if remove_mono_dipo_T:
            pl_map.data[0] = planck_utils.subtract_mono_di(
                pl_map.data[0], window_tuple[0].data, pl_map.nside)
        if remove_mono_dipo_pol:
            pl_map.data[1] = planck_utils.subtract_mono_di(
                pl_map.data[1], window_tuple[1].data, pl_map.nside)
            pl_map.data[2] = planck_utils.subtract_mono_di(
                pl_map.data[2], window_tuple[1].data, pl_map.nside)

        alms[hm, freq] = sph_tools.get_alms(pl_map, window_tuple, niter, lmax)

spin_pairs = ['spin0xspin0', 'spin0xspin2', 'spin2xspin0', 'spin2xspin2']

Db_dict = {}
spec_name_list = []
for c1, freq1 in enumerate(freqs):
    for c2, freq2 in enumerate(freqs):
        if c1 > c2: continue
        for s1, hm1 in enumerate(splits):
            for s2, hm2 in enumerate(splits):
                if (s1 > s2) & (c1 == c2): continue

                prefix = '%s/%s_%sx%s_%s-%sx%s' % (mcmDir, experiment, freq1,
                                                   experiment, freq2, hm1, hm2)
Exemple #9
0
def get_spectra(window,
                maps_info_list,
                car_box,
                type,
                lmax,
                binning_file,
                ps_method="master",
                mbb_inv=None,
                compute_T_only=False):
    """compute the power spectra in the patch

    Parameters
    ----------
    window: so_map
        the window function of the patch
    maps_info_list: list of dicts describing the data maps
      dictionnary should contain the name, the data type ("IQU" or "I") and optionally a calibration factor to apply to the map
      note that all map in the list should have the same data type
    car_box: 2x2 array
      an array of the form [[dec0,rac0],[dec1,ra1]] it encompasses the patch
      and we will only load in memory the map inside the box
    type: string
        the type of binning, either bin Cl or bin Dl
    lmax : integer
        the maximum multipole to consider for the spectra computation
    ps_method: string
      the method for the computation of the power spectrum
      can be "master", "pseudo", or "2dflat" for now
    binning_file: text file
      a binning file with three columns bin low, bin high, bin mean
      note that either binning_file or bin_size should be provided
    mbb_inv: 2d array
      the inverse mode coupling matrix, not in use for 2dflat
    compute_T_only: boolean
        True to compute only T spectra

    """

    ht_list = []
    name_list = []

    if not compute_T_only:
        window = (window, window)

    for map_info in maps_info_list:

        split = so_map.read_map(map_info["name"], car_box=car_box)

        if compute_T_only and map_info["data_type"] == "IQU":
            split.data = split.data[0]
            split.ncomp = 1

        if map_info["cal"] is not None:
            split.data *= map_info["cal"]

        if ps_method in ["master", "pseudo"]:
            print("SPHT of %s in the patch" % map_info["name"])
            alms = sph_tools.get_alms(split, window, niter=0, lmax=lmax + 50)
            ht_list += [alms]

        elif ps_method == "2dflat":
            print("FFT of %s in the patch" % map_info["name"])
            ffts = flat_tools.get_ffts(split, window, lmax)
            ht_list += [ffts]

        name_list += [map_info["id"]]

    split_num = np.arange(len(maps_info_list))

    if compute_T_only:
        if ps_method in ["master", "pseudo"]:
            spectra = None
        elif ps_method == "2dflat":
            spectra = ["II"]
    else:
        if ps_method in ["master", "pseudo"]:
            spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"]
        elif ps_method == "2dflat":
            spectra = ["II", "IQ", "IU", "QI", "QQ", "QU", "UI", "UQ", "UU"]

    ps_dict = {}
    spec_name_list = []

    for name1, ht1, c1 in zip(name_list, ht_list, split_num):
        for name2, ht2, c2 in zip(name_list, ht_list, split_num):
            if c1 > c2: continue

            spec_name = "%sx%s" % (name1, name2)

            if ps_method in ["master", "pseudo"]:
                l, ps = so_spectra.get_spectra(ht1, ht2, spectra=spectra)
                ells, ps_dict[spec_name] = so_spectra.bin_spectra(
                    l,
                    ps,
                    binning_file,
                    lmax,
                    type=type,
                    mbb_inv=mbb_inv,
                    spectra=spectra)

            elif ps_method == "2dflat":
                ells, ps_dict[spec_name] = flat_tools.power_from_fft(ht1,
                                                                     ht2,
                                                                     type=type)

            spec_name_list += [spec_name]

    if compute_T_only:
        # to make TT only behave the same as the other cases, make it a dictionnary
        if ps_method in ["master", "pseudo"]:
            spectra = ["TT"]
            for spec_name in spec_name_list:
                ps_dict[spec_name] = {"TT": ps_dict[spec_name]}

    return spectra, spec_name_list, ells, ps_dict
plt.savefig("%s/corr_namaster.png" % (test_dir))
plt.clf()
plt.close()

print("cov mat sim")
nsims = 1000
Db_list = []
for iii in range(nsims):
    print(iii)
    cmb = template.copy()
    cmb.data = hp.sphtfunc.synfast(ps_theory["TT"],
                                   nside,
                                   new=True,
                                   verbose=False)
    cmb.data -= np.mean(cmb.data * window.data)
    alm = sph_tools.get_alms(cmb, window, niter, lmax)
    ls, ps = so_spectra.get_spectra(alm, alm)
    lb, Db = so_spectra.bin_spectra(ls,
                                    ps,
                                    binning_file,
                                    lmax,
                                    type=type,
                                    mbb_inv=mbb_inv)
    Db_list += [Db]

mean = np.mean(Db_list, axis=0)
std = np.std(Db_list, axis=0)

plt.plot(lb,
         np.sqrt(cov_pspy.diagonal()) / np.sqrt(cov_namaster.diagonal()),
         label="pspy/namaster")
Exemple #11
0
def get_spectra(
    window,
    maps_info_list,
    car_box,
    type,
    lmax,
    binning_file,
    ps_method="master",
    mbb_inv=None,
    compute_T_only=False,
    vk_mask=None,
    hk_mask=None,
    transfer_function=None,
    binary=None,
):
    """Compute the power spectra in the patch

    Parameters
    ----------
    window: so_map
        the window function of the patch
    maps_info_list: list of dicts describing the data maps
      dictionnary should contain the name, the data type ("IQU" or "I") and optionally a calibration factor to apply to the map
      note that all map in the list should have the same data type
    car_box: 2x2 array
      an array of the form [[dec0,rac0],[dec1,ra1]] it encompasses the patch
      and we will only load in memory the map inside the box
    type: string
        the type of binning, either bin Cl or bin Dl
    lmax : integer
        the maximum multipole to consider for the spectra computation
    ps_method: string
      the method for the computation of the power spectrum
      can be "master", "pseudo", or "2dflat" for now
    binning_file: text file
      a binning file with three columns bin low, bin high, bin mean
      note that either binning_file or bin_size should be provided
    mbb_inv: 2d array
      the inverse mode coupling matrix, not in use for 2dflat
    compute_T_only: boolean
        True to compute only T spectra
    vk_mask: list
      the vertical band to filter out from 2D FFT (format is [-lx, +lx])
    hk_mask: list
      the horizontal band to filter out from 2D FFT (format is [-ly, +ly])
    transfer_function: str
      the path to the transfer function
    binary: so_map
      the binary mask to be used in the kspace filter process
    """

    ht_list = []
    name_list = []

    if not compute_T_only:
        window = (window, window)

    for map_info in maps_info_list:

        split = so_map.read_map(map_info["name"], car_box=car_box)

        if compute_T_only and map_info["data_type"] == "IQU":
            split.data = split.data[0]
            split.ncomp = 1

        if map_info["cal"] is not None:
            split.data *= map_info["cal"]

        use_kspace_filter = vk_mask is not None or hk_mask is not None
        if use_kspace_filter:
            timer.start("Filter {} in the patch...".format(
                os.path.basename(map_info["name"])))
            split = get_filtered_map(split, binary, vk_mask, hk_mask)
            timer.stop()

        if ps_method in ["master", "pseudo"]:
            timer.start("SPHT of {} in the patch...".format(
                os.path.basename(map_info["name"])))
            alms = sph_tools.get_alms(split, window, niter=0, lmax=lmax + 50)
            if use_kspace_filter:
                alms /= np.product(split.data.shape[-2:])
            ht_list += [alms]
            timer.stop()

        elif ps_method == "2dflat":
            timer.start("FFT of {} in the patch...".format(
                os.path.basename(map_info["name"])))
            ffts = flat_tools.get_ffts(split, window, lmax)
            ht_list += [ffts]
            timer.stop()

        name_list += [map_info["id"]]

    split_num = np.arange(len(maps_info_list))

    if compute_T_only:
        if ps_method in ["master", "pseudo"]:
            spectra = None
        elif ps_method == "2dflat":
            spectra = ["II"]
    else:
        if ps_method in ["master", "pseudo"]:
            spectra = ["TT", "TE", "TB", "ET", "BT", "EE", "EB", "BE", "BB"]
        elif ps_method == "2dflat":
            spectra = ["II", "IQ", "IU", "QI", "QQ", "QU", "UI", "UQ", "UU"]

    ps_dict = {}
    spec_name_list = []

    for name1, ht1, c1 in zip(name_list, ht_list, split_num):
        for name2, ht2, c2 in zip(name_list, ht_list, split_num):
            if c1 > c2:
                continue

            spec_name = "%sx%s" % (name1, name2)

            if ps_method in ["master", "pseudo"]:
                l, ps = so_spectra.get_spectra(ht1, ht2, spectra=spectra)
                ells, ps_dict[spec_name] = so_spectra.bin_spectra(
                    l,
                    ps,
                    binning_file,
                    lmax,
                    type=type,
                    mbb_inv=mbb_inv,
                    spectra=spectra)
                if use_kspace_filter:
                    _, _, tf, _ = np.loadtxt(transfer_function, unpack=True)
                    if compute_T_only:
                        ps_dict[spec_name] /= tf[np.where(ells < lmax)]
                    else:
                        for spec in spectra:
                            ps_dict[spec_name][spec] /= tf[np.where(
                                ells < lmax)]

            elif ps_method == "2dflat":
                ells, ps_dict[spec_name] = flat_tools.power_from_fft(ht1,
                                                                     ht2,
                                                                     type=type)

            spec_name_list += [spec_name]

    if compute_T_only:
        # to make TT only behave the same as the other cases, make it a dictionnary
        if ps_method in ["master", "pseudo"]:
            spectra = ["TT"]
            for spec_name in spec_name_list:
                ps_dict[spec_name] = {"TT": ps_dict[spec_name]}

    return spectra, spec_name_list, ells, ps_dict
        window.plot(file_name="%s/window_%s_%s" % (plot_dir, scan, run),
                    title=scan)

        window = (window, window)

        mbb_inv, Bbl = so_mcm.mcm_and_bbl_spin0and2(window,
                                                    binning_file,
                                                    lmax=lmax,
                                                    type="Dl",
                                                    niter=niter,
                                                    save_file="%s/%s_%s" %
                                                    (mcm_dir, scan, run))

        alm = {}
        for split in split_list:
            alm[split] = sph_tools.get_alms(sim[split], window, niter, lmax)

        for c0, s0 in enumerate(split_list):
            for c1, s1 in enumerate(split_list):
                if c1 > c0: continue

                spec_name = "%s_%sx%s_%s" % (scan, s0, s1, run)

                l, ps = so_spectra.get_spectra(alm[s0],
                                               alm[s1],
                                               spectra=spectra)
                lb, Db_dict[spec_name] = so_spectra.bin_spectra(
                    l,
                    ps,
                    binning_file,
                    lmax,
        noise1 = so_map.white_noise(template, rms_uKarcmin_T=15)

        split0 = cmb_car.copy()
        split0.data += noise0.data
        split1 = cmb_car.copy()
        split1.data += noise1.data

        split0_filt = split0.copy()
        split1_filt = split1.copy()

        split0_filt = so_map_preprocessing.get_map_kx_ky_filtered_pyfftw(
            split0_filt, apo, d['filter_dict'])
        split1_filt = so_map_preprocessing.get_map_kx_ky_filtered_pyfftw(
            split1_filt, apo, d['filter_dict'])

        alm0 = sph_tools.get_alms(split0, window_tuple, niter, lmax)
        alm1 = sph_tools.get_alms(split1, window_tuple, niter, lmax)

        alm0_filt = sph_tools.get_alms(split0_filt, window_tuple, niter, lmax)
        alm1_filt = sph_tools.get_alms(split1_filt, window_tuple, niter, lmax)

        l, ps = so_spectra.get_spectra(alm0, alm1, spectra=spectra)
        l, ps_filt = so_spectra.get_spectra(alm0_filt,
                                            alm1_filt,
                                            spectra=spectra)

        lb, Db_dict = so_spectra.bin_spectra(l,
                                             ps,
                                             binning_file,
                                             lmax,
                                             type=type,