Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed typo in analyze.py #52

Open
wants to merge 49 commits into
base: 2.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e6d6bc2
fixed typo in analyze.py
miguelzuma Mar 22, 2016
37aaa68
Merge branch '2.2' of https://github.com/baudren/montepython_public i…
Oct 11, 2016
7d72cfc
writes error_log for failed models
Oct 13, 2016
7ed9391
added error codes to record error_log
Oct 14, 2016
5c493d7
fixed conditionals for err_code selection
Dec 6, 2016
7211aa3
fixed missing __init__.py file
Dec 6, 2016
cda0d53
Merge pull request #1 from ardok-m/2.2
miguelzuma Dec 6, 2016
3028656
changes in print_error
Dec 6, 2016
f118410
Merge branch '2.2' of https://github.com/miguelzuma/montepython_zuma …
Dec 6, 2016
e5498ec
commented out CMASS data (use bao_boss_aniso instead)
Dec 6, 2016
1565294
uncommented data: exclusion was made automatic
Dec 7, 2016
17cf1ec
improved input param handling: error with param scale 0
Dec 14, 2016
5a5b782
Merge pull request #2 from ardok-m/debug_scale
miguelzuma Mar 12, 2017
2b26f71
Merge branch '2.2' of https://github.com/baudren/montepython_public i…
Mar 13, 2017
a69f5bc
fixed TypeError np.max for numpy 1.12
Apr 17, 2017
9674a28
fixed contours level error handling.
Apr 18, 2017
68ade95
Merge pull request #3 from ardok-m/2.2
miguelzuma Apr 20, 2017
aa04984
added Planck_compressed likelihood arXiv:1502.01590
May 22, 2017
a6cb593
fix angular diamter distance. Needed comoving
May 22, 2017
a1ce273
separed mean and sigma values
May 23, 2017
a5c6cf2
added triangular prior and fixed some bugs
May 23, 2017
3954be2
changed name of parent likelihood, behavior is the same
May 25, 2017
20cba93
store values as object to be accesible by wic data_mp_likelihoods.py
May 31, 2017
8141385
added only_error optiont to create error file when using NS
Jul 27, 2017
fa8d974
error_log file per chain
Jul 27, 2017
d7513a7
Merge pull request #4 from ardok-m/2.2
miguelzuma Jul 28, 2017
343b5b0
exclude from analysis error_log files
Aug 8, 2017
b86ddd6
fixed missing ''
Sep 14, 2017
b81e791
fixed missing . in suffix
Sep 14, 2017
dd07d3b
Merge pull request #5 from ardok-m/2.2
miguelzuma Sep 25, 2017
f9c3269
modified bao_boss to interact with work_in_class
Oct 11, 2017
40a4a1e
Merge branch '2.2' of https://github.com/baudren/montepython_public i…
Oct 11, 2017
7ac5186
Merge pull request #6 from ardok-m/2.2
miguelzuma Oct 18, 2017
a55a551
updated to match stable CosmoHammer version
Oct 23, 2017
afeb21d
fixed option input cosmo_hammer
Oct 24, 2017
a738c0c
fixed derived CH when not computed
Oct 25, 2017
b70913f
save errors in different file
Oct 25, 2017
50ad150
fixed problems saving errors diff files
Oct 25, 2017
13f0ab7
added function to store derived paramters from cosmo
Oct 27, 2017
47b2fa4
remove nan's from error-log file
Oct 27, 2017
de36365
Merge branch '2.2' of https://github.com/baudren/montepython_public i…
Oct 27, 2017
e418f3b
Merge pull request #7 from ardok-m/2.2
miguelzuma Oct 27, 2017
16085a3
convert to -loglkl the CH loglkl when analyzing
Nov 14, 2017
b040c2f
Merge pull request #8 from ardok-m/2.2
miguelzuma Nov 30, 2017
f8b4142
sort by name the derived parameters (placed at the end of the param l…
May 14, 2018
62315b9
fixed Planck_compressed. Use default CLASS vars
May 28, 2018
81788c5
fixed error storing derived parameters
Jun 20, 2018
bbcedf8
fixed identation in store_cosmo_derived
Jun 20, 2018
a816613
Merge pull request #9 from ardok-m/2.2
miguelzuma Jun 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions data/bao_2014.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ LOWZ 0.32 8.47 0.17 3
CMASS 0.57 13.77 0.13 3
#
# BOSS LyaQSO (DA/rs) Font-Ribera et al. 1311.1767
# LyaQSO 2.36 10.8 0.4 5
LyaQSO 2.36 10.8 0.4 5
#
# BOSS LyaQSO c/(H rs) Font-Ribera et al. 1311.1767
# LyaQSO 2.36 9.0 0.3 6
LyaQSO 2.36 9.0 0.3 6
#
# SDSS DR7 MGS, Ross et al. 1409.3242v1
MGS 0.15 4.47 0.16 3
23 changes: 23 additions & 0 deletions montepython/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,23 @@ def compute_posterior(information_instances):
"'%s-%s' 2d-plot" % (
info.plotted_parameters[info.native_index],
info.plotted_parameters[info.native_second_index]))
except ValueError as e:
if str(e) == "Contour levels must be increasing":
warnings.warn(
"The routine could not find the contour of the " +
"'%s-%s' 2d-plot. \n " % (
info.plotted_parameters[info.native_index],
info.plotted_parameters[info.native_second_index]) +
'The error is: "Contour levels must be increasing"' +
" but " + str(ctr_level(info.n, info.levels[:2])) +
" were found. This may happen when most" +
" points fall in the same bin.")
else:
warnings.warn(
"The routine could not find the contour of the " +
"'%s-%s' 2d-plot" % (
info.plotted_parameters[info.native_index],
info.plotted_parameters[info.native_second_index]))

