Beispiel #1
0
def get_lencmbs_lib_tensor(fn_tensCls, res=14, cache_sims=True, nsims=120, num_threads=4):
    """
    Default simulation library of 120 lensed CMB sims.
    Lensing is always performed at lcell 0.745 amin or so, and lensed CMB are generated on a square with sides lcell 2 ** res
    Will build all phases at the very first call if not already present.
    """
    HD_ellmat = get_ellmat(res, HD_res=res)
    ellmax_sky = 6000
    fsky = int(np.round(np.prod(HD_ellmat.lsides) / 4. / np.pi * 1000.))
    lib_skyalm = ffs_covs.ell_mat.ffs_alm_pyFFTW(HD_ellmat, num_threads=num_threads,
                                                 filt_func=lambda ell: ell <= ellmax_sky)
    skypha = sims.ffs_phas.ffs_lib_phas(LENSITDIR + '/temp/%s_sims/fsky%04d/len_alms_tens/skypha/' % (nsims, fsky), 4,
                                        lib_skyalm,
                                        nsims_max=nsims)
    if not skypha.is_full() and pbs.rank == 0:
        for _i, idx in misc.misc_utils.enumerate_progress(np.arange(nsims), label='Generating CMB phases'):
            skypha.get_sim(idx)
    pbs.barrier()
    
    #  cls_unl, cls_len = get_fidcls(ellmax_sky=ellmax_sky)
    if fn_tensCls == None:
        cls_unl = get_fidtenscls(ellmax_sky=ellmax_sky)
        cl_file_root = 'lensit_fiducial_tensCls'
    else:
        cls_unl = get_tenscls(fn_tensCls, ellmax_sky=ellmax_sky)
        cl_file_root = os.path.splitext(os.path.basename(fn_tensCls))[0]
    print('cl_file_root = %s'%cl_file_root)
    
    lib_dir = LENSITDIR + '/temp/%s/%s_sims/fsky%04d/len_alms_tens/' % (cl_file_root, nsims, fsky)
    print('get_lencmbs_lib_tensor: alm lib_dir = %s'%lib_dir)
    
    return sims.ffs_cmbs.sims_cmb_len(lib_dir, lib_skyalm, cls_unl,
            lib_pha=skypha, cache_lens=cache_sims, 
            do_tensor_only=True) # added
Beispiel #2
0
def get_lencmbs_lib(res=14, cache_sims=True, nsims=120, num_threads=4):
    """
    Default simulation library of 120 lensed CMB sims.
    Lensing is always performed at lcell 0.745 amin or so, and lensed CMB are generated on a square with sides lcell 2 ** res
    Will build all phases at the very first call if not already present.
    """
    HD_ellmat = get_ellmat(res, HD_res=res)
    ellmax_sky = 6000
    fsky = int(np.round(np.prod(HD_ellmat.lsides) / 4. / np.pi * 1000.))
    lib_skyalm = ffs_covs.ell_mat.ffs_alm_pyFFTW(
        HD_ellmat,
        num_threads=num_threads,
        filt_func=lambda ell: ell <= ellmax_sky)
    skypha = sims.ffs_phas.ffs_lib_phas(
        LENSITDIR + '/temp/%s_sims/fsky%04d/len_alms/skypha' % (nsims, fsky),
        4,
        lib_skyalm,
        nsims_max=nsims)
    if not skypha.is_full() and pbs.rank == 0:
        for _i, idx in misc.misc_utils.enumerate_progress(
                np.arange(nsims), label='Generating CMB phases'):
            skypha.get_sim(idx)
    pbs.barrier()
    cls_unl, cls_len = get_fidcls(ellmax_sky=ellmax_sky)
    return sims.ffs_cmbs.sims_cmb_len(
        LENSITDIR + '/temp/%s_sims/fsky%04d/len_alms' % (nsims, fsky),
        lib_skyalm,
        cls_unl,
        lib_pha=skypha,
        cache_lens=cache_sims)
