Skip to content
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

Update dynesty to save blobs and prior #102

Merged
merged 1 commit into from
Sep 20, 2023
Merged
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
27 changes: 13 additions & 14 deletions cosmosis/samplers/dynesty/dynesty_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@


def log_probability_function(p):
return pipeline.likelihood(p)[0]
results = pipeline.run_results(p)
# also save the prior because we want the posterior which dynesty does not report
blob = np.concatenate([[results.prior], results.extra])
return results.like, blob

def prior_transform(p):
return pipeline.denormalize_vector_from_prior(p)

class DynestySampler(ParallelSampler):
parallel_output = False
sampler_outputs = [('log_weight', float), ("like", float)]
sampler_outputs = [('log_weight', float), ("prior", float), ("post", float)]

def config(self):
global pipeline
Expand All @@ -34,14 +37,6 @@ def config(self):
print_progress_default = logs.is_enabled_for(logs.logging.INFO)
self.print_progress = self.read_ini("print_progress", bool, print_progress_default)

# if self.mode=='dynamic':
# raise ValueError("Dynesty mode 'dynamic' not yet implemented (sorry)")

for sec,name in pipeline.extra_saves:
col = "{}--{}".format(sec,name)
print("WARNING: DYNESTY DOES NOT SUPPORT DERIVED PARAMS - NOT SAVING {}".format(col))
self.output.del_column(col)

self.converged = False


Expand All @@ -63,7 +58,8 @@ def execute(self):
update_interval = self.update_interval,
first_update = {'min_ncall':self.min_ncall, 'min_eff':self.min_eff},
queue_size = self.queue_size,
pool = self.pool
pool = self.pool,
blob=True
)

sampler.run_nested(dlogz=self.dlogz)
Expand All @@ -77,15 +73,18 @@ def execute(self):
sample = self.sample,
# update_interval = self.update_interval,
queue_size = self.queue_size,
pool = self.pool
pool = self.pool,
blob=True
)
sampler.run_nested(dlogz_init=self.dlogz)

results = sampler.results
results.summary()

for sample, logwt, logl in zip(results['samples'],results['logwt'], results['logl']):
self.output.parameters(sample, logwt, logl)
for sample, logwt, logl, derived in zip(results['samples'],results['logwt'], results['logl'], results['blob']):
prior = derived[0]
post = prior + logl
self.output.parameters(sample, logwt, prior, post, derived[1:])

self.output.final("efficiency", results['eff'])
self.output.final("nsample", len(results['samples']))
Expand Down
Loading