Exemple #1
0
    def __init__(self, cl_unl, cl_len, ivfs1, lib_dir=None, ivfs2=None, npad=2):
        """ library for fetching quadratic estimator results, designed to make it easy to combine quadratic estimators.
                cl_unl = spec.camb_clfile object containing unlensed theory power spectra.
                cl_len = spec.camb_clfile object containing lensed theory power spectra.
                ivfs1  = a sims.ivf.library object which which provides the first leg for the quadratic estimators.
                (optional) lib_dir = directory to store hash for this object, as well as cached estimator mean-fields.
                (optional) ivfs2   = a sims.ivf.library object which provides the second leg for the quadratic estimators. defaults to ivf1.
                (optional) npad    = padding to use for convolutions used when computing quadratic estimators.
        """
        
        self.cl_unl     = cl_unl
        self.cl_len     = cl_len
        self.ivfs1      = ivfs1
        self.lib_dir    = lib_dir
        
        if ivfs2 == None:
            ivfs2 = ivfs1
        self.ivfs2      = ivfs2
        
        self.npad       = npad
        
        self.qes        = {} # estimators
        self.qfs        = {} # estimator fields
        self.qrs        = {} # estimator responses

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

                if not os.path.exists(lib_dir + "/sim_hash.pk"):
                    pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )
            ql.mpi.barrier()
            util.hash_check( pk.load( open(lib_dir + "/sim_hash.pk", 'r') ), self.hashdict() )
Exemple #2
0
    def __init__(self, pix, transf, sky_lib, lib_dir, nlev_t=0.0, nlev_p=0.0, phase=None):
        """ initialize the library.
             pix     = a maps.pix object with the sky pixelization.
             transf  = a transfer function (with a multiplication operation defined for maps.tebbft),
                       usually describing the effect of an instrumental beam.
             sky_lib = a library object with a get_sim_tqu() method which provides the sky map realizations.
             lib_dir = directory to store the hash for the library.
             nlev_t  = temperature noise level. either a scalar or an map-sized 2D array.
                       noise realizations in each temperature pixel are given by nlev_t * standard normal.
             nlev_p  = polarization noise level. either a scalar or an map-sized 2D array.
                       noise realizations in each polarization pixel are given by nlev_p * standard normal.
             (optional) phase = phas.library object used to control the random number stream.
        """
        self.pix     = pix
        self.transf  = transf
        self.sky_lib = sky_lib
        self.lib_dir = lib_dir
        self.phase   = phase

        self.nlev_t  = nlev_t
        self.nlev_p  = nlev_p

        if (self.phase == None):
            self.phase = phas.library( 3*pix.nx*pix.ny, lib_dir = self.lib_dir + "/phase" )

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

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )
        ql.mpi.barrier()
        util.hash_check( pk.load( open(lib_dir + "/sim_hash.pk", 'r') ), self.hashdict() )
Exemple #3
0
    def __init__(self, pix, cl_unl, lib_dir, phase=None):
        """ initialize the library.
             pix     = a maps.pix object with the sky pixelization.
             cl_unl  = a spec.camb_clfile object containing the unlensed CMB temperature+polarization power spectra.
             lib_dir = directory to store the hash for the library.
             (optional) phase = phas.library object used to control the random number stream.
        """
        self.pix = pix
        self.cl_unl = cl_unl
        self.lib_dir = lib_dir
        self.phase = phase

        if self.phase == None:
            lmax = cl_unl.lmax
            self.phase = phas.library(8 * pix.nx * (pix.ny / 2 + 1),
                                      lib_dir=self.lib_dir + "/phase")

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

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump(self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w'))
        ql.mpi.barrier()
        util.hash_check(pk.load(open(lib_dir + "/sim_hash.pk", 'r')),
                        self.hashdict())
Exemple #4
0
    def __init__(self, lib_dir):
        self.lib_dir = lib_dir

        if not os.path.exists(lib_dir):
            os.makedirs(lib_dir)

        if not os.path.exists(lib_dir + "/sim_hash.pk"):
            pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )
        util.hash_check( pk.load( open(lib_dir + "/sim_hash.pk", 'r') ), self.hashdict() )
