def TestInverseLensing(): f = lens.fid_displacement() sim1,sim2 = lens.fid_two_HD_unlensed_cmb_sim() N_max = 3 testmap = sim1 len_map = f.lens_map(testmap) f_mat = f.M_mat maps = [testmap,len_map,f.dx_in_grid_units(),f.dy_in_grid_units(),f_mat['kappa'],f_mat['omega'],len_map-testmap] titles = ['input map','lensed map','$d_0(x)$ in grid units','$d_1(x)$ in grid units','$\kappa(x)$','$\omega(x)$','Len map - Unl map'] for i in xrange(N_max + 1) : print "N_it",i finv = f.inverse_displacement_NR(NR_iter = i) maps.append(finv.lens_map(len_map) - testmap) titles.append('residuals, NR it ' + str(i)) maps.append(np.log10(np.abs(finv.lens_map(len_map) - testmap))) titles.append('$\log_{10}$ abs. residuals, NR it ' + str(i)) """ plot all the maps """ pp = PdfPages("TestInverseLensing.pdf") for map,title in zip(maps,titles) : pl.figure() pl.imshow(map) pl.title(title) pl.colorbar() pp.savefig() pl.close() pp.close()
def TestConjGradInversionHighRes(): """ We first generate high displacements and then subsample """ Cl_len = lens.fiducial_Cl_len() Cl_unl = lens.fiducial_Cl_unl() res_low_arr= [8,9,10,11] sN_uKamin,Beam_FWHM_amin = [35.,5.] Nit = [] total_time = [] status = [] pp = PdfPages("TestHighResConjGRadInv.pdf") def plot_save_map(map,title) : pl.figure() pl.imshow(map) pl.title(title) pl.colorbar() pp.savefig() pl.close() def callback(v): CovPhi.cg_it += 1 print "This is callback",CovPhi.cg_it for res_low in res_low_arr : res_high = res_low + 1 lside = 0.0005*2**res_low f = lens.fid_displacement(params = [sN_uKamin,Beam_FWHM_amin,res_low,res_high,lside]) f_inv = f.inverse_displacement_NR(NR_iter = 2) CovPhi = lens.LensedCovMatrix(sN_uKamin,Beam_FWHM_amin,Cl_unl,Cl_len,res_low,res_high,lside,f,f_inv,verbose = False) dat1,dat2 = CovPhi.get_two_sims() plot_save_map(dat1.map,"Input map, res_low = " + str(res_low)) CovPhi.cg_it = 0 t0= time.time() output_map,stat = CovPhi.apply_cg_inverse_to_LD_map(dat1.map,callback = callback) total_time.append( (time.time() -t0)/60.) # minutes Nit.append(CovPhi.cg_it) status.append(stat) plot_save_map(np.log10(np.fft.fftshift(np.fft.fft2(dat1.map)).real**2), \ 'Input map, $\log_{10}$ Spectrum') # Cov^-1 T has cov Cov^-1 and thus the inverse spectrum of the map modulo lensing plot_save_map(-np.log10(np.fft.fftshift(np.fft.fft2(output_map.reshape((2**res_low,2**res_low))).real**2)), \ 'Current solution vector, $\log_{10}$ Inverse Spectrum, Nit = ' + str(CovPhi.cg_it)) print Nit print status print total_time np.savetxt('TestConjGradInversionHighRes_Nit.txt',Nit) np.savetxt('TestConjGradInversionHighRes_Nit_status.txt',status) np.savetxt('TestConjGradInversionHighRes_total_time_min.txt',status) pp.close()
def TestCurvatureMatrices(): ell_grid = np.logspace(1.,np.log10(2000),100) w = ell_grid**2*(ell_grid + 1)**2 def plot_noiselevels(tag): pl.figure(tag) pl.loglog() pl.xlim(min(ell_grid),max(ell_grid)) pl.plot(ell_grid,1e7/Fpp*w/2./np.pi,label = '$\phi\phi$') pl.plot(ell_grid,1e7/FOO*w/2./np.pi,label = '$\Omega\Omega$') pl.legend(loc= 'lower left') pl.xlabel('$\ell$') pl.ylabel('$10^{7}[\ell(\ell + 1)]^2 N_\ell /2\pi$') pl.setp(pl.gca().get_legend().get_frame(), visible=False) pl.title('Noise levels ' + tag) pl.close('all') for tag,sN_uKamin,Beam_FWHM_amin in zip(['TT'],[35.],[5.]) : Cl_len = lens.fiducial_Cl_len(tag) Cl_len.extend_lmin(0,Cl_len.Cl[0]) print 'Beam',Beam_FWHM_amin print 'noise',sN_uKamin lib128 = lens.Fisher_lib(sN_uKamin,Beam_FWHM_amin,Cl_len) Fpp,FOO,FpO = lib128.get_phiOmega_curvature_matrix(ell_grid) pp = PdfPages('TestCurvatureMatrix.pdf') plot_noiselevels(tag) pp.savefig() """ plot correlation """ pl.figure() pl.plot(ell_grid,-FpO/np.sqrt(FOO*Fpp)) pl.title('corr. coeff. $F_{\phi\Omega}/\sqrt{F_{\phi\phi} F_{\Omega\Omega}}$') pl.ylabel('corr. coeff. $F_{\phi\Omega}/\sqrt{F_{\phi\phi} F_{\Omega\Omega}}$') pp.savefig() pl.close() f = lens.fid_displacement() f_inv = f.inverse_displacement_NR(NR_iter = 2) Beam_FWHM_amin,sN_uKamin,res_low,res_high,lside = lens.fid_params() Cl_len = lens.fiducial_Cl_len() Cl_unl = lens.fiducial_Cl_len() Cl_unl.extend_lmin(0,Cl_unl.Cl[0]) CovPhi = lens.LensedCovMatrix(sN_uKamin,Beam_FWHM_amin,Cl_unl,Cl_len,res_low,res_high,lside,f,f_inv,verbose = False) Fpp,FOO = CovPhi.get_curvature_kappaomega() def plot_save_map(map,title) : pl.figure() pl.imshow(map) pl.title(title) pl.colorbar() pp.savefig() pl.close() plot_save_map(np.fft.fftshift(1e-7*Fpp),'$10^{-7}$ Curvature (F) matrix $\kappa \kappa$') plot_save_map(np.fft.fftshift(1e-7*FOO),'$10^{-7}$ Curvature (F) matrix $\omega \omega$') pp.close()
def TestConjGradInversion(): f = lens.fid_displacement() f_inv = f.inverse_displacement_NR(NR_iter = 2) Beam_FWHM_amin,sN_uKamin,res_low,res_high,lside = lens.fid_params() Cl_len = lens.fiducial_Cl_len() Cl_unl = lens.fiducial_Cl_unl() CovPhi = lens.LensedCovMatrix(sN_uKamin,Beam_FWHM_amin,Cl_unl,Cl_len,res_low,res_high,lside,f,f_inv,verbose = False) dat1,dat2 = CovPhi.get_two_sims() print "Starting conj. gradient inversion for u = CovInv T with conditioner :" CovPhi.cg_it = 0 pp = PdfPages("TestConjGradInvFakeData.pdf") def plot_save_map(map,title) : pl.figure() pl.imshow(map) pl.title(title) pl.colorbar() pp.savefig() pl.close() def callback(v): CovPhi.cg_it += 1 print 'This is callback ! it',CovPhi.cg_it plot_save_map(v.reshape(LD_shape),'Current solution vector, Nit = ' + str(CovPhi.cg_it)) plot_save_map(-np.log10(np.fft.fftshift(np.fft.fft2(v.reshape(LD_shape)).real**2)), \ 'Current solution vector, $\log_{10}$ Inverse Spectrum, Nit = ' + str(CovPhi.cg_it)) ly,lx = CovPhi.likelihood_gradient_dxdy(v.reshape(LD_shape)) plot_save_map(lx,'Likelihood gradient (wo det) - $d(x)$ direction'+ str(CovPhi.cg_it)) plot_save_map(ly,'Likelihood gradient (wo det) - $d(y)$ direction'+ str(CovPhi.cg_it)) lphi,lOmega = CovPhi.likelihood_gradient_phiOmega(v.reshape(LD_shape)) plot_save_map(lphi,'Likelihood gradient (wo det) - $\phi(x)$ direction (finite diffs.)'+ str(CovPhi.cg_it)) plot_save_map(lOmega,'Likelihood gradient (wo det) - $\Omega(x)$ direction (finite diffs.)'+ str(CovPhi.cg_it)) print -0.5*np.sum(dat1.map*v.reshape(LD_shape))/CovPhi.low_res_cub.npix() # likelihood term plot_save_map(dat1.map,'Input sim. data map') plot_save_map(np.log10(np.fft.fftshift(np.fft.fft2(dat1.map)).real**2), \ 'Input map, $\log_{10}$ Spectrum') # Cov^-1 T has cov Cov^-1 and thus the inverse spectrum of the map modulo lensing LD_shape = np.array( (2**res_low,2**res_low) ) output_map,status = CovPhi.apply_cg_inverse_to_LD_map(dat1.map,callback = callback) print 'Status',status pp.close()
def TestInterp(): f = lens.fid_displacement() f_inv = f.inverse_displacement_NR(NR_iter = 2) Beam_FWHM_amin,sN_uKamin,res_low,res_high,lside = lens.fid_params() Cl_len = lens.fiducial_Cl_len() Cl_unl = lens.fiducial_Cl_unl() CovPhi = lens.LensedCovMatrix(sN_uKamin,Beam_FWHM_amin,Cl_unl,Cl_len,res_low,res_high,lside,f,f_inv,verbose = False) dat1,dat2 = CovPhi.get_two_HD_unlensed_cmb_sims() del dat2 """ Setting up splines """ s = CovPhi.high_res_cub.shape() rmin = CovPhi.high_res_cub.rmin() xcoord = np.arange(s[1])*rmin[1] ycoord = np.arange(s[0])*rmin[0] k = 3 lx = (np.outer(np.ones(s[0]),xcoord) + f.dx.map).flatten() ly = (np.outer(ycoord,np.ones(s[1]),) + f.dy.map).flatten() lxy = np.zeros((s[0],s[1],2)) lxy[:,:,0] = ly.reshape(s) lxy[:,:,1] = lx.reshape(s) maps = [] title = [] maps.append(dat1) title.append("Input map") print "RectBivariateSpline" t0 =time.time() maps.append(interpolate.RectBivariateSpline(ycoord,xcoord,dat1,kx = k,ky = k).ev(ly,lx).reshape(s)) t0 = utils.PrtAndRstTime(1,t0) title.append("RectBivariateSpline") print "Lin. interp" t0 =time.time() maps.append(interpolate.interpn((ycoord,xcoord), dat1,lxy, method='linear',bounds_error= False)) title.append("Lin. interp.") t0 = utils.PrtAndRstTime(1,t0) print "splinefd2. interp" t0 =time.time() maps.append(interpolate.interpn((ycoord,xcoord), dat1,lxy, method='splinef2d',bounds_error= False)) title.append("splinefd2. interp") t0 = utils.PrtAndRstTime(1,t0) maps.append(maps[1]-maps[0]) title.append('Len - Unl') maps.append(maps[2]-maps[1]) title.append('Res. Lin - rectbiv.') maps.append(maps[3]-maps[1]) title.append('Res. splinef2d - rectbiv.') resmap = f_inv.lens_map(f.lens_map(dat1))- dat1 maps.append(np.log10(np.abs(resmap))) title.append('delensed Lensed map - map $\log_{10}$ res.') maps.append(resmap) title.append('delensed Lensed map - map res.') pp = PdfPages("TestInterp.pdf") def plot_save_map(map,title) : pl.figure() pl.imshow(map) pl.title(title) pl.colorbar() pp.savefig() pl.close() for map,title in zip(maps,title) : plot_save_map(map,title) pp.close()