Esempio n. 1
0
    def test_copy(self):
        params = Params(("attr1", 1), ("attr2", 2))

        params2 = params.copy()

        assert np.all(params.keys == params2.keys)
        assert np.all(params.values == params2.values)
Esempio n. 2
0
    def test_get(self):
        params = Params(("attr1", 1), ("attr2", 2))

        assert params.get("attr1") == 1
        assert params.get("attr2") == 2

        with pytest.raises(KeyError):
            params.get("inexistent")
Esempio n. 3
0
 def test_copy(self):
     params = Params(("attr1", 1),
                     ("attr2", 2))
     
     params2 = params.copy()
     
     assert np.all(params.keys == params2.keys)
     assert np.all(params.values == params2.values)
Esempio n. 4
0
 def test_get(self):
     params = Params(("attr1", 1),
                     ("attr2", 2))
     
     assert params.get("attr1") == 1
     assert params.get("attr2") == 2
     
     with pytest.raises(KeyError):
         params.get("inexistent")
Esempio n. 5
0
    def test_names_immutability(self):
        keys = ["attr1", "attr2"]
        params = Params((keys[0], 1), (keys[1], 2))

        params.keys.append("irrelevant")

        assert np.all(params.keys == keys)
Esempio n. 6
0
    def test_attr_looup(self):
        params = Params(("attr1", 1), ("attr2", 2))

        assert params.attr1 == 1
        assert params.attr2 == 2

        with pytest.raises(AttributeError):
            params.inexistent
Esempio n. 7
0
 def createChainContext(self, p):
     """
     Returns a new instance of a chain context 
     """
     try:
         p = Params(*zip(self.params.keys, p))
     except Exception:
         # no params or params has no keys
         pass
     return ChainContext(self, p)
Esempio n. 8
0
    def test_createChainContext_params(self):
        keys = ["a", "b"]
        params = Params((keys[0], 0), (keys[1], 1))
        chain = LikelihoodComputationChain()
        chain.params = params

        p = np.array([1, 2])
        ctx = chain.createChainContext(p)

        assert ctx is not None
        assert np.all(ctx.getParams().keys == keys)
        assert np.all(ctx.getParams()[0] == p[0])
        assert np.all(ctx.getParams()[1] == p[1])
Esempio n. 9
0
 def test_str(self):
     params = Params(("attr1", 1))
     s = str(params)
     assert "=" in s
#!/usr/bin/env python
"""
Runs CosmoHammer with a rosenbrock density module
"""
from __future__ import print_function, division, absolute_import, unicode_literals

from cosmoHammer import CosmoHammerSampler
from cosmoHammer import LikelihoodComputationChain
from cosmoHammer.modules import RosenbrockModule

from cosmoHammer.util import Params

#parameter start center, min, max, start width
params = Params(("x", [1, -10, 10, 0.1]), ("y", [1, -10, 10, 0.1]))

chain = LikelihoodComputationChain()

rosenbrock = RosenbrockModule()

chain.addLikelihoodModule(rosenbrock)
chain.setup()

sampler = CosmoHammerSampler(params=params,
                             likelihoodComputationChain=chain,
                             filePrefix="rosenbrock",
                             walkersRatio=50,
                             burninIterations=100,
                             sampleIterations=100)
sampler.startSampling()

try:
Esempio n. 11
0
 def test_duplicated_key(self):
     with pytest.raises(KeyError):
         _ = Params(("attr1", 1), ("attr1", 2))
Esempio n. 12
0
    def test_value_assignment(self):
        values = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
        params = Params(("attr1", values[0]), ("attr2", values[1]))

        params[:, 0] = 0
        assert np.all(params[:, 0] == 0)
Esempio n. 13
0
import os
import time
from cosmoHammer.util import Params
from cosmoHammer import MpiCosmoHammerSampler
from cosmoHammer import CosmoHammerSampler
from cosmoHammer import LikelihoodComputationChain

#  from cosmoHammer.pso.MpiParticleSwarmOptimizer import MpiParticleSwarmOptimizer

# The parameter space is defined
# paramters = [peak, min., max., jump] ===> A rough idea about the prior

