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
11 changes: 11 additions & 0 deletions montepython/io_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,22 @@ def create_output_files(command_line, data):
data.out_name = os.path.join(
command_line.folder, outname_base)+str(suffix)+'.txt'
print('Creating %s\n' % data.out_name)
if command_line.print_cosmo:
data.cosmo_out_name = os.path.join(
command_line.folder, outname_base)+str(suffix)+'.cosmo'
data.cosmo_out = open(data.cosmo_out_name, 'w')
print('Creating %s\n' % data.cosmo_out_name)
else:
data.out_name = os.path.join(
command_line.folder, outname_base)+command_line.chain_number+'.txt'
data.out = open(data.out_name, 'w')
print('Creating %s\n' % data.out_name)
if command_line.print_cosmo:
data.cosmo_out_name = os.path.join(
command_line.folder, outname_base)+command_line.chain_number+'.cosmo'
data.cosmo_out = open(data.cosmo_out_name, 'w')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is particularly nice to leave files open like this, but I acknowledge that this is how MontePython is coded. (This is why the last line of chain files are sometimes corrupted!)
In a perfect world, you always open files using a context manager (i.e. using with)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! But I think, for readability, it's better to keep it like how the usual .txt files are written.

print('Creating %s\n' % data.cosmo_out_name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda old school; why not do something like this:

python
print(f'Creating {data.cosmo_out_name}\n')

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied the print statement for the .txt files ;) but yes I agree that's nicer.


# in case of a restart, copying the whole thing in the new file
if command_line.restart is not None:
# Construct filename of old chain from input.
Expand Down
3 changes: 3 additions & 0 deletions montepython/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,9 @@ def chain(cosmo, data, command_line):
# multiplicity of the point and start the loop again
if get_new_position(
data, sigma_eig, U, k, Cholesky, Rotation) is True:
# Prints the position even if it is not accepted
if command_line.print_cosmo:
io_mp.print_vector([data.cosmo_out], 1, 0.0, data)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you did not store a file handle before, what you can do is simply to open the file in append mode:

with open(data.cosmo_out_name, 'a') as fid:
    io_mp.print_vector([fid], 1, 0.0, data)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much nicer. I wonder why MontePython doesn't already do this? Maybe it's slow to re-open the each time?

newloglike = sampler.compute_lkl(cosmo, data)
else: # reject step
rej += 1
Expand Down
8 changes: 8 additions & 0 deletions montepython/parser_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ def create_parser():
<**>--display-each-chi2<**> : bool
<++>Shows the effective chi2 from each likelihood and the total.<++>
Useful e.g. if you run at the bestfit point with -f 0 (flag)<++>
<**>--print-cosmo<**> : bool
<++>Prints CLASS inputs before each loglkl evaluation.<++>
Inputs are printed in a separate file from the chains. (flag)<++>
<**>--parallel-chains<**> : bool
<++>Option for when running parallel without MPI<++>.
Informs the code you are running parallel chains. This
Expand Down Expand Up @@ -809,6 +812,11 @@ def create_parser():
# display option
runparser.add_argument('--display-each-chi2', help=helpdict['display-each-chi2'],
dest='display_each_chi2', action='store_true')

# print cosmo inputs before evaluating loglkl
runparser.add_argument('--print-cosmo', help=helpdict['print-cosmo'],
dest='print_cosmo', action='store_true')

# -- parallel chains without MPI (OPTIONAL)
runparser.add_argument('--parallel-chains', help=helpdict['parallel-chains'],
action='store_true')
Expand Down