def A(self, u): s, a = self.dof.unzip(u) # U"u = [S"s, 0a] Uu = self.dof.zip(en.harm2map(en.map_mul(self.iS, en.map2harm(s))), a * 0) # P'N"P u PNPu = self.PT(en.map_mul(self.iN, self.P(u))) return Uu + PNPu
def A(self, xmap): #enmap.write_map(args.odir + "/A1.fits", xmap) iPx = mul(self.iP, enmap.map2harm(xmap)) #enmap.write_map(args.odir + "/A2.fits", iPx.real) SiPx = enmap.harm2map(mul(self.S, iPx)) #enmap.write_map(args.odir + "/A3.fits", SiPx) NiPx = mul(self.noise, enmap.harm2map(iPx)) #enmap.write_map(args.odir + "/A3.fits", NiPx) res = xmap + SiPx + NiPx #enmap.write_map(args.odir + "/A4.fits", res) return res
def M(self, u): # Multiplying things out, the full expression for A is: # [ S" + sum(N") sum(N"T) ] # [ sum(T'N") sum(T'T) ] # A reasonable approximation for this is # [ S" + sum(sigma^{-2}) 0 ] # [ 0 sum(T'T) ] # which can be directly inverted. s, a = self.dof.unzip(u) # Solve for the cmb signal component res_s = en.harm2map(en.map_mul(self.S_prec, en.map2harm(s))) res_a = np.linalg.solve(self.TT, a) return self.dof.zip(res_s, res_a)
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")
apowers = {} cpowers = {} 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)
parser.add_argument("-L", "--lmax-scale", type=float, default=np.inf) parser.add_argument("-O", "--order", type=int, default=3) args = parser.parse_args() # Compute total 2d spectrum for the given input files, which must # all have compatible geometry nfile = len(args.ifiles) ftot = None ps_auto = None for ifile in args.ifiles: print("Reading %s" % ifile) m = enmap.read_map(ifile) m = eval("m" + args.slice) m = m.apod(args.apod) m = enmap.map2harm(m) if ftot is None: ftot = m * 0 ps_auto = enmap.zeros(m.shape[:1] + m.shape, m.wcs, m.dtype) ftot += m ps_auto += m[:, None] * np.conj(m[None, :]) print("Computing cross spectrum") # Compute auto spectrum ps_auto /= nfile**2 ftot /= nfile # Compute total spectrum ps_tot = ftot[:, None] * np.conj(ftot[None, :]) if len(args.ifiles) > 1: # Subtract to get cross spectrum ps_cross = ps_tot - ps_auto
kappa_stack[polcomb] = 0. if cluster: 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)
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 blur(m, sigma): l = np.sum(m.lmap()**2,0)**0.5 fm = enmap.map2harm(m) fm *= np.exp(-0.5*l**2*sigma**2)[None,:,:] return enmap.harm2map(fm)
def M(self, xmap): #enmap.write_map(args.odir + "/M1.fits", xmap) res = enmap.harm2map(mul(self.prec, enmap.map2harm(xmap))) #enmap.write_map(args.odir + "/M2.fits", res) return res
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)