path = os.path.abspath(os.path.join(__file__, os.pardir))
path = path + '/existing_models/'
params = Params(("NoH", [275, 10, 550, 3]),
                ("n_ion", [90.00, 10.00, 180.00, 1]),
                ("R_mfp", [30.00, 5.00, 60.00, 0.5]))


class RunMCMC:
    """ sampler & MPI sampler class """
    def __init__(self, data, nbins, noise=0., div=1.0, like_func='c'):
        """
        :param data: load your data
        :param nbins: number of k-modes in powerspectrum OR
         number of triangle contributions in bispectrum (for covariance matrix)
        :param noise: system noise, e.g. SKA, MWA noise response (if any), default 0.0,
        :param div: likelihood normalization factor, default 1.0,
        :param like_func: choose between complex likelihood function (use 'c'), and normal function (use 'n')

        """
Esempio n. 14
0
    def test_names_access(self):
        keys = ["attr1", "attr2"]
        params = Params((keys[0], 1), (keys[1], 2))

        assert np.all(params.keys == keys)
Esempio n. 15
0
    def test_slicing(self):
        values = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
        params = Params(("attr1", values[0]), ("attr2", values[1]))

        assert np.all(values[0] == values[0])
        assert np.all(values[:, 1] == values[:, 1])
Esempio n. 16
0
    def test_params_access_2d(self):
        values = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
        params = Params(("attr1", values[0]), ("attr2", values[1]))

        assert np.all(params.values == values)
Esempio n. 17
0
    def test_params_access_1d(self):
        values = [1, 2]
        params = Params(("attr1", values[0]), ("attr2", values[1]))

        assert np.all(params.values == values)
from __future__ import print_function, division, absolute_import, unicode_literals

import numpy as np

from cosmoHammer import LikelihoodComputationChain
from cosmoHammer import CosmoHammerSampler
from cosmoHammer.util import InMemoryStorageUtil
from cosmoHammer.util import Params

from cosmoHammer.modules import PseudoCmbModule
from cosmoHammer.pso.ParticleSwarmOptimizer import ParticleSwarmOptimizer

#parameter start center, min, max, start width
params = Params(
    ("hubble", [70, 65, 80, 3]), ("ombh2", [0.0226, 0.01, 0.03, 0.001]),
    ("omch2", [0.122, 0.09, 0.2, 0.01]),
    ("scalar_amp", [2.1e-9, 1.8e-9, 2.35e-9, 1e-10]),
    ("scalar_spectral_index", [0.96, 0.8, 1.2, 0.02]),
    ("re_optical_depth", [0.09, 0.01, 0.1, 0.03]), ("sz_amp", [1, 0, 2, 0.4]))

chain = LikelihoodComputationChain(params[:, 1], params[:, 2])
chain.params = params

chain.addLikelihoodModule(PseudoCmbModule())
chain.setup()

# find the best fit value and update our params knowledge
print("find best fit point")
pso = ParticleSwarmOptimizer(chain, params[:, 1], params[:, 2])
psoTrace = np.array([pso.gbest.position.copy() for _ in pso.sample()])
params[:, 0] = pso.gbest.position
Esempio n. 19
0
from cosmoHammer import MpiCosmoHammerSampler
from cosmoHammer import LikelihoodComputationChain
from cosmoHammer.util import Params
from MCMC.Power_sampler.Power_like import PSlikeModule as slk
from MCMC.Power_sampler.Power_core import PScore

#======================================================================================================================#

params = Params(("n_ion", [250., 10.0, 510.0, 1.0]),
                ("R_mfp", [38.0, 5.0, 70.0, .5]),
                ("NoH", [750.0, 10.0, 1510.0, 1.0]))
#==========The parameter space is defined================================#
#======================================================================================================================#

chain = LikelihoodComputationChain(min=params[:, 1], max=params[:, 2])

chain.addCoreModule(
    PScore())  #=========setting up the modules===================#

chain.addLikelihoodModule(slk())

chain.setup()
#======================================================================================================================#

sampler = MpiCosmoHammerSampler(
    params=params,
    likelihoodComputationChain=
    chain,  #=============mpi sampler===============================#
    filePrefix="Powerspectrum_THANN_",
    walkersRatio=10,
    burninIterations=250,