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)): if c1 > c2: continue l, ps = so_spectra.get_spectra(alm1, alm2, spectra=spectra) spec_name = '%sx%s' % (name1, name2) lb, Db_dict[spec_name] = so_spectra.bin_spectra(l, ps, binning_file, lmax, type=type, mbb_inv=mbb_inv, spectra=spectra) spec_name_list += [spec_name] so_spectra.write_ps('%s/spectra_%s.dat' % (spectraDir, spec_name), lb, Db_dict[spec_name], type=type, spectra=spectra)
if (id_exp1 == id_exp2) & (id_f1 > id_f2): continue if (id_exp1 > id_exp2): continue for spec in spectra: ps_dict[spec, "auto"] = [] ps_dict[spec, "cross"] = [] for s1 in range(nsplits1): for s2 in range(nsplits2): mbb_inv, Bbl = so_mcm.read_coupling( prefix="%s/%s_%sx%s_%s" % (mcm_dir, exp1, f1, exp2, f2), spin_pairs=spin_pairs) l, ps_master = so_spectra.get_spectra( master_alms[exp1, f1, s1], master_alms[exp2, f2, s2], spectra=spectra) spec_name = "%s_%s_%s_%dx%s_%s_%d_%05d" % ( type, exp1, f1, s1, exp2, f2, s2, iii) lb, ps = so_spectra.bin_spectra(l, ps_master, binning_file, lmax, type=type, mbb_inv=mbb_inv, spectra=spectra) if write_all_spectra: so_spectra.write_ps(specDir +
#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() nlb = 40
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) mcm_inv, mbb_inv, Bbl = so_mcm.read_coupling( prefix=prefix, spin_pairs=spin_pairs, unbin=True) l, ps = so_spectra.get_spectra(alms[hm1, freq1], alms[hm2, freq2], spectra=spectra) spec_name = '%s_%sx%s_%s-%sx%s' % (experiment, freq1, experiment, freq2, hm1, hm2) l, cl, lb, Db = planck_utils.process_planck_spectra( l, ps, binning_file, lmax, type=type, mcm_inv=mcm_inv, spectra=spectra) spec_name_list += [spec_name] so_spectra.write_ps('%s/spectra_%s.dat' % (spectraDir, spec_name), lb,
# the window is given by the product of the survey window and the mask window window.data *= mask.data # let's look at it window.plot(file_name="%s/window" % (test_dir), hp_gnomv=(lon, lat, 3500, 1)) print("Compute spin0 power spectra a la pspy") t0 = time.time() # Compute spin 0 spectra a la pspy mbb_inv, Bbl = so_mcm.mcm_and_bbl_spin0(window, binning_file, lmax=lmax, type=type, niter=niter) alm = sph_tools.get_alms(split, window, niter, lmax) l, ps = so_spectra.get_spectra(alm) lb, Cb_pspy = so_spectra.bin_spectra(l, ps, binning_file, lmax, type=type, mbb_inv=mbb_inv) print("pspy run in %.2f s" % (time.time() - t0)) # Compute spin 0 spectra a la namaster print("Compute spin0 power spectra a la namaster") nlb = 40 field = nmt.NmtField(window.data, [split.data]) cl_coupled = nmt.compute_coupled_cell(field, field) b = nmt.NmtBin(nside, nlb=nlb) lb = b.get_effective_ells()
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
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: models[preset][freq].update({"alms": alms}) import pickle pickle.dump(models, open("./models_{}.pkl".format(nside), "wb"))
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") plt.xlabel(r"$\ell$", fontsize=20)
window = so_window.create_apodization(binary, apo_type="C1", apo_radius_degree=apo_radius_degree_survey) cmb=template.synfast(clfile) # Compute spin 0 spin 2 spectra a la pspy t0=time.time() mbb_inv_pure, Bbl_pure = so_mcm.mcm_and_bbl_spin0and2((window,window), binning_file, lmax=lmax, niter=niter, type=type, pure=True) alm_pure = sph_tools.get_pure_alms(cmb, (window,window), niter, lmax) l, ps_pure = so_spectra.get_spectra(alm_pure, alm_pure, spectra=spectra) lb, ps_dict_pure = so_spectra.bin_spectra(l, ps_pure, binning_file, lmax, type=type, mbb_inv=mbb_inv_pure, spectra=spectra) print("pspy run in %.2f s"%(time.time()-t0)) # Compute pure spin 2 spectra a la namaster t0=time.time() def compute_master(f_a,f_b,wsp) :
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
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, type="Dl", mbb_inv=mbb_inv, spectra=spectra) so_spectra.write_ps("%s/spectra_%s.dat" % (spectra_dir, spec_name), lb, Db_dict[spec_name], type="Dl",
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, mbb_inv=mbb_inv, spectra=spectra) lb, Db_dict_filt = so_spectra.bin_spectra(l, ps_filt, binning_file, lmax,