Beispiel #3
0
    def __init__(self,cl_unl,res,sN_uKamin,Beam_FWHM_amin,verbose = False,lsides = (rtpi4,rtpi4),lib_dir = None):
        assert len(res) == 2 and len(lsides) == 2

        self.shape = (2 ** res[0],2 ** res[1])
        self.lsides = lsides
        self.verbose = verbose
        self.lib_dir = lib_dir


        ellmax1 = self.ellmax_grid() + 1
        self.cl_unl = np.zeros(ellmax1)
        self.cl_unl[0:min((ellmax1,len(cl_unl)))] = cl_unl[0:min((ellmax1,len(cl_unl)))]

         # Build nbar * (cl * bl **2 + noise) :
        bl = get_bl(Beam_FWHM_amin,ell_max=ellmax1-1)
        self._cl_totnb = (self.cl_unl * bl ** 2 + get_cl_noise(sN_uKamin,ell_max=ellmax1 - 1)) \
                         * np.prod(np.array(self.shape) / self.lsides)
        # Build ell matrix and save it to disk in int16 format for further use :
        if not os.path.exists(self.lib_dir) and pbs.rank == 0 :
            try : os.mkdir(self.lib_dir)
            except : print '** failed to make directory :',lib_dir
        print "ffs_unlcov: caching ellmap"
        kmin = 2 * np.pi / np.array(lsides)
        ky2 = ut.Freq(np.arange(self.shape[0]), self.shape[0]) ** 2 * kmin[0] ** 2
        kx2 = ut.Freq(np.arange(self.shape[1] / 2 + 1), self.shape[1]) ** 2 * kmin[1] ** 2
        ones = np.ones(self.shape[0])
        freq_map = np.sqrt(np.outer(ky2, ones[0:self.shape[1]/2 + 1]) + np.outer(ones, kx2))
        freq_map = np.uint16(np.round(freq_map - 0.5))  # map of integer ell. in int16 format.
        np.save(self.lib_dir + '/ellmap.npy', freq_map)
        assert np.all(self._cl_totnb[self.get_ellmat()] > 0.)
        pbs.barrier()
Beispiel #4
0
def get_lencmbs_lib_for_MCN1(res=14, cache_sims=True, nsims=120, num_threads=4,path_to_inputs='.', Beam_FWHM_amin=0.0):
    """
    Default simulation library of 120 lensed CMB sims.
    Lensing is always performed at lcell 0.745 amin or so, and lensed CMB are generated on a square with sides lcell 2 ** res
    Will build all phases at the very first call if not already present.
    """
    HD_ellmat = get_ellmat(res, HD_res=res)
    ellmax_sky = 6000
    fsky = int(np.round(np.prod(HD_ellmat.lsides) / 4. / np.pi * 1000.))
    lib_skyalm = ffs_covs.ell_mat.ffs_alm_pyFFTW(HD_ellmat, num_threads=num_threads,
                                                 filt_func=lambda ell: ell <= ellmax_sky)
    if os.path.exists('/global'):
        skypha = sims.ffs_phas.ffs_lib_phas('/global/cscratch1/sd/markm/lensit/temp/%s_sims/fsky%04d/beam%d/len_alms/skypha' % (nsims, fsky, Beam_FWHM_amin*10), 4, lib_skyalm, nsims_max=nsims)
    else:
        skypha = sims.ffs_phas.ffs_lib_phas('/media/sf_C_DRIVE/Users/DarkMatter42/OneDrive - University of Sussex/LensIt/temp/%s_sims/fsky%04d/beam%d/len_alms/skypha' % (nsims, fsky, Beam_FWHM_amin*10), 4, lib_skyalm, nsims_max=nsims)
			
    if not skypha.is_full() and pbs.rank == 0:
        for _i, idx in misc.misc_utils.enumerate_progress(np.arange(nsims), label='Generating CMB phases'):
            skypha.get_sim(idx)
    pbs.barrier()
    cls_unl, cls_len = get_fidcls(ellmax_sky=ellmax_sky,path_to_inputs=path_to_inputs)
    if os.path.exists('/global'):
        return sims.ffs_cmbs.sims_cmb_len_for_MCN1('/global/cscratch1/sd/markm/lensit/temp/MCN1_sims/%s_sims/fsky%04d/beam%d/len_alms' % (nsims, fsky, Beam_FWHM_amin*10), lib_skyalm, cls_unl, lib_pha=skypha, cache_lens=cache_sims)
    else:
        return sims.ffs_cmbs.sims_cmb_len_for_MCN1('/media/sf_C_DRIVE/Users/DarkMatter42/OneDrive - University of Sussex/LensIt/temp/MCN1_sims/%s_sims/fsky%04d/beam%d/len_alms' % (nsims, fsky, Beam_FWHM_amin*10), lib_skyalm, cls_unl, lib_pha=skypha, cache_lens=cache_sims)
