def test_mask_flat_c2(self): msk_apo = nmt.mask_apodization_flat(self.msk, self.lx, self.ly, self.aposize, apotype="C2") # Below transition self.assertTrue((msk_apo[self.ny // 2:, :] < 1E-10).all()) # Above transition self.assertTrue((np.fabs(msk_apo[:self.ioff, :] - 1.) < 1E-10).all()) # Within transition ind_transition = np.arange(self.ioff, self.ny // 2, dtype=int) x = self.inv_xthr * np.fabs( (self.ny / 2. - ind_transition) * self.ly / self.ny) f = 0.5 * (1 - np.cos(x * np.pi)) self.assertTrue( (np.fabs(msk_apo[ind_transition, :] - f[:, None]) < 1E-10).all())
def test_mask_flat_errors(self): with self.assertRaises(ValueError): # Badly shaped input nmt.mask_apodization_flat(self.msk[0], self.lx, self.ly, self.aposize, apotype="C1") with self.assertRaises(RuntimeError): # Negative apodization nmt.mask_apodization_flat(self.msk, self.lx, self.ly, -self.aposize, apotype="C1") with self.assertRaises(RuntimeError): # Wrong apodization type nmt.mask_apodization_flat(self.msk, self.lx, self.ly, self.aposize, apotype="C3")
def load_maps(self): import pymaster as nmt import healpy # Parameters that we need at this point apod_size = self.config['apodization_size'] apod_type = self.config['apodization_type'] # Load the various input maps and their metadata map_file = self.open_input('diagnostic_maps', wrapper=True) pix_info = map_file.read_map_info('mask') area = map_file.file['maps'].attrs["area"] nbin_source = map_file.file['maps'].attrs['nbin_source'] nbin_lens = map_file.file['maps'].attrs['nbin_lens'] # Choose pixelization and read mask and systematics maps pixel_scheme = choose_pixelization(**pix_info) # Load the mask. It should automatically be the same shape as the # others, based on how it was originally generated. # We remove any pixels that are at or below our threshold (default=0) mask = map_file.read_map('mask') mask_threshold = self.config['mask_threshold'] mask[mask <= mask_threshold] = 0 mask[np.isnan(mask)] = 0 mask_sum = mask.sum() f_sky = area / 41253. if self.rank == 0: print(f"Unmasked area = {area}, fsky = {f_sky}") # Load all the maps in. # TODO: make it possible to just do a subset of these ngal_maps = [map_file.read_map(f'ngal_{b}') for b in range(nbin_lens)] g1_maps = [map_file.read_map(f'g1_{b}') for b in range(nbin_source)] g2_maps = [map_file.read_map(f'g2_{b}') for b in range(nbin_source)] # Mask any pixels which have the healpix bad value for m in g1_maps + g2_maps + ngal_maps: mask[m == healpy.UNSEEN] = 0 if self.config['flip_g2']: for g2 in g2_maps: w = np.where(g2 != healpy.UNSEEN) g2[w] *= -1 # TODO: load systematics maps here, once we are making them. syst_nc = None syst_wl = None map_file.close() # Cut out any pixels below the threshold, # zeroing out any pixels there cut = mask < mask_threshold mask[cut] = 0 # We also apply this cut to the count maps, # since otherwise the pixels below threshold would contaminate # the mean calculation below. for ng in ngal_maps: ng[cut] = 0 # Convert the number count maps to overdensity maps. # First compute the overall mean object count per bin. # Maybe we should do this in the mapping code itself? n_means = [ng.sum() / mask_sum for ng in ngal_maps] # and then use that to convert to overdensity d_maps = [(ng / mu) - 1 for (ng, mu) in zip(ngal_maps, n_means)] d_fields = [] wl_fields = [] if pixel_scheme.name == 'gnomonic': lx = np.radians(pixel_scheme.size_x) ly = np.radians(pixel_scheme.size_y) # Apodize the mask if apod_size > 0: if self.rank == 0: print( f"Apodizing mask with size {apod_size} deg and method {apod_type}" ) mask = nmt.mask_apodization_flat(mask, lx, ly, apod_size, apotype=apod_type) elif self.rank == 0: print("Not apodizing mask.") for i, d in enumerate(d_maps): # Density for gnomonic maps if self.rank == 0: print(f"Generating gnomonic density field {i}") field = nmt.NmtFieldFlat(lx, ly, mask, [d], templates=syst_nc) d_fields.append(field) for i, (g1, g2) in enumerate(zip(g1_maps, g2_maps)): # Density for gnomonic maps if self.rank == 0: print(f"Generating gnomonic lensing field {i}") field = nmt.NmtFieldFlat(lx, ly, mask, [g1, g2], templates=syst_wl) wl_fields.append(field) elif pixel_scheme.name == 'healpix': # Apodize the mask if apod_size > 0: if self.rank == 0: print( f"Apodizing mask with size {apod_size} deg and method {apod_type}" ) mask = nmt.mask_apodization(mask, apod_size, apotype=apod_type) elif self.rank == 0: print("Not apodizing mask.") for i, d in enumerate(d_maps): # Density for gnomonic maps print(f"Generating healpix density field {i}") field = nmt.NmtField(mask, [d], templates=syst_nc) d_fields.append(field) for i, (g1, g2) in enumerate(zip(g1_maps, g2_maps)): # Density for gnomonic maps print(f"Generating healpix lensing field {i}") field = nmt.NmtField(mask, [g1, g2], templates=syst_wl) wl_fields.append(field) else: raise ValueError( f"Pixelization scheme {pixel_scheme.name} not supported by NaMaster" ) return pixel_scheme, d_fields, wl_fields, nbin_source, nbin_lens, f_sky
clee[0] = 0 clbb[0] = 0 clte[0] = 0 nltt[0] = 0 nlee[0] = 0 nlbb[0] = 0 nlte[0] = 0 #Initialize pixelization from mask fmi, mask_hsc = fm.read_flat_map("data/mask_lss_flat.fits") if not os.path.isfile(fname_mask + '.fits'): if w_mask: mask_raw = mask_hsc.copy() if o.aposize > 0: mask = nmt.mask_apodization_flat( mask_raw.reshape([fmi.ny, fmi.nx]), fmi.lx_rad, fmi.ly_rad, o.aposize).flatten() else: mask = mask_raw.copy() else: mask = np.ones_like(mask_hsc) fmi.write_flat_map(fname_mask + ".fits", mask) fdum, mask = fm.read_flat_map(fname_mask + ".fits") fsky = fmi.lx_rad * fmi.ly_rad * np.sum(mask) / (4 * np.pi * fmi.nx * fmi.ny) #Read contaminant maps if w_cont: fgt = np.zeros([2, 1, len(mask)]) dum, fgt[0, 0, :] = fm.read_flat_map("data/cont_lss_star_flat.fits") #Stars
binit = lambda x: binner.bin(x)[1] cents = binner.centers #ells_coupled = cents # === ENMAP TO NAMASTER === # get the extent and shape of our geometry Ly, Lx = enmap.extent(shape, wcs) Ny, Nx = shape[-2:] if not (do_mask): mask = mask * 0. + 1. else: if do_apod: mask = nmt.mask_apodization_flat(mask, Lx, Ly, aposize=apod_width, apotype=apotype) # Mask fraction frac = np.sum(mask) * 1. / mask.size area = enmap.area(shape, wcs) * frac * (180. / np.pi)**2. print("Masked area sq.deg.: ", area) w2 = np.mean(mask**2.) # Naive power scaling factor N = args.Nsims s = stats.Stats() # Stats collector for i in range(N): if (i + 1) % 10 == 0: print(i + 1)
def dig_hole(x, y, r): rad = (np.sqrt((xarr - x)**2 + (yarr - y)**2)).flatten() return np.where(rad < r)[0] mask[dig_hole(0.3 * Lx, 0.6 * Ly, 0.05 * np.sqrt(Lx * Ly))] = 0. mask[dig_hole(0.7 * Lx, 0.12 * Ly, 0.07 * np.sqrt(Lx * Ly))] = 0. mask[dig_hole(0.7 * Lx, 0.8 * Ly, 0.03 * np.sqrt(Lx * Ly))] = 0. mask[np.where(xarr.flatten() < Lx / 16.)] = 0 mask[np.where(xarr.flatten() > 15 * Lx / 16.)] = 0 mask[np.where(yarr.flatten() < Ly / 16.)] = 0 mask[np.where(yarr.flatten() > 15 * Ly / 16.)] = 0 mask = mask.reshape([Ny, Nx]) mask = nmt.mask_apodization_flat(mask, Lx, Ly, aposize=2., apotype="C1") plt.figure() plt.imshow(mask, interpolation='nearest', origin='lower') plt.colorbar() # Binning scheme l0_bins = np.arange(Nx / 8) * 8 * np.pi / Lx lf_bins = (np.arange(Nx / 8) + 1) * 8 * np.pi / Lx b = nmt.NmtBinFlat(l0_bins, lf_bins) ells_uncoupled = b.get_effective_ells() # Let's create a fictitious theoretical power spectrum to generate # Gaussian simulations: larr = np.arange(3000.) clarr = ((larr + 50.) / 300.)**(-1.1) + 0.5
yarr = np.ones(mi.nx)[None, :] * np.arange(mi.ny)[:, None] * mi.ly / mi.ny def dig_hole(x, y, r): rad = (np.sqrt((xarr - x)**2 + (yarr - y)**2)).flatten() return np.where(rad < r)[0] nholes = 50 for i in np.arange(nholes): r = np.random.rand(3) mask[dig_hole(r[0] * mi.lx, r[1] * mi.ly, (0.005 + 0.02 * r[2]) * np.sqrt(mi.lx * mi.ly))] = 0. mask_raw = mask.copy() if aposize > 0: mask = nmt.mask_apodization_flat(mask_raw.reshape([mi.ny, mi.nx]), mi.lx * DTOR, mi.ly * DTOR, aposize) mi.write_flat_map(fname_mask, mask.flatten()) mi, mask = fm.read_flat_map(fname_mask + '.npz') mi = fm.FlatMapInfo([212.5, 222.], [-2., 2.], nx=792, ny=336) mask = mask.reshape([mi.ny, mi.nx]) if plotres: mi.view_map(mask.flatten(), addColorbar=False, colorMax=1, colorMin=0) if w_cont: if not os.path.isfile(prefix + "_contaminants.npz"): fgt, fgq, fgu = nmt.synfast_flat(int(mi.nx), int(mi.ny), mi.lx * DTOR, mi.ly * DTOR, [clttfg, cleefg, clbbfg, cltefg], pol=True)
def xcorr_flatsky(modedecomp=False, simkey="512_alfven3_0002_a_z", Imapkey="", deglen=10, apotype="C2", aposcale=0.5, Epure=True, Bpure=True): TQU = SimsTQU(fn=simkey, modedecomp=modedecomp, ikey=Imapkey) # Define flat-sky field # - Lx and Ly: the size of the patch in the x and y dimensions (in radians) Lx = deglen * np.pi / 180. # arbitrarily set this to a deglen x deglen deg box Ly = deglen * np.pi / 180. # - Nx and Ny: the number of pixels in the x and y dimensions Nx = 512 Ny = 512 # Define mask mask = np.ones_like(TQU.T).flatten() xarr = np.ones(Ny)[:, None] * np.arange(Nx)[None, :] * Lx / Nx yarr = np.ones(Nx)[None, :] * np.arange(Ny)[:, None] * Ly / Ny #Let's also trim the edges mask[np.where(xarr.flatten() < Lx / 16.)] = 0 mask[np.where(xarr.flatten() > 15 * Lx / 16.)] = 0 mask[np.where(yarr.flatten() < Ly / 16.)] = 0 mask[np.where(yarr.flatten() > 15 * Ly / 16.)] = 0 mask = mask.reshape([Ny, Nx]) mask = nmt.mask_apodization_flat(mask, Lx, Ly, aposize=aposcale, apotype=apotype) # Fields: # Once you have maps it's time to create pymaster fields. # Note that, as in the full-sky case, you can also pass # contaminant templates and flags for E and B purification # (see the documentation for more details) f0 = nmt.NmtFieldFlat(Lx, Ly, mask, [TQU.T]) f2 = nmt.NmtFieldFlat(Lx, Ly, mask, [TQU.Q, TQU.U], purify_b=Bpure, purify_e=Epure) # Bins: # For flat-sky fields, bandpowers are simply defined as intervals in ell, and # pymaster doesn't currently support any weighting scheme within each interval. l0_bins = np.arange(Nx / 8) * 8 * np.pi / Lx lf_bins = (np.arange(Nx / 8) + 1) * 8 * np.pi / Lx b = nmt.NmtBinFlat(l0_bins, lf_bins) # The effective sampling rate for these bandpowers can be obtained calling: ells_uncoupled = b.get_effective_ells() # Workspaces: # As in the full-sky case, the computation of the coupling matrix and of # the pseudo-CL estimator is mediated by a WorkspaceFlat case, initialized # by calling its compute_coupling_matrix method: w00 = nmt.NmtWorkspaceFlat() w00.compute_coupling_matrix(f0, f0, b) w02 = nmt.NmtWorkspaceFlat() w02.compute_coupling_matrix(f0, f2, b) w22 = nmt.NmtWorkspaceFlat() w22.compute_coupling_matrix(f2, f2, b) # Computing power spectra: # As in the full-sky case, you compute the pseudo-CL estimator by # computing the coupled power spectra and then decoupling them by # inverting the mode-coupling matrix. This is done in two steps below, # but pymaster provides convenience routines to do this # through a single function call cl00_coupled = nmt.compute_coupled_cell_flat(f0, f0, b) cl00_uncoupled = w00.decouple_cell(cl00_coupled) cl02_coupled = nmt.compute_coupled_cell_flat(f0, f2, b) cl02_uncoupled = w02.decouple_cell(cl02_coupled) cl22_coupled = nmt.compute_coupled_cell_flat(f2, f2, b) cl22_uncoupled = w22.decouple_cell(cl22_coupled) TT = cl00_uncoupled[0] TE = cl02_uncoupled[0] TB = cl02_uncoupled[1] EE = cl22_uncoupled[0] EB = cl22_uncoupled[1] BE = cl22_uncoupled[2] BB = cl22_uncoupled[3] if modedecomp: outroot = "/data/seclark/BlakesleySims/simdata/ModeDecomp/xcorrdata/" else: outroot = "/data/seclark/BlakesleySims/xcorrdata/" outfn = simkey + "_deglen{}_{}apod{}_EBpure{}{}.h5".format( deglen, apotype, aposcale, Epure, Bpure) with h5py.File(outroot + outfn, 'w') as f: TTdset = f.create_dataset(name='TT', data=TT) TEdset = f.create_dataset(name='TE', data=TE) TBdset = f.create_dataset(name='TB', data=TB) EEdset = f.create_dataset(name='EE', data=EE) EBdset = f.create_dataset(name='EB', data=EB) BEdset = f.create_dataset(name='BE', data=BE) BBdset = f.create_dataset(name='BB', data=BB) TTdset.attrs['deglen'] = deglen TTdset.attrs['Epure'] = Epure TTdset.attrs['Bpure'] = Bpure TTdset.attrs['ell_binned'] = ells_uncoupled