Skip to content

Print cosmology before evaluating loglkl#4

Open
EBHolm wants to merge 2 commits into
3.5from
3-print-cosmo
Open

Print cosmology before evaluating loglkl#4
EBHolm wants to merge 2 commits into
3.5from
3-print-cosmo

Conversation

@EBHolm

@EBHolm EBHolm commented Feb 15, 2023

Copy link
Copy Markdown
Collaborator

Can now be run as

python montepython_public/montepython/MontePython.py -p XXX.param -o XXX --print-cosmo

This creates a file (with the name XXX_1.cosmo) for every ordinary chain file (named XXX_1.txt) which contains exactly the same information, but writes a new line every time the cosmology is being loaded, before CLASS is executed with it. Thus, to track down CLASS errors, one can look in the last line of XXX_1.cosmo for a crashed run and rerun CLASS with the offending input outside of MontePython.

@EBHolm EBHolm requested a review from ThomasTram February 15, 2023 13:30

@ThomasTram ThomasTram left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work! 🥇 I added some "best practice" suggestions, but since you are not doing a PR against the public MontePython (or are you...? 😄 ) you can just follow the hack'ish approach of leaving file handles open...

Comment thread montepython/io_mp.py
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.

Comment thread montepython/io_mp.py
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')
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.

Comment thread montepython/mcmc.py
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants