Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
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()