while k < 20: if k == 0: delensed = maps.copy() else: kappa_model_filtered = enmap.samewcs( fmaps.filter_map(kappa_model, wfilter, modlmap_dat, lowPass=kellmax, highPass=kellmin), init_kappa_model) phi_model = lt.kappa_to_phi(kappa_model_filtered, modlmap_dat) grad_phi = enmap.grad(phi_model) delensed = lensing.delens_map(maps.copy(), grad_phi, nstep=nstep_delens, order=lens_order, mode="spline", border="cyclic") if k == 0: io.quickPlot2d(delensed - maps, out_dir + "firstdiff.png", verbose=True) fkmaps_update = enmap.samewcs(fftfast.fft(delensed, axes=[-2, -1]), measured) * kbeam_dat qest_maxlike.updateTEB_X(fkmaps_update, alreadyFTed=True) qest_maxlike.updateTEB_Y() kappa_recon = enmap.samewcs( qest_maxlike.getKappa(polcomb).real, measured) #kappa_recon_filtered = enmap.samewcs(fmaps.filter_map(kappa_recon,wfilter, #modlmap_dat,lowPass=kellmax,highPass=kellmin),init_kappa_model)
for k, index in enumerate(my_tasks): if rank == 0: print( ("Rank ", rank, " doing job ", k + 1, " / ", len(my_tasks), "...")) unlensed = parray_sim.get_unlensed_cmb(seed=index, scalar=False) luteb, dummy = sverif_cmb.add_power("unlensed", unlensed) kappa = parray_sim.get_kappa(ktype="grf", vary=False, seed=index + 1000000) phi, fphi = lt.kappa_to_phi(kappa, parray_sim.modlmap, return_fphi=True) grad_phi = enmap.grad(phi) lensed = lensing.lens_map(unlensed.copy(), grad_phi, order=lens_order) llteb, dummy = sverif_cmb.add_power("lensed", lensed) pdelensed = lensing.delens_map(lensed, grad_phi, nstep=delens_steps, order=lens_order) lpteb, dummy = sverif_cmb.add_power("pdelensed", pdelensed) qest.updateTEB_X(llteb[0], llteb[1], llteb[2], alreadyFTed=True) qest.updateTEB_Y(alreadyFTed=True) with io.nostdout(): rawkappa_TT = qest.getKappa("TT").real rawkappa_EB = qest.getKappa("EB").real kappa_recon_TT = enmap.ndmap(rawkappa_TT, wcs_dat) kappa_recon_EB = enmap.ndmap(rawkappa_EB, wcs_dat) if rank == 0 and k == 0: io.quickPlot2d(kappa_recon_TT, out_dir + "rtt.png") io.quickPlot2d(kappa_recon_EB, out_dir + "reb.png")
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 fkmaps = fftfast.fft(observed, axes=[-2, -1]) fkmaps = np.nan_to_num(fkmaps / kbeam_sim) * fMaskCMB_T measured = enmap.samewcs( fftfast.ifft(fkmaps, axes=[-2, -1], normalize=True).real, observed) if i == 0: io.quickPlot2d((measured - lensed), out_dir + "test2.png") grad_phi = grad_phi_true delensed = lensing.delens_map(measured.copy(), grad_phi, nstep=nstep, order=lens_order, mode="spline", border="cyclic") residual = delensed - unlensed res_stack += residual res_stack /= Nstack p = res_stack * 100. / unlensed p[np.abs(p) > 100] = np.nan io.quickPlot2d(p, out_dir + "resstack.png") io.quickPlot2d((lensed - unlensed) * 100. / unlensed, out_dir + "lensres.png") # io.quickPlot2d(res_stack,out_dir+"resstack.png") # io.quickPlot2d((lensed-unlensed),out_dir+"lensres.png") """ NOTES
simple_delensed = lensing.lens_map_flat_pix(lensed.copy(), -alpha_pix, order=lens_order) grad_phi = enmap.grad(phi) lens_residual = lensed - unlensed #io.quickPlot2d(lens_residual,out_dir+"lensres.png") simple_residual = simple_delensed - unlensed #io.quickPlot2d(simple_residual,out_dir+"simpleres.png") iters = [] for nstep in range(1, 8): iter_delensed = lensing.delens_map(lensed.copy(), grad_phi, nstep=nstep, order=lens_order, mode="spline", border="cyclic") # Check residual iter_residual = iter_delensed - unlensed iters.append(iter_residual) #io.quickPlot2d(iter_residual,out_dir+"iterres_"+str(nstep).zfill(2)+".png") bin_edges = np.arange(0., 20., 0.5) binner = stats.bin2D(modr_sim, bin_edges) cents, prof = binner.bin(lens_residual) pl.add(cents, np.abs(prof),
# io.quickPlot2d(alpha_pix2[0],out_dir+"alpha_pixx2.png") # io.quickPlot2d(alpha_pix2[1],out_dir+"alpha_pixy2.png") TCMB = 2.7255e6 ps = powspec.read_spectrum("../alhazen/data/cl_lensinput.dat") cmb_map = old_div(enmap.rand_map(shape, wcs, ps), TCMB) lensed = lensing.lens_map_flat_pix(cmb_map, alpha_pix, order=5) #lensed = lensing.lens_map_flat(cmb_map, phi) io.quickPlot2d(cmb_map, out_dir + "unlensed.png") io.quickPlot2d(lensed, out_dir + "lensed.png") io.quickPlot2d(lensed - cmb_map, out_dir + "diff.png") alpha = enmap.gradf(fphi) io.quickPlot2d(alpha[0], out_dir + "alphax.png") io.quickPlot2d(alpha[1], out_dir + "alphay.png") io.quickPlot2d(np.sum(alpha**2, 0)**0.5, out_dir + "alpha.png") kappa_inverted = -0.5 * enmap.div(alpha, normalize=False) io.quickPlot2d(kappa_inverted, out_dir + "kappainv.png") diffper = np.nan_to_num((kappaMap - kappa_inverted) * 100. / kappa_inverted) diffper[kappaMap < 0.005] = 0. io.quickPlot2d(diffper, out_dir + "kappadiff.png") #delensed = lensing.lens_map_flat_pix(lensed, -alpha_pix,order=5) alpha = enmap.grad(phi) delensed = lensing.delens_map(lensed, alpha, nstep=5, order=5) io.quickPlot2d(delensed, out_dir + "delensed.png") io.quickPlot2d(delensed - cmb_map, out_dir + "delensdiff.png")
fc = enmap.FourierCalc(shape_dat, wcs_dat) cluster_power = fc.power2d(kappa_model)[0] for j in range(niter): #kappa_iter_recon = enmap.ndmap(fmaps.filter_map(kappa_iter_recon,kappa*0.+1.,parray_sim.modlmap,lowPass=kellmax,highPass=kellmin),wcs_sim) # convert kappa to alpha rphitt, rfphitt = lt.kappa_to_phi(kappa_iter_recon, parray_dat.modlmap, return_fphi=True) rgrad_phitt = enmap.grad(rphitt) # delens original lensed with current model delensed = lensing.delens_map(lensed.copy(), rgrad_phitt, nstep=delens_steps, order=lens_order) delensed = enmap.ndmap( fmaps.filter_map(delensed, delensed * 0. + 1., parray_sim.modlmap, lowPass=tellmax, highPass=tellmin), wcs_sim) # get fft of delensed map and reconstruct llteb = enmap.fft(delensed, normalize=False) qest.updateTEB_X(llteb, alreadyFTed=True) qest.updateTEB_Y(llteb, alreadyFTed=True) with io.nostdout(): rawkappa = qest.getKappa("TT").real kappa_recon = enmap.ndmap(rawkappa, wcs_dat)
pld = io.Plotter(scaleY='log') ells = np.arange(tellmin,tellmax,1) ucltt = theory.uCl('TT',ells) pld.add(ells,ucltt*ells**2.) for t in range(Niters): ipower = fmaps.get_simple_power_enmap(enmap1=delensed) lcents, ccltt = lbinner_dat.bin(ipower) if rank==0: print((np.sum(ccltt*lcents*np.diff(lbin_edges)))) pld.add(lcents,ccltt*lcents**2.,color="r",alpha=(t+1.)/Niters) rphi, rfphi = lt.kappa_to_phi(kappa_model,parray_dat.modlmap,return_fphi=True) rgrad_phi = enmap.grad(rphi) #delensed = lensing.delens_map(measured.copy(), rgrad_phi, nstep=0, order=lens_order) delensed = lensing.delens_map(delensed, rgrad_phi, nstep=0, order=lens_order) delensed = enmap.ndmap(fmaps.filter_map(delensed,delensed*0.+1.,parray_dat.modlmap,lowPass=tellmax,highPass=tellmin),wcs_dat) # get fft of delensed map and reconstruct llteb = enmap.fft(delensed,normalize=False) qest.updateTEB_X(llteb,alreadyFTed=True) qest.updateTEB_Y(llteb,alreadyFTed=True) with io.nostdout(): rawkappa = qest.getKappa("TT").real kappa_recon = enmap.ndmap(np.nan_to_num(rawkappa),wcs_dat) kappa_recon = enmap.ndmap(fmaps.filter_map(kappa_recon,kappa_recon*0.+1.,parray_dat.modlmap,lowPass=kellmax,highPass=kellmin),wcs_dat)