Beispiel #5
0
def get_noisefree_maps_lib_for_MCN1(params, exp, LDres, HDres=14, cache_lenalms=True, cache_maps=False, nsims=120, num_threads=4,path_to_inputs='.'):
    """
    Default simulation library of 120 full flat sky sims with no instrument noise at resolution LDres.
    Different exp at same resolution share the same random phases both in CMB and noise
        Will build all phases at the very first call if not already present.
    :param exp: 'Planck', 'S4' ... See get_config
    :param LDres: 14 : cell length is 0.745 amin, 13 : 1.49 etc.
    :return: sim library instance
    """
    #_, _, Beam_FWHM_amin, ellmin, ellmax = get_config(exp)
    Beam_FWHM_amin = params.Beam_FWHM_amin
    ellmin = params.lmin
    ellmax = params.lmax
    len_cmbs = get_lencmbs_lib_for_MCN1(res=HDres, cache_sims=cache_lenalms, nsims=nsims,path_to_inputs=path_to_inputs, Beam_FWHM_amin=Beam_FWHM_amin)
    lmax_sky = len_cmbs.lib_skyalm.ellmax
    cl_transf = hp.gauss_beam(Beam_FWHM_amin / 60. * np.pi / 180., lmax=lmax_sky)
    lib_datalm = ffs_covs.ell_mat.ffs_alm_pyFFTW(get_ellmat(LDres, HDres), filt_func=lambda ell: ell <= lmax_sky,
                                                 num_threads=num_threads)
    fsky = int(np.round(np.prod(len_cmbs.lib_skyalm.ell_mat.lsides) / 4. / np.pi * 1000.))
    vcell_amin2 = np.prod(lib_datalm.ell_mat.lsides) / np.prod(lib_datalm.ell_mat.shape) * (180 * 60. / np.pi) ** 2
    
    if os.path.exists('/global'):
        pixpha = sims.ffs_phas.pix_lib_phas('/global/cscratch1/sd/markm/lensit/temp/%s_sims/fsky%04d/beam%d/res%s/pixpha' % (nsims, fsky, Beam_FWHM_amin*10, LDres), 3, lib_datalm.ell_mat.shape, nsims_max=nsims)
    else:
        pixpha = sims.ffs_phas.pix_lib_phas('/media/sf_C_DRIVE/Users/DarkMatter42/OneDrive - University of Sussex/LensIt/temp/%s_sims/fsky%04d/beam%d/res%s/pixpha' % (nsims, fsky, Beam_FWHM_amin*10, LDres), 3, lib_datalm.ell_mat.shape, nsims_max=nsims)
    		
    if not pixpha.is_full() and pbs.rank == 0:
        for _i, idx in misc.misc_utils.enumerate_progress(np.arange(nsims), label='Generating Noise phases'):
            pixpha.get_sim(idx)
    pbs.barrier()
    if os.path.exists('/global'):
        lib_dir = '/global/cscratch1/sd/markm/lensit/temp/AN_sims_for_MCN1/%s_sims/fsky%04d/beam%d/res%s/%s/maps' % (nsims, fsky, Beam_FWHM_amin*10, LDres, exp)
    else:
        lib_dir = '/media/sf_C_DRIVE/Users/DarkMatter42/OneDrive - University of Sussex/LensIt/temp/AN_sims_for_MCN1/%s_sims/fsky%04d/beam%d/res%s/%s/maps' % (nsims, fsky, Beam_FWHM_amin*10, LDres, exp)
    return sims.ffs_maps.lib_noisefree(lib_dir, lib_datalm, len_cmbs, cl_transf, cache_sims=cache_maps)
