def smooth_pix(map, pixrad): fmap = enmap.fft(map) ky = np.fft.fftfreq(map.shape[-2]) kx = np.fft.fftfreq(map.shape[-1]) kr2 = ky[:, None]**2 + kx[None, :]**2 fmap *= np.exp(-0.5 * kr2 * pixrad**2) map = enmap.ifft(fmap).real return map
def smooth_tophat(map, rad): # Will use flat sky approximation here. It's not a good approximation for # our big maps, but this doesn't need to be accurate anyway ny,nx = map.shape[-2:] refy, refx = ny/2,nx/2 pos = map.posmap() pos[0] -= pos[0,refy,refx] pos[1] -= pos[1,refy,refx] r2 = np.sum(pos**2,0) kernel = (r2 < rad**2).astype(dtype) / (np.pi*rad**2) / map.size**0.5 * map.area() kernel = np.roll(kernel,-refy,0) kernel = np.roll(kernel,-refx,1) res = enmap.ifft(enmap.fft(map)*np.conj(enmap.fft(kernel))).real return res
def sim_points(shape, wcs, info): # Simulate the point amplitudes N = enmap.area(shape, wcs) * info.density*(180/np.pi)**2 n = N*(info.minamp/info.amp)**(info.alpha+1) amps = info.minamp*np.random.uniform(0,1,n)**(1/(info.alpha+1)) amps = np.maximum(-100*np.abs(info.amp),np.minimum(100*np.abs(info.amp), amps)) # Simulate the polarization psi = np.random.uniform(0,np.pi,n) amps = amps[None,:] * np.array([psi*0+1,np.cos(2*psi)*info.pol,np.sin(2*psi)*info.pol]) # Simulate positions uniformly in pixels ipos = np.array([np.random.uniform(0,shape[-2],n),np.random.uniform(0,shape[-1],n)]) pos = enmap.pix2sky(wcs, ipos) # Draw the points on a canvas using convolution. This requires all points # to have integer pixel positions and the same radius. rawmap = np.zeros(shape) for i in range(shape[0]): rawmap[i][tuple(ipos.astype(int))] = amps[i] l = np.sum(enmap.lmap(shape,wcs)**2,0)**0.5 kernel = np.exp(-0.5*l**2*info.rad**2) # actually perform the convolution pmap = enmap.ifft(enmap.fft(rawmap)*kernel[None]).real print np.max(pmap), np.min(pmap) return pmap
def map_ifft(x): return enmap.ifft(x).real
def debug(): teb = enmap.ifft(enmap.map2harm(unlensed)).real lteb = enmap.ifft(klteb).real if pol: io.quickPlot2d(unlensed[0], out_dir + "tmap.png") io.quickPlot2d(unlensed[1], out_dir + "qmap.png") io.quickPlot2d(unlensed[2], out_dir + "umap.png") io.quickPlot2d(teb[1], out_dir + "emap.png") io.quickPlot2d(teb[2], out_dir + "bmap.png") io.quickPlot2d(lensed[0], out_dir + "ltmap.png") io.quickPlot2d(lensed[1], out_dir + "lqmap.png") io.quickPlot2d(lensed[2], out_dir + "lumap.png") io.quickPlot2d(lteb[1], out_dir + "lemap.png") io.quickPlot2d(lteb[2], out_dir + "lbmap.png") else: io.quickPlot2d(unlensed, out_dir + "tmap.png") io.quickPlot2d(lensed, out_dir + "ltmap.png") t = teb[0, :, :] e = teb[1, :, :] b = teb[2, :, :] nt = noise[0, :, :] ne = noise[1, :, :] nb = noise[2, :, :] ntt2d = np.nan_to_num(fmaps.get_simple_power_enmap(nt) / kbeam_sim**2.) nee2d = np.nan_to_num(fmaps.get_simple_power_enmap(ne) / kbeam_sim**2.) nbb2d = np.nan_to_num(fmaps.get_simple_power_enmap(nb) / kbeam_sim**2.) utt2d = fmaps.get_simple_power_enmap(t) uee2d = fmaps.get_simple_power_enmap(e) ute2d = fmaps.get_simple_power_enmap(enmap1=t, enmap2=e) ubb2d = fmaps.get_simple_power_enmap(b) debug_edges = np.arange(2, 12000, 80) dbinner = stats.bin2D(modlmap_sim, debug_edges) cents, utt = dbinner.bin(utt2d) cents, uee = dbinner.bin(uee2d) cents, ute = dbinner.bin(ute2d) cents, ntt = dbinner.bin(ntt2d) cents, nee = dbinner.bin(nee2d) cents, nbb = dbinner.bin(nbb2d) #cents, ubb = dbinner.bin(ubb2d) tl = lteb[0, :, :] el = lteb[1, :, :] bl = lteb[2, :, :] ltt2d = fmaps.get_simple_power_enmap(tl) lee2d = fmaps.get_simple_power_enmap(el) lte2d = fmaps.get_simple_power_enmap(enmap1=tl, enmap2=el) lbb2d = fmaps.get_simple_power_enmap(bl) cents, ltt = dbinner.bin(ltt2d) cents, lee = dbinner.bin(lee2d) cents, lte = dbinner.bin(lte2d) cents, lbb = dbinner.bin(lbb2d) lcltt, lclee, lclte, lclbb = ( x for x in cmb.unpack_cmb_theory(theory, fine_ells, lensed=True)) cltt, clee, clte, clbb = ( x for x in cmb.unpack_cmb_theory(theory, fine_ells, lensed=False)) pl = io.Plotter(scaleY='log', scaleX='log') pl.add(cents, utt * cents**2., color="C0", marker="o", ls="none") pl.add(cents, uee * cents**2., color="C1", marker="o", ls="none") #pl.add(cents,ubb*cents**2.,color="C2",ls="-") pl.add(fine_ells, cltt * fine_ells**2., color="C0", ls="--") pl.add(fine_ells, clee * fine_ells**2., color="C1", ls="--") #pl.add(fine_ells,clbb*fine_ells**2.,color="C2",ls="--") pl.done(out_dir + "ccomp.png") pl = io.Plotter(scaleX='log') pl.add(cents, ute * cents**2., color="C0", marker="o", ls="none") pl.add(fine_ells, clte * fine_ells**2., color="C0", ls="--") pl.done(out_dir + "ccompte.png") # sells,stt,see,sbb,ste = np.loadtxt("data/cl_lensed.dat",unpack=True) # stt *= 2.*np.pi/TCMB**2./sells/(sells+1.) # see *= 2.*np.pi/TCMB**2./sells/(sells+1.) # sbb *= 2.*np.pi/TCMB**2./sells/(sells+1.) pl = io.Plotter(scaleY='log') #,scaleX='log') # pl.add(sells,stt*sells**2.,color="C0",ls="-") # pl.add(sells,see*sells**2.,color="C1",ls="-") # pl.add(sells,sbb*sells**2.,color="C2",ls="-") pl.add(cents, ltt * cents**2., color="C0", marker="o", ls="none") pl.add(cents, lee * cents**2., color="C1", marker="o", ls="none") pl.add(cents, lbb * cents**2., color="C2", marker="o", ls="none") pl.add(cents, ntt * cents**2., color="C0", ls="-.", alpha=0.4) pl.add(cents, nee * cents**2., color="C1", ls="-.", alpha=0.4) pl.add(cents, nbb * cents**2., color="C2", ls="-.", alpha=0.4) pl.add(fine_ells, lcltt * fine_ells**2., color="C0", ls="--") pl.add(fine_ells, lclee * fine_ells**2., color="C1", ls="--") pl.add(fine_ells, lclbb * fine_ells**2., color="C2", ls="--") pl.done(out_dir + "lccomp.png") pl = io.Plotter(scaleX='log') pl.add(cents, lte * cents**2., color="C0", ls="-") pl.add(fine_ells, lclte * fine_ells**2., color="C0", ls="--") pl.done(out_dir + "lccompte.png")
for polcomb in pol_list: kappa_stack[polcomb] = 0. if cluster: profiles[polcomb] = [] else: apowers[polcomb] = [] cpowers[polcomb] = [] for i in range(Nsims): print(i) unlensed = enmap.rand_map(shape_sim, wcs_sim, ps) lensed = lensing.lens_map_flat_pix(unlensed, alpha_pix, order=lens_order) klteb = enmap.map2harm(lensed) klteb_beam = klteb * kbeam_sim lteb_beam = enmap.ifft(klteb_beam).real noise = enmap.rand_map(shape_sim, wcs_sim, ps_noise, scalar=True) observed = lteb_beam + noise measured = enmap.downgrade(observed, analysis_pixel_scale / sim_pixel_scale) if i == 0: #debug() shape_dat, wcs_dat = measured.shape, measured.wcs lxmap_dat, lymap_dat, modlmap_dat, angmap_dat, lx_dat, ly_dat = fmaps.get_ft_attributes_enmap( shape_dat, wcs_dat) nT = ntfunc(modlmap_dat) nP = npfunc(modlmap_dat) kbeam_dat = cmb.gauss_beam(modlmap_dat, beam_arcmin)
profiles[polcomb] = [] if maxlike: kappa_stack_maxlike[polcomb] = 0. profiles_maxlike[polcomb] = [] else: apowers[polcomb] = [] cpowers[polcomb] = [] for i in range(Nsims): print(i) unlensed = enmap.rand_map(shape_sim, wcs_sim, ps) lensed = lensing.lens_map_flat_pix(unlensed, alpha_pix, order=lens_order) klteb = enmap.map2harm(lensed) klteb_beam = klteb * kbeam_sim lteb_beam = enmap.ifft(klteb_beam).real noise = enmap.rand_map(shape_sim, wcs_sim, ps_noise, scalar=True) observed = lteb_beam + noise measured = enmap.downgrade(observed, analysis_pixel_scale / sim_pixel_scale) if i == 0: shape_dat, wcs_dat = measured.shape, measured.wcs modr_dat = enmap.modrmap(shape_dat, wcs_dat) * 180. * 60. / np.pi # === ESTIMATOR === template_dat = fmaps.simple_flipper_template_from_enmap( shape_dat, wcs_dat) lxmap_dat, lymap_dat, modlmap_dat, angmap_dat, lx_dat, ly_dat = fmaps.get_ft_attributes_enmap( shape_dat, wcs_dat)
w4 = np.mean(taper**4.) modlmap = imap.modlmap() ellmax = 6000 ellmin = 200 ellwidth = 40 bin_edges = np.arange(ellmin, ellmax, ellwidth) binner = stats.bin2D(modlmap, bin_edges) imap = imap * taper phi = phi * taper if i == 0: io.quickPlot2d(phi, os.environ['WORK'] + "/web/plots/phimap.png") print("IQU to TEB...") teb = enmap.ifft(enmap.map2harm(imap)).real print("Powers...") t = teb[0, :, :] e = teb[1, :, :] b = teb[2, :, :] spec2d = {} spec2d['tt'] = np.nan_to_num(fmaps.get_simple_power_enmap(t)) / w2 spec2d['ee'] = np.nan_to_num(fmaps.get_simple_power_enmap(e)) / w2 spec2d['bb'] = np.nan_to_num(fmaps.get_simple_power_enmap(b)) / w2 spec2d['te'] = np.nan_to_num(fmaps.get_simple_power_enmap(t, enmap2=e)) / w2 spec2d['pp'] = np.nan_to_num( fmaps.get_simple_power_enmap(phi)) / w2 * (modlmap * (modlmap + 1.))**2. / 4.
def add(res, m, desc): m2 = enmap.ifft(enmap.map2harm(m)).real res.tqu.append(m.copy()) res.teb.append(m2) res.desc.append(desc) print desc
def smooth_gauss(m, sigma): l = np.sum(m.lmap()**2, 0)**0.5 return np.real(en.ifft(en.fft(m) * np.exp(-0.5 * (l * sigma)**2)))
def to_eb(m): return enmap.ifft(enmap.map2harm(m)).real m_scal_u, m_scal_l, m_tens_u, m_tens_l, m_tot_u, m_tot_l = [enmap.downgrade(to_eb(i),os) for i in [m_scal_u, m_scal_l, m_tens_u, m_tens_l, m_tot_u, m_tot_l]]
import numpy as np, argparse from scipy import ndimage from enlib import enmap parser = argparse.ArgumentParser() parser.add_argument("ifile") parser.add_argument("ofile") parser.add_argument("-r", "--apod-radius", type=int, default=64) args = parser.parse_args() def make_apod(shape, rad): mask = np.zeros(shape[-2:]) mask[rad:-rad, rad:-rad] = 1 w = np.maximum(1 - ndimage.distance_transform_edt(1 - mask) / rad, 0) ** 3 return w teb = enmap.read_map(args.ifile) tqu = enmap.harm2map(enmap.fft(teb)) mask = make_apod(teb.shape, args.apod_radius) tqu_mask = tqu * mask[None] teb_mask = enmap.ifft(enmap.map2harm(tqu_mask)).real res = enmap.samewcs([teb, tqu, tqu_mask, teb_mask], teb) enmap.write_map(args.ofile, res)
inputKappaMap, szMap = getKappaSZ(b,snap,massIndex,px,thetaMap.shape) # === DEFLECTION MAP === a = alphaMaker(thetaMap) alpha = a.kappaToAlpha(inputKappaMap,test=False) alphamod = 180.*60.*np.sum(alpha**2,0)**0.5/np.pi print "alphaint ", alphamod[thetaMap*60.*180./np.pi<10.].mean() pos = thetaMap.posmap() + alpha pix = thetaMap.sky2pix(pos, safe=False) lensedTQU = lensing.displace_map(map, pix,order=5) lensedMapX = enmap.ifft(enmap.map2harm(lensedTQU)).real lensedMapY = lensedMapX.copy() if szX: lensedMapX += (szMap/TCMB) if szY: lensedMapY += (szMap/TCMB) fotX = enmap.fft(lensedMapX,normalize=False) fotY = enmap.fft(lensedMapY,normalize=False) print "Reconstructing" , i , " ..." qest.updateTEB_X(fotX,alreadyFTed=True) qest.updateTEB_Y(fotY,alreadyFTed=True) kappa = enmap.samewcs(qest.getKappa(polCombList[0]).real,thetaMap)