예제 #1
0
def get_fiducial_sim_lib(path_to_libs = None):
    from fslens.sims import sims
    import sims_generic
    import covmatrices
    import likelihoods
    import displacements as dp
    from fslens.misc import jc_camb as camb
    # Needs about 748 sim to 4pi surface with this params.
    # Let's do twice as much.

    """
    There is a trade-off between high resolution and number of iteration in the conjugate gradient.
    If the resolution is lower each step is  much faster but the inacuracy in the inverse maping drives the erros
    and make it need a higher number of iterates.
    E.g. for PL2015 params, with res 8 I need 50 iterate , done in 5 sec total.
    with res 9 I ned 20 iterates, done in 10 sec total.
    """
    LD_res = 8
    HD_res = 8
    beam_FWHM_amin = 5.
    sN_uKamin = 35.
    nsims = 100

    HD_shape = (2**HD_res,2**HD_res) # resolution for CMB and lensing operation done at
    LD_shape = (2**LD_res,2**LD_res) # resolution of the dat map
    lcell_side_amin = 1.7 # length of lcell of the data resolution
    lside = lcell_side_amin / 60./180.*np.pi * LD_shape[0]*np.ones(2) # pixel res always the same always at 1.7 amin.

    cl_unl = camb.spectra_fromcambfile('/Users/jcarron/SpyderProjects/jpipe/inputs'
                                       '/cls/base_plikHM_TT_lowTEB_lensing_lenspotentialCls.dat')['tt'][:]
    cl_len = camb.spectra_fromcambfile('/Users/jcarron/SpyderProjects/jpipe/inputs'
                                       '/cls/base_plikHM_TT_lowTEB_lensing_lensedCls.dat')['tt'][:]
    cl_pp = camb.spectra_fromcambfile('/Users/jcarron/SpyderProjects/jpipe/inputs'
                                      '/cls/base_plikHM_TT_lowTEB_lensing_lenspotentialCls.dat')['pp'][:]
    cl_noise = (sN_uKamin * np.pi / 180. / 60.) ** 2*np.ones(20000)  # simple flat noise Cls
    cl_pp[0:2] = cl_pp[2]
    path_to_libs = '/Users/jcarron/data/flatsky_lens_simlibs/test_wPL2015' if path_to_libs is None else path_to_libs
    lib_cmb_unl = sims_generic.Gauss_sim_generic(path_to_libs + '/unl_cmb', cl_unl, HD_shape, lside, nsims_max=nsims)
    lib_noise = sims_generic.Gauss_sim_generic(path_to_libs + '/noise', cl_noise, LD_shape, lside, nsims_max=nsims)
    lib_pp = sims_generic.Gauss_sim_generic(path_to_libs + '/pp', cl_pp, LD_shape, lside, nsims_max=nsims)
    lib_OO = None
    lib_displ = sims.displ_2dsim(lib_pp, lib_OO)
    lib_displ_sim0 = sims_generic.sim_lib_shuffle(lib_displ, shuffle=lambda idx : 0)


    lib_cmb_len = sims.lencmb_sim_lib(lib_cmb_unl, lib_displ) # Library for lensed CMB
    lib_cmb_len_displ0 = sims.lencmb_sim_lib(lib_cmb_unl, lib_displ_sim0) # Library for lensed CMB's by identical potential

    sim_lib = sims.flatsky_sim_lib(lib_cmb_len, lib_noise, lib_dir=path_to_libs + '/full_sims',
                                   beam_FWHM_amin=beam_FWHM_amin, cache_sims=True, mask_map = None)
    fixedphi_sim_lib = sims.flatsky_sim_lib(lib_cmb_len_displ0, lib_noise, beam_FWHM_amin=beam_FWHM_amin,
                                            lib_dir=path_to_libs+'/fxp_sims', cache_sims=True, mask_map = None)
    unl_cov = covmatrices.flatsky_unlcov(cl_unl, (HD_res, HD_res), lside, sN_uKamin=sN_uKamin, Beam_FWHM_amin=beam_FWHM_amin, verbose=False)
    len_cov = covmatrices.flatsky_lencov(cl_unl, cl_len, LD_res, HD_res, lside,
                                         f = dp.identity_displacement(HD_shape), f_inv = dp.identity_displacement(HD_shape),
                                         verbose=False, sN_uKamin=sN_uKamin, Beam_FWHM_amin =beam_FWHM_amin)
    Fisher_lib = likelihoods.flatsky_Fisher_lib(cl_unl, (HD_res, HD_res), lside, sN_uKamin, beam_FWHM_amin, verbose=False)
    return [sim_lib,fixedphi_sim_lib,LD_res,HD_res,lside,sN_uKamin,beam_FWHM_amin,cl_unl,cl_len,cl_pp,cl_noise,unl_cov,len_cov,Fisher_lib]
