-
Notifications
You must be signed in to change notification settings - Fork 2
Print cosmology before evaluating loglkl #4
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
base: 3.5
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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') | ||
| print('Creating %s\n' % data.cosmo_out_name) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kinda old school; why not do something like this:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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.