ax2dsub.set_xticks(info.ticks[info.native_second_index])
ax2dsub.set_yticks(info.ticks[info.native_index])
Expand Down Expand Up @@ -1382,13 +1399,18 @@ def recover_folder_and_files(files):
# The following list defines the substring that a chain should contain for
# the code to recognise it as a proper chain.
substrings = ['.txt', '__']
# The following variable defines the substring that identify error_log
# files and therefore there must not be taken into account in the analysis.
substring_err = 'error_log'

limit = 10
# If the first element is a folder, grab all chain files inside
if os.path.isdir(files[0]):
folder = os.path.normpath(files[0])
files = [os.path.join(folder, elem) for elem in os.listdir(folder)
if not os.path.isdir(os.path.join(folder, elem))
and not os.path.getsize(os.path.join(folder, elem)) < limit
and (substring_err not in elem)
and all([x in elem for x in substrings])]
# Otherwise, extract the folder from the chain file-name.
else:
Expand All @@ -1402,6 +1424,7 @@ def recover_folder_and_files(files):
if os.path.join(folder, elem) in np.copy(files)
and not os.path.isdir(os.path.join(folder, elem))
and not os.path.getsize(os.path.join(folder, elem)) < limit
and (substring_err not in elem)
and all([x in elem for x in substrings])]
basename = os.path.basename(folder)
return folder, files, basename
Expand Down
78 changes: 65 additions & 13 deletions montepython/cosmo_hammer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

import io_mp
import sampler
from cosmoHammer.likelihood.chain.LikelihoodComputationChain import (
LikelihoodComputationChain)
from cosmoHammer.sampler.CosmoHammerSampler import CosmoHammerSampler
from cosmoHammer import MpiCosmoHammerSampler
from cosmoHammer import LikelihoodComputationChain
#from cosmoHammer.likelihood.chain.LikelihoodComputationChain import (
# LikelihoodComputationChain)
#from cosmoHammer.sampler.CosmoHammerSampler import CosmoHammerSampler
from cosmoHammer.util.SampleFileUtil import SampleFileUtil

# Cosmo Hammer subfolder and name separator
Expand All @@ -33,6 +35,7 @@
# Cosmo Hammer file names ending, after the defined 'base_name'
name_arguments = '.arguments'
name_chain = 'chain_CH__sampling.txt'
name_error_chain = 'chain_CH__sampling-error_log.txt'

# Cosmo Hammer option prefix
CH_prefix = 'CH_'
Expand All @@ -48,6 +51,10 @@
{'help': 'Number of sample iterations',
'type': int}}

# List of strings that will contain the derived parameters names.
# Filled in run using the MontePython class data
# Used in DerivedUtil.persistValues
CH_derived_names = []

def run(cosmo, data, command_line):
"""
Expand All @@ -57,6 +64,9 @@ def run(cosmo, data, command_line):
# Store the parameters inside the format expected by CosmoHammer
# TODO: about the derived params?
parameter_names = data.get_mcmc_parameters(["varying"])
# Fill now CH_derived_names to be used in DerivedUtil.persistValues
global CH_derived_names
CH_derived_names= data.get_mcmc_parameters(["derived"])

# Ensure that their prior is bound and flat
is_flat, is_bound = sampler.check_flat_bound_priors(
Expand Down Expand Up @@ -91,6 +101,7 @@ def run(cosmo, data, command_line):
# here, since data must be called before cosmo.
chain.addCoreModule(data)
chain.addCoreModule(cosmo)
chain.addCoreModule(store_cosmo_derived)

# Add each likelihood class as a LikelihoodModule
for likelihood in data.lkl.itervalues():
Expand All @@ -101,7 +112,9 @@ def run(cosmo, data, command_line):
file_prefix = os.path.join(command_line.folder, CH_subfolder, chain_name)

# Recover the User options
data.CH_arguments = {}
data.CH_arguments = {'walkersRatio': 50,
'burninIterations': 10,
'sampleIterations': 30}
for arg in CH_user_arguments:
value = getattr(command_line, CH_prefix+arg)
if value != -1:
Expand All @@ -127,13 +140,10 @@ def run(cosmo, data, command_line):
num_threads = 1

# Create the Sampler object
sampler_hammer = CosmoHammerSampler(
sampler_hammer = MpiCosmoHammerSampler(
params=params,
likelihoodComputationChain=chain,
filePrefix=file_prefix,
walkersRatio=50,
burninIterations=10,
sampleIterations=30,
storageUtil=derived_util,
threadCount=num_threads,
**data.CH_arguments)
Expand All @@ -158,6 +168,8 @@ def from_CH_output_to_chains(folder):

This function will be called by the module :mod:`analyze`.
"""
chainsClean, lklClean = [], [] # Arrays to store the valid steps.
error = [] # Array to store the step did not could not be computed.