Beispiel #6
0
def get_maps_lib(exp, LDres, HDres=14, cache_lenalms=True, cache_maps=False, \
    nsims=120, num_threads=4, do_tensor_only=False, fn_tensCls=None):
# end CH
    """
    Default simulation library of 120 full flat sky sims for exp 'exp' at resolution LDres.
    Different exp at same resolution share the same random phases both in CMB and noise
        Will build all phases at the very first call if not already present.
    :param exp: 'Planck', 'S4' ... See get_config
    :param LDres: 14 : cell length is 0.745 amin, 13 : 1.49 etc.
    :return: sim library instance
    """
    sN_uKamin, sN_uKaminP, Beam_FWHM_amin, ellmin, ellmax = get_config(exp)
    # Added by Chen Heinrich for Lensnet (2019/10)
    #len_cmbs = get_lencmbs_lib(
    #    #res=HDres, cache_sims=cache_lenalms, nsims=nsims)
    if do_tensor_only == False:
        len_cmbs = get_lencmbs_lib(
            res=HDres, cache_sims=cache_lenalms, nsims=nsims) #CH19
    else:
        len_cmbs = get_lencmbs_lib_tensor(fn_tensCls,
            res=HDres, cache_sims=cache_lenalms, nsims=nsims, \
            num_threads=num_threads) #CH19    
    # end CH
    lmax_sky = len_cmbs.lib_skyalm.ellmax
    cl_transf = hp.gauss_beam(
        Beam_FWHM_amin / 60. * np.pi / 180., lmax=lmax_sky)
    lib_datalm = ffs_covs.ell_mat.ffs_alm_pyFFTW(get_ellmat(LDres, HDres), filt_func=lambda ell: ell <= lmax_sky,
                                                 num_threads=num_threads)
    fsky = int(
        np.round(np.prod(len_cmbs.lib_skyalm.ell_mat.lsides) / 4. / np.pi * 1000.))
    vcell_amin2 = np.prod(lib_datalm.ell_mat.lsides) / \
        np.prod(lib_datalm.ell_mat.shape) * (180 * 60. / np.pi) ** 2
    nTpix = sN_uKamin / np.sqrt(vcell_amin2)
    nPpix = sN_uKaminP / np.sqrt(vcell_amin2)

    pixpha = sims.ffs_phas.pix_lib_phas(LENSITDIR + '/temp/%s_sims/fsky%04d/res%s/pixpha' % (nsims, fsky, LDres), 3,
                                        lib_datalm.ell_mat.shape, nsims_max=nsims)
    if not pixpha.is_full() and pbs.rank == 0:
        for _i, idx in misc.misc_utils.enumerate_progress(np.arange(nsims), label='Generating Noise phases'):
            pixpha.get_sim(idx)
    pbs.barrier()
    
    # CH: takes care of different map location if maps are cached
    if fn_tensCls == None:
        cl_file_root = 'lensit_fiducial_tensCls'
    else:
        cl_file_root = os.path.splitext(os.path.basename(fn_tensCls))[0]
    
    if do_tensor_only == True:
        lib_dir = LENSITDIR + \
            '/temp/%s/%s_sims/fsky%04d/res%s/%s/maps_tens' % (cl_file_root, nsims, fsky, LDres, exp)
    else:
        lib_dir = LENSITDIR + \
            '/temp/%s_sims/fsky%04d/res%s/%s/maps' % (nsims, fsky, LDres, exp)
    print('get_maps_lib: maps lib_dir = %s'%lib_dir)  

    return sims.ffs_maps.lib_noisemap(lib_dir, lib_datalm, len_cmbs, cl_transf, nTpix, nPpix, nPpix,
            pix_pha=pixpha, cache_sims=cache_maps)
