def get_sacc_binning(self, ell_eff, lini, lend, windows=None): """ Generate a SACC binning object. :param ell_eff: list of effective multipoles. :param lini,lend: bandpower edges. :param windows: optional list of bandpower window functions. """ typ, ell, dell, t1, q1, t2, q2 = [], [], [], [], [], [], [] for t1i in range(self.nbins): for t2i in range(t1i, self.nbins): for i_l, l in enumerate(ell_eff): typ.append('F') ell.append(l) dell.append(lend[i_l] - lini[i_l]) t1.append(t1i) q1.append('P') t2.append(t2i) q2.append('P') if windows is None: return sacc.Binning(typ, ell, t1, q1, t2, q2, deltaLS=dell) else: return sacc.Binning(typ, ell, t1, q1, t2, q2, deltaLS=dell, windows=windows)
def getBinning(tracers): larr = np.arange(lmin, lmax, dl) binning = [] #Array of ell values typ, ell, t1, q1, t2, q2 = [], [], [], [], [], [] ntr = len(tracers) for i1 in np.arange(ntr): for i2 in np.arange(ntr - i1) + i1: for l in larr: typ.append('F') ell.append(l) t1.append(i1) t2.append(i2) q1.append('P') q2.append('P') return sacc.Binning(typ, ell, t1, q1, t2, q2)
def get_sacc_binning(self, with_windows=False): typ, ell, t1, q1, t2, q2 = [], [], [], [], [], [] l_eff = self.bins.get_effective_ells() windows = None if with_windows: windows_wsp = {} for b1 in range(self.n_bpss): for b2 in range(b1, self.n_bpss): name = self.get_workspace_label(b1, b2) windows_wsp[name] = {} wsp = self.workspaces[name] bpw_win = wsp.get_bandpower_windows() windows_wsp[name]['EE'] = bpw_win[0, :, 0, :] windows_wsp[name]['EB'] = bpw_win[1, :, 1, :] windows_wsp[name]['BE'] = bpw_win[2, :, 2, :] windows_wsp[name]['BB'] = bpw_win[3, :, 3, :] windows = [] for b1, b2, s1, s2, l1, l2 in self.get_cell_iterator(): if (b1 == b2) and (s1 == s2): types = ['EE', 'EB', 'BB'] # Do not double-count EB else: types = ['EE', 'EB', 'BE', 'BB'] name_wsp = self.get_workspace_label(b1, b2) for ty in types: for il, l in enumerate(l_eff): ell.append(l) typ.append(ty) t1.append(b1 * self.nsplits + s1) t2.append(b2 * self.nsplits + s2) q1.append('C') q2.append('C') if with_windows: windows.append( sacc.Window(self.larr_all, windows_wsp[name_wsp][ty][il])) return sacc.Binning(typ, ell, t1, q1, t2, q2, windows=windows)
typ = q1 + q2 for b in range(nells): w = windows[b, ic] lmean = np.sum(ls * w) / np.sum(w) win = sacc.Window(ls, w) ls_arr.append(lmean) w_arr.append(win) q1_arr += nells * [q1] q2_arr += nells * [q2] t1_arr += nells * [t1] t2_arr += nells * [t2] typ_arr += nells * [typ] bins = sacc.Binning(typ_arr, ls_arr, t1_arr, q1_arr, t2_arr, q2_arr, windows=w_arr) #SACC file s = sacc.SACC(tracers, bins, mean=meanvec, precision=precis, meta={'data_name': 'BK15_bmode_analysis'}) #Save SACC file s.saveToHDF("BK15.sacc") s.printInfo()
wins.append( sacc.Window( np.arange(l - 50, l + 50), np.exp(-(1.0 * np.arange(-50, 50))**2 / (2 * 20.**2)))) ## We refer to tracers by their index t1.append(t1i) t2.append(t2i) ## Here we have density cross-correlations so "P" as point ## except for CMB where q1.append('S') q2.append('S') ## values and errors val.append(np.random.uniform(0, 10)) err.append(np.random.uniform(1, 2)) binning = sacc.Binning(type, ell, t1, q1, t2, q2, windows=wins) mean = sacc.MeanVec(val) ## We need to add covariance matrix. We will use ell_block_diagonal ## where everything is coupled across tracers/redshifts at the same ell but not ## across ell with fixed 10% off-diagonal elements Np = binning.size() cov = np.zeros((Np, Np)) for i in range(Np): for j in range(i, Np): if ell[i] == ell[j]: cov[i, j] = err[i] * err[j] if (i != j): cov[i, j] /= 10 cov[j, i] = cov[i, j] precision = sacc.Precision(cov,
def process_catalog(o): #Read z-binning print "Bins" z0_bins, zf_bins, lmax_bins = np.loadtxt(o.fname_bins_z, unpack=True) nbins = len(z0_bins) cat = fc.Catalog(read_from=o.fname_in) #Get weights, compute binary mask based on weights, and apodize it if needed print "Window" mask = Mask(cat, o.nside, o.theta_apo) nside = mask.nside #Get contaminant templates #TODO: check resolution if o.templates_fname != "none": templates = [[t] for t in hp.read_map(o.templates_fname, field=None)] ntemp = len(templates) else: templates = None ntemp = 0 #Generate bandpowers binning scheme (we're assuming all maps will use the same bandpowers!) print "Bandpowers" bpw = nmt.NmtBin(nside, nlb=o.delta_ell) ell_eff = bpw.get_effective_ells() tracers = [] #Generate tracers #TODO: pass extra sampling parameters zs, nzs, mps = bin_catalog(cat, z0_bins, zf_bins, mask) if mrank != 0: return for zar, nzar, mp, lmax in zip(zs, nzs, mps, lmax_bins): zav = np.average(zar, weights=nzar) print "-- z-bin: %3.2f " % zav tracers.append(Tracer(mp, zar, nzar, lmax, mask, templates=templates)) if o.save_map: hp.write_map("map_%3.2f.fits" % zav, mp) cat.rewind() print "Compute power spectra" #Compute coupling matrix #TODO: (only done once, assuming all maps have the same mask!) print " Computing coupling matrix" w = nmt.NmtWorkspace() if not (os.path.isfile(o.nmt_workspace)): w.compute_coupling_matrix(tracers[0].field, tracers[0].field, bpw) if o.nmt_workspace != "none": w.write_to(o.nmt_workspace) else: w.read_from(o.nmt_workspace) #Compute all cross-correlations def compute_master(fa, fb, wsp, clb): cl_coupled = nmt.compute_coupled_cell(fa, fb) cl_decoupled = wsp.decouple_cell(cl_coupled, cl_bias=clb) return cl_decoupled #If attempting to deproject contaminant templates, we need an estimate of the true power spectra. #This can be done interatively from a first guess using cl_bias=0, but I haven't coded that up yet. #For the moment we will use cl_guess=0. cl_guess = np.zeros(3 * nside) t1 = time() print " Computing power spectrum" cls_all = {} for b1 in np.arange(nbins): f1 = tracers[b1].field for b2 in np.arange(b1, nbins): f2 = tracers[b2].field if ntemp > 0: cl_bias = nmt.deprojection_bias(f1, f2, w, cl_theory) else: cl_bias = None cls_all[(b1, b2)] = compute_master(f1, f2, w, clb=cl_bias)[0] print 'Computed bin: ', b1, b2, ' in ', time() - t1, ' s' if debug: plt.figure() plt.plot(ell_eff, cls_all[(b1, b1)]) plt.xscale('log') plt.yscale('log') plt.xlabel(r'$l$') plt.ylabel(r'$C_{l}$') plt.show() print "Translating into SACC" #Transform everything into SACC format #1- Generate SACC tracers stracers = [ sacc.Tracer("tr_b%d" % i, "point", t.zarr, t.nzarr, exp_sample="gals") for i, t in enumerate(tracers) ] #2- Define SACC binning typ, ell, t1, q1, t2, q2 = [], [], [], [], [], [] for i1 in np.arange(nbins): for i2 in np.arange(i1, nbins): lmax = min(tracers[i1].lmax, tracers[i2].lmax) for l in ell_eff[ell_eff < lmax]: typ.append('F') ell.append(l) t1.append(i1) t2.append(i2) q1.append('P') q2.append('P') sbin = sacc.Binning(typ, ell, t1, q1, t2, q2) ssbin = sacc.SACC(stracers, sbin) #3- Arrange power spectra into SACC mean vector vec = np.zeros((ssbin.size(), )) for t1i, t2i, ells, ndx in ssbin.sortTracers(): lmax = min(tracers[t1i].lmax, tracers[t2i].lmax) vec[ndx] = cls_all[(t1i, t2i)][np.where(ell_eff < lmax)[0]] svec = sacc.MeanVec(vec) #4- Create SACC file and write to file csacc = sacc.SACC(stracers, sbin, svec) csacc.saveToHDF(o.fname_out)
T = sacc.Tracer('bin_%d' % i_t, 'point', z, nz, exp_sample=o.hsc_field) T.addColumns({'ndens': t.ndens_perad * np.ones_like(nz)}) sacc_tracers.append(T) #Binning and mean type, ell, dell, t1, q1, t2, q2 = [], [], [], [], [], [], [] for t1i in range(nbins): for t2i in range(t1i, nbins): for i_l, l in enumerate(ell_eff): type.append('F') #Fourier-space ell.append(l) dell.append(lend[i_l] - lini[i_l]) t1.append(t1i) q1.append('P') t2.append(t2i) q2.append('P') sacc_binning = sacc.Binning(type, ell, t1, q1, t2, q2, deltaLS=dell) sacc_mean = sacc.MeanVec(cls_all.flatten()) if covar is None: sacc_precision = None else: sacc_precision = sacc.Precision(covar, "dense", is_covariance=True, binning=sacc_binning) sacc_meta = {'Field': o.hsc_field, 'Area_rad': area_patch} s = sacc.SACC(sacc_tracers, sacc_binning, sacc_mean, precision=sacc_precision, meta=sacc_meta) s.printInfo()
T=sacc.Tracer('bin_%d'%i_t,'point',z,nz,exp_sample=o.hsc_field) T.addColumns({'ndens':t.ndens_perad*np.ones_like(nz)}) sacc_tracers.append(T) #Binning and mean type,ell,dell,t1,q1,t2,q2=[],[],[],[],[],[],[] for t1i in range(nbins) : for t2i in range(t1i,nbins) : for i_l,l in enumerate(ell_eff) : type.append('F') #Fourier-space ell.append(l) dell.append(lend[i_l]-lini[i_l]) t1.append(t1i) q1.append('P') t2.append(t2i) q2.append('P') sacc_binning=sacc.Binning(type,ell,t1,q1,t2,q2,deltaLS=dell,windows=windows_sacc) sacc_mean=sacc.MeanVec(cls_all.flatten()) if covar is None : sacc_precision=None else : sacc_precision=sacc.Precision(covar,"dense",is_covariance=True, binning=sacc_binning) sacc_meta={'Field':o.hsc_field,'Area_rad':area_patch} s=sacc.SACC(sacc_tracers,sacc_binning,sacc_mean,precision=sacc_precision,meta=sacc_meta) s.printInfo() s.saveToHDF(o.prefix_out+'.sacc') #Save noise sacc_binning_noise=sacc.Binning(type,ell,t1,q1,t2,q2,deltaLS=dell) sacc_mean_noise=sacc.MeanVec(nls_all.flatten()) s=sacc.SACC(sacc_tracers,sacc_binning_noise,sacc_mean_noise,precision=None,meta=sacc_meta) s.printInfo() s.saveToHDF(o.prefix_out+'_noise.sacc')
pol_names = ['E', 'B'] for i1 in range(2 * nfreqs): b1 = i1 // 2 p1 = i1 % 2 for i2 in range(i1, 2 * nfreqs): b2 = i2 // 2 p2 = i2 % 2 ty = pol_names[p1] + pol_names[p2] for il, ll in enumerate(ells_bpw): ell.append(ll) typ.append(ty) t1.append(b1) t2.append(b2) q1.append('C') q2.append('C') bins = sacc.Binning(typ, ell, t1, q1, t2, q2) bpw_model = bpw_model.reshape([2 * nfreqs, 2 * nfreqs, n_bpw])[np.triu_indices(2 * nfreqs)].flatten() mean_model = sacc.MeanVec(bpw_model) sacc_model = sacc.SACC(tracers, bins, mean=mean_model) os.system('mkdir -p ' + prefix_out) sacc_model.saveToHDF(prefix_out + "/cells_model.sacc") nhits_binary = np.zeros_like(nhits) inv_sqrtnhits = np.zeros_like(nhits) inv_sqrtnhits[nhits > 1E-3] = 1. / np.sqrt(nhits[nhits > 1E-3]) nhits_binary[nhits > 1E-3] = 1 #Add noise ylf = 1 nell = np.zeros([nfreqs, lmax + 1]) _, nell[:, 2:], _ = Simons_Observatory_V3_SA_noise(sens,
def get_binnings(self, with_windows=True): # Get windows if needed win_coadd = None win_nulls = None if with_windows: win_coadd = [] win_nulls = [] ls_win = self.s_splits.binning.windows[0].ls.copy() nls = len(ls_win) windows = np.zeros( [self.nbands, 2, self.nbands, 2, self.n_bpws, nls]) for t1, t2, typ, ells, ndx in self.sorting: b1, s1 = self.tracer_number_to_band_split(t1) b2, s2 = self.tracer_number_to_band_split(t2) typ = typ.decode() p1 = self.index_pol[typ[0]] p2 = self.index_pol[typ[1]] if (s1 == 0) and (s2 == 0): for b, i in enumerate(ndx): w = self.s_splits.binning.windows[i].w windows[b1, p1, b2, p2, b, :] = w if not ((b1 == b2) and (p1 == p2)): windows[b2, p2, b1, p1, b, :] = w # Binnings for coadds typ, ell, t1, q1, t2, q2 = [], [], [], [], [], [] for i1 in range(2 * self.nbands): b1 = i1 // 2 p1 = i1 % 2 for i2 in range(i1, 2 * self.nbands): b2 = i2 // 2 p2 = i2 % 2 ty = self.pol_names[p1] + self.pol_names[p2] for il, ll in enumerate(self.ells): ell.append(ll) typ.append(ty) t1.append(b1) t2.append(b2) q1.append('C') q2.append('C') if with_windows: win_coadd.append( sacc.Window(ls_win, windows[b1, p1, b2, p2, il])) self.bins_coadd = sacc.Binning(typ, ell, t1, q1, t2, q2, windows=win_coadd) # Binnings for nulls typ, ell, t1, q1, t2, q2 = [], [], [], [], [], [] for i_null, (i, j, k, l) in enumerate(self.pairings): for b1 in range(self.nbands): tr1 = self.ind_nulls['band%d_null%dm%d' % (b1 + 1, i + 1, j + 1)] for p1 in range(2): for b2 in range(self.nbands): tr2 = self.ind_nulls['band%d_null%dm%d' % (b2 + 1, k + 1, l + 1)] for p2 in range(2): ty = self.pol_names[p1] + self.pol_names[p2] for il, ll in enumerate(self.ells): ell.append(ll) typ.append(ty) t1.append(tr1) t2.append(tr2) q1.append('C') q2.append('C') if with_windows: win_nulls.append( sacc.Window( ls_win, windows[b1, p1, b2, p2, il])) self.bins_nulls = sacc.Binning(typ, ell, t1, q1, t2, q2, windows=win_nulls)