def get_sim(sim_idx, beam_fwhm=beam_fwhm, noise_level=noise_level): ret = act_sim.getActpolCmbSim(None, coords, sim_idx, cmb_dir, doBeam=False, pixelFac=1) np.random.seed(sim_idx) # handle mean for i in range(len(ret)): ret[i].data -= np.mean(ret[i].data) ret[i] = enmap.from_flipper(ret[i]) # beam convolve owcs, shape = (ret[0].wcs.copy(), ret[0].shape) tqu = np.zeros((3, ) + ret[0].shape) tqu[0], tqu[1], tqu[2] = (ret[0], ret[1], ret[2]) tqu = enmap.enmap(tqu, owcs) alm = curvedsky.map2alm(tqu, lmax=lmax) del ret _, beam = cmblens.theory.get_gauss_beam(np.arange(lmax + 1), beam_fwhm) fl = np.sqrt(beam) log.info("beam convolution") for i in range(alm.shape[0]): alm[i] = hp.sphtfunc.almxfl(alm[i], fl) tqu = curvedsky.alm2map(alm[:, None], tqu.copy()[:, None])[:, 0] if noise_level > 0: log.info("adding noise") for i in range(len(tqu)): noise = enmap.to_flipper(tqu[i]).getTemplate() l_n = np.arange(lmax + 1000) noise_fact = 1. if i == 0 else np.sqrt(2) nl = cmblens.theory.get_white_noise_power(l_n, noise_level * noise_fact) ps = nl.reshape((1, 1, nl.size)) noise = curvedsky.rand_map(shape, owcs, ps, lmax=(lmax + 1000)) #noise.fillWithGaussianRandomField(l_n, nl, bufferFactor=1) tqu[i] += noise del noise # beam deconvolve log.info("beam deconvolution") alm = curvedsky.map2alm(tqu, lmax=lmax) for i in range(alm.shape[0]): alm[i] = hp.sphtfunc.almxfl(alm[i], 1. / fl) tqu = curvedsky.alm2map(alm[:, None], tqu.copy()[:, None])[:, 0] return (tqu[0], tqu[1], tqu[2]) # TQU
def map2power(iqu, mpibox): from orphics.tools.stats import bin2D, bin1D bin_edges = np.arange(200, 4000, 40) print("Map 2 alm...") alm = curvedsky.map2alm(iqu.astype("float64"), lmax=5000) del iqu cls = hp.alm2cl(alm) del alm fineells = np.arange(0, cls.shape[1], 1) print("Binning...") lbinner = bin1D(bin_edges) def b(cls): ells, cl1d = lbinner.binned(fineells, fineells * cls) ells, norm = lbinner.binned(fineells, fineells) cl1d /= norm return ells, cl1d ells, cltt = b(cls[0, :]) ells, clee = b(cls[1, :]) ells, clbb = b(cls[2, :]) ells, clte = b(cls[3, :]) ells, cleb = b(cls[4, :]) ells, cltb = b(cls[5, :]) mpibox.add_to_stats("TT", cltt) mpibox.add_to_stats("EE", clee) mpibox.add_to_stats("BB", clbb) mpibox.add_to_stats("TE", clte) mpibox.add_to_stats("EB", cleb) mpibox.add_to_stats("TB", cltb) return ells
def tqu2teb(tmap, qmap, umap): tqu = np.zeros((3, ) + tmap.shape) tqu[0], tqu[1], tqu[2] = (tmap, qmap, umap) tqu = enmap.enmap(tqu, tmap.wcs) alm = curvedsky.map2alm(tqu, lmax=lmax) teb = curvedsky.alm2map(alm[:, None], tqu.copy()[:, None], spin=0)[:, 0] del tqu return (teb[0], teb[1], teb[2])
def make_sim(self,seed): with bench.show("Lensing operation...") if self.rank==0 else ignore(): full,kappa = lensing.rand_map(self.fshape, self.fwcs, self.ps, lmax=self.lmax, maplmax=self.lmax, seed=seed, verbose=True if self.rank==0 else False, dtype=self.dtype,output="lk") alms = curvedsky.map2alm(full,lmax=self.lmax) ps_data = hp.alm2cl(alms.astype(np.complex128)) del alms self.mpibox.add_to_stats("fullsky_ps",ps_data) south = full.submap(self.pos_south) equator = full.submap(self.pos_eq) ksouth = kappa.submap(self.pos_south) kequator = kappa.submap(self.pos_eq) del full del kappa if self.count==0: self.shape['s'], self.wcs['s'] = south.shape, south.wcs self.shape['e'], self.wcs['e'] = equator.shape, equator.wcs for m in ['s','e']: self.taper[m],self.w2[m] = fmaps.get_taper(self.shape[m],taper_percent = 18.0,pad_percent = 4.0,weight=None) self.w4[m] = np.mean(self.taper[m]**4.) self.w3[m] = np.mean(self.taper[m]**3.) self.rotator = fmaps.MapRotatorEquator(self.shape['s'],self.wcs['s'],self.wdeg,self.hdeg,width_multiplier=0.6, height_multiplier=1.2,downsample=True,verbose=True if self.rank==0 else False, pix_target_override_arcmin=self.pix_intermediate) self.taper['r'] = self.rotator.rotate(self.taper['s']) self.w2['r'] = np.mean(self.taper['r']**2.) self.w4['r'] = np.mean(self.taper['r']**4.) self.w3['r'] = np.mean(self.taper['r']**3.) self.shape['r'], self.wcs['r'] = self.rotator.shape_final, self.rotator.wcs_final self.fc = {} self.binner = {} self.modlmap = {} for m in ['s','e','r']: self.fc[m] = enmap.FourierCalc(self.shape[m],self.wcs[m]) self.modlmap[m] = enmap.modlmap(self.shape[m],self.wcs[m]) self.binner[m] = bin2D(self.modlmap[m],self.bin_edges) self.cents = self.binner['s'].centers self._init_qests() self.count += 1 south *= self.taper['s'] equator *= self.taper['e'] ksouth *= self.taper['s'] kequator *= self.taper['e'] return south,equator,ksouth,kequator
lensed, kappa, unlensed = lensing.rand_map( shape, wcs, ps, lmax=lmax, maplmax=maplmax, seed=(seed, index), verbose=True if rank == 0 else False, dtype=dtype, output="lku") save("lensed", lensed, index) save("unlensed", unlensed, index) save("kappa", kappa, index) l_alm = curvedsky.map2alm(lensed, lmax=lmax) u_alm = curvedsky.map2alm(unlensed, lmax=lmax) k_alm = curvedsky.map2alm(kappa, lmax=lmax) del lensed del unlensed del kappa lcls = hp.alm2cl(l_alm.astype(np.complex128)) ucls = hp.alm2cl(u_alm.astype(np.complex128)) kcls = hp.alm2cl(k_alm.astype(np.complex128)) del l_alm del u_alm del k_alm
taper_percent=12.0, pad_percent=3.0, weight=None) binner4 = stats.bin2D(nmap.modlmap(), bin_edges) p2d, _, _ = fc4.power2d(nmap * taper4) / w24 shape4 = nmap.shape wcs4 = nmap.wcs cents, p1d4 = binner4.bin(p2d) if rank == 0 and k == 0: io.quickPlot2d(nmap * taper4, io.dout_dir + "nptapered.png") nmap = mgP.get_map() p2d, _, _ = fc.power2d(nmap * taper) / w2 cents, p1d5 = binner.bin(p2d) alm = curvedsky.map2alm(imap.astype(np.float64), lmax=4000) del imap cls = hp.alm2cl(alm) mpibox.add_to_stack("full", cls) mpibox.add_to_stack("cut", p1d) mpibox.add_to_stack("cut2", p1d2) mpibox.add_to_stack("cut3", p1d3) mpibox.add_to_stack("cut4", p1d4) mpibox.add_to_stack("cut5", p1d5) print((k, i)) mpibox.get_stacks() if rank == 0:
#elfact = 1. p2d = elfact * cmb.white_noise_with_atm_func(modlmap,uk_arcmin=10.0,lknee=4000,alpha=-4.5,dimensionless=False,TCMB=2.7255e6) p2d[modlmap<90]=0. p2d[modlmap>4000]=0. io.quickPlot2d(np.fft.fftshift(np.log10(p2d)),io.dout_dir+"p2d.png") mg = enmap.MapGen(shape,wcs,p2d.reshape(1,1,modlmap.shape[0],modlmap.shape[1])) imap = mg.get_map() io.quickPlot2d(imap,io.dout_dir+"cmb.png") # imap2 = mg.get_map() # io.quickPlot2d(imap2,io.dout_dir+"cmb2.png") dtype = np.complex128 rtype = np.zeros([0],dtype=dtype).real.dtype print("alms...") alm = curvedsky.map2alm(imap,lmax=4000) powfac = 0.5 ps_data = enmap.multi_pow(alm*alm.conj()[None,None],powfac).astype(rtype) print(alm) print((alm.shape)) pspec = np.ones((8000)) pspec[:200] = 0. pspec[4000:] = 0. rand_alm,ainfo = curvedsky.rand_alm(ps=pspec,lmax=4000,return_ainfo=True) #rand_map = curvedsky.rand_map(shape,wcs,pspec) #rand_alm = curvedsky.map2alm(rand_map,lmax=4000) ps_alm = enmap.multi_pow(rand_alm*rand_alm.conj()[None,None],powfac).astype(rtype) new_sim_alm = rand_alm *np.nan_to_num(ps_data/ps_alm)