コード例 #1
0
    def __init__(self,
                 lib_dir,
                 sim_lib,
                 cinv_t,
                 cinv_p,
                 cl_weights,
                 soltn_lib=None):
        self.cinv_t = cinv_t
        self.cinv_p = cinv_p
        super(library_cinv_sepTP, self).__init__(lib_dir,
                                                 sim_lib,
                                                 cl_weights,
                                                 soltn_lib=soltn_lib)

        if mpi.rank == 0:
            fname_mask = os.path.join(self.lib_dir, "fmask.fits.gz")
            if not os.path.exists(fname_mask):
                fmask = self.cinv_t.get_fmask()
                assert np.all(fmask == self.cinv_p.get_fmask())
                hp.write_map(fname_mask, fmask)

        mpi.barrier()
        utils.hash_check(
            pk.load(open(os.path.join(lib_dir, "filt_hash.pk"), 'rb')),
            self.hashdict())
コード例 #2
0
ファイル: phas.py プロジェクト: markm42/plancklens
    def __init__(self, fname, idtype="INTEGER"):
        if not os.path.exists(fname) and mpi.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 STRING)" % idtype)
            con.commit()
        mpi.barrier()

        self.con = sqlite3.connect(fname, timeout=3600., detect_types=sqlite3.PARSE_DECLTYPES)
コード例 #3
0
ファイル: maps.py プロジェクト: markm42/plancklens
 def __init__(self, sims_cmb_len, cl_transf, nside=2048, lib_dir=None):
     self.sims_cmb_len = sims_cmb_len
     self.cl_transf = cl_transf
     self.nside = nside
     if lib_dir is not None:
         fn_hash = os.path.join(lib_dir, 'sim_hash.pk')
         if mpi.rank == 0 and not os.path.exists(fn_hash):
             pk.dump(self.hashdict(), open(fn_hash, 'wb'), protocol=2)
         mpi.barrier()
         hash_check(self.hashdict(), pk.load(open(fn_hash, 'rb')))
コード例 #4
0
    def __init__(self,
                 lib_dir,
                 lmax,
                 nside,
                 cl,
                 transf,
                 ninv,
                 pcf='default',
                 chain_descr=None):
        assert lib_dir is not None and lmax >= 1024 and nside >= 512, (lib_dir,
                                                                       lmax,
                                                                       nside)
        super(cinv_p, self).__init__(lib_dir, lmax)

        self.nside = nside
        self.cl = cl
        self.transf = transf
        self.ninv = ninv

        pcf = os.path.join(lib_dir, "dense.pk") if pcf == 'default' else None
        if chain_descr is None:            chain_descr = \
[[2, ["split(dense(" + pcf + "), 32, diag_cl)"], 512, 256, 3, 0.0, cd_solve.tr_cg,cd_solve.cache_mem()],
            [1, ["split(stage(2),  512, diag_cl)"], 1024, 512, 3, 0.0, cd_solve.tr_cg, cd_solve.cache_mem()],
            [0, ["split(stage(1), 1024, diag_cl)"], lmax, nside, np.inf, 1.0e-5, cd_solve.tr_cg, cd_solve.cache_mem()]]
        n_inv_filt = util.jit(opfilt_pp.alm_filter_ninv, ninv,
                              transf[0:lmax + 1])
        self.chain = util.jit(multigrid.multigrid_chain, opfilt_pp,
                              chain_descr, cl, n_inv_filt)

        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)

            if not os.path.exists(os.path.join(lib_dir, "filt_hash.pk")):
                pk.dump(self.hashdict(),
                        open(os.path.join(lib_dir, "filt_hash.pk"), 'wb'),
                        protocol=2)

            if not os.path.exists(os.path.join(self.lib_dir, "fbl.dat")):
                fel, fbl = self._calc_febl()
                np.savetxt(os.path.join(self.lib_dir, "fel.dat"), fel)
                np.savetxt(os.path.join(self.lib_dir, "fbl.dat"), fbl)

            if not os.path.exists(os.path.join(self.lib_dir, "tal.dat")):
                np.savetxt(os.path.join(self.lib_dir, "tal.dat"),
                           self._calc_tal())

            if not os.path.exists(os.path.join(self.lib_dir, "fmask.fits.gz")):
                hp.write_map(os.path.join(self.lib_dir, "fmask.fits.gz"),
                             self._calc_mask())

        mpi.barrier()
        utils.hash_check(
            pk.load(open(os.path.join(lib_dir, "filt_hash.pk"), 'rb')),
            self.hashdict())
コード例 #5
0
 def __init__(self, lib_dir, qcls_lib):
     self.lib_dir = lib_dir
     self.qclibs = qcls_lib
     hname = os.path.join(lib_dir, 'qeclav_hash.pk')
     if mpi.rank == 0:
         if not os.path.exists(lib_dir):
             os.makedirs(lib_dir)
         if not os.path.exists(hname):
             pk.dump(self.hashdict(), open(hname, 'wb'), protocol=2)
     mpi.barrier()
     utils.hash_check(pk.load(open(hname, 'rb')), self.hashdict())
