Esempio n. 1
0
    def __init__(self, lib_dir, cltt, clte, clee, lmaxphi=2500, dL=10, lps=None):

        if lps is None:
            lps = [1]
            for l in range(2, 111, 10):
                lps.append(l)
            for l in range(lps[-1] + 30, 580, 30):
                lps.append(l)
            for l in range(lps[-1] + 100, lmaxphi // 2, 100):
                lps.append(l)
            for l in range(lps[-1] + 300, lmaxphi, 300):
                lps.append(l)
            if lps[-1] != lmaxphi:
                lps.append(lmaxphi)
            lps = np.array(lps)

        self.dL = dL
        self.lps = lps

        self.cltt = cltt
        self.clte = clte
        self.clee = clee

        self.lmaxphi = lps[-1]

        self.n1 = {}
        if not os.path.exists(lib_dir):
            os.makedirs(lib_dir)
        if not os.path.exists(os.path.join(lib_dir, 'n1_hash.pk')):
            pk.dump(self.hashdict(), open(os.path.join(lib_dir, 'n1_hash.pk'), 'wb'), protocol=2)
        hash_check(self.hashdict(), pk.load(open(os.path.join(lib_dir, 'n1_hash.pk'), 'rb')))
        self.npdb = sql.npdb(os.path.join(lib_dir, 'npdb.db'))
        self.fldb = sql.fldb(os.path.join(lib_dir, 'fldb.db'))

        self.lib_dir = lib_dir
Esempio n. 2
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())
Esempio n. 3
0
 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')))
Esempio n. 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())
Esempio n. 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())
Esempio n. 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])))
Esempio n. 7
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']
Esempio n. 8
0
    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
Esempio n. 9
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]
Esempio n. 10
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())
Esempio n. 11
0
    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
Esempio n. 12
0
    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'))
Esempio n. 13
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
Esempio n. 14
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())
Esempio n. 15
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())
Esempio n. 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',
                 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())