# 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 field_0 = nmt.NmtField(window[0].data, [split.data[0]], n_iter=niter, wcs=window[0].data.wcs)
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 c1 == c2: print('auto %dx%d' % (c1, c2)) for s1 in spectra: Db_dict_auto[s1] += [Db_dict[spec_name][s1]] else:
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"))
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 + "/%s.dat" % spec_name, lb, ps, type, spectra=spectra) for count, spec in enumerate(spectra): if (s1 == s2) & (exp1 == exp2): if count == 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
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) plt.legend() plt.savefig("%s/compare.png" % (test_dir)) plt.clf() plt.close()
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) : cl_coupled = nmt.compute_coupled_cell(f_a, f_b) cl_decoupled = wsp.decouple_cell(cl_coupled) return cl_decoupled fyp = nmt.NmtField(window.data,[cmb.data[1], cmb.data[2]], purify_e=True, purify_b=True, n_iter_mask_purify=niter, n_iter=niter)
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
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, type=type, mbb_inv=mbb_inv, spectra=spectra) so_spectra.write_ps('%s/spectra_%03d.dat' % (spectraDir, iii), lb, Db_dict, type=type,