コード例 #6
0
 def __init__(self, lib_dir, qcls_lib):
     self.lib_dir = lib_dir
     self.qclibs = qcls_lib
     hname = os.path.join(lib_dir, 'qeclav_hash.pk')
     if mpi.rank == 0:
         if not os.path.exists(lib_dir):
             os.makedirs(lib_dir)
         if not os.path.exists(hname):
             pk.dump(self.hashdict(), open(hname, 'wb'), protocol=2)
     mpi.barrier()
     utils.hash_check(pk.load(open(hname, 'rb')), self.hashdict())
     self.mc_sims_mf = np.sort(
         np.unique(np.concatenate([qcl.mc_sims_mf for qcl in self.qclibs])))
コード例 #7
0
ファイル: sql.py プロジェクト: pcampeti/plancklens
    def __init__(self, fname, idtype="STRING"):
        if not os.path.exists(fname) and mpi.rank == 0:
            con = sqlite3.connect(fname,
                                  detect_types=sqlite3.PARSE_DECLTYPES,
                                  timeout=3600)
            cur = con.cursor()
            cur.execute("CREATE TABLE fldb (id %s PRIMARY KEY, fl REAL)" %
                        idtype)
            con.commit()
        mpi.barrier()

        self.con = sqlite3.connect(fname,
                                   timeout=3600.,
                                   detect_types=sqlite3.PARSE_DECLTYPES)
コード例 #8
0
ファイル: phas.py プロジェクト: markm42/plancklens
    def __init__(self, lib_dir, get_state_func=np.random.get_state, nsims_max=None):
        if not os.path.exists(lib_dir) and mpi.rank == 0:
            os.makedirs(lib_dir)
        self.nmax = nsims_max
        fn_hash = os.path.join(lib_dir, 'sim_hash.pk')
        if mpi.rank == 0 and not os.path.exists(fn_hash):
            pk.dump(self.hashdict(), open(fn_hash, 'wb'), protocol=2)
        mpi.barrier()

        hsh = pk.load(open(fn_hash, 'rb'))
        utils.hash_check(hsh, self.hashdict(), ignore=['lib_dir'])

        self._rng_db = rng_db(os.path.join(lib_dir, 'rngdb.db'), idtype='INTEGER')
        self._get_rng_state = get_state_func
コード例 #9
0
    def __init__(self, lib_dir, ivfs1, ivfs2, nside, clte=None, lmax_qlm=None, resplib=None):
        if lmax_qlm is None:
            lmax_qlm = 3 * nside -1
        self.lib_dir = lib_dir
        self.prefix = lib_dir
        self.lmax_qlm = {'T': lmax_qlm, 'P': lmax_qlm, 'PS': lmax_qlm}
        if clte is None:
            self.f2map1 = lib_filt2map(ivfs1, nside)
            self.f2map2 = lib_filt2map(ivfs2, nside)
        else:
            self.f2map1 = lib_filt2map_sepTP(ivfs1, nside, clte)
            self.f2map2 = lib_filt2map_sepTP(ivfs2, nside, clte)
        assert self.lmax_qlm['T'] == self.lmax_qlm['P'], 'implement this'
        fnhash = os.path.join(self.lib_dir, "qe_sim_hash.pk")
        if (mpi.rank == 0) and (not os.path.exists(fnhash)):
            if not os.path.exists(self.lib_dir): os.makedirs(self.lib_dir)
            pk.dump(self.hashdict(), open(fnhash, 'wb'), protocol=2)
        mpi.barrier()

        ut.hash_check(pk.load(open(fnhash, 'rb')), self.hashdict())
        if mpi.rank == 0:
            if not os.path.exists(os.path.join(lib_dir, 'fskies.dat')):
                print("Caching sky fractions...")
                ms = {1: self.get_mask(1), 2: self.get_mask(2)}
                fskies = {}
                for i in [1, 2]:
                    for j in [1, 2][i - 1:]:
                        fskies[10 * i + j] = np.mean(ms[i] * ms[j])
                with open(os.path.join(lib_dir, 'fskies.dat'), 'w') as f:
                    for lab, _f in zip(np.sort(list(fskies.keys())),
                                       np.array(list(fskies.values()))[np.argsort(list(fskies.keys()))]):
                        f.write('%4s %.5f \n' % (lab, _f))
        mpi.barrier()
        fskies = {}
        with open(os.path.join(lib_dir, 'fskies.dat')) as f:
            for line in f:
                (key, val) = line.split()
                fskies[int(key)] = float(val)
        self.fskies = fskies
        self.fsky11 = fskies[11]
        self.fsky12 = fskies[12]
        self.fsky22 = fskies[22]

        self.resplib = resplib

        self.keys_fund = ['ptt', 'xtt', 'p_p', 'x_p', 'p', 'x', 'stt', 'ftt','f_p', 'f','dtt', 'ntt', 'a_p',
                          'pte', 'pet', 'ptb', 'pbt', 'pee', 'peb', 'pbe', 'pbb',
                          'xte', 'xet', 'xtb', 'xbt', 'xee', 'xeb', 'xbe', 'xbb']
        self.keys = self.keys_fund + ['p_tp', 'x_tp', 'p_te', 'p_tb', 'p_eb', 'x_te', 'x_tb', 'x_eb', 'ptt_bh_n',
                                      'ptt_bh_s', 'ptt_bh_f', 'ptt_bh_d', 'dtt_bh_p', 'stt_bh_p', 'ftt_bh_d']
