Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 48 additions & 0 deletions montepython/io_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,54 @@ def print_vector(out, N, loglkl, data):
data.mcmc_parameters[elem]['last_accepted'])
out[j].write('\n')

def print_error_log(cosmo, data, failure_message):
folder = os.path.dirname(data.out_name)
fname = os.path.join(folder, 'error_chains.log')
with open(fname, 'a+') as out:
for elem in data.get_mcmc_parameters(['varying']):
out.write('%.6e\t' %
data.mcmc_parameters[elem]['current'])
out.write(str(int(get_error_code(failure_message))))
out.write('\n')

fname = os.path.join(folder, 'error_params.log')
with open(fname, 'a+') as out:
out.write(str(cosmo.pars) + '\n')

fname = os.path.join(folder, 'error_msg.log')
with open(fname, 'a+') as out:
out.write(str(cosmo.pars) + '\n')
out.write(str(failure_message) + '\n')
out.write('\n')
out.write(78*'#' + '\n')
out.write(78*'#' + '\n')
out.write('\n')

def get_error_code(failure_message):
"""
Based on github.com/miguelzuma/montepython_zuma/io_mp.py
print_error function
"""
error_msg = str(failure_message)
err_code = -1 # -1 is for unmarked error

# errors in background are 1-10, perturbations 10-99...
if 'Shooting failed' in error_msg:
err_code = 0
elif all(x in error_msg for x in ['Gradient', 'scalar']):
err_code = 1
elif all(x in error_msg for x in ['Gradient', 'tensor']):
err_code = 2
elif all(x in error_msg for x in ['Ghost', 'scalar']):
err_code = 3
elif all(x in error_msg for x in ['Ghost', 'tensor']):
err_code = 4
elif 'Isnan v_X' in error_msg:
err_code = 10
elif all(x in error_msg for x in ['early_smg', 'adiabatic']):
err_code = 11

return err_code

def refresh_file(data):
"""
Expand Down
18 changes: 18 additions & 0 deletions montepython/parser_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,20 @@ def create_parser():
file, and pass the value with flag -f <value>. A warning
may still appear, but you can safely disregard it.
<++>
<**>--print-errors<**> : bool
<++>Keep track of failed steps. <++>
It will write three files:
a) error_chains.log: values of cosmo varied parameters + error code.
The error code is: 0 for shooting error, 1 for gradient
instability for scalar field perturbations, 2 for gradient
instability for metric tensor perturbations, 3 for ghost instability
for scalar field perturbations, 4 for ghost instability for metric
tensor perturbations, 10 for Isnan v_X and 11 for an error setting
the initial conditions for early_smg for an adiabatic mode. You
can add your own modifying get_error_code in io_mp.py.
b) error_msg.log: cosmo.pars dictionary + error message
c) error_params.log: cosmo.pars dictionary
<++>

For MultiNest, PolyChord and Cosmo Hammer arguments, see
:mod:`MultiNest`, :mod:`PolyChord` and :mod:`cosmo_hammer`.
Expand Down Expand Up @@ -810,6 +824,10 @@ def create_parser():
runparser.add_argument('--parallel-chains', help=helpdict['parallel-chains'],
action='store_true')

# -- write error log (OPTIONAL)
runparser.add_argument('--print-errors', help=helpdict['print-errors'],
action='store_true')

###############
# MCMC restart from chain or best fit file
runparser.add_argument('-r', '--restart', help=helpdict['r'],
Expand Down
4 changes: 4 additions & 0 deletions montepython/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,8 @@ def compute_lkl(cosmo, data):
else:
cosmo.compute(["szpowerspectrum"])
except CosmoComputationError as failure_message:
if data.command_line.print_errors:
io_mp.print_error_log(cosmo, data, failure_message)
sys.stderr.write(str(failure_message)+'\n')
sys.stderr.flush()
return data.boundary_loglike
Expand All @@ -752,6 +754,8 @@ def compute_lkl(cosmo, data):
#print('cosmo params')
#print(data.cosmo_arguments)
#print(data.cosmo_arguments['tau_reio'])
if data.command_line.print_errors:
io_mp.print_error_log(cosmo, data, failure_message)
sys.stderr.write(str(failure_message)+'\n')
sys.stderr.flush()
return data.boundary_loglike
Expand Down