def test_toeplitz_02(self): l_toeplitz = self.nside l_exact = self.nside // 2 dl_band = self.nside // 4 we = nmt.NmtWorkspace() we.compute_coupling_matrix(self.f0, self.f2, self.b) ce = we.get_coupling_matrix().reshape([3*self.nside, 2, 3*self.nside, 2]) ce = ce[:, 0, :, 0] wt = nmt.NmtWorkspace() wt.compute_coupling_matrix(self.f0, self.f2, self.b, l_toeplitz=l_toeplitz, l_exact=l_exact, dl_band=dl_band) ct = wt.get_coupling_matrix().reshape([3*self.nside, 2, 3*self.nside, 2]) ct = ct[:, 0, :, 0] # Check that the approximate matrix is constructed # as expected. self.compare_toeplitz(ce, ct, l_toeplitz, l_exact, dl_band) # Check that it's not a bad approximation (0.5% for these ells) cle = we.decouple_cell(nmt.compute_coupled_cell(self.f0, self.f2)) clt = wt.decouple_cell(nmt.compute_coupled_cell(self.f0, self.f2)) self.assertTrue(np.all(np.fabs(clt[0]/cle[0]-1) < 5E-3))
def mastest(self): f0 = nmt.NmtField(self.msk, [self.mps[0]], wcs=self.wcs, n_iter=0) f2 = nmt.NmtField(self.msk, [self.mps[1], self.mps[2]], wcs=self.wcs, n_iter=0) f = [f0, f2] for ip1 in range(2): for ip2 in range(ip1, 2): if ip1 == ip2 == 0: cl_bm = np.array([self.cltt]) elif ip1 == ip2 == 1: cl_bm = np.array( [self.clee, self.cleb, self.cleb, self.clbb]) else: cl_bm = np.array([self.clte, self.cltb]) w = nmt.NmtWorkspace() w.compute_coupling_matrix(f[ip1], f[ip2], self.b) cl = w.decouple_cell(nmt.compute_coupled_cell(f[ip1], f[ip2])) self.assertTrue(np.amax(np.fabs(cl - cl_bm)) <= 1E-10) # TEB w = nmt.NmtWorkspace() w.compute_coupling_matrix(f[0], f[1], self.b, is_teb=True) c00 = nmt.compute_coupled_cell(f[0], f[0]) c02 = nmt.compute_coupled_cell(f[0], f[1]) c22 = nmt.compute_coupled_cell(f[1], f[1]) cl = w.decouple_cell( np.array([c00[0], c02[0], c02[1], c22[0], c22[1], c22[2], c22[3]])) cl_bm = np.array([ self.cltt, self.clte, self.cltb, self.clee, self.cleb, self.cleb, self.clbb ]) self.assertTrue(np.amax(np.fabs(cl - cl_bm)) <= 1E-10)
def load_from_dir(self, mcm_dir): """Read information from a nawrapper mode coupling directory.""" with open(os.path.join(mcm_dir, "mcm.json"), "r") as read_file: data = json.load(read_file) # convert lists into numpy arrays for bk in ["ells", "bpws", "weights"]: data["bin_kwargs"][bk] = np.array(data["bin_kwargs"][bk]) self.bins = nabin(**data["bin_kwargs"]) self.lb = self.bins.get_effective_ells() self.workspace_dict = {} if "w00" in data: self.w00 = nmt.NmtWorkspace() self.w00.read_from(os.path.join(mcm_dir, data["w00"])) self.workspace_dict[(0, 0)] = self.w00 if "w02" in data: self.w02 = nmt.NmtWorkspace() self.w02.read_from(os.path.join(mcm_dir, data["w02"])) self.workspace_dict[(0, 2)] = self.w02 if "w20" in data: self.w20 = nmt.NmtWorkspace() self.w20.read_from(os.path.join(mcm_dir, data["w20"])) self.workspace_dict[(2, 0)] = self.w20 if "w22" in data: self.w22 = nmt.NmtWorkspace() self.w22.read_from(os.path.join(mcm_dir, data["w22"])) self.workspace_dict[(2, 2)] = self.w22
def test_field_masked(self): nside = 64 b = nmt.NmtBin.from_nside_linear(nside, 16) msk = hp.read_map("test/benchmarks/msk.fits", verbose=False) mps = np.array( hp.read_map("test/benchmarks/mps.fits", verbose=False, field=[0, 1, 2])) mps_msk = np.array([m * msk for m in mps]) f0 = nmt.NmtField(msk, [mps[0]]) f0_msk = nmt.NmtField(msk, [mps_msk[0]], masked_on_input=True) f2 = nmt.NmtField(msk, [mps[1], mps[2]]) f2_msk = nmt.NmtField(msk, [mps_msk[1], mps_msk[2]], masked_on_input=True) w00 = nmt.NmtWorkspace() w00.compute_coupling_matrix(f0, f0, b) w02 = nmt.NmtWorkspace() w02.compute_coupling_matrix(f0, f2, b) w22 = nmt.NmtWorkspace() w22.compute_coupling_matrix(f2, f2, b) def mkcl(w, f, g): return w.decouple_cell(nmt.compute_coupled_cell(f, g)) c00 = mkcl(w00, f0, f0).flatten() c02 = mkcl(w02, f0, f2).flatten() c22 = mkcl(w22, f2, f2).flatten() c00_msk = mkcl(w00, f0_msk, f0_msk).flatten() c02_msk = mkcl(w02, f0_msk, f2_msk).flatten() c22_msk = mkcl(w22, f2_msk, f2_msk).flatten() self.assertTrue(np.all(np.fabs(c00 - c00_msk) / np.mean(c00) < 1E-10)) self.assertTrue(np.all(np.fabs(c02 - c02_msk) / np.mean(c02) < 1E-10)) self.assertTrue(np.all(np.fabs(c22 - c22_msk) / np.mean(c22) < 1E-10))
def test_toeplitz_22(): l_toeplitz = WT.nside l_exact = WT.nside // 2 dl_band = WT.nside // 4 we = nmt.NmtWorkspace() we.compute_coupling_matrix(WT.f2, WT.f2, WT.b) ce = we.get_coupling_matrix().reshape([3*WT.nside, 4, 3*WT.nside, 4]) ce_pp = ce[:, 0, :, 0] ce_mm = ce[:, 0, :, 3] wt = nmt.NmtWorkspace() wt.compute_coupling_matrix(WT.f2, WT.f2, WT.b, l_toeplitz=l_toeplitz, l_exact=l_exact, dl_band=dl_band) ct = wt.get_coupling_matrix().reshape([3*WT.nside, 4, 3*WT.nside, 4]) ct_pp = ct[:, 0, :, 0] ct_mm = ct[:, 0, :, 3] # Check that the approximate matrix is constructed # as expected. compare_toeplitz(ce_pp, ct_pp, l_toeplitz, l_exact, dl_band) compare_toeplitz(ce_mm, ct_mm, l_toeplitz, l_exact, dl_band) # Check that it's not a bad approximation (0.5% for these ells) cle = we.decouple_cell(nmt.compute_coupled_cell(WT.f2, WT.f2)) clt = wt.decouple_cell(nmt.compute_coupled_cell(WT.f2, WT.f2)) assert np.all(np.fabs(clt[0]/cle[0]-1) < 5E-3)
def setUp(self): # This is to avoid showing an ugly warning # that has nothing to do with pymaster if (sys.version_info > (3, 1)): warnings.simplefilter("ignore", ResourceWarning) self.nside = 64 self.nlb = 16 self.npix = hp.nside2npix(self.nside) msk = hp.read_map("test/benchmarks/msk.fits", verbose=False) mps = np.array( hp.read_map("test/benchmarks/mps.fits", verbose=False, field=[0, 1, 2])) self.b = nmt.NmtBin.from_nside_linear(self.nside, self.nlb) self.f0 = nmt.NmtField(msk, [mps[0]]) self.f2 = nmt.NmtField(msk, [mps[1], mps[2]]) self.f0_half = nmt.NmtField(msk[:self.npix // 4], [mps[0, :self.npix // 4]]) # Half nside self.w = nmt.NmtWorkspace() self.w.read_from("test/benchmarks/bm_nc_np_w00.fits") self.w02 = nmt.NmtWorkspace() self.w02.read_from("test/benchmarks/bm_nc_np_w02.fits") self.w22 = nmt.NmtWorkspace() self.w22.read_from("test/benchmarks/bm_nc_np_w22.fits") cls = np.loadtxt("test/benchmarks/cls_lss.txt", unpack=True) l, cltt, clee, clbb, clte, nltt, nlee, nlbb, nlte = cls self.ll = l[:3 * self.nside] self.cltt = cltt[:3 * self.nside] + nltt[:3 * self.nside] self.clee = clee[:3 * self.nside] + nlee[:3 * self.nside] self.clbb = clbb[:3 * self.nside] + nlbb[:3 * self.nside] self.clte = clte[:3 * self.nside]
def test_workspace_bandpower_windows(): # This tests the bandpower window functions returned by NaMaster # Compute MCMs w00 = nmt.NmtWorkspace() w00.compute_coupling_matrix(WT.f0, WT.f0, WT.b) w02 = nmt.NmtWorkspace() w02.compute_coupling_matrix(WT.f0, WT.f2, WT.b) w22 = nmt.NmtWorkspace() w22.compute_coupling_matrix(WT.f2, WT.f2, WT.b) # Create some random theory power spectra larr = np.arange(3*WT.nside) cltt = (larr+1.)**-0.8 clee = cltt.copy() clbb = 0.1*clee clte = np.sqrt(cltt)*0.01 cltb = 0.1*clte cleb = 0.01*clbb # For each spin combination, test that decouple-couple is the # same as bandpass-convolutions. def compare_bpw_convolution(cl_th, w): cl_dec_a = w.decouple_cell(w.couple_cell(cl_th)) bpws = w.get_bandpower_windows() cl_dec_b = np.einsum('ijkl, kl', bpws, cl_th) assert (np.amax(np.fabs(cl_dec_a-cl_dec_b)) <= 1E-10) # 00 compare_bpw_convolution(np.array([cltt]), w00) compare_bpw_convolution(np.array([clte, cltb]), w02) compare_bpw_convolution(np.array([clee, cleb, cleb, clbb]), w22)
def test_workspace_full_master(): # Test compute_full_master w = nmt.NmtWorkspace() w.compute_coupling_matrix(WT.f0, WT.f0, WT.b) # OK init w_half = nmt.NmtWorkspace() w_half.compute_coupling_matrix(WT.f0_half, WT.f0_half, WT.b_half) c = nmt.compute_full_master(WT.f0, WT.f0, WT.b) assert c.shape == (1, WT.b.bin.n_bands) with pytest.raises(RuntimeError): # Incompatible bandpowers nmt.compute_full_master(WT.f0, WT.f0, WT.b_doub) with pytest.raises(ValueError): # Incompatible resolutions nmt.compute_full_master(WT.f0, WT.f0_half, WT.b) # Passing correct input workspace w.compute_coupling_matrix(WT.f0, WT.f0, WT.b) # Computing from correct workspace c = nmt.compute_full_master(WT.f0, WT.f0, WT.b, workspace=w) assert c.shape == (1, WT.b.bin.n_bands) with pytest.raises(RuntimeError): # Inconsistent workspace nmt.compute_full_master(WT.f0_half, WT.f0_half, WT.b_half, workspace=w) # Incorrect input spectra with pytest.raises(ValueError): nmt.compute_full_master(WT.f0, WT.f0, WT.b, cl_noise=WT.n_bad) with pytest.raises(ValueError): nmt.compute_full_master(WT.f0, WT.f0, WT.b, cl_guess=WT.n_bad) with pytest.raises(RuntimeError): nmt.compute_full_master(WT.f0, WT.f0, WT.b, cl_noise=WT.n_half) with pytest.raises(RuntimeError): nmt.compute_full_master(WT.f0, WT.f0, WT.b, cl_guess=WT.n_half)
def test_unbinned_mcm_io(): f0 = nmt.NmtField(WT.msk, [WT.mps[0]]) w = nmt.NmtWorkspace() w.compute_coupling_matrix(f0, f0, WT.b) w.write_to("test/wspc.fits") assert w.has_unbinned w1 = nmt.NmtWorkspace() w1.read_from("test/wspc.fits") assert w1.has_unbinned w2 = nmt.NmtWorkspace() w2.read_from("test/wspc.fits", read_unbinned_MCM=False) assert w2.has_unbinned is False with pytest.raises(ValueError): w2.check_unbinned() with pytest.raises(ValueError): w2.write_to("dum") with pytest.raises(ValueError): w2.get_coupling_matrix() with pytest.raises(ValueError): w2.update_coupling_matrix(None) with pytest.raises(ValueError): w2.couple_cell(np.ones([1, 3*WT.nside])) with pytest.raises(ValueError): w2.get_bandpower_windows() os.system("rm test/wspc.fits")
def test_workspace_full_master(self): # Test compute_full_master w = nmt.NmtWorkspace() w.compute_coupling_matrix(self.f0, self.f0, self.b) # OK init w_half = nmt.NmtWorkspace() w_half.compute_coupling_matrix(self.f0_half, self.f0_half, self.b_half) c = nmt.compute_full_master(self.f0, self.f0, self.b) self.assertEqual(c.shape, (1, self.b.bin.n_bands)) with self.assertRaises(RuntimeError): # Incompatible bandpowers nmt.compute_full_master(self.f0, self.f0, self.b_doub) with self.assertRaises(ValueError): # Incompatible resolutions nmt.compute_full_master(self.f0, self.f0_half, self.b) # Passing correct input workspace w.compute_coupling_matrix(self.f0, self.f0, self.b) # Computing from correct workspace c = nmt.compute_full_master(self.f0, self.f0, self.b, workspace=w) self.assertEqual(c.shape, (1, self.b.bin.n_bands)) with self.assertRaises(RuntimeError): # Inconsistent workspace nmt.compute_full_master(self.f0_half, self.f0_half, self.b_half, workspace=w) # Incorrect input spectra with self.assertRaises(ValueError): nmt.compute_full_master(self.f0, self.f0, self.b, cl_noise=self.n_bad) with self.assertRaises(ValueError): nmt.compute_full_master(self.f0, self.f0, self.b, cl_guess=self.n_bad) with self.assertRaises(RuntimeError): nmt.compute_full_master(self.f0, self.f0, self.b, cl_noise=self.n_half) with self.assertRaises(RuntimeError): nmt.compute_full_master(self.f0, self.f0, self.b, cl_guess=self.n_half)
def __init__(self, mask_in, nside, bin_w, lmin, lmax, beams, wsp = True): ''' class for Band-Power-Estimation; Define the **apodized mask**, **beam weights**, **nside**, **bin-scheme**, **ell** ------------------------ beams : a numpy array which include fwhms for every frequency. Deconvolve to ** lmax=2*nside ** ''' self.mask = nmt.mask_apodization(mask_in, 6, apotype='C2') self.nside = nside; self.lmax = lmax; self.Nf = len(beams); self.beams = beams; # self.beam = hp.gauss_beam(beams/60/180*np.pi, lmax = 3*self.nside); # self.b = nmt.NmtBin(self.nside, nlb=bin_w, lmax=self.lmax, is_Dell = True) self.b = self.bands(bin_w = bin_w, lmin = lmin, lmax = lmax); self.ell_n = self.b.get_effective_ells(); self.lbin = len(self.ell_n) # self.w00 = []; # self.w02 = []; self.w22 = []; # - To construct a empty template with a mask to calculate the **coupling matrix** if wsp is True: qu = np.ones((2, 12*self.nside**2)) for i in range(self.Nf): beam_i = hp.gauss_beam(beams[i]/60/180*np.pi, lmax = 3*self.nside - 1); # m0 = nmt.NmtField(self.mask,[qu[0]],purify_e=False, purify_b=True, beam = beam_i); # # construct a workspace that calculate the coupling matrix first. # _w00 = nmt.NmtWorkspace() # _w00.compute_coupling_matrix(m0, m0, self.b) ## spin-0 with spin-0 # self.w00.append(_w00); for j in range(self.Nf): beam_j = hp.gauss_beam(beams[j]/60/180*np.pi, lmax = 3*self.nside - 1); m20 = nmt.NmtField(self.mask, qu, purify_e=False, purify_b=True, beam = beam_i); m21 = nmt.NmtField(self.mask, qu, purify_e=False, purify_b=True, beam = beam_j); # _w02 = nmt.NmtWorkspace() # _w02.compute_coupling_matrix(m0, m21, self.b) ## spin-0 with spin-2 _w22 = nmt.NmtWorkspace() _w22.compute_coupling_matrix(m20, m21, self.b) ## spin-2 with spin-2 # self.w02.append(_w02); self.w22.append(_w22)
def _compute_workspace(self): mask1 = os.path.basename(self.data['tracers'][self.tr1]['mask']) mask2 = os.path.basename(self.data['tracers'][self.tr2]['mask']) # Remove the extension mask1 = os.path.splitext(mask1)[0] mask2 = os.path.splitext(mask2)[0] if self.read_symm: mask1, mask2 = mask2, mask1 fname = os.path.join(self.outdir, 'w__{}__{}.fits'.format(mask1, mask2)) w = nmt.NmtWorkspace() if not os.path.isfile(fname): n_iter = self.data['healpy']['n_iter_mcm'] if 'toeplizt' in self.data: l_toeplitz = self.data['toeplizt']['l_toeplitz'] l_exact = self.data['toeplizt']['l_exact'] dl_band = self.data['toeplizt']['dl_band'] else: l_toeplitz = l_exact = dl_band = -1 f1, f2 = self.get_fields() w.compute_coupling_matrix(f1.f, f2.f, self.b, n_iter=n_iter, l_toeplitz=l_toeplitz, l_exact=l_exact, dl_band=dl_band) w.write_to(fname) else: w.read_from(fname) return w
def get_nmt_workspaces(self, recalculate=False): if recalculate: self.calculate_mode_coupling() for name, value in self.masks['powerspectrum'].items(): wsp = nmt.NmtWorkspace() wsp.read_from(value['nmt_wsp']['path']) yield name, wsp
def calculate_mode_coupling(self): # iterate through masks defined in the power spectrum list for var in self.masks['powerspectrum'].values(): # read apodized mask from hdf5 file with h5py.File(self.hdf5_path, 'r') as f: mask = f[var['record']][...] # create the binning object binning = nmt.NmtBin.from_edges(**var['nmt_bin']['args']) # define some random fields on which to calculate the # mode coupling matrix beam = var['nmt_field']['args'].pop('beam', None) if beam is not None: beam = _gaussian_beam(beam / 60. * np.pi / 180., np.arange(3 * self.nside)) q1, u1, q2, u2 = np.random.randn(4, len(mask)) f1 = nmt.NmtField(mask, [q1, u1], **var['nmt_field']['args'], beam=beam) f2 = nmt.NmtField(mask, [q2, u2], **var['nmt_field']['args'], beam=beam) # get namaster workspace object and compute coupling matrix wsp = nmt.NmtWorkspace() wsp.compute_coupling_matrix(f1, f2, binning) path = Path(var['nmt_wsp']['path']) if path.exists(): path.unlink() # save to file wsp.write_to(str(path))
def get_workspaces(fields): """ :param fields: tuple of tuple of fields to compute the mode-coupling matrix for. Shape is nbis x (f0, f2) """ spins = [0, 2] * len(fields) ws = [] fields = sum(fields, ()) # Flatten the tuple of tuples zbin1 = 1 c1 = 0 for f1, s1 in zip(fields, spins): c2 = c1 zbin2 = int(c2 / 2) + 1 for f2, s2 in zip(fields[c1:], spins[c1:]): print("Computing {}{}_{}{}".format(s1, s2, zbin1, zbin2)) suffix = "_w{}{}_{}{}.dat".format(s1, s2, zbin1, zbin2) w = nmt.NmtWorkspace() get_coupling_matrix(w, f1, f2, suffix) ws.append(w) if not ((c2 + 1) % 2): zbin2 += 1 c2 += 1 if not ((c1 + 1) % 2): zbin1 += 1 c1 += 1 return ws
def get_cl(trs, b1, b2): nl = np.zeros(3*o.nside) if (b1 == b2) and (not o.full_noise): fname_nl = predir + "maps_metacal_bin%d_ns%d_nells.npz" % (b1, o.nside) d = np.load(fname_nl) nl[2:] = d['nl_cov'] sl = ccl.angular_cl(cosmo, trs[b1], trs[b2], ls) if o.old_nka: return np.array([sl + nl, cl0, cl0, nl]) else: w = nmt.NmtWorkspace() predir_mcm = predir + 'cls_metacal_mcm_bins_' fname_mcm = predir_mcm + '%d%d_ns%d.fits' % (b1, b2, o.nside) if os.path.isfile(fname_mcm): w.read_from(fname_mcm) else: fname_mcm = predir_mcm + '%d%d_ns%d.fits' % (b2, b1, o.nside) if os.path.isfile(fname_mcm): w.read_from(fname_mcm) else: raise ValueError("Can't find MCM " + fname_mcm) mskprod = get_field(b1, return_mask=True) if b1 == b2: mskprod *= mskprod else: mskprod *= get_field(b2, return_mask=True) fsky = np.mean(mskprod) return w.couple_cell([sl, cl0, cl0, cl0])/fsky + np.array([nl, cl0, cl0, nl])
def test_lite_cont(self): f0 = nmt.NmtField(self.msk, [self.mps[0]], templates=[[self.tmp[0]]]) f2 = nmt.NmtField(self.msk, [self.mps[1], self.mps[2]], templates=[[self.tmp[1], self.tmp[2]]]) f2l = nmt.NmtField(self.msk, [self.mps[1], self.mps[2]], templates=[[self.tmp[1], self.tmp[2]]]) f2e = nmt.NmtField(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.NmtWorkspace() w.compute_coupling_matrix(f0, f2e, self.b) clb = nlth dlb = nmt.deprojection_bias(f0, f2, clth+nlth) clb += dlb cl = w.decouple_cell(nmt.compute_coupled_cell(f0, f2l), cl_bias=clb) tl = np.loadtxt("test/benchmarks/bm_yc_np_c02.txt", unpack=True)[1:, :] tlb = np.loadtxt("test/benchmarks/bm_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())
def load_wsp(self, fitting_model): """ Method to load an NmtWorkspace object from file instead of recomputing it. """ wsp = nmt.NmtWorkspace() return wsp.read_from( str( self.meta.wsp_mcm_fpath(self.nlb, fitting_model, self.power_conf)))
def setUp(self): self.w = nmt.NmtWorkspace() self.w.read_from("test/benchmarks/bm_nc_np_w00.dat") self.nside = self.w.wsp.cs.n_eq l, cltt, clee, clbb, clte, nltt, nlee, nlbb, nlte = np.loadtxt( "test/benchmarks/cls_lss.txt", unpack=True) self.l = l[:3 * self.nside] self.cltt = cltt[:3 * self.nside] + nltt[:3 * self.nside]
def test_workspace_rebin(): b4 = nmt.NmtBin.from_nside_linear(WT.nside, 4) w = nmt.NmtWorkspace() w.read_from("test/benchmarks/bm_yc_yp_w02.fits") # OK read w.update_bins(b4) assert (w.wsp.bin.n_bands == b4.bin.n_bands) b4 = nmt.NmtBin.from_nside_linear(WT.nside//2, 4) with pytest.raises(RuntimeError): # Wrong lmax w.update_bins(b4)
def des_sh_nls_rot_gal(des_mask_gwl, des_opm_mean, des_data_folder_gwl, output_folder, b, nbins=4, galaxy_treatment='metacal'): des_wl_noise_file = os.path.join( output_folder, "des_sh_{}_rot0-10_noise_ns4096.npz".format(galaxy_treatment)) if os.path.isfile(des_wl_noise_file): N_wl = np.load(des_wl_noise_file)['cls'] return N_wl N_wl = [] for ibin in range(nbins): rotated_cls = [] ws = nmt.NmtWorkspace() fname = os.path.join(output_folder, 'w22_{}{}.dat'.format(1 + ibin, 1 + ibin)) ws.read_from(fname) for irot in range(10): map_file_e1 = os.path.join( des_data_folder_gwl, 'map_{}_bin{}_rot{}_counts_e1_ns4096.fits'.format( galaxy_treatment, ibin, irot)) map_file_e2 = os.path.join( des_data_folder_gwl, 'map_{}_bin{}_rot{}_counts_e2_ns4096.fits'.format( galaxy_treatment, ibin, irot)) map_we1 = hp.read_map(map_file_e1) map_we2 = hp.read_map(map_file_e2) map_e1 = (map_we1 / des_mask_gwl[ibin] - (map_we1.sum() / des_mask_gwl[ibin].sum())) / des_opm_mean[ibin] map_e2 = (map_we2 / des_mask_gwl[ibin] - (map_we2.sum() / des_mask_gwl[ibin].sum())) / des_opm_mean[ibin] map_e1[np.isnan(map_e1)] = 0. map_e2[np.isnan(map_e2)] = 0. sq = map_e1 su = -map_e2 f = nmt.NmtField(des_mask_gwl[ibin], [sq, su]) cls = ws.decouple_cell(nmt.compute_coupled_cell(f, f)).reshape( (2, 2, -1)) rotated_cls.append(cls) N_wl.append(np.mean(rotated_cls, axis=0)) np.savez(des_wl_noise_file, l=b.get_effective_ells(), cls=N_wl) return N_wl
def test_workspace_rebin(self): b4 = nmt.NmtBin.from_nside_linear(self.nside, 4) w = nmt.NmtWorkspace() w.read_from("test/benchmarks/bm_yc_yp_w02.fits") # OK read w.update_bins(b4) self.assertEqual(w.wsp.bin.n_bands, b4.bin.n_bands) b4 = nmt.NmtBin.from_nside_linear(self.nside // 2, 4) with self.assertRaises(RuntimeError): # Wrong lmax w.update_bins(b4)
def test_workspace_io(self) : w=nmt.NmtWorkspace() with self.assertRaises(RuntimeError) : #Invalid writing w.write_to("test/wspc.dat") w.read_from("test/benchmarks/bm_yc_yp_w02.dat") #OK read self.assertEqual(w.wsp.nside,64) with self.assertRaises(RuntimeError) : #Can't write on that file w.write_to("tests/wspc.dat") with self.assertRaises(RuntimeError) : #File doesn't exist w.read_from("none")
def get_mcm(f1, f2, jk_region=None): fname = p.get_fname_mcm(f1, f2, jk_region=jk_region) mcm = nmt.NmtWorkspace() try: mcm.read_from(fname) except: # print(" Computing MCM") mcm.compute_coupling_matrix(f1.field, f2.field, bpw.bn) mcm.write_to(fname) return mcm
def autoWSP_EnB(self, maps, beams=None): # assemble NaMaster fields if beams is None: f2 = nmt.NmtField(self._apomask, [maps[1], maps[2]], purify_e=True, purify_b=True) else: f2 = nmt.NmtField(self._apomask, [maps[1], maps[2]], purify_e=True, purify_b=True, beam=hp.gauss_beam(beams, 3*self._nside-1)) # prepare workspace w = nmt.NmtWorkspace() w.compute_coupling_matrix(f2, f2, self._b) return w
def autoWSP_TT(self, maps, beams=None): # assemble NaMaster fields if beams is None: f0 = nmt.NmtField(self._apomask, [maps[0]]) else: f0 = nmt.NmtField(self._apomask, [maps[0]], beam=hp.gauss_beam(beams, 3*self._nside-1)) # prepare workspace w = nmt.NmtWorkspace() w.compute_coupling_matrix(f0, f0, self._b) return w
def __init__(self, mask_in, nside, bin_w, lmax, beam = None, wsp = True): ''' class for Band-Power-Estimation; Define the **apodized mask**, **beam weights**, **nside**, **bin-scheme**, **ell** Needs to be revised for the beam correction. Different frequency have different sigma, which may lead to different wsp... ''' self.mask = nmt.mask_apodization(mask_in, 6, apotype='C2') self.nside = nside; self.lmax = lmax # self.beam = hp.gauss_beam(beam/60/180*np.pi, lmax = 3*self.nside); self.b = nmt.NmtBin(self.nside, nlb=bin_w, lmax=self.lmax, is_Dell = True) self.ell_n = self.b.get_effective_ells(); self.lbin = len(self.ell_n) # - To construct a empty template with a mask to calculate the **coupling matrix** if wsp is True: qu = np.ones((2, 12*self.nside**2)) m0 = nmt.NmtField(self.mask,[qu[0]],purify_e=False, purify_b=True) m2 = nmt.NmtField(self.mask, qu, purify_e=False, purify_b=True)#, beam=bl) # construct a workspace that calculate the coupling matrix first. _w00 = nmt.NmtWorkspace() _w00.compute_coupling_matrix(m0, m0, self.b) ## spin-0 with spin-0 _w02 = nmt.NmtWorkspace() _w02.compute_coupling_matrix(m0, m2, self.b) ## spin-0 with spin-2 _w22 = nmt.NmtWorkspace() _w22.compute_coupling_matrix(m2, m2, self.b) ## spin-2 with spin-2 self.w00 = _w00 self.w02 = _w02 self.w22 = _w22
def get_workspace_from_spins_masks(spin1, spin2, mask1, mask2): ws = nmt.NmtWorkspace() if mask2 >= mask1: fname = os.path.join( obsdir, 'w{}{}_{}{}.dat'.format(spin1, spin2, mask1, mask2)) else: fname = os.path.join( obsdir, 'w{}{}_{}{}.dat'.format(spin2, spin1, mask2, mask1)) print('Reading', fname) ws.read_from(fname) return ws
def test_workspace_rebeam(): w = nmt.NmtWorkspace() w.read_from("test/benchmarks/bm_yc_yp_w02.fits") # OK read lmax = w.wsp.lmax_fields b = np.ones(lmax+1)*2. w.update_beams(b, b) # All good b2 = np.ones(lmax//2+1)*2. # Too short with pytest.raises(ValueError): w.update_beams(b, b2) b2 = 1. # Not array with pytest.raises(ValueError): w.update_beams(b, b2)
def test_workspace_shorten(): w = nmt.NmtWorkspace() w.read_from("test/benchmarks/bm_yc_yp_w02.fits") # OK read lmax = w.wsp.lmax larr = np.arange(lmax + 1) larr_long = np.arange(2 * lmax + 1) cls = 100. / (larr + 10.) cls_long = 100. / (larr_long + 10.) cls_c = w.couple_cell([cls, cls]) cls_c_long = w.couple_cell([cls_long, cls_long]) assert np.all(cls_c == cls_c_long)