コード例 #10
0
 def __init__(self, lib_dir, qeA, qeB, mc_sims_mf):
     self.lib_dir = lib_dir
     self.prefix = lib_dir
     self.qeA = qeA
     self.qeB = qeB
     self.mc_sims_mf = mc_sims_mf
     fsname = os.path.join(lib_dir, 'fskies.dat')
     hname = os.path.join(self.lib_dir, 'qcl_sim_hash.pk')
     if mpi.rank == 0:
         if not os.path.exists(lib_dir): os.makedirs(lib_dir)
         if not os.path.exists(fsname):
             print('Caching sky fractions...')
             ms = {
                 1: self.qeA.get_mask(1),
                 2: self.qeA.get_mask(2),
                 3: self.qeB.get_mask(1),
                 4: self.qeB.get_mask(2)
             }
             assert np.all([m.shape == ms[1].shape for m in ms.values()
                            ]), (m.shape for m in ms.values())
             fskies = {}
             for i in [1, 2, 3, 4]:
                 for j in [1, 2, 3, 4][i - 1:]:
                     fskies[10 * i + j] = np.mean(ms[i] * ms[j])
             fskies[1234] = np.mean(ms[1] * ms[2] * ms[3] * ms[4])
             with open(fsname, 'w') as f:
                 for lab, _f in zip(
                         np.sort(list(fskies.keys())),
                         np.array(list(fskies.values()))[np.argsort(
                             list(fskies.keys()))]):
                     f.write('%4s %.5f \n' % (lab, _f))
         if not os.path.exists(hname):
             if not os.path.exists(self.lib_dir):
                 os.makedirs(self.lib_dir)
             pk.dump(self.hashdict(), open(hname, 'wb'), protocol=2)
     mpi.barrier()
     utils.hash_check(pk.load(open(hname, 'rb')), self.hashdict())
     self.npdb = sql.npdb(os.path.join(lib_dir, 'cldb.db'))
     fskies = {}
     with open(fsname) as f:
         for line in f:
             (key, val) = line.split()
             fskies[int(key)] = float(val)
     self.fskies = fskies
     self.fsky1234 = fskies[1234]
     self.fsky11 = fskies[11]
     self.fsky12 = fskies[12]
     self.fsky22 = fskies[22]
コード例 #11
0
    def __init__(self, lib_dir, sim_lib, cl_weights, soltn_lib=None, cache=True):


        self.lib_dir = lib_dir
        self.sim_lib = sim_lib
        self.cl = cl_weights
        self.soltn_lib = soltn_lib
        self.cache = cache
        fn_hash = os.path.join(lib_dir, 'filt_hash.pk')
        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)
            if not os.path.exists(fn_hash):
                pk.dump(self.hashdict(), open(fn_hash, 'wb'), protocol=2)
        mpi.barrier()
        utils.hash_check(pk.load(open(fn_hash, 'rb')), self.hashdict())
コード例 #12
0
ファイル: qresp.py プロジェクト: markm42/plancklens
    def __init__(self, lib_dir, lmax_ivf, cls_weight, cls_cmb, fal, lmax_qlm):
        self.lmax_qe = lmax_ivf
        self.lmax_qlm = lmax_qlm
        self.cls_weight = cls_weight
        self.cls_cmb = cls_cmb
        self.fal = fal
        self.lib_dir = lib_dir

        fn_hash = os.path.join(lib_dir, 'resp_hash.pk')
        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)
            if not os.path.exists(fn_hash):
                pk.dump(self.hashdict(), open(fn_hash, 'wb'), protocol=2)
        mpi.barrier()
        ut.hash_check(pk.load(open(fn_hash, 'rb')), self.hashdict())
        self.npdb = sql.npdb(os.path.join(lib_dir, 'npdb.db'))
