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 c=nmt.compute_full_master(self.f0,self.f0,self.b_doub) with self.assertRaises(ValueError) : #Incompatible resolutions c=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) c=nmt.compute_full_master(self.f0,self.f0,self.b,workspace=w) #Computing from correct workspace self.assertEqual(c.shape,(1,self.b.bin.n_bands)) with self.assertRaises(RuntimeError) : #Inconsistent workspace c=nmt.compute_full_master(self.f0_half,self.f0_half,self.b_half,workspace=w) #Incorrect input spectra with self.assertRaises(ValueError) : c=nmt.compute_full_master(self.f0,self.f0,self.b,cl_noise=self.n_bad) with self.assertRaises(ValueError) : c=nmt.compute_full_master(self.f0,self.f0,self.b,cl_guess=self.n_bad) with self.assertRaises(RuntimeError) : c=nmt.compute_full_master(self.f0,self.f0,self.b,cl_noise=self.n_half) with self.assertRaises(RuntimeError) : c=nmt.compute_full_master(self.f0,self.f0,self.b,cl_guess=self.n_half)
def cross_teb(self, maps, mask=None, aposcale=None, binning=None): """ Cross PS, apply NaMaster estimator to TQU maps with(out) masks, requires NaMaster, healpy, numpy packages. Parameters ---------- maps : numpy.ndarray A six-row array of T, Q, U maps, arranged as {T,Q,U,T,Q,U}, with polarization in CMB convention. mask : numpy.ndarray mask map Returns ------- pseudo-PS results : tuple of numpy.ndarray (ell, TT, EE, BB) """ assert isinstance(maps, np.ndarray) assert (maps.shape[0] == 6) # fix resolution and apodization _nside = hp.get_nside(maps[0]) # apodization if aposcale is None: aposcale = 1.0 _apd_mask = nmt.mask_apodization(mask, aposcale, apotype='Smooth') _mapI01 = maps[0] _mapQ01 = maps[1] _mapU01 = maps[2] _mapI02 = maps[3] _mapQ02 = maps[4] _mapU02 = maps[5] # assemble NaMaster fields _f01 = nmt.NmtField(_apd_mask, [_mapI01]) _f21 = nmt.NmtField(_apd_mask, [_mapQ01, _mapU01]) _f02 = nmt.NmtField(_apd_mask, [_mapI02]) _f22 = nmt.NmtField(_apd_mask, [_mapQ02, _mapU02]) # initialize binning scheme with ? ells per bandpower if binning is None: binning = 16 else: assert isinstance(binning, int) _b = nmt.NmtBin(_nside, nlb=binning) # MASTER estimator _cl00 = nmt.compute_full_master(_f01, _f02, _b) # scalar - scalar _cl22 = nmt.compute_full_master(_f21, _f22, _b) # tensor - tensor return _b.get_effective_ells(), _cl00[0], _cl22[0], _cl22[3]
def auto_teb(self, maps, fwhms=None): assert isinstance(maps, np.ndarray) assert (maps.shape == (3,self._npix)) # assemble NaMaster fields if fwhms is None: _f0 = nmt.NmtField(self._mask[0], [maps[0]]) _f2 = nmt.NmtField(self._mask[0], [maps[1], maps[2]], purify_e=False, purify_b=True) else: _f0 = nmt.NmtField(self._mask[0], [maps[0]], beam=hp.gauss_beam(fwhms, 3*self._nside-1)) _f2 = nmt.NmtField(self._mask[0], [maps[1], maps[2]], purify_e=False, purify_b=True, beam=hp.gauss_beam(fwhms, 3*self._nside-1)) # estimate PS _cl00 = nmt.compute_full_master(_f0, _f0, self._b) _cl02 = nmt.compute_full_master(_f0, _f2, self._b) _cl22 = nmt.compute_full_master(_f2, _f2, self._b) return (self._modes, _cl00[0], _cl02[0], _cl02[1], _cl22[0], _cl22[1], _cl22[3])
def est_cl(w, fld1, fld2, b, me='step', ccl=None): '''Estimate Cl.''' if me == 'full': print('>> Computing full master...') cl = nmt.compute_full_master(fld1, fld2, b, workspace=w) cl_decoupled = cl[0] elif me == 'step': if ccl is None: # compute the coupled full-sky angular power spectra # this is equivalent to Healpy.anafast on masked maps print('>> Computing coupled Cl...') cl_coupled = nmt.compute_coupled_cell(fld1, fld2) else: cl_coupled = [ccl] # decouple into bandpowers by inverting the binned coupling matrix print('>> Decoupling Cl...') cl_decoupled = w.decouple_cell(cl_coupled)[0] else: sys.exit('>> Wrong me.') # get the effective ells print('>> Getting effective ells...') ell = b.get_effective_ells() return ell, cl_decoupled
def make_compute_all_serie(dir_name, NSIDE, Bin, photoz_min, photoz_max, nshells, nmocks): input_name = dir_name + '/outputs/mask.fits' #mask = hp.read_map('/home/drc01/sobreira/andluiz/halogen/outputs/mask.fits', verbose=False) mask = hp.read_map(input_name, verbose=False) # ******** Apodize mask ******** # The following function calls create apodized versions of the raw mask # with an apodization scale of 1.0 degrees using three different methods # mask_apod = nmt.mask_apodization(mask, 1.0, apotype="Smooth") # Computing coupling matrix print 'Computing coupling matrix' wsp = coupling_matrix(NSIDE, mask, Bin) ell = Bin.get_effective_ells() np.savetxt(dir_name + '/outputs/cldata/ell.dat', ell.T) for i in range(nmocks): print ' --- MOCK ' + str(i + 1) + ' --- ' if i < 10: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock000' + str( i) + '_masked.dat' elif i >= 10 and i < 100: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock00' + str( i) + '_masked.dat' elif i >= 100 and i < 1000: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock0' + str( i) + '_masked.dat' else: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock' + str( i) + '_masked.dat' #Getting overdensity maps for i-th mock overmaps = make_overmap_single(catalog_name, mask, NSIDE, photoz_min, photoz_max, nshells, i) print 'Computing namaster' for j in range(nshells): dmap = overmaps[j, :] f0 = nmt.NmtField(mask, [dmap]) cl_decoupled = nmt.compute_full_master(f0, f0, Bin, cl_noise=None, cl_guess=None, workspace=wsp)[0] output_name = dir_name + '/outputs/cldata/cl_mock' + str( i) + '_shell' + str(j) + '.dat ' np.savetxt(output_name, cl_decoupled) return
def cross_teb(self, maps, wsp=None, fwhms=[None,None]): assert isinstance(maps, np.ndarray) assert (maps.shape == (6,self._npix)) # assemble NaMaster fields if fwhms[0] is None: _f01 = nmt.NmtField(self._mask[0], [maps[0]]) _f21 = nmt.NmtField(self._mask[0], [maps[1], maps[2]], purify_e=False, purify_b=True) else: _f01 = nmt.NmtField(self._mask[0], [maps[0]], beam=hp.gauss_beam(fwhms[0], 3*self._nside-1)) _f21 = nmt.NmtField(self._mask[0], [maps[1], maps[2]], purify_e=False, purify_b=True, beam=hp.gauss_beam(fwhms[0], 3*self._nside-1)) if fwhms[1] is None: _f02 = nmt.NmtField(self._mask[0], [maps[3]]) _f22 = nmt.NmtField(self._mask[0], [maps[4], maps[5]], purify_e=False, purify_b=True) else: _f02 = nmt.NmtField(self._mask[0], [maps[3]], beam=hp.gauss_beam(fwhms[1], 3*self._nside-1)) _f22 = nmt.NmtField(self._mask[0], [maps[4], maps[5]], purify_e=False, purify_b=True, beam=hp.gauss_beam(fwhms[1], 3*self._nside-1)) # estimate PS _cl00 = nmt.compute_full_master(_f01, _f02, self._b) _cl02 = nmt.compute_full_master(_f01, _f22, self._b) _cl22 = nmt.compute_full_master(_f21, _f22, self._b) return (self._modes, _cl00[0], _cl02[0], _cl02[1], _cl22[0], _cl22[1], _cl22[3])
def Auto_TEB(self, maps, fwhm): ''' Calculate the auto-power spectra; 6 kinds of PS for each l-bin; Output ------------------------ cls_all, with order TT TE TB EE EB BB. ''' cls_all = np.ones((6, self.lbin)) beam = hp.gauss_beam(fwhm / 60 / 180 * np.pi, lmax=3 * self.nside - 1) t = nmt.NmtField(self.mask, [maps[0]], purify_e=False, purify_b=True, beam=beam) ### no need to purify?? 2020.07.03 qu = nmt.NmtField(self.mask, maps[1:3], purify_e=False, purify_b=True, beam=beam) cls_all[0] = nmt.compute_full_master(t, t, self.b) #TT cls_all[1:3] = nmt.compute_full_master(t, qu, self.b) #TE, TB cls_EB = nmt.compute_full_master(qu, qu, self.b) cls_all[3] = cls_EB[0] #EE cls_all[4] = cls_EB[1] #EB cls_all[5] = cls_EB[3] #BB return cls_all
def autoBP_EnB(self, maps, wsp=None, 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)) # estimate PS if wsp is None: cl22 = nmt.compute_full_master(f2, f2, self._b) return (self._modes, self.rebinning(cl22[0]), self.rebinning(cl22[3])) else: cl22c = nmt.compute_coupled_cell(f2, f2) cl22 = wsp.decouple_cell(cl22c) return (self._modes, self.rebinning(cl22[0]), self.rebinning(cl22[3]))
def autoBP_TT(self, maps, wsp=None, beams=None): dat = maps[0] # assemble NaMaster fields if beams is None: f0 = nmt.NmtField(self._apomask, [dat]) else: f0 = nmt.NmtField(self._apomask, [dat], beam=hp.gauss_beam(beams, 3*self._nside-1)) # estimate PS if wsp is None: cl00 = nmt.compute_full_master(f0, f0, self._b) return (self._modes, self.rebinning(cl00[0])) else: cl00c = nmt.compute_coupled_cell(f0, f0) cl00 = wsp.decouple_cell(cl00c) return (self._modes, self.rebinning(cl00[0]))
def Cl_func(map_,mask,b,w): """ Parameters ---------- map_ : clustering or lensing map mask : mask applied to map (same array of ones used in each instace) b : number of ells per bandpower w : workspace corresponding to nside Returns ------- cl_00[0] : Cl value for the given map """ f_0 = nmt.NmtField(mask,[map_]) # initialise spin-0 cl_00 = nmt.compute_full_master(f_0,f_0,b,workspace=w) # computer MASTER estimator using a workspace return cl_00[0]
def crosBP_BB(self, maps, wsp=None, beams=[None,None]): # assemble NaMaster fields if beams[0] is None: f21 = nmt.NmtField(self._apomask, [maps[1], maps[2]], purify_e=False, purify_b=True) else: f21 = nmt.NmtField(self._apomask, [maps[1], maps[2]], purify_e=False, purify_b=True, beam=hp.gauss_beam(beams[0], 3*self._nside-1)) if beams[1] is None: f22 = nmt.NmtField(self._apomask, [maps[4], maps[5]], purify_e=False, purify_b=True) else: f22 = nmt.NmtField(self._apomask, [maps[4], maps[5]], purify_e=False, purify_b=True, beam=hp.gauss_beam(beams[1], 3*self._nside-1)) # estimate PS if wsp is None: cl22 = nmt.compute_full_master(f21, f22, self._b) return (self._modes, self.rebinning(cl22[3])) else: cl22c = nmt.compute_coupled_cell(f21, f22) cl22 = wsp.decouple_cell(cl22c) return (self._modes, self.rebinning(cl22[3]))
def cross_t(self, maps, mask=None, aposcale=None, binning=None): """ Cross PS, apply NaMaster estimator to T (scalar) map with(out) masks, requires NaMaster, healpy, numpy packages. Parameters ---------- maps : numpy.ndarray A two-row array array of two T maps. mask : numpy.ndarray mask map Returns ------- pseudo-PS results : tuple of numpy.ndarray (ell, TT) """ assert isinstance(maps, np.ndarray) assert (maps.shape[0] == 2) # fix resolution and apodization _nside = hp.get_nside(maps[0]) # apodization if aposcale is None: aposcale = 1.0 _apd_mask = nmt.mask_apodization(mask, aposcale, apotype='Smooth') _mapI01 = maps[0] _mapI02 = maps[1] # assemble NaMaster fields _f01 = nmt.NmtField(_apd_mask, [_mapI01]) _f02 = nmt.NmtField(_apd_mask, [_mapI02]) # initialize binning scheme with ? ells per bandpower if binning is None: binning = 16 else: assert isinstance(binning, int) _b = nmt.NmtBin(_nside, nlb=binning) # MASTER estimator _cl00 = nmt.compute_full_master(_f01, _f02, _b) # scalar - scalar return _b.get_effective_ells(), _cl00[0]
def auto_eb(self, maps, mask=None, aposcale=None, binning=None): """ Auto PS, apply NaMaster estimator to QU (spin-2) maps with(out) masks, requires NaMaster, healpy, numpy packages. Parameters ---------- maps : numpy.ndarray A two-row array of Q, U maps, with polarization in CMB convention. mask : numpy.ndarray mask map Returns ------- pseudo-PS results : tuple of numpy.ndarray (ell, EE, BB) """ assert isinstance(maps, np.ndarray) assert (maps.shape[0] == 2) # fix resolution and apodization _nside = hp.get_nside(maps[0]) # apodization if aposcale is None: aposcale = 1.0 _apd_mask = nmt.mask_apodization(mask, aposcale, apotype='Smooth') _mapQ = maps[0] _mapU = maps[1] # assemble NaMaster fields _f2 = nmt.NmtField(_apd_mask, [_mapQ, _mapU]) # initialize binning scheme with ? ells per bandpower if binning is None: binning = 16 else: assert isinstance(binning, int) _b = nmt.NmtBin(_nside, nlb=binning) # MASTER estimator _cl22 = nmt.compute_full_master(_f2, _f2, _b) # tensor - tensor return _b.get_effective_ells(), _cl22[0], _cl22[3]
def crosBP_TT(self, maps, wsp=None, beams=[None,None]): dat1 = maps[0] dat2 = maps[3] # assemble NaMaster fields if beams[0] is None: f01 = nmt.NmtField(self._apomask, [dat1]) else: f01 = nmt.NmtField(self._apomask, [dat1], beam=hp.gauss_beam(beams[0], 3*self._nside-1)) if beams[1] is None: f02 = nmt.NmtField(self._apomask, [dat2]) else: f02 = nmt.NmtField(self._apomask, [dat2], beam=hp.gauss_beam(beams[1], 3*self._nside-1)) # estimate PS if wsp is None: cl00 = nmt.compute_full_master(f01, f02, self._b) return (self._modes, self.rebinning(cl00[0])) else: cl00c = nmt.compute_coupled_cell(f01, f02) cl00 = wsp.decouple_cell(cl00c) return (self._modes, self.rebinning(cl00[0]))
def estimate_bandpowers(hmap, maskval=hp.UNSEEN, nest=True, nside=2048, apodize=True, aposcale=0.1, nbins=32, work=None, cache_maps=False, key=None): bins = nmt.NmtBin.from_nside_linear(nside, nbins) # First, check whether map is NEST or RING. If NEST, re-order. if nest: print( "Converting power spectrum from NEST to RING for pseudo-C_\ell estimation." ) hmap = hp.pixelfunc.reorder(hmap, inp='NEST', out='RING') mask = np.ones_like(hmap) mask[hmap == maskval] = 0 # Apodize mask?? if apodize: mask = nmt.mask_apodization(mask, aposcale) # Convert to a fluctuation field. hmap[mask > 0] = hmap[mask > 0] / np.average(hmap[mask > 0], weights=mask[mask > 0]) - 1. if cache_maps: fitsio.write(f'map-{key}.fits', hmap_ngal, clobber=True) fitsio.write(f'map-{key}.fits', mask, clobber=False) field = nmt.NmtField(mask, hmap[np.newaxis, :]) if work == None: w = nmt.NmtWorkspace() w.compute_coupling_matrix(field, field, bins) else: w = work cl = nmt.compute_full_master(field, field, bins, workspace=w) l_eff = bins.get_effective_ells() return l_eff, cl, w
mask_kappa = pickle.load(open(dir + '/data/mask_celestial.pkl', 'rb')) #load unapodized mask map_kappa = [ pickle.load(open(dir + '/data/dat_klm_celestial.pkl', 'rb')) * mask_kappa ] #load map and mask mask_kappa_apo = pickle.load( open(dir + '/pickled/masks/celestial/mask_sim_celestial_apo.pkl', 'rb')) #load apodized mask print('initializing kappa field') f_kappa = nmt.NmtField(mask_kappa_apo, map_kappa) # initialize fields if args.kappa: print('computing auto power spectrum') cls = nmt.compute_full_master(f_kappa, f_kappa, b) print('pickling') pickle.dump( cls, open(dir + '/pickled/power_spectra/ps_kk_' + str(n) + '_new.pkl', 'wb')) print('cps stored as:', dir + '/pickled/power_spectra/ps_kk_' + str(n) + '_new.pkl') else: map_obj = [ pickle.load( open(dir + '/pickled/maps/celestial/map_' + obj + '_celestial.pkl', 'rb')) ] mask_obj = pickle.load( open(
def compute_power_spectra(self, pixel_scheme, i, j, k, f_wl, f_d, w00, w02, w22, ell_bins, noise, cl_theory): # Compute power spectra # TODO: now all possible auto- and cross-correlation are computed. # This should be tunable. # TODO: Fix window functions, and how to save them. # k refers to the type of measurement we are making import sacc import pymaster CEE = sacc.standard_types.galaxy_shear_cl_ee CBB = sacc.standard_types.galaxy_shear_cl_bb CdE = sacc.standard_types.galaxy_shearDensity_cl_e CdB = sacc.standard_types.galaxy_shearDensity_cl_b Cdd = sacc.standard_types.galaxy_density_cl type_name = NAMES[k] print( f"Process {self.rank} calcluating {type_name} spectrum for bin pair {i},{i}" ) sys.stdout.flush() # We need the theory spectrum for this pair theory = cl_theory[(i, j, k)] # The binning information - effective (mid) ell values and # the window information ls = ell_bins.get_effective_ells() win = [ell_bins.get_window(b) for b, l in enumerate(ls)] if k == SHEAR_SHEAR: workspace = w22 field_i = f_wl[i] field_j = f_wl[j] cl_guess = [ theory, np.zeros_like(theory), np.zeros_like(theory), np.zeros_like(theory) ] results_to_use = [(0, CEE), (3, CBB)] elif k == POS_POS: workspace = w00 field_i = f_d[i] field_j = f_d[j] cl_guess = [theory] results_to_use = [(0, Cdd)] elif k == SHEAR_POS: workspace = w02 field_i = f_wl[i] field_j = f_d[j] cl_guess = [theory, np.zeros_like(theory)] results_to_use = [(0, CdE), (1, CdB)] cl_noise = self.compute_noise(i, j, k, ell_bins, workspace, noise) if pixel_scheme.name == 'healpix': c = pymaster.compute_full_master(field_i, field_j, ell_bins, cl_noise=cl_noise, cl_guess=cl_guess, workspace=workspace) elif pixel_scheme.name == 'gnomonic': c = pymaster.compute_full_master_flat(field_i, field_j, ell_bins, cl_noise=cl_noise, cl_guess=cl_guess, ells_guess=cl_theory['ell'], workspace=workspace) for index, name in results_to_use: self.results.append(Measurement(name, ls, c[index], win, i, j))
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 xcorr_TEB(I_Afield, Q_Afield, U_Afield, I_Bfield, Q_Bfield, U_Bfield, apod_mask=None, bins=None, nside=2048, savedata=True, EBpure=True, dataname=["A", "B"], savestr="", verbose=0, data_root="../data/", **kwargs): print("Starting.") if EBpure: purify_e = True purify_b = True # spin 2 fields EB_Afield = nmt.NmtField(apod_mask, [Q_Afield, U_Afield], purify_e=purify_e, purify_b=purify_b) EB_Bfield = nmt.NmtField(apod_mask, [Q_Bfield, U_Bfield], purify_e=purify_e, purify_b=purify_b) # spin 0 fields T_Afield = nmt.NmtField(apod_mask, [I_Afield]) T_Bfield = nmt.NmtField(apod_mask, [I_Bfield]) if verbose: print("Computed TEB for both fields") # define workspace w = nmt.NmtWorkspace() if verbose: print("Workspace ready") if bins == None: bins, ell_binned = make_bins(nside=nside, binwidth=20, ellmax=1001) else: ell_binned = bins.get_effective_ells() #Compute MASTER estimator #spin-0 x spin-0 ClAA_00 = nmt.compute_full_master(T_Afield, T_Afield, bins) ClAB_00 = nmt.compute_full_master(T_Afield, T_Bfield, bins) ClBB_00 = nmt.compute_full_master(T_Bfield, T_Bfield, bins) #spin-0 x spin-2 ClAA_02 = nmt.compute_full_master(T_Afield, EB_Afield, bins) ClAB_02 = nmt.compute_full_master(T_Afield, EB_Bfield, bins) ClBB_02 = nmt.compute_full_master(T_Bfield, EB_Bfield, bins) #spin-2 x spin-2 ClAA_22 = nmt.compute_full_master(EB_Afield, EB_Afield, bins) ClAB_22 = nmt.compute_full_master(EB_Afield, EB_Bfield, bins) ClBB_22 = nmt.compute_full_master(EB_Bfield, EB_Bfield, bins) if verbose: print("Data ready to be saved") if savedata: Aname = dataname[0] Bname = dataname[1] out_fn = data_root + "Cl_{}_{}_TEB_EBpure_{}_{}_{}.h5".format( Aname, Bname, EBpure, nside, savestr) print("Saving data to {}".format(out_fn)) with h5py.File(out_fn, 'w') as f: dset = f.create_dataset(name='ClAB_00', data=ClAB_00) dset1 = f.create_dataset(name='ClAA_00', data=ClAA_00) dset2 = f.create_dataset(name='ClBB_00', data=ClBB_00) dset3 = f.create_dataset(name='ClAB_02', data=ClAB_02) dset4 = f.create_dataset(name='ClAA_02', data=ClAA_02) dset5 = f.create_dataset(name='ClBB_02', data=ClBB_02) dset6 = f.create_dataset(name='ClAB_22', data=ClAB_22) dset7 = f.create_dataset(name='ClAA_22', data=ClAA_22) dset8 = f.create_dataset(name='ClBB_22', data=ClBB_22) dset.attrs['nside'] = nside dset.attrs['EBpure'] = EBpure dset.attrs['ell_binned'] = ell_binned # add arbitrary kwargs as attributes for key in kwargs.keys(): dset.attrs[key] = kwargs[key]
def xcorr_T_EB(I_Afield, Q_Bfield, U_Bfield, apod_mask=None, bins=None, nside=2048, savedata=True, EBpure=True, CFM=False, Cerrors=False, dataname=["A", "B"], savestr="", verbose=0, data_root="../data/", **kwargs): print("Starting.") if EBpure: purify_e = True purify_b = True if verbose: print("Purifying E and B") print("fsky = {}".format(np.sum(apod_mask) / len(apod_mask))) print("Q_Bfield: ", Q_Bfield.shape, Q_Bfield.dtype) # spin 2 fields EB_Bfield = nmt.NmtField(apod_mask, [Q_Bfield, U_Bfield], purify_e=purify_e, purify_b=purify_b) if verbose: print("spin 2 done, now to spin 0") # spin 0 fields T_Afield = nmt.NmtField(apod_mask, [I_Afield]) if verbose: print("Computed TEB for both fields") # define workspace w = nmt.NmtWorkspace() if verbose: print("Workspace ready") if bins == None: bins, ell_binned = make_bins(nside=nside, binwidth=20, ellmax=1001) else: ell_binned = bins.get_effective_ells() if CFM: if verbose: print("Compute full master") #Compute MASTER estimator #spin-0 x spin-2 ClAB_02 = nmt.compute_full_master(T_Afield, EB_Bfield, bins) if Cerrors: #spin-0 x spin-0 ClAA_00 = nmt.compute_full_master(T_Afield, T_Afield, bins) #spin-2 x spin-2 ClBB_22 = nmt.compute_full_master(EB_Bfield, EB_Bfield, bins) else: w.compute_coupling_matrix(T_Afield, EB_Bfield, bins) if verbose: print("Mode coupling matrix computed") # Compute pseudo-Cls and deconvolve mask mode-coupling matrix to get binned bandpowers ClAB_02 = w.decouple_cell(nmt.compute_coupled_cell( T_Afield, EB_Bfield)) if Cerrors: print("errors not implementsed for non CFM") if Cerrors: #error = sqrt( TT*BB * (1/fsky) * (1/(2*ell + 1)) * (1/ellbinwidth) ) TT = ClAA_00[0] EE = ClBB_22[0] BB = ClBB_22[3] fsky = np.sum(apod_mask / len(apod_mask)) ell_binwidth = ell_binned[1] - ell_binned[0] # assumes constant err_TE = (TT * EE) / (fsky * (2 * ell_binned + 1) * ell_binwidth) err_TB = (TT * BB) / (fsky * (2 * ell_binned + 1) * ell_binwidth) if verbose: print("Data ready to be saved") if savedata: Aname = dataname[0] Bname = dataname[1] out_fn = data_root + "Cl_{}_{}_TEB_EBpure_{}_{}_{}.h5".format( Aname, Bname, EBpure, nside, savestr) print("Saving data to {}".format(out_fn)) with h5py.File(out_fn, 'w') as f: #dset1= f.create_dataset(name='ClAA_00', data=ClAA_00) dset = f.create_dataset(name='ClAB_02', data=ClAB_02) if Cerrors: TT = f.create_dataset(name='TT', data=TT) EE = f.create_dataset(name='EE', data=EE) BB = f.create_dataset(name='BB', data=BB) errTE = f.create_dataset(name='err_TE', data=err_TE) errTB = f.create_dataset(name='err_TB', data=err_TB) #dset8= f.create_dataset(name='ClBB_22', data=ClBB_22) dset.attrs['nside'] = nside dset.attrs['EBpure'] = EBpure dset.attrs['ell_binned'] = ell_binned # add arbitrary kwargs as attributes for key in kwargs.keys(): dset.attrs[key] = kwargs[key]
def make_compute_single_parallel(dir_name, mask, wsp, NSIDE, photoz_min, photoz_max, nshells, Bin, i): print ' --- MOCK ' + str(i + 1) + ' --- ' if i < 10: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock000' + str( i) + '_masked.dat' elif i >= 10 and i < 100: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock00' + str( i) + '_masked.dat' elif i >= 100 and i < 1000: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock0' + str( i) + '_masked.dat' else: catalog_name = dir_name + '/mock_cats/HALOGENlamps_V2.0.0_DNF_mock' + str( i) + '_masked.dat' #Getting overdensity maps for mock i overmaps = make_overmap_single(catalog_name, mask, NSIDE, photoz_min, photoz_max, nshells, i) #Setting cl of this mock nell = Bin.get_n_bands() cl_mock = np.zeros(shape=(nshells, nell)) print 'Computing namaster' for j in range(nshells): #Overdensity map for mock i and redshift shell j dmap = overmaps[j, :] """ Computes the full MASTER estimate of the power spectrum of two fields (f1 and f2). This is equivalent to successively calling: - :func:`pymaster.NmtWorkspace.compute_coupling_matrix` - :func:`pymaster.deprojection_bias` - :func:`pymaster.compute_coupled_cell` - :func:`pymaster.NmtWorkspace.decouple_cell` :param NmtField f1,f2: fields to correlate :param NmtBin b: binning scheme defining output bandpower :param cl_noise: noise bias (i.e. angular power spectrum of masked noise realizations) (optional). :param cl_guess: set of power spectra corresponding to a best-guess of the true power spectra of f1 and f2. Needed only to compute the contaminant cleaning bias (optional). :param NmtWorkspace workspace: object containing the mode-coupling matrix associated with an incomplete sky coverage. If provided, the function will skip the computation of the mode-coupling matrix and use the given information. :return: set of decoupled bandpowers """ f0 = nmt.NmtField(mask, [dmap]) cl_decoupled = nmt.compute_full_master(f0, f0, Bin, cl_noise=None, cl_guess=None, workspace=wsp) cl_mock[j, :] = cl_decoupled[0] #Saving measures on this mock i #dir_name = os.getcwd() #output_name = '/home/drc01/sobreira/andluiz/halogen/outputs/cldata/cl_mock' + str(i) + '.dat ' output_name = dir_name + '/outputs/cldata/cl_mock' + str(i) + '.dat' np.savetxt(output_name, cl_mock.T) return
Q_353, U_353 = hp.read_map(data_dir + data_name + FITS_end, field=[1, 2], verbose=True) Q_217, U_217 = hp.read_map(data_dir + data_name2 + FITS_end, field=[1, 2], verbose=True) # non-pure EB_npure_353 = nmt.NmtField(mask_apod, [Q_353, U_353]) EB_npure_217 = nmt.NmtField(mask_apod, [Q_217, U_217]) #print "mask and maps read in" w_npure = nmt.NmtWorkspace() #print "computing coupling matrix" w_npure.compute_coupling_matrix(EB_npure_353, EB_npure_217, bins) #print "coupling matrix done" # try "compute_full_master" for comparison Cl_2x2 = nmt.compute_full_master(EB_npure_353, EB_npure_217, bins) Cl_EE = Cl_2x2[0] Cl_BB = Cl_2x2[3] # pure EB_pure_353 = nmt.NmtField(mask_apod, [Q_353, U_353], purify_e=True, purify_b=True) EB_pure_217 = nmt.NmtField(mask_apod, [Q_217, U_217], purify_e=True, purify_b=True) #print "mask and maps read in (pure)" w_pure = nmt.NmtWorkspace() #print "computing coupling matrix (pure)" w_pure.compute_coupling_matrix(EB_pure_353, EB_pure_217, bins) #print "coupling matrix done (pure)" # try "compute_full_master" for comparison
def namaster_spec(arr, mask, nlb=50, is_Dell=True): nmtbin = nmt.NmtBin.from_nside_linear(NSIDE, nlb, is_Dell=is_Dell) f0 = nmt.NmtField(mask, [arr]) bpws = nmt.compute_full_master(f0, f0, nmtbin) return nmtbin.get_effective_ells(), bpws[0]
# pure EB_pure_1 = nmt.NmtField(mask_apod, [Q_1,U_1], purify_e = True, purify_b = True) # loop over freq2 for i in range(N_freq2): # read in the maps and compute the mode-coupling matrix for non-pure and pure fields T_2 = hp.read_map(data_dir+data_name2_arr[i]+FITS_end, field=[0], verbose=False) beam2 = (hp.gauss_beam(fwhm=FWHM2_arr[i]*(np.pi/180.0/60.0), lmax=3*Nside-1, pol=False)) # extract beam # non-pure T_npure_2 = nmt.NmtField(mask_apod, [T_2], beam=beam2) w_npure = nmt.NmtWorkspace() w_npure.compute_coupling_matrix(T_npure_2, EB_npure_1, bins) w_npure_Dl = nmt.NmtWorkspace() w_npure_Dl.compute_coupling_matrix(T_npure_2, EB_npure_1, bins2) # "compute_full_master" -- gives identical results to decouple_cell below Cl_TE_TB = nmt.compute_full_master(T_npure_2, EB_npure_1, bins) Cl_TE = Cl_TE_TB[0] Cl_TB = Cl_TE_TB[1] Dl_TE_TB = nmt.compute_full_master(T_npure_2, EB_npure_1, bins2) Dl_TE = Dl_TE_TB[0] * scalefac Dl_TB = Dl_TE_TB[1] * scalefac # gaussian covariance -- not yet implemented in my version #cw = nmt.NmtCovarianceWorkspace() #cw.compute_coupling_coefficients(T_npure_2, T_npure_2, T_npure_2, T_npure_2) # also compute auto-spectra for error bars Cl_TT = nmt.compute_full_master(T_npure_2, T_npure_2, bins) Cl_2x2 = nmt.compute_full_master(EB_npure_1, EB_npure_1, bins) Cl_EE = Cl_2x2[0] Cl_BB = Cl_2x2[3] Dl_TT = nmt.compute_full_master(T_npure_2, T_npure_2, bins2) * scalefac Dl_2x2 = nmt.compute_full_master(EB_npure_1, EB_npure_1, bins2)
mask = nmt.mask_apodization(hp.read_map("mask.fits", verbose=False), 1., apotype="Smooth") hp.mollview(mask, coord=['G', 'C'], title='Apodized mask') plt.show() #Read healpix maps and initialize a spin-0 and spin-2 field f_0 = nmt.NmtField(mask, [hp.read_map("maps.fits", field=0, verbose=False)]) f_2 = nmt.NmtField(mask, hp.read_map("maps.fits", field=[1, 2], verbose=False)) #Initialize binning scheme with 4 ells per bandpower b = nmt.NmtBin(nside, nlb=4) #Compute MASTER estimator #spin-0 x spin-0 cl_00 = nmt.compute_full_master(f_0, f_0, b) #spin-0 x spin-2 cl_02 = nmt.compute_full_master(f_0, f_2, b) #spin-2 x spin-2 cl_22 = nmt.compute_full_master(f_2, f_2, b) #Plot results ell_arr = b.get_effective_ells() plt.plot(ell_arr, cl_00[0], 'r-', label='TT') plt.plot(ell_arr, np.fabs(cl_02[0]), 'g-', label='TE') plt.plot(ell_arr, cl_22[0], 'b-', label='EE') plt.plot(ell_arr, cl_22[3], 'y-', label='BB') plt.loglog() plt.xlabel('$\\ell$', fontsize=16) plt.ylabel('$C_\\ell$', fontsize=16) plt.legend(loc='upper right', ncol=2, labelspacing=0.1)