def test_workspace_flat_deprojection_bias(self) : #Test derojection_bias_flat c=nmt.deprojection_bias_flat(self.f0,self.f0,self.b,self.l,self.n_good) self.assertEqual(c.shape,(1,self.b.bin.n_bands)) with self.assertRaises(ValueError) : #Wrong cl_guess c=nmt.deprojection_bias_flat(self.f0,self.f0,self.b,self.l,self.n_bad) with self.assertRaises(ValueError) : #Wrong cl_guess c=nmt.deprojection_bias_flat(self.f0,self.f0,self.b,self.l,self.nb_good) with self.assertRaises(RuntimeError) : #Incompatible resolutions c=nmt.deprojection_bias_flat(self.f0,self.f0_half,self.b,self.l,self.n_good)
def test_workspace_flat_deprojection_bias(): # Test derojection_bias_flat c = nmt.deprojection_bias_flat(WT.f0, WT.f0, WT.b, WT.ll, WT.n_good) assert c.shape == (1, WT.b.bin.n_bands) with pytest.raises(ValueError): # Wrong cl_guess nmt.deprojection_bias_flat(WT.f0, WT.f0, WT.b, WT.ll, WT.n_bad) with pytest.raises(ValueError): # Wrong cl_guess nmt.deprojection_bias_flat(WT.f0, WT.f0, WT.b, WT.ll, WT.nb_good) with pytest.raises(RuntimeError): # Incompatible resolutions nmt.deprojection_bias_flat(WT.f0, WT.f0_half, WT.b, WT.ll, WT.n_good)
def mastest(self,wtemp,wpure) : prefix="test/benchmarks/bm_f" if wtemp : prefix+="_yc" f0=nmt.NmtFieldFlat(self.lx,self.ly,self.msk,[self.mps[0]],templates=[[self.tmp[0]]]) f2=nmt.NmtFieldFlat(self.lx,self.ly,self.msk,[self.mps[1],self.mps[2]], templates=[[self.tmp[1],self.tmp[2]]], purify_b=wpure) else : prefix+="_nc" f0=nmt.NmtFieldFlat(self.lx,self.ly,self.msk,[self.mps[0]]) f2=nmt.NmtFieldFlat(self.lx,self.ly,self.msk,[self.mps[1],self.mps[2]],purify_b=wpure) f=[f0,f2] if wpure : prefix+="_yp" else : prefix+="_np" for ip1 in range(2) : for ip2 in range(ip1,2) : if ip1==ip2==0 : clth=np.array([self.cltt]); nlth=np.array([self.nltt]); elif ip1==ip2==1 : clth=np.array([self.clee,0*self.clee,0*self.clbb,self.clbb]); nlth=np.array([self.nlee,0*self.nlee,0*self.nlbb,self.nlbb]); else : clth=np.array([self.clte,0*self.clte]); nlth=np.array([self.nlte,0*self.nlte]); w=nmt.NmtWorkspaceFlat() w.compute_coupling_matrix(f[ip1],f[ip2],self.b) clb=w.couple_cell(self.l,nlth) if wtemp : dlb=nmt.deprojection_bias_flat(f[ip1],f[ip2],self.b,self.l,clth+nlth) tlb=np.loadtxt(prefix+'_cb%d%d.txt'%(2*ip1,2*ip2),unpack=True)[1:,:] self.assertTrue((np.fabs(dlb-tlb)<=np.fmin(np.fabs(dlb),np.fabs(tlb))*1E-5).all()) clb+=dlb cl=w.decouple_cell(nmt.compute_coupled_cell_flat(f[ip1],f[ip2],self.b),cl_bias=clb) tl=np.loadtxt(prefix+'_c%d%d.txt'%(2*ip1,2*ip2),unpack=True)[1:,:] self.assertTrue((np.fabs(cl-tl)<=np.fmin(np.fabs(cl),np.fabs(tl))*1E-5).all())
def get_dpj_bias(self, trc, lth, clth, cl_coupled, wsp, bpws): """ Estimate the deprojection bias :param trc: list of Tracers. :param lth: list of multipoles. :param clth: list of guess power spectra sampled at the multipoles stored in `lth`. :param cl_coupled: mode-coupled measurements of the power spectrum (before subtracting the deprojection bias). :param wsp: NaMaster workspace. :param bpws: NaMaster bandpowers. """ #Compute deprojection bias if os.path.isfile(self.get_output_fname('dpj_bias', ext='sacc')): print("Reading deprojection bias") s = sacc.SACC.loadFromHDF( self.get_output_fname('dpj_bias', ext='sacc')) cls_deproj_all = s.mean.vector.reshape([self.ncross, self.nell]) else: print("Computing deprojection bias") cls_deproj_all = [] i_x = 0 for i in range(self.nbins): for j in range(i, self.nbins): print(i, j) cl_deproj_bias = nmt.deprojection_bias_flat( trc[i].field, trc[j].field, bpws, lth, [clth[i_x]])[0] cls_deproj_all.append(cl_deproj_bias) i_x += 1 cls_deproj_all = np.array(cls_deproj_all) #Remove deprojection bias cls_all = [] i_x = 0 for i in range(self.nbins): for j in range(i, self.nbins): cls_all.append( wsp.decouple_cell([cl_coupled[i_x]], cl_bias=[cls_deproj_all[i_x]])[0]) i_x += 1 return np.array(cls_all), cls_deproj_all
def test_lite_cont(self): f0 = nmt.NmtFieldFlat(self.lx, self.ly, self.msk, [self.mps[0]], templates=[[self.tmp[0]]]) tmps = [[self.tmp[1], self.tmp[2]]] f2 = nmt.NmtFieldFlat(self.lx, self.ly, self.msk, [self.mps[1], self.mps[2]], templates=tmps) f2l = nmt.NmtFieldFlat(self.lx, self.ly, self.msk, [self.mps[1], self.mps[2]], templates=tmps, lite=True) f2e = nmt.NmtFieldFlat(self.lx, self.ly, self.msk, None, lite=True, spin=2) clth = np.array([self.clte, 0*self.clte]) nlth = np.array([self.nlte, 0*self.nlte]) w = nmt.NmtWorkspaceFlat() w.compute_coupling_matrix(f0, f2e, self.b) clb = w.couple_cell(self.ll, nlth) dlb = nmt.deprojection_bias_flat(f0, f2, self.b, self.ll, clth+nlth) clb += dlb cl = w.decouple_cell(nmt.compute_coupled_cell_flat(f0, f2l, self.b), cl_bias=clb) tl = np.loadtxt("test/benchmarks/bm_f_yc_np_c02.txt", unpack=True)[1:, :] tlb = np.loadtxt("test/benchmarks/bm_f_yc_np_cb02.txt", unpack=True)[1:, :] self.assertTrue((np.fabs(dlb-tlb) <= np.fmin(np.fabs(dlb), np.fabs(tlb))*1E-5).all()) self.assertTrue((np.fabs(cl-tl) <= np.fmin(np.fabs(cl), np.fabs(tl))*1E-5).all())
])) else: cl00_th = np.zeros([1, b.get_n_bands()]) cl02_th = np.zeros([2, b.get_n_bands()]) cl22_th = np.zeros([4, b.get_n_bands()]) dum, cl00_th[0], cl02_th[0], cl02_th[1], cl22_th[0], cl22_th[1], cl22_th[ 2], cl22_th[3] = np.loadtxt(prefix + "_cl_th.txt", unpack=True) #Compute noise and deprojection bias if not os.path.isfile(prefix + "_clb00.npy"): print("Computing deprojection and noise bias 00") #Compute noise bias clb00 = w00.couple_cell(l, np.array([nltt])) #Compute deprojection bias if w_cont and (not o.no_deproject) and (not o.no_debias): clb00 += nmt.deprojection_bias_flat(f0, f0, b, l, [cltt]) np.save(prefix + "_clb00", clb00) else: clb00 = np.load(prefix + "_clb00.npy") if not os.path.isfile(prefix + "_clb02.npy"): print("Computing deprojection and noise bias 02") clb02 = w02.couple_cell(l, np.array([nlte, 0 * nlte])) if w_cont and (not o.no_deproject) and (not o.no_debias): clb02 += nmt.deprojection_bias_flat(f0, f2, b, l, [clte, 0 * clte]) np.save(prefix + "_clb02", clb02) else: clb02 = np.load(prefix + "_clb02.npy") if not os.path.isfile(prefix + "_clb22.npy"): print("Computing deprojection and noise bias 22") clb22 = w22.couple_cell(l, np.array([nlee, 0 * nlee, 0 * nlbb, nlbb])) if w_cont and (not o.no_deproject) and (not o.no_debias):
cl22_th=w22.decouple_cell(w22.couple_cell(l,np.array([clee,0*clee,0*clbb,clbb]))) np.savetxt(prefix+"_cl_th.txt", np.transpose([b.get_effective_ells(),cl22_th[0],cl22_th[1],cl22_th[2],cl22_th[3]])) else : cl22_th=np.zeros([4,b.get_n_bands()]) dum,cl22_th[0],cl22_th[1],cl22_th[2],cl22_th[3]=np.loadtxt(prefix+"_cl_th.txt",unpack=True) #Compute noise and deprojection bias if not os.path.isfile(prefix+"_clb22.npy") : print("Computing deprojection and noise bias 22") #Compute noise bias clb22=w22.couple_cell(l,np.array([nlee/beam**2,0*nlee,0*nlbb,nlbb/beam**2])) #Compute deprojection bias print("Deproj") if w_cont : clb22+=nmt.deprojection_bias_flat(f2,f2,b,l,[clee*beam**2+nlee,0*clee,0*clbb,clbb*beam**2+nlbb]) np.save(prefix+"_clb22",clb22) else : clb22=np.load(prefix+"_clb22.npy") #Compute mean and variance over nsims simulations cl22_all=[] for i in np.arange(nsims) : #if i%100==0 : print("%d-th sim"%(i+o.isim_ini)) if not os.path.isfile(prefix+"_cl_%04d.txt"%(o.isim_ini+i)) : np.random.seed(1000+o.isim_ini+i) f2=get_fields() cl22=w22.decouple_cell(nmt.compute_coupled_cell_flat(f2,f2,b),cl_bias=clb22) np.savetxt(prefix+"_cl_%04d.txt"%(o.isim_ini+i),
w22 = nmt.NmtWorkspaceFlat() w22.compute_coupling_matrix(f2, f2, b) clb22 = w22.couple_cell(l, np.array([nlee, 0 * nlee, 0 * nlbb, nlbb])) c22 = w22.decouple_cell(nmt.compute_coupled_cell_flat(f2, f2, b), cl_bias=clb22) w22.write_to(prefix + '_w22.dat') np.savetxt(prefix + '_c22.txt', np.transpose([leff, c22[0], c22[1], c22[2], c22[3]])) #With contaminants prefix = 'bm_f_yc_np' f0 = nmt.NmtFieldFlat(lx, ly, msk, [dt], [[st]]) f2 = nmt.NmtFieldFlat(lx, ly, msk, [dq, du], [[sq, su]]) w00 = nmt.NmtWorkspaceFlat() w00.compute_coupling_matrix(f0, f0, b) clb00 = nmt.deprojection_bias_flat(f0, f0, b, l, [cltt + nltt]) np.savetxt(prefix + '_cb00.txt', np.transpose([leff, clb00[0]])) clb00 += w00.couple_cell(l, np.array([nltt])) c00 = w00.decouple_cell(nmt.compute_coupled_cell_flat(f0, f0, b), cl_bias=clb00) w00.write_to(prefix + '_w00.dat') np.savetxt(prefix + '_c00.txt', np.transpose([leff, c00[0]])) w02 = nmt.NmtWorkspaceFlat() w02.compute_coupling_matrix(f0, f2, b) clb02 = nmt.deprojection_bias_flat(f0, f2, b, l, [clte + nlte, 0 * clte]) np.savetxt(prefix + '_cb02.txt', np.transpose([leff, clb02[0], clb02[1]])) clb02 += w02.couple_cell(l, np.array([nlte, 0 * nlte])) c02 = w02.decouple_cell(nmt.compute_coupled_cell_flat(f0, f2, b), cl_bias=clb02) w02.write_to(prefix + '_w02.dat') np.savetxt(prefix + '_c02.txt', np.transpose([leff, c02[0], c02[1]]))
def mastest(wtemp, wpure, do_teb=False): prefix = "test/benchmarks/bm_f" if wtemp: prefix += "_yc" f0 = nmt.NmtFieldFlat(WT.lx, WT.ly, WT.msk, [WT.mps[0]], templates=[[WT.tmp[0]]]) f2 = nmt.NmtFieldFlat(WT.lx, WT.ly, WT.msk, [WT.mps[1], WT.mps[2]], templates=[[WT.tmp[1], WT.tmp[2]]], purify_b=wpure) else: prefix += "_nc" f0 = nmt.NmtFieldFlat(WT.lx, WT.ly, WT.msk, [WT.mps[0]]) f2 = nmt.NmtFieldFlat(WT.lx, WT.ly, WT.msk, [WT.mps[1], WT.mps[2]], purify_b=wpure) f = [f0, f2] if wpure: prefix += "_yp" else: prefix += "_np" for ip1 in range(2): for ip2 in range(ip1, 2): if ip1 == ip2 == 0: clth = np.array([WT.cltt]) nlth = np.array([WT.nltt]) elif ip1 == ip2 == 1: clth = np.array([WT.clee, 0*WT.clee, 0*WT.clbb, WT.clbb]) nlth = np.array([WT.nlee, 0*WT.nlee, 0*WT.nlbb, WT.nlbb]) else: clth = np.array([WT.clte, 0*WT.clte]) nlth = np.array([WT.nlte, 0*WT.nlte]) w = nmt.NmtWorkspaceFlat() w.compute_coupling_matrix(f[ip1], f[ip2], WT.b) clb = w.couple_cell(WT.ll, nlth) if wtemp: dlb = nmt.deprojection_bias_flat(f[ip1], f[ip2], WT.b, WT.ll, clth+nlth) tlb = np.loadtxt(prefix+'_cb%d%d.txt' % (2*ip1, 2*ip2), unpack=True)[1:, :] assert (np.fabs(dlb-tlb) <= np.fmin(np.fabs(dlb), np.fabs(tlb))*1E-5).all() clb += dlb cl = w.decouple_cell(nmt.compute_coupled_cell_flat(f[ip1], f[ip2], WT.b), cl_bias=clb) tl = np.loadtxt(prefix+'_c%d%d.txt' % (2*ip1, 2*ip2), unpack=True)[1:, :] assert (np.fabs(cl-tl) <= np.fmin(np.fabs(cl), np.fabs(tl))*1E-5).all() # TEB if do_teb: clth = np.array([WT.cltt, WT.clte, 0*WT.clte, WT.clee, 0*WT.clee, 0*WT.clbb, WT.clbb]) nlth = np.array([WT.nltt, WT.nlte, 0*WT.nlte, WT.nlee, 0*WT.nlee, 0*WT.nlbb, WT.nlbb]) w = nmt.NmtWorkspaceFlat() w.compute_coupling_matrix(f[0], f[1], WT.b, is_teb=True) c00 = nmt.compute_coupled_cell_flat(f[0], f[0], WT.b) c02 = nmt.compute_coupled_cell_flat(f[0], f[1], WT.b) c22 = nmt.compute_coupled_cell_flat(f[1], f[1], WT.b) cl = np.array([c00[0], c02[0], c02[1], c22[0], c22[1], c22[2], c22[3]]) t00 = np.loadtxt(prefix+'_c00.txt', unpack=True)[1:, :] t02 = np.loadtxt(prefix+'_c02.txt', unpack=True)[1:, :] t22 = np.loadtxt(prefix+'_c22.txt', unpack=True)[1:, :] tl = np.array([t00[0], t02[0], t02[1], t22[0], t22[1], t22[2], t22[3]]) cl = w.decouple_cell(cl, cl_bias=w.couple_cell(WT.ll, nlth)) assert (np.fabs(cl-tl) <= np.fmin(np.fabs(cl), np.fabs(tl))*1E-5).all()
#Read theory power spectra data = np.loadtxt(o.cl_theory, unpack=True) lth = data[0] clth = data[1:] else: raise ValueError( "Must provide a valid method to compute the deprojection bias\n") cls_all = [] i_x = 0 for i in range(nbins): for j in range(i, nbins): cl_coupled = nmt.compute_coupled_cell_flat(tracers[i].field, tracers[j].field, bpws) if o.cont_deproj_bias: cl_deproj_bias = nmt.deprojection_bias_flat( tracers[i].field, tracers[j].field, bpws, lth, [clth[i_x]]) else: cl_deproj_bias = None cls_all.append( wsp.decouple_cell(cl_coupled, cl_bias=cl_deproj_bias)[0]) cls_all = np.array(cls_all) n_cross = len(cls_all) n_ell = len(ell_eff) #Compute covariance matrix if o.covar_opt == 'NONE': covar = None elif ((o.covar_opt == 'data') or (o.covar_opt == 'theory')): print("Computing Gaussian covariance") if o.covar_opt == 'data': lmax = int(