コード例 #13
0
ファイル: nhl.py プロジェクト: carronj/plancklens
    def __init__(self, lib_dir, ivfs, cls_weight, lmax_qlm, resplib=None):
        self.lmax_qlm = lmax_qlm
        self.cls_weight = cls_weight
        self.ivfs = ivfs
        fn_hash = os.path.join(lib_dir, 'nhl_hash.pk')
        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)
            if not os.path.exists(fn_hash):
                pk.dump(self.hashdict(), open(fn_hash, 'wb'), protocol=2)
        mpi.barrier()
        utils.hash_check(pk.load(open(fn_hash, 'rb')), self.hashdict())

        self.lib_dir = lib_dir
        self.npdb = sql.npdb(os.path.join(lib_dir, 'npdb.db'))
        self.fsky = np.mean(self.ivfs.get_fmask())
        self.resplib = resplib
コード例 #14
0
    def __init__(self,
                 lib_dir,
                 lmax,
                 cls_unl,
                 lib_pha=None,
                 dlmax=1024,
                 nside_lens=4096,
                 facres=0,
                 nbands=16,
                 verbose=True):
        if not os.path.exists(lib_dir) and mpi.rank == 0:
            os.makedirs(lib_dir)
        mpi.barrier()
        fields = _get_fields(cls_unl)

        if lib_pha is None and mpi.rank == 0:
            lib_pha = phas.lib_phas(os.path.join(lib_dir, 'phas'), len(fields),
                                    lmax + dlmax)
        else:  # Check that the lib_alms are compatible :
            assert lib_pha.lmax == lmax + dlmax
        mpi.barrier()

        self.lmax = lmax
        self.dlmax = dlmax
        # lenspyx parameters:
        self.nside_lens = nside_lens
        self.nbands = nbands
        self.facres = facres

        self.unlcmbs = sims_cmb_unl(cls_unl, lib_pha)
        self.lib_dir = lib_dir
        self.fields = _get_fields(cls_unl)

        fn_hash = os.path.join(lib_dir, 'sim_hash.pk')
        if mpi.rank == 0 and not os.path.exists(fn_hash):
            pk.dump(self.hashdict(), open(fn_hash, 'wb'), protocol=2)
        mpi.barrier()
        utils.hash_check(self.hashdict(), pk.load(open(fn_hash, 'rb')))
        try:
            import lenspyx
        except ImportError:
            print("Could not import lenspyx module")
            lenspyx = None
        self.lens_module = lenspyx
        self.verbose = verbose
コード例 #15
0
if args.ivt:
    jobs += [(idx, 't') for idx in range(args.imin, args.imax + 1)]
    if args.ds and args.imin >= 0:  #  Make data to avoid problems with ds librairies
        jobs += [(-1, 't')]
if args.ivp:
    jobs += [(idx, 'p') for idx in range(args.imin, args.imax + 1)]
    if args.ds and args.imin >= 0:  #  Make data to avoid problems with ds librairies
        jobs += [(-1, 'p')]
for i, (idx, lab) in enumerate(jobs[mpi.rank::mpi.size]):
    print('rank %s filtering sim %s %s, job %s in %s' %
          (mpi.rank, idx, lab, i, len(jobs)))
    if lab == 't':
        par.ivfs.get_sim_tlm(idx)
    elif lab == 'p':
        par.ivfs.get_sim_elm(idx)  # This will cache blm as well.
mpi.barrier()

# --- unnormalized QE calculation
qlibs = [par.qlms_dd] * args.dd + [par.qlms_ss] * args.ss + [par.qlms_ds
                                                             ] * args.ds
jobs = []
for qlib in qlibs:
    for k in args.k:
        jobs += [(qlib, idx, k) for idx in range(args.imin, args.imax + 1)]

for i, (qlib, idx, k) in enumerate(jobs[mpi.rank::mpi.size]):
    print('rank %s doing QE sim %s %s, qlm_lib %s, job %s in %s' %
          (mpi.rank, idx, k, qlib.lib_dir, i, len(jobs)))
    qlib.get_sim_qlm(k, idx)