Beispiel #7
0
def get_maps_lib(exp,
                 LDres,
                 HDres=14,
                 cache_lenalms=True,
                 cache_maps=False,
                 nsims=120,
                 num_threads=4):
    """
    Default simulation library of 120 full flat sky sims for exp 'exp' at resolution LDres.
    Different exp at same resolution share the same random phases both in CMB and noise
        Will build all phases at the very first call if not already present.
    :param exp: 'Planck', 'S4' ... See get_config
    :param LDres: 14 : cell length is 0.745 amin, 13 : 1.49 etc.
    :return: sim library instance
    """
    sN_uKamin, sN_uKaminP, Beam_FWHM_amin, ellmin, ellmax = get_config(exp)
    len_cmbs = get_lencmbs_lib(res=HDres,
                               cache_sims=cache_lenalms,
                               nsims=nsims)
    lmax_sky = len_cmbs.lib_skyalm.ellmax
    cl_transf = hp.gauss_beam(Beam_FWHM_amin / 60. * np.pi / 180.,
                              lmax=lmax_sky)
    lib_datalm = ffs_covs.ell_mat.ffs_alm_pyFFTW(
        get_ellmat(LDres, HDres),
        filt_func=lambda ell: ell <= lmax_sky,
        num_threads=num_threads)
    fsky = int(
        np.round(
            np.prod(len_cmbs.lib_skyalm.ell_mat.lsides) / 4. / np.pi * 1000.))
    vcell_amin2 = np.prod(lib_datalm.ell_mat.lsides) / np.prod(
        lib_datalm.ell_mat.shape) * (180 * 60. / np.pi)**2
    nTpix = sN_uKamin / np.sqrt(vcell_amin2)
    nPpix = sN_uKaminP / np.sqrt(vcell_amin2)

    pixpha = sims.ffs_phas.pix_lib_phas(LENSITDIR +
                                        '/temp/%s_sims/fsky%04d/res%s/pixpha' %
                                        (nsims, fsky, LDres),
                                        3,
                                        lib_datalm.ell_mat.shape,
                                        nsims_max=nsims)
    if not pixpha.is_full() and pbs.rank == 0:
        for _i, idx in misc.misc_utils.enumerate_progress(
                np.arange(nsims), label='Generating Noise phases'):
            pixpha.get_sim(idx)
    pbs.barrier()
    lib_dir = LENSITDIR + '/temp/%s_sims/fsky%04d/res%s/%s/maps' % (
        nsims, fsky, LDres, exp)
    return sims.ffs_maps.lib_noisemap(lib_dir,
                                      lib_datalm,
                                      len_cmbs,
                                      cl_transf,
                                      nTpix,
                                      nPpix,
                                      nPpix,
                                      pix_pha=pixpha,
                                      cache_sims=cache_maps)
Beispiel #8
0
    def __init__(self, fname, idtype="INTEGER"):
        import pbs
        if not os.path.exists(fname) and pbs.rank == 0:
            con = sqlite3.connect(fname, detect_types=sqlite3.PARSE_DECLTYPES, timeout=3600)
            cur = con.cursor()
            cur.execute("create table rngdb (id %s PRIMARY KEY, "
                        "type STRING, pos INTEGER, has_gauss INTEGER,cached_gaussian REAL, keys array)" % idtype)
            con.commit()
        pbs.barrier()

        self.con = sqlite3.connect(fname, timeout=3600., detect_types=sqlite3.PARSE_DECLTYPES)