Exemple #5
0
    def __init__(self, obs_lib, lib_dir=None):
        """ initialize the inverse-variance filter.
                obs_lib            = library object (likely from sims.obs) which a get_sim_tqu(i) method for returning the map for simulation i.
                (optional) lib_dir = directory to store the hash of this object and likely cache files as well.
        """
        self.obs_lib = obs_lib
        
        if lib_dir != None:
            if ql.mpi.rank == 0:
                if not os.path.exists(lib_dir):
                    os.makedirs(lib_dir)

                if not os.path.exists(lib_dir + "/sim_hash.pk"):
                    pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )
            ql.mpi.barrier()
            util.hash_check( pk.load( open(lib_dir + "/sim_hash.pk", 'r') ), self.hashdict() )
Exemple #6
0
    def __init__(self, obs_lib, lib_dir=None):
        """ initialize the inverse-variance filter.
                obs_lib            = library object (likely from sims.obs) which a get_sim_tqu(i) method for returning the map for simulation i.
                (optional) lib_dir = directory to store the hash of this object and likely cache files as well.
        """
        self.obs_lib = obs_lib

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

                if not os.path.exists(lib_dir + "/sim_hash.pk"):
                    pk.dump(self.hashdict(), open(lib_dir + "/sim_hash.pk", "w"))
            ql.mpi.barrier()
            util.hash_check(pk.load(open(lib_dir + "/sim_hash.pk", "r")), self.hashdict())
Exemple #7
0
    def __init__(self,
                 pix,
                 transf,
                 sky_lib,
                 lib_dir,
                 nlev_t=0.0,
                 nlev_p=0.0,
                 phase=None):
        """ initialize the library.
             pix     = a maps.pix object with the sky pixelization.
             transf  = a transfer function (with a multiplication operation defined for maps.tebbft),
                       usually describing the effect of an instrumental beam.
             sky_lib = a library object with a get_sim_tqu() method which provides the sky map realizations.
             lib_dir = directory to store the hash for the library.
             nlev_t  = temperature noise level. either a scalar or an map-sized 2D array.
                       noise realizations in each temperature pixel are given by nlev_t * standard normal.
             nlev_p  = polarization noise level. either a scalar or an map-sized 2D array.
                       noise realizations in each polarization pixel are given by nlev_p * standard normal.
             (optional) phase = phas.library object used to control the random number stream.
        """
        self.pix = pix
        self.transf = transf
        self.sky_lib = sky_lib
        self.lib_dir = lib_dir
        self.phase = phase

        self.nlev_t = nlev_t
        self.nlev_p = nlev_p

        if (self.phase == None):
            self.phase = phas.library(3 * pix.nx * pix.ny,
                                      lib_dir=self.lib_dir + "/phase")

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

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump(self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w'))
        ql.mpi.barrier()
        util.hash_check(pk.load(open(lib_dir + "/sim_hash.pk", 'r')),
                        self.hashdict())
Exemple #8
0
    def __init__(self, size, lib_dir, seed=None, random=np.random.standard_normal):
        """ initialize the library.
             * size              = size of array to be simulated for for each index.
             * lib_dir           = directory to store information on the random state for each index.
             * (optional) seed   = np.random seed for the 0th index. if set to None, then seed will
                                   be initialized from clock after prompting user to enter a keystroke.
             * (optional) random = function random(size) for calling the random number generator.
        """
        self.size    = size
        self.lib_dir = lib_dir
        self.random  = random

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

            if not os.path.exists(lib_dir + "/state_%04d.pk"%0):
                # intialize seed.
                if seed != None:
                    np.random.seed(seed)
                else:
                    keyseed = raw_input("quicklens::sims::phas: enter several random strokes on the keyboard followed by enter to initialize the random seed.\n")
                    assert(len(keyseed) > 0)
                    #Ensure that seed is less than or equal to 2**32
                    np.random.seed(np.abs(int(hash(keyseed)))%(2**32))

                # store the current state.
                pk.dump( np.random.get_state(), open(lib_dir + "/state_%04d.pk"%0, 'w') )

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )

        mpi.barrier()
        util.hash_check( pk.load( open(lib_dir + "/sim_hash.pk", 'r') ), self.hashdict() )

        if seed != None:
            np.random.seed(seed)
            self.random(size=self.size)
            self.check_state_final(0)