mpi.barrier()
コード例 #16
0
    def __init__(self,
                 lib_dir,
                 lmax,
                 nside,
                 cl,
                 transf,
                 ninv,
                 marge_maps_t=(),
                 marge_monopole=False,
                 marge_dipole=False,
                 pcf='default',
                 rescal_cl='default'):
        """Instance for joint temperature-polarization filtering

            Here ninv is a  list of lists with mask paths and / or inverse pixel noise levels.
            TT, (QQ + UU) / 2 if len(ninv) == 2 or TT, QQ, QU UU if == 4
            e.g. [[iNevT,mask1,mask2,..],[iNevP,mask1,mask2...]]


        """
        assert (lmax >= 1024)
        assert (nside >= 512)
        assert len(
            ninv) == 2 or len(ninv) == 4  # TT, (QQ + UU)/2 or TT,QQ,QU,UU

        if rescal_cl == 'default':
            rescal_cl = np.sqrt(
                np.arange(lmax + 1, dtype=float) *
                np.arange(1, lmax + 2, dtype=float) / 2. / np.pi)
        elif rescal_cl is None:
            rescal_cl = np.ones(lmax + 1, dtype=float)
        dl = {k: rescal_cl**2 * cl[k][:lmax + 1]
              for k in cl.keys()}  # rescaled cls (Dls by default)
        transf_dl = transf[:lmax + 1] * utils.cli(rescal_cl)

        self.lmax = lmax
        self.nside = nside
        self.cl = cl
        self.transf = transf
        self.ninv = ninv
        self.marge_maps_t = marge_maps_t
        self.marge_maps_p = []

        self.lib_dir = lib_dir
        self.rescal_cl = rescal_cl

        pcf = lib_dir + "/dense_tp.pk" if pcf == 'default' else None
        chain_descr = [[
            3, ["split(dense(" + pcf + "), 64, diag_cl)"], 256, 128, 3, 0.0,
            cd_solve.tr_cg,
            cd_solve.cache_mem()
        ],
                       [
                           2, ["split(stage(3),  256, diag_cl)"], 512, 256, 3,
                           0.0, cd_solve.tr_cg,
                           cd_solve.cache_mem()
                       ],
                       [
                           1, ["split(stage(2),  512, diag_cl)"], 1024, 512, 3,
                           0.0, cd_solve.tr_cg,
                           cd_solve.cache_mem()
                       ],
                       [
                           0, ["split(stage(1), 1024, diag_cl)"], lmax, nside,
                           np.inf, 1.0e-5, cd_solve.tr_cg,
                           cd_solve.cache_mem()
                       ]]

        n_inv_filt = util.jit(opfilt_tp.alm_filter_ninv,
                              ninv,
                              transf_dl,
                              marge_maps_t=marge_maps_t,
                              marge_monopole=marge_monopole,
                              marge_dipole=marge_dipole)
        self.chain = util.jit(multigrid.multigrid_chain, opfilt_tp,
                              chain_descr, dl, n_inv_filt)

        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)

            if not os.path.exists(os.path.join(lib_dir, "filt_hash.pk")):
                pk.dump(self.hashdict(),
                        open(os.path.join(lib_dir, "filt_hash.pk"), 'wb'),
                        protocol=2)

            # if (not os.path.exists(self.lib_dir + "/fbl.dat")):
            #    fel, fbl = self.calc_febl()
            #    fel.write(self.lib_dir + "/fel.dat", lambda l: 1.0)
            #    fbl.write(self.lib_dir + "/fbl.dat", lambda l: 1.0)

            # if (not os.path.exists(self.lib_dir + "/tal.dat")):
            #    tal = self.calc_tal()
            #    tal.write(self.lib_dir + "/tal.dat", lambda l: 1.0)

            if not os.path.exists(os.path.join(self.lib_dir, "fmask.fits.gz")):
                fmask = self.calc_mask()
                hp.write_map(os.path.join(self.lib_dir, "fmask.fits.gz"),
                             fmask)

        mpi.barrier()
        utils.hash_check(
            pk.load(open(os.path.join(lib_dir, "filt_hash.pk"), 'rb')),
            self.hashdict())