예제 #2
0
def test_cg_inversion(split=True, k=3, NR=1, callback=None):
    """
    Tests the cg gradient inversion.
    Take one displacement, build umap with it, and builds a new Tmap with it.
    """
    f = sim_lib.cmb_sims.displ_sims.get_sim(0)
    f.split = split
    f.k = k
    f.NR_iter = NR
    Tmap = sim_lib.get_sim(nsims - 1)  # Not the same displacement as the one we test the cg with.
    len_cov_f = covmatrices.flatsky_lencov(
        cl_unl, cl_len, LD_res, HD_res, lside, f, f.inverse(), sN_uKamin, beam_FWHM_amin, verbose=False
    )
    # cl_unl, cl_len, LD_res, HD_res, lside, f, f_inv,sN_uKamin,Beam_FWHM_amin,
    import pylab as pl

    pl.ioff()
    from matplotlib.backends.backend_pdf import PdfPages

    pp = PdfPages(path_to_figs + "/cg_inversion.pdf")

    def plot_map(map, label=""):
        pl.figure()
        pl.title(label)
        pl.imshow(map)
        pl.colorbar()
        pp.savefig()
        pl.close()

    plot_map(Tmap, "input")
    if callback is None:
        len_cov_f.it = 0

        def callback(map):
            len_cov_f.it += 1
            print "This is callback", len_cov_f.it
            plot_map(len_cov_f.apply(map.reshape(LD_shape)), str(len_cov_f.it))
            plot_map(unl_cov_clen.apply(map.reshape(LD_shape)), str(len_cov_f.it) + "len_cov not dipl")

    print "testsuite::applying cg inversion, and transforming back."
    t_i = time.time()
    umap = len_cov_f.apply_cg_inverse(Tmap, callback=callback)[0].reshape(LD_shape)
    extime = np.round(time.time() - t_i, 2)
    new_Tmap = len_cov_f.apply(umap)
    svar = np.round(np.sqrt(np.var(new_Tmap - Tmap)), 7)

    par_dict = {"split": f.split, "spline k": f.k, "NR it.": f.NR_iter}
    out_dict = {
        "cg it.": len_cov_f.it,
        "max. res. in uK": np.round(np.max(new_Tmap - Tmap), 7),
        " rms res. in uK ": svar,
        "ex. time in sec. :": extime,
    }
    print "------------------------------------------------------"
    print par_dict.keys()
    print par_dict.values()
    print out_dict.keys()
    print out_dict.values()
    print "------------------------------------------------------"
    pp.close()
    return par_dict, out_dict
예제 #3
0
)
# FIXME : I think there is  bug in unl_cov, if I use this in the reconstruction
# FIXME : instead of len_cov with different LD and HD res, then something weird
unl_cov = covmatrices.flatsky_unlcov(
    cl_unl, (LD_res, LD_res), lside, sN_uKamin=sN_uKamin, Beam_FWHM_amin=beam_FWHM_amin, verbose=False
)
unl_cov_clen = covmatrices.flatsky_unlcov(
    cl_len, (LD_res, LD_res), lside, sN_uKamin=sN_uKamin, Beam_FWHM_amin=beam_FWHM_amin, verbose=False
)

len_cov = covmatrices.flatsky_lencov(
    cl_unl,
    cl_len,
    LD_res,
    HD_res,
    lside,
    f=dp.identity_displacement(HD_shape),
    f_inv=dp.identity_displacement(HD_shape),
    verbose=False,
    sN_uKamin=sN_uKamin,
    Beam_FWHM_amin=beam_FWHM_amin,
)
Fisher_lib = likelihoods.flatsky_Fisher_lib(cl_unl, (HD_res, HD_res), lside, sN_uKamin, beam_FWHM_amin, verbose=False)

# Dumping params for convenience
f = open(path_to_libs + "/params.dat", "w")
f.write("LD res         : " + str(LD_res) + "\n")
f.write("HD res         : " + str(LD_res) + "\n")
f.write("sN_uKamin      : " + str(np.round(sN_uKamin, 2)) + "\n")
f.write("beam_FWHM_amin : " + str(np.round(beam_FWHM_amin, 2)) + "\n")
f.write("nsims          : " + str(nsims) + "\n")
f.close()