Skip to content

Commit

Permalink
make a boosted posteriors file from polychord
Browse files Browse the repository at this point in the history
  • Loading branch information
joezuntz committed Oct 30, 2024
1 parent ab828be commit 089ebe3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
68 changes: 68 additions & 0 deletions cosmosis/samplers/polychord/polychord_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import sys
from ...runtime.utils import mkdir
from ...output import TextColumnOutput
import warnings

prior_type = ct.CFUNCTYPE(None,
Expand Down Expand Up @@ -302,6 +303,73 @@ def sample(self):

self.converged = True

if self.is_master() and self.boost_posteriors:
self.make_boosted_posterior_file()

Check warning on line 307 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L307

Added line #L307 was not covered by tests

def make_boosted_posterior_file(self):
# two cases where there is nothing to boost
if not self.polychord_outfile_root:
return
if not self.boost_posteriors:
return

Check warning on line 314 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L311-L314

Added lines #L311 - L314 were not covered by tests

old = self.output

Check warning on line 316 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L316

Added line #L316 was not covered by tests

boosted_filename = os.path.join(self.base_dir, self.polychord_outfile_root + ".txt")

Check warning on line 318 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L318

Added line #L318 was not covered by tests

# This is a horribly awkward hack
if isinstance(self.output, TextColumnOutput):

Check warning on line 321 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L321

Added line #L321 was not covered by tests
# make sure everything is written out already
old.flush()

Check warning on line 323 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L323

Added line #L323 was not covered by tests

new_output_filename = old.filename_base + "_boosted.txt"
main_output_info = TextColumnOutput.load_from_options({"filename":old._filename, "delimiter":old.delimiter})

Check warning on line 326 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L325-L326

Added lines #L325 - L326 were not covered by tests

_, _, metadata, comments, final_metadata = main_output_info
metadata = metadata[0]
comments = comments[0]
final_metadata = final_metadata[0]

Check warning on line 331 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L328-L331

Added lines #L328 - L331 were not covered by tests

# make a new output object
new = TextColumnOutput.from_options({"filename":new_output_filename})

Check warning on line 334 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L334

Added line #L334 was not covered by tests

# write the main bits of metadata from the output file
new.metadata("sampler", "polychord")
new.comment("NOTE: Boosted posterior file")
self.write_header(new)
for md in metadata:
new.metadata(md, metadata[md])
for c in comments:
new.comment(c)

Check warning on line 343 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L337-L343

Added lines #L337 - L343 were not covered by tests


# write the parameter rows from the old file
with open(boosted_filename, "r") as f:
for line in f:
values = [float(x) for x in line.split()]
weight = values[0]
like = values[1]
params = values[2:]
prior = params[-1]
post = like + prior
new.parameters(params, like, post, weight)

Check warning on line 355 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L347-L355

Added lines #L347 - L355 were not covered by tests


# This hasn't been written yet because the original output file has not been closed.
# so we have to use the saved one in the old object. The * is because a comment is also included
for md in old._final_metadata:
new.final(md, *old._final_metadata[md])
new.final("log_z", self.log_z)
new.final("log_z_error", self.log_z_err)

Check warning on line 363 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L360-L363

Added lines #L360 - L363 were not covered by tests

# fake these first two, and add the completion marker.
new.final("evaluations", 0)
new.final("successes", 0)
new.final("complete", 1)
new.close()

Check warning on line 369 in cosmosis/samplers/polychord/polychord_sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/polychord/polychord_sampler.py#L366-L369

Added lines #L366 - L369 were not covered by tests



def output_params(self, ndead, nlive, npars, live, dead, logweights, log_z, log_z_err):
# Polychord repeats output, but with changed weights, so reset to the start
# of the chain to overwrite them.
Expand Down
28 changes: 16 additions & 12 deletions cosmosis/samplers/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ def __init__(self, ini, pipeline, output=None):
self.distribution_hints = Hints()
self.write_header()

def write_header(self):
if self.output:
for p in self.pipeline.output_names():
self.output.add_column(p, float)
for p,ptype in self.sampler_outputs:
self.output.add_column(p, ptype)
self.output.metadata("n_varied", len(self.pipeline.varied_params))
self.attribution.write_output(self.output)
for key, value in self.collect_run_metadata().items():
self.output.metadata(key, value)
def write_header(self, output=None):
if output is None:
output = self.output
if output is None:
return

Check warning on line 61 in cosmosis/samplers/sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/sampler.py#L61

Added line #L61 was not covered by tests

for p in self.pipeline.output_names():
output.add_column(p, float)
for p,ptype in self.sampler_outputs:
output.add_column(p, ptype)
output.metadata("n_varied", len(self.pipeline.varied_params))
self.attribution.write_output(output)
for key, value in self.collect_run_metadata().items():
output.metadata(key, value)
blinding_header = self.ini.getboolean("output","blinding-header", fallback=False)
if blinding_header and self.output:
self.output.blinding_header()
if blinding_header:
output.blinding_header()

Check warning on line 73 in cosmosis/samplers/sampler.py

View check run for this annotation

Codecov / codecov/patch

cosmosis/samplers/sampler.py#L73

Added line #L73 was not covered by tests

def collect_run_metadata(self):
info = {
Expand Down

0 comments on commit 089ebe3

Please sign in to comment.