コード例 #17
0
ファイル: filt_cinv.py プロジェクト: markm42/plancklens
    def __init__(self,
                 lib_dir,
                 lmax,
                 nside,
                 cl,
                 transf,
                 ninv,
                 marge_maps_t=(),
                 marge_monopole=False,
                 marge_dipole=False,
                 pcf='default',
                 rescal_cl='default',
                 chain_descr=None,
                 transf_p=None):
        """Instance for joint temperature-polarization filtering

            Args:
                lib_dir: a few quantities might get cached there
                lmax: CMB filtering performed up to multipole lmax
                nside: healpy resolution of the input maps
                cl: fiducial CMB spectra used to filter the data (dict with 'tt', 'te', 'ee', 'bb' keys)
                transf: CMB transfer function in temperature
                ninv: list of lists with mask paths and / or inverse pixel noise levels.
                        TT, (QQ + UU) / 2 if len(ninv) == 2 or TT, QQ, QU UU if == 4
                        e.g. [[iNevT,mask1,mask2,..],[iNevP,mask1,mask2...]]
                marge_maps_t: maps to project out in the filtering (T-part)
                marge_monopole: marginalizes out the T monopole if set
                marge_dipole: marginalizes out the T dipole if set

                chain_descr: preconditioner mulitgrid chain description (if different from default)
                transf_p: polarization transfer function (if different from temperature)


        """
        assert (lmax >= 1024)
        assert (nside >= 512)
        assert len(
            ninv) == 2 or len(ninv) == 4  # TT, (QQ + UU)/2 or TT,QQ,QU,UU

        if rescal_cl == 'default':
            rescal_cl = {
                a: np.sqrt(
                    np.arange(lmax + 1, dtype=float) *
                    np.arange(1, lmax + 2, dtype=float) / 2. / np.pi)
                for a in ['t', 'e', 'b']
            }
        elif rescal_cl is None:
            rescal_cl = {
                a: np.ones(lmax + 1, dtype=float)
                for a in ['t', 'e', 'b']
            }
        elif rescal_cl == 'tonly':
            rescal_cl = {a: np.ones(lmax + 1, dtype=float) for a in ['e', 'b']}
            rescal_cl['t'] = np.sqrt(
                np.arange(lmax + 1, dtype=float) *
                np.arange(1, lmax + 2, dtype=float) / 2. / np.pi)
        else:
            assert 0
        for k in rescal_cl.keys():
            rescal_cl[k] /= np.mean(
                rescal_cl[k]
            )  # in order not mess around with the TEB relative weights of the spectra
        dl = {
            k: rescal_cl[k[0]] * rescal_cl[k[1]] * cl[k][:lmax + 1]
            for k in cl.keys()
        }  # rescaled cls (Dls by default)
        if transf_p is None:
            transf_p = transf
        transf_dls = {
            a: transf_p[:lmax + 1] * utils.cli(rescal_cl[a])
            for a in ['e', 'b']
        }
        transf_dls['t'] = transf[:lmax + 1] * utils.cli(rescal_cl['t'])

        self.lmax = lmax
        self.nside = nside
        self.cl = cl
        self.transf_t = transf
        self.transf_p = transf_p
        self.ninv = ninv
        self.marge_maps_t = marge_maps_t
        self.marge_maps_p = []

        self.lib_dir = lib_dir
        self.rescal_cl = rescal_cl

        if chain_descr is None:
            pcf = lib_dir + "/dense_tp.pk" if pcf == 'default' else None
            chain_descr = [[
                3, ["split(dense(" + pcf + "), 64, diag_cl)"], 256, 128, 3,
                0.0, cd_solve.tr_cg,
                cd_solve.cache_mem()
            ],
                           [
                               2, ["split(stage(3),  256, diag_cl)"], 512, 256,
                               3, 0.0, cd_solve.tr_cg,
                               cd_solve.cache_mem()
                           ],
                           [
                               1, ["split(stage(2),  512, diag_cl)"], 1024,
                               512, 3, 0.0, cd_solve.tr_cg,
                               cd_solve.cache_mem()
                           ],
                           [
                               0, ["split(stage(1), 1024, diag_cl)"], lmax,
                               nside, np.inf, 1.0e-5, cd_solve.tr_cg,
                               cd_solve.cache_mem()
                           ]]

        n_inv_filt = util.jit(opfilt_tp.alm_filter_ninv,
                              ninv,
                              transf_dls['t'],
                              b_transf_e=transf_dls['e'],
                              b_transf_b=transf_dls['b'],
                              marge_maps_t=marge_maps_t,
                              marge_monopole=marge_monopole,
                              marge_dipole=marge_dipole)
        self.chain = util.jit(multigrid.multigrid_chain, opfilt_tp,
                              chain_descr, dl, n_inv_filt)

        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)

            if not os.path.exists(os.path.join(lib_dir, "filt_hash.pk")):
                pk.dump(self.hashdict(),
                        open(os.path.join(lib_dir, "filt_hash.pk"), 'wb'),
                        protocol=2)

            if not os.path.exists(os.path.join(lib_dir, "fal.pk")):
                pk.dump(self._calc_fal(),
                        open(os.path.join(lib_dir, "fal.pk"), 'wb'),
                        protocol=2)

            if not os.path.exists(os.path.join(self.lib_dir, "fmask.fits.gz")):
                fmask = self.calc_mask()
                hp.write_map(os.path.join(self.lib_dir, "fmask.fits.gz"),
                             fmask)

        mpi.barrier()
        utils.hash_check(
            pk.load(open(os.path.join(lib_dir, "filt_hash.pk"), 'rb')),
            self.hashdict())