Exemple #9
0
    def __init__(self, pix, cl_unl, lib_dir, phase=None):
        """ initialize the library.
             pix     = a maps.pix object with the sky pixelization.
             cl_unl  = a spec.camb_clfile object containing the unlensed CMB temperature+polarization power spectra.
             lib_dir = directory to store the hash for the library.
             (optional) phase = phas.library object used to control the random number stream.
        """
        self.pix     = pix
        self.cl_unl  = cl_unl
        self.lib_dir = lib_dir
        self.phase   = phase

        if self.phase == None:
            lmax = cl_unl.lmax
            self.phase = phas.library( 8*pix.nx*(pix.ny/2+1), lib_dir = self.lib_dir + "/phase" )

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

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )
        ql.mpi.barrier()
        util.hash_check( pk.load( open(lib_dir + "/sim_hash.pk", 'r') ), self.hashdict() )
Exemple #10
0
    def __init__(self, qeA, lib_dir, qeB=None, mc_sims_mf=None, npad=2, lmax_lcl=5000, maxcache=True):
        """ initialize the qecl library.
                qeA     = sims.qest.library object providing the first quadratic estimator.
                lib_dir = directory to store the hash of this object, as well as cached spectra.
                (optional) qeB        =
                (optional) mc_sims_mf = simulations to use when calculating the mean-fields.
                                        to avoid monte carlo noise bias, different sims are always
                                        used for mfA and mfB. these can be specified explicitly by
                                        passing a tuple (mc_sims_mfA, mc_sims_mfB). if a single array
                                        is passed then instead (mc_sims_mf[::2], mc_sims_mf[1::2]) is
                                        used. if mc_sims_mf=None then no mean-field subtraction is
                                        performed.
                (optional) npad       = padding factor to apply when calculating the semi-analytical N0 bias.
                (optional) maxcache   = aggressively cache to disk when calculating N0 biases.
        """
        if not qeB:
            qeB = qeA

        self.qeA         = qeA
        self.qeB         = qeB
        self.npad        = npad
        self.lmax_lcl    = lmax_lcl
        self.maxcache    = maxcache

        self.lib_dir     = lib_dir

        if isinstance(mc_sims_mf, tuple):
            self.mc_sims_mfA, self.mc_sims_mfB = mc_sims_mf
        else:
            if mc_sims_mf == None:
                self.mc_sims_mfA = None
                self.mc_sims_mfB = None
            else:
                self.mc_sims_mfA = mc_sims_mf[0::2].copy()
                self.mc_sims_mfB = mc_sims_mf[1::2].copy()
        
        if ql.mpi.rank == 0:
            if not os.path.exists(lib_dir):
                os.makedirs(lib_dir)

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump( self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w') )

            # calculate several heuristics for the sky overlap between
            # qeA.ivfs and qeB.ivfs, to be used for correcting the spectra.
            if not os.path.exists(lib_dir + "/qecl_fcut.dat"):
                mask1, mask2 = [ m.flatten() for m in self.qeA.get_fmasks() ]
                mask3, mask4 = [ m.flatten() for m in self.qeB.get_fmasks() ]

                shape = mask1.shape
                assert( shape == mask2.shape )
                assert( shape == mask3.shape )
                assert( shape == mask4.shape )
                npix = mask1.size

                fcut11 = np.sum( mask1**2 ) / npix
                fcut12 = np.sum( mask1 * mask2 ) / npix
                fcut13 = np.sum( mask1 * mask3 ) / npix
                fcut14 = np.sum( mask1 * mask4 ) / npix
                fcut22 = np.sum( mask2**2 ) / npix
                fcut23 = np.sum( mask2 * mask3 ) / npix
                fcut24 = np.sum( mask2 * mask4 ) / npix
                fcut33 = np.sum( mask3**2 ) / npix
                fcut34 = np.sum( mask3 * mask4 ) / npix
                fcut44 = np.sum( mask4**2 ) / npix

                fcut1234 = np.sum( mask1 * mask2 * mask3 * mask4 ) / npix

                np.savetxt( lib_dir.format(prefix="outputs") + "/qecl_fcut.dat",
                            [ fcut1234,
                              fcut11, fcut12, fcut13, fcut14,
                              fcut22, fcut23, fcut24,
                              fcut33, fcut34,
                              fcut44 ] )
        ql.mpi.barrier()
        util.hash_check( pk.load( open(lib_dir.format(prefix="outputs") + "/sim_hash.pk", 'r') ), self.hashdict() )

        [ self.fcut1234,
          self.fcut11, self.fcut12, self.fcut13, self.fcut14,
          self.fcut22, self.fcut23, self.fcut24,
          self.fcut33, self.fcut34,
          self.fcut44 ] = np.loadtxt(lib_dir.format(prefix="outputs") + "/qecl_fcut.dat")
Exemple #11
0
    def __init__(self,
                 qeA,
                 lib_dir,
                 qeB=None,
                 mc_sims_mf=None,
                 npad=2,
                 lmax_lcl=5000,
                 maxcache=True):
        """ initialize the qecl library.
                qeA     = sims.qest.library object providing the first quadratic estimator.
                lib_dir = directory to store the hash of this object, as well as cached spectra.
                (optional) qeB        =
                (optional) mc_sims_mf = simulations to use when calculating the mean-fields.
                                        to avoid monte carlo noise bias, different sims are always
                                        used for mfA and mfB. these can be specified explicitly by
                                        passing a tuple (mc_sims_mfA, mc_sims_mfB). if a single array
                                        is passed then instead (mc_sims_mf[::2], mc_sims_mf[1::2]) is
                                        used. if mc_sims_mf=None then no mean-field subtraction is
                                        performed.
                (optional) npad       = padding factor to apply when calculating the semi-analytical N0 bias.
                (optional) maxcache   = aggressively cache to disk when calculating N0 biases.
        """
        if not qeB:
            qeB = qeA

        self.qeA = qeA
        self.qeB = qeB
        self.npad = npad
        self.lmax_lcl = lmax_lcl
        self.maxcache = maxcache

        self.lib_dir = lib_dir

        if isinstance(mc_sims_mf, tuple):
            self.mc_sims_mfA, self.mc_sims_mfB = mc_sims_mf
        else:
            if mc_sims_mf == None:
                self.mc_sims_mfA = None
                self.mc_sims_mfB = None
            else:
                self.mc_sims_mfA = mc_sims_mf[0::2].copy()
                self.mc_sims_mfB = mc_sims_mf[1::2].copy()

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

            if not os.path.exists(lib_dir + "/sim_hash.pk"):
                pk.dump(self.hashdict(), open(lib_dir + "/sim_hash.pk", 'w'))

            # calculate several heuristics for the sky overlap between
            # qeA.ivfs and qeB.ivfs, to be used for correcting the spectra.
            if not os.path.exists(lib_dir + "/qecl_fcut.dat"):
                mask1, mask2 = [m.flatten() for m in self.qeA.get_fmasks()]
                mask3, mask4 = [m.flatten() for m in self.qeB.get_fmasks()]

                shape = mask1.shape
                assert (shape == mask2.shape)
                assert (shape == mask3.shape)
                assert (shape == mask4.shape)
                npix = mask1.size

                fcut11 = np.sum(mask1**2) / npix
                fcut12 = np.sum(mask1 * mask2) / npix
                fcut13 = np.sum(mask1 * mask3) / npix
                fcut14 = np.sum(mask1 * mask4) / npix
                fcut22 = np.sum(mask2**2) / npix
                fcut23 = np.sum(mask2 * mask3) / npix
                fcut24 = np.sum(mask2 * mask4) / npix
                fcut33 = np.sum(mask3**2) / npix
                fcut34 = np.sum(mask3 * mask4) / npix
                fcut44 = np.sum(mask4**2) / npix

                fcut1234 = np.sum(mask1 * mask2 * mask3 * mask4) / npix

                np.savetxt(
                    lib_dir.format(prefix="outputs") + "/qecl_fcut.dat", [
                        fcut1234, fcut11, fcut12, fcut13, fcut14, fcut22,
                        fcut23, fcut24, fcut33, fcut34, fcut44
                    ])
        ql.mpi.barrier()
        util.hash_check(
            pk.load(
                open(lib_dir.format(prefix="outputs") + "/sim_hash.pk", 'r')),
            self.hashdict())

        [
            self.fcut1234, self.fcut11, self.fcut12, self.fcut13, self.fcut14,
            self.fcut22, self.fcut23, self.fcut24, self.fcut33, self.fcut34,
            self.fcut44
        ] = np.loadtxt(lib_dir.format(prefix="outputs") + "/qecl_fcut.dat")