Print cosmology before evaluating loglkl#4
Conversation
ThomasTram
left a comment
There was a problem hiding this comment.
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...
| 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') |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
I agree! But I think, for readability, it's better to keep it like how the usual .txt files are written.
| 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) |
There was a problem hiding this comment.
This is kinda old school; why not do something like this:
python
print(f'Creating {data.cosmo_out_name}\n')
There was a problem hiding this comment.
I just copied the print statement for the .txt files ;) but yes I agree that's nicer.
| 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) |
There was a problem hiding this comment.
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)There was a problem hiding this comment.
That's much nicer. I wonder why MontePython doesn't already do this? Maybe it's slow to re-open the each time?
Can now be run as
python montepython_public/montepython/MontePython.py -p XXX.param -o XXX --print-cosmoThis 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.