コード例 #18
0
ファイル: n1.py プロジェクト: markm42/plancklens
    def get_n1(self, kA, k_ind, cl_kind, ftlA, felA, fblA, Lmax, kB=None, ftlB=None, felB=None, fblB=None,
               clttfid=None, cltefid=None, cleefid=None, n1_flat=lambda ell: np.ones(len(ell), dtype=float),
               recache=False, remove_only=False, sglLmode=True):
        r"""Calls a N1 bias

            Args:
                kA: qe_key of QE spectrum first leg
                k_ind: anisotropy source key ('p', for standard lensing N1)
                cl_kind: spectrum of anisotropy source ('p', for standard lensing N1)
                ftlA: first leg T-filtering isotropic approximation
                      (typically :math:`\frac{1}{C_\ell^{TT} + N_\ell^{TT}}`)
                felA: first leg E-filtering isotropic approximation
                      (typically :math:`\frac{1}{C_\ell^{EE} + N_\ell^{EE}}`)
                fblA: first leg B-filtering isotropic approximation
                     (typically :math:`\frac{1}{C_\ell^{BB} + N_\ell^{BB}}`)
                Lmax: maximum multipole of output N1
                kB(optional): qe_key of QE spectrum second leg (if different from the first)
                ftlB(optional): second leg T-filtering isotropic approximation (if different from the first)
                felB(optional): second leg E-filtering isotropic approximation (if different from the first)
                fblB(optional): second leg B-filtering isotropic approximation (if different from the first)
                clttfid(optional): CMB TT spectrum used in QE weights (if different from instance cltt for map-level CMB spectrum)
                cltefid(optional): CMB TE spectrum used in QE weights (if different from instance clte for map-level CMB spectrum)
                cleefid(optional): CMB EE spectrum used in QE weights (if different from instance clee for map-level CMB spectrum)
                n1_flat(optional): function used to flatten the discretized output before returning splined entire array

            Returns:
                N1 bias in the form of a numpy array of size Lmax + 1

            Note:
                This can be called with MPI using a number of processes; in this case the calculations for each multipole will be distributed among these.

        """
        if kB is None: kB = kA
        # FIXME:
        if kA[0] == 's' or kB[0] == 's':
            assert kA[0] == kB[0], 'point source implented following DH gradient convention, you wd probably need to pick a sign there'
        if ftlB is None: ftlB = ftlA
        if felB is None: felB = felA
        if fblB is None: fblB = fblA

        clttfid = self.cltt if clttfid is None else clttfid
        cltefid = self.clte if cltefid is None else cltefid
        cleefid = self.clee if cleefid is None else cleefid


        if kA in estimator_keys and kB in estimator_keys:
            if kA < kB:
                return self.get_n1(kB, k_ind, cl_kind, ftlB, felB, fblB, Lmax, ftlB=ftlA, felB=felA, fblB=fblA, kB=kA,
                                   clttfid=clttfid, cltefid=cltefid, cleefid=cleefid, n1_flat=n1_flat, sglLmode=sglLmode)

            idx = 'splined_kA' + kA + '_kB' + kB + '_ind' + k_ind
            idx += '_clpp' + clhash(cl_kind)
            idx += '_ftlA' + clhash(ftlA)
            idx += '_felA' + clhash(felA)
            idx += '_fblA' + clhash(fblA)
            idx += '_ftlB' + clhash(ftlB)
            idx += '_felB' + clhash(felB)
            idx += '_fblB' + clhash(fblB)
            idx += '_clttfid' + clhash(clttfid)
            idx += '_cltefid' + clhash(cltefid)
            idx += '_cleefid' + clhash(cleefid)
            idx += '_Lmax%s' % Lmax

            ret = self.npdb.get(idx)
            if ret is not None:
                if not recache and not remove_only:
                    return ret
                else:
                    self.npdb.remove(idx)
                    if remove_only:
                        return np.zeros_like(ret)
                    ret = None
            if ret is None:
                Ls = np.unique(np.concatenate([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], np.arange(1, Lmax + 1)[::20], [Lmax]]))
                if sglLmode:
                    n1L = np.zeros(len(Ls), dtype=float)
                    for i, L in enumerate(Ls[mpi.rank::mpi.size]):
                        print("n1: doing L %s kA %s kB %s kind %s" % (L, kA, kB, k_ind))
                        n1L[i] = (self._get_n1_L(L, kA, kB, k_ind, cl_kind, ftlA, felA, fblA, ftlB, felB, fblB, clttfid, cltefid, cleefid, remove_only=remove_only))
                    if mpi.size > 1:
                        mpi.barrier()
                        for i, L in enumerate(Ls): # reoading cached n1L's
                            n1L[i] = (self._get_n1_L(L, kA, kB, k_ind, cl_kind, ftlA, felA, fblA, ftlB, felB, fblB, clttfid,
                                             cltefid, cleefid, remove_only=remove_only))

                else: # entire vector from f90 openmp call
                    lmin_ftlA = np.min([np.where(np.abs(fal) > 0.)[0] for fal in [ftlA, felA, fblA]])
                    lmin_ftlB = np.min([np.where(np.abs(fal) > 0.)[0] for fal in [ftlB, felB, fblB]])
                    n1L = n1f.n1(Ls, cl_kind, kA, kB, k_ind, self.cltt, self.clte, self.clee,
                                 clttfid, cltefid, cleefid,  ftlA, felA, fblA, ftlB, felB, fblB,
                                  lmin_ftlA, lmin_ftlB,  self.dL, self.lps)

                ret = np.zeros(Lmax + 1)
                ret[1:] =  spline(Ls, np.array(n1L) * n1_flat(Ls), s=0., ext='raise', k=3)(np.arange(1, Lmax + 1) * 1.)
                ret[1:] *= cli(n1_flat(np.arange(1, Lmax + 1) * 1.))
                self.npdb.add(idx, ret)
                return ret
            return self.npdb.get(idx)

        if (kA in estimator_keys_derived) and (kB in estimator_keys_derived):
            ret = 0.
            for (tk1, cl1) in _get_est_derived(kA, Lmax):
                for (tk2, cl2) in _get_est_derived(kB, Lmax):
                    tret = self.get_n1(tk1, k_ind, cl_kind, ftlA, felA, fblA, Lmax, ftlB=ftlB, felB=felB, fblB=fblB,
                                       clttfid=clttfid, cltefid=cltefid, cleefid=cleefid,
                                       kB=tk2, n1_flat=n1_flat, sglLmode=sglLmode)
                    tret *= cl1[:Lmax + 1]
                    tret *= cl2[:Lmax + 1]
                    ret += tret
            return ret
        elif (kA in estimator_keys_derived) and (kB in estimator_keys):
            ret = 0.
            for (tk1, cl1) in _get_est_derived(kA, Lmax):
                tret = self.get_n1(tk1, k_ind, cl_kind, ftlA, felA, fblA, Lmax, ftlB=ftlB, felB=felB, fblB=fblB, kB=kB,
                                   clttfid=clttfid, cltefid=cltefid, cleefid=cleefid,
                                   n1_flat=n1_flat, sglLmode=sglLmode)
                tret *= cl1[:Lmax + 1]
                ret += tret
            return ret
        elif (kA in estimator_keys) and (kB in estimator_keys_derived):
            ret = 0.
            for (tk2, cl2) in _get_est_derived(kB, Lmax):
                tret = self.get_n1(kA, k_ind, cl_kind, ftlA, felA, fblA, Lmax, ftlB=ftlB, felB=felB, fblB=fblB, kB=tk2,
                                   clttfid=clttfid, cltefid=cltefid, cleefid=cleefid,
                                   n1_flat=n1_flat, sglLmode=sglLmode)
                tret *= cl2[:Lmax + 1]
                ret += tret
            return ret
        assert 0