chain_name = [a for a in folder.split(os.path.sep) if a][-2]
base_name = os.path.join(folder, chain_name)
Expand All @@ -172,17 +184,25 @@ def from_CH_output_to_chains(folder):
# multiplicity. This does not mean that the acceptance rate is one, but
# points where the code stayed are duplicated in the file.

## First, reshape the lkl array
lkl = np.array([[elem] for elem in lkl])
# First, separe errors from accepted points:
for x, p in zip(chains, lkl):
if p != -np.inf:
chainsClean.append(x)
lklClean.append([-p]) # MP stores -loglkl in its files.
else:
error.append([val for val in x if not np.isnan(val)]) # Remove nan's

## Create the array of ones
ones = np.array([[1] for _ in range(len(lkl))])
ones = np.array([[1] for _ in range(len(lklClean))])


## Concatenate everything and save to file
final = np.concatenate((ones, lkl, chains), axis=1)
final = np.concatenate((ones, lklClean, chainsClean), axis=1)
output_folder = os.path.join(folder, '..')
output_chain_path = os.path.join(output_folder, name_chain)
output_error_chain_path = os.path.join(output_folder, name_error_chain)
np.savetxt(output_chain_path, final)
np.savetxt(output_error_chain_path, error)


class DerivedUtil(SampleFileUtil):
Expand All @@ -195,8 +215,12 @@ def persistValues(self, posFile, probFile, pos, prob, data):
"""
# extend the pos array to also contain the value of the derived
# parameters

derived_not_computed = [np.NaN] * len(CH_derived_names)

derived = np.array(
[[a for a in elem.itervalues()] for elem in data])
#[[a for a in elem.itervalues()] if elem else derived_not_computed for elem in data])
[[elem[key] for key in sorted(elem.iterkeys())] if elem else derived_not_computed for elem in data])
final = np.concatenate((pos, derived), axis=1)

posFile.write("\n".join(
Expand All @@ -207,3 +231,31 @@ def persistValues(self, posFile, probFile, pos, prob, data):
probFile.write("\n".join([str(p) for p in prob]))
probFile.write("\n")
probFile.flush()


def store_cosmo_derived(ctx):
"""
Store derived parameters. Based on sampler.compute_lkl
"""
from classy import CosmoSevereError

cosmo = ctx.get("cosmo")
data = ctx.get("data")

if data.get_mcmc_parameters(['derived']) != []:
try:
derived = cosmo.get_current_derived_parameters(
data.get_mcmc_parameters(['derived']))
except AttributeError:
# This happens if the classy wrapper is still using the old
# convention, expecting data as the input parameter
cosmo.get_current_derived_parameters(data)
except CosmoSevereError:
raise io_mp.CosmologicalModuleError(
"Could not write the current derived parameters")

ctx.add('key_data', derived)

# If we want the derived parameters stored rescaled
# derived_scaled = {elem: value/data.mcmc_parameters[elem]['scale'] for elem, value in derived.iteritems()}
# ctx.add('key_data', derived_scaled)
14 changes: 12 additions & 2 deletions montepython/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ def __init__(self, command_line, path):
self.out = None
self.out_name = ''

# Create the variable err, and err_name to keep the errors
# will be initialised later by the :mod:`io_mp` module
self.err = None
self.err_name = ''

# If the parameter file is not a log.param, the path will be read
# before reading the parameter file.
if self.param.find('log.param') == -1:
Expand Down Expand Up @@ -995,8 +1000,13 @@ def __init__(self, array, key):
dict.__init__(self)

self['initial'] = array[0:4]
self['scale'] = array[4]
self['role'] = array[-1]
if array[4]:
self['scale'] = array[4]
else:
raise io_mp.ParameterError("You chose scale 0 to param {}. Correct\
it.".format(key))

self['role'] = array[5]
self['tex_name'] = io_mp.get_tex_name(key)
if array[3] == 0:
self['status'] = 'fixed'
Expand Down
1 change: 1 addition & 0 deletions montepython/initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def initialise(custom_command=''):
io_mp.create_output_files(command_line, data)
# NS: Creating the NS subfolder and the MultiNest arguments
elif command_line.method == 'NS':
io_mp.create_output_files(command_line, data, only_error=True)
from nested_sampling import initialise as initialise_ns
initialise_ns(cosmo, data, command_line)

Expand Down
Loading