Beispiel #9
0
    def __init__(self, lib_dir,cl_unl, cl_len, LD_res, HD_res,f, f_inv,sN_uKamin,beam_FWHM_amin,lside = np.sqrt(4 * np.pi),verbose=False):
        """
        f and finv are displacement field classes. Number of points on each side 2**LD_res,2**HD_res.
        f and f_inv must have a f.lens_map routine that does the lensing of map 2**HD_res by 2**HD_res.
        f_inv must also have a f.det_M routine which returns the determinant of the magnification matrix
        at all points of the map.
        """
        self.LD_cub = ut.library_datacube(np.ones(2, dtype=int) * LD_res, np.ones(2) * lside, verbose=verbose)
        self.HD_cub = ut.library_datacube(np.ones(2, dtype=int) * HD_res, np.ones(2) * lside, verbose=verbose)
        self.LD_shape = tuple(self.LD_cub.shape())
        self.HD_shape = tuple(self.HD_cub.shape())
        self.HD_rshape = (self.HD_shape[0],self.HD_shape[1]/2 + 1)
        self.lsides = (lside,lside)

        self.f_inv = f_inv  # inverse displacement
        self.f = f  # displacement
        ellmax1 = self.ellmax_grid() + 1

        self.cl_unl = np.zeros(ellmax1)
        self.cl_unl[0:min((ellmax1,len(cl_unl)))] = cl_unl[0:min((ellmax1,len(cl_unl)))]
        self.cl_len = np.zeros(ellmax1)
        self.cl_len[0:min((ellmax1,len(cl_len)))] = cl_len[0:min((ellmax1,len(cl_len)))]
        self.cl_noise = get_cl_noise(sN_uKamin,ell_max = ellmax1 - 1) # Flat noise for now. could change that
        self.bl = get_bl(beam_FWHM_amin,ell_max=ellmax1 - 1)

        self.sN_uKamin = sN_uKamin
        self.beam_FWHM_amin = beam_FWHM_amin
        self.verbose = verbose
        self.lib_dir = lib_dir

        if not os.path.exists(self.lib_dir) and pbs.rank == 0 :
            # Build ell matrix and save it for further use.
            try : os.mkdir(self.lib_dir)
            except :
                print '** failed to make directory :',lib_dir

        print "ffs_lencov: caching HD_ellmap"
        ky2 = ut.Freq(np.arange(self.HD_shape[0]), self.HD_shape[0]) ** 2 * np.pi
        ones = np.ones(self.HD_shape[0])
        freq_map = np.sqrt(np.outer(ky2, ones[0:self.HD_shape[1]/2 + 1]) + np.outer(ones, ky2[0:self.HD_shape[1]/2 + 1]))
        freq_map = np.uint16(np.round(freq_map - 0.5))  # map of integer ell. in int16 format.
        np.save(lib_dir + '/HDellmap.npy', freq_map)
        pbs.barrier()
Beispiel #10
0
from fslens.misc.lens_utils import enumerate_progress,mk_session_seed

parser = argparse.ArgumentParser(description='')
parser.add_argument('parfile', type=str, nargs=1)
parfile = parser.parse_args().parfile[0]


par = imp.load_source('par',parfile)

if pbs.rank == 0 :
    print "++++ Setup ok."
    print "I see ",pbs.size," MPI proc."
    if os.getenv("OMP_NUM_THREADS") is not None :
        print "I see ",int(os.getenv("OMP_NUM_THREADS"))," OMP NUM THREADS"
pbs.barrier()

def generate_sims(sim_lib, label=''):
    if pbs.rank  == 0 :
        for i, idx in enumerate_progress(np.arange(par.nsims)[::pbs.size], 'building sims:' + label):
            sim = sim_lib.get_sim(idx)
    else :
        for idx in np.arange(par.nsims)[pbs.rank::pbs.size] :
            sim = sim_lib.get_sim(idx)

# Generating lowest levels sims (in fact only storing the rng states) :
np.random.set_state(mk_session_seed(verbose = False))
for lib in par.base_libs:
    if lib is not None and not lib.is_full() : generate_sims(lib,label = lib.lib_dir)

# Performing lensing operations on sims :