コード例 #19
0
    def __init__(self,
                 lib_dir,
                 lmax,
                 nside,
                 cl,
                 transf,
                 ninv,
                 marge_monopole=True,
                 marge_dipole=True,
                 marge_maps=(),
                 pcf='default',
                 chain_descr=None):

        assert lib_dir is not None and lmax >= 1024 and nside >= 512, (lib_dir,
                                                                       lmax,
                                                                       nside)
        assert isinstance(ninv, list)
        super(cinv_t, self).__init__(lib_dir, lmax)

        self.nside = nside
        self.cl = cl
        self.transf = transf
        self.ninv = ninv
        self.marge_monopole = marge_monopole
        self.marge_dipole = marge_dipole
        self.marge_maps = marge_maps

        pcf = os.path.join(
            lib_dir, "dense.pk"
        ) if pcf == 'default' else ''  # Dense matrices will be cached there.
        if chain_descr is None:            chain_descr = \
[[3, ["split(dense(" + pcf + "), 64, diag_cl)"], 256, 128, 3, 0.0, cd_solve.tr_cg, cd_solve.cache_mem()],
            [2, ["split(stage(3),  256, diag_cl)"], 512, 256, 3, 0.0, cd_solve.tr_cg, cd_solve.cache_mem()],
            [1, ["split(stage(2),  512, diag_cl)"], 1024, 512, 3, 0.0, cd_solve.tr_cg, cd_solve.cache_mem()],
            [0, ["split(stage(1), 1024, diag_cl)"], lmax, nside, np.inf, 1.0e-5, cd_solve.tr_cg, cd_solve.cache_mem()]]

        n_inv_filt = util.jit(opfilt_tt.alm_filter_ninv,
                              ninv,
                              transf[0:lmax + 1],
                              marge_monopole=marge_monopole,
                              marge_dipole=marge_dipole,
                              marge_maps=marge_maps)
        self.chain = util.jit(multigrid.multigrid_chain, opfilt_tt,
                              chain_descr, cl, n_inv_filt)
        if mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)

            if not os.path.exists(os.path.join(lib_dir, "filt_hash.pk")):
                pk.dump(self.hashdict(),
                        open(os.path.join(lib_dir, "filt_hash.pk"), 'wb'),
                        protocol=2)

            if not os.path.exists(os.path.join(self.lib_dir, "ftl.dat")):
                np.savetxt(os.path.join(self.lib_dir, "ftl.dat"),
                           self._calc_ftl())

            if not os.path.exists(os.path.join(self.lib_dir, "tal.dat")):
                np.savetxt(os.path.join(self.lib_dir, "tal.dat"),
                           self._calc_tal())

            if not os.path.exists(os.path.join(self.lib_dir, "fmask.fits.gz")):
                hp.write_map(os.path.join(self.lib_dir, "fmask.fits.gz"),
                             self._calc_mask())

        mpi.barrier()
        utils.hash_check(
            pk.load(open(os.path.join(lib_dir, "filt_hash.pk"), 'rb')),
            self.hashdict())