diff --git a/.travis.yml b/.travis.yml index 7d971e4..e7351a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,24 @@ language: python python: - "2.7" - - "3.3" + - "3.4" before_install: - - sudo apt-get update -qq - - sudo apt-get install -qq libatlas-dev libatlas-base-dev liblapack-dev gfortran - - wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh - - chmod +x miniconda.sh - - ./miniconda.sh -b - - export PATH=/home/travis/miniconda/bin:$PATH + - sudo apt-get update + - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test + - sudo apt-get -qq update + - sudo apt-get -qq install g++-4.8 gcc-4.8 + - sudo ln -sf /usr/bin/gcc-4.8 /usr/bin/gcc + - sudo ln -sf /usr/bin/g++-4.8 /usr/bin/g++ + # We do this conditionally because it saves us some downloading if the + # version is the same. + - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then + wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh; + else + wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; + fi + - bash miniconda.sh -b -p $HOME/miniconda + - export PATH="$HOME/miniconda/bin:$PATH" - conda update --yes conda - travis_retry conda install --yes python=$TRAVIS_PYTHON_VERSION pip numpy scipy diff --git a/HISTORY.rst b/HISTORY.rst index d12b9d6..015777e 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,13 @@ History ------- +0.6.0 (2016-08-15) +++++++++++++++++++ +- Order preserving parameter objects +- Fixing logging issue +- Prettified examples + + 0.5.0 (2015-05-12) ++++++++++++++++++ - Flat package structure diff --git a/cosmoHammer/__init__.py b/cosmoHammer/__init__.py index 452314e..ba6d6de 100644 --- a/cosmoHammer/__init__.py +++ b/cosmoHammer/__init__.py @@ -6,7 +6,7 @@ This is the CosmoHammer package. """ -__version__ = '0.5.0' +__version__ = '0.6.0' __author__ = 'Joel Akeret' __credits__ = 'Institute for Astronomy ETHZ, Institute of 4D Technologies FHNW' diff --git a/cosmoHammer/pso/BestFitPositionGenerator.py b/cosmoHammer/pso/BestFitPositionGenerator.py index 57f1ddf..14c7a2c 100644 --- a/cosmoHammer/pso/BestFitPositionGenerator.py +++ b/cosmoHammer/pso/BestFitPositionGenerator.py @@ -7,7 +7,6 @@ import numpy from cosmoHammer.pso.ParticleSwarmOptimizer import ParticleSwarmOptimizer -from cosmoHammer.pso.ParaboloidFitter import ParaboloidFitter from cosmoHammer.pso.CurvatureFitter import CurvatureFitter class BestFitPositionGenerator(object): diff --git a/cosmoHammer/pso/ParaboloidFitter.py b/cosmoHammer/pso/ParaboloidFitter.py deleted file mode 100644 index 65ed477..0000000 --- a/cosmoHammer/pso/ParaboloidFitter.py +++ /dev/null @@ -1,175 +0,0 @@ -''' -Created on Oct 30, 2013 - -@author: J.Akeret -''' -from __future__ import print_function, division, absolute_import, \ - unicode_literals - -from numpy.linalg.linalg import norm -from scipy.optimize.minpack import leastsq -from scipy.optimize import minimize -import numpy -import sys - -def parabola(p, x): - """ - Computation of the paraboloid for the given curvature matrix and samples. - :param x: vector containing the lower triangle of the matrix and the offset from the true mean - :param p: list of samples - - :return: vector y from f(x,p) - """ - - theta = x - leng, dim = theta.shape - R, mu = transform(dim, p) - - v = numpy.zeros(leng) -# theta += numpy.outer(mu, numpy.ones(len(theta))).T - for i,thetaj in enumerate(theta): -# v[i] = numpy.dot(thetaj.T,numpy.dot(R, thetaj)) + 2 * numpy.dot(thetaj, numpy.dot(R, mu)) - v[i] = numpy.dot(thetaj.T,numpy.dot(R, thetaj)) + numpy.dot(thetaj, mu) - - return numpy.array(v) - - -def errfunc(p,theta,delta): - """ - Error function defined by f(theta) - delta - :param p: list of samples - :param theta: the curvature matrix. see parabola def - :param delta: the measured values - """ - return parabola(p, theta) - delta - -def transform(dim, p): - """ - Transforms a vector containg the lower triangle of a matrix into a symmetric matrix - - :param p: the vector - - :return: the matrix and left over values - """ - R = numpy.zeros((dim,dim)) - k=0 - for i in range(dim): - for j in range(0,i+1): - R[i,j]= p[k] - k +=1 - - R += R.T - numpy.diag(R.diagonal()) - - xbar = p[k:] - - return R, xbar - -def reverse(dim, R): - """ - Transforms a symmetric matrix into a vector containig the lower triangle - - :param R: the symmetric matrix - - :return: the vector - """ - p = numpy.zeros(dim*(dim+1)/2) - k=0 - for i in range(dim): - for j in range(0,i+1): - p[k] = R[i,j] - k +=1 - - return p - - -class ParaboloidFitter(object): - ''' - Fits a paraboloid centered around the global best fit of the PSO by estimating a curvarture - matrix with the particle given in the swarm - - :param swarm: list of particles - :param gbest: the global best particle at the last iteration - ''' - - - def __init__(self, swarm, gbest): - ''' - Constructor - ''' - self.swarm = swarm - self.gbest = gbest - - def fit(self): - """ - Fits the paraboloid to the swarm particles - - :return: the mean = global best position and the estimated covariance matrix - """ - - scale = 10**0 - dim = len(self.gbest.position) - - x = numpy.array([particle.position * scale for particle in self.swarm]) - theta = (x - self.gbest.position * scale) / (self.gbest.position * scale) - norms = numpy.array(list(map(norm, theta))) - #print(Counter(b)) - b = (norms < 0.1) - theta = theta[b] - fitness = numpy.array([particle.fitness * scale for particle in self.swarm]) - -# b = numpy.logical_and((norms < 0.1), fitness != -numpy.inf) - fitness = fitness[b] - delta = -2*(fitness - self.gbest.fitness * scale) - - p0 = numpy.zeros(dim*(dim+1)/2 + dim) - popt, _cov,infodict,mesg,ier = leastsq(errfunc, p0, args=(theta,delta),full_output=True) - print(mesg) - - - ss_err=(infodict['fvec']**2).sum() - ss_tot=((delta-delta.mean())**2).sum() - rsquared=1-(ss_err/ss_tot) - print("rsquared", rsquared) - R, mu = transform(dim, popt) - print(mu) -# print("found R:\n", R) - _cov = rescale(R, self.gbest.position, dim) - print("found _cov:\n", _cov) - -# cons = ( -# {'type': 'ineq', -# 'fun' : lambda x: bound(x)}) -# -# res = minimize(errfunc2, p0, args=(theta, delta), constraints=cons, method='SLSQP', options={'disp': True, "ftol":10**-10}) -# popt=res.x -# R, mu = transform(dim, popt) -# print(mu) -# # print("found R:\n", R) -# _cov = rescale(R, self.gbest.position, dim) -# print("found _cov:\n", _cov) - -# eigen = numpy.linalg.eigvals(R) -# print("-->eigen:", min(eigen), max(eigen), min(eigen)/max(eigen)) -# R2 = numpy.empty((dim, dim)) -# for i in range(dim): -# for j in range(dim): -# R2[i,j] = R[i,j]/self.gbest.position[i]/self.gbest.position[j] -# -# print("R\n", R2) - -# _cov = rescale(R, self.gbest.position, dim) -# print("found _cov:\n", _cov) -# print("=> _cov diag", cov2.diagonal()) -# sigma = numpy.sqrt(numpy.diag(cov2)) -# print( "=> found sigma:", sigma) - - return self.gbest.position, _cov - -def rescale(R, v, dim): - _cov = numpy.linalg.inv(R) - #rescaling - cov2 = numpy.empty((dim, dim)) - for i in range(dim): - for j in range(dim): - cov2[i,j] = _cov[i,j] * v[i] * v[j] - return cov2 \ No newline at end of file diff --git a/doc/source/user/usage.rst b/doc/source/user/usage.rst index e15425a..27c1b6e 100644 --- a/doc/source/user/usage.rst +++ b/doc/source/user/usage.rst @@ -16,15 +16,14 @@ To run CosmoHammer in order to sample the WMAP 9 year likelihood, you would do s from pycambWrapper import PyCambCoreModule #parameter start center, min, max, start width - params = np.array( - [[70, 40, 100, 3], - [0.0226, 0.005, 0.1, 0.001], - [0.122, 0.01, 0.99, 0.01], - [2.1e-9, 1.48e-9, 5.45e-9, 1e-10], - [0.96, 0.5, 1.5, 0.02], - [0.09, 0.01, 0.8, 0.03], - [1,0,2,0.4] ]) - + 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( min=params[:,1], max=params[:,2]) diff --git a/examples/DerivedParamterFileUtil.py b/examples/DerivedParamterFileUtil.py index 49c5402..cbd87c4 100644 --- a/examples/DerivedParamterFileUtil.py +++ b/examples/DerivedParamterFileUtil.py @@ -11,8 +11,8 @@ ''' from cosmoHammer.util import SampleFileUtil -VALUE_1_KEY = "value1" -VALUE_2_KEY = "value2" +VALUE_1_KEY = "derived_params_key" +VALUE_2_KEY = "derived_params_key" OUTPUT_ORDER = [VALUE_2_KEY, VALUE_1_KEY] @@ -35,7 +35,8 @@ def persistValues(self, posFile, probFile, pos, prob, data): #handle special parameters for derParams in data: - self.paramsFile.write("\t".join(derParams[key] for key in OUTPUT_ORDER)) + if len(derParams) > 0: + self.paramsFile.write("\t".join([str(derParams[key]) for key in OUTPUT_ORDER])) self.paramsFile.write("\n") self.paramsFile.flush(); \ No newline at end of file diff --git a/examples/wmap_tester.py b/examples/wmap_tester.py index 0594be0..bb461bc 100755 --- a/examples/wmap_tester.py +++ b/examples/wmap_tester.py @@ -16,7 +16,7 @@ pyCambCore = PyCambCoreModule.PyCambCoreModule() -cambCore = CambCoreModule.CambCoreModule() +cambCore = CambCoreModule() wmap3Likelihood = wmap3.WmapLikelihoodModule() wmap5Likelihood = wmap5.WmapLikelihoodModule() wmap7Likelihood = wmap7.WmapLikelihoodModule() diff --git a/setup.py b/setup.py index 9bef44a..0cd7f22 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ def run_tests(self): setup( name="cosmoHammer", - version='0.5.0', + version='0.6.0', author='Joel Akeret', author_email="jakeret@phys.ethz.ch", url="http://www.cosmology.ethz.ch/research/software-lab/cosmohammer.html", @@ -60,6 +60,6 @@ def run_tests(self): 'Natural Language :: English', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', ], ) diff --git a/tox.ini b/tox.ini index b3310d3..5438bb0 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26, py27, docs-ci +envlist = py26, py27, py34, docs-ci [testenv] setenv =