From 6310fdcfa40b11fe5a7020dea3b6e5dae2822241 Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Sun, 8 Sep 2024 13:36:16 +0100 Subject: [PATCH] test that the peak hint is working --- cosmosis/samplers/dynesty/dynesty_sampler.py | 3 +-- cosmosis/test/test_samplers.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cosmosis/samplers/dynesty/dynesty_sampler.py b/cosmosis/samplers/dynesty/dynesty_sampler.py index 03ac21fe..5c8397a4 100755 --- a/cosmosis/samplers/dynesty/dynesty_sampler.py +++ b/cosmosis/samplers/dynesty/dynesty_sampler.py @@ -87,13 +87,12 @@ def execute(self): 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.parameters(sample, derived[1:], logwt, prior, post) self.output.final("efficiency", results['eff']) self.output.final("nsample", len(results['samples'])) self.output.final("log_z", results['logz'][-1]) self.output.final("log_z_error", results['logzerr'][-1]) - self.converged = True diff --git a/cosmosis/test/test_samplers.py b/cosmosis/test/test_samplers.py index dfefdc7c..7e6c3a80 100644 --- a/cosmosis/test/test_samplers.py +++ b/cosmosis/test/test_samplers.py @@ -14,7 +14,7 @@ # parameters, so our expected prior is 1/6 for each of them. EXPECTED_LOG_PRIOR = 2*np.log(1./6) -def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=False, no_extra=False, pp_extra=True, pp_2d=True, **options): +def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=False, no_extra=False, pp_extra=True, pp_2d=True, hints_peak=True, **options): sampler_class = Sampler.registry[name] @@ -61,6 +61,16 @@ def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=Fals # but not all of them assert not np.all(pr==-np.inf) + if hints_peak: + assert sampler.distribution_hints.has_peak() + peak = sampler.distribution_hints.get_peak() + idx = output["post"].argmax() + assert np.isclose(output["parameters--p1"][idx], peak[0]) + assert np.isclose(output["parameters--p2"][idx], peak[1]) + sampler.distribution_hints.del_peak() + assert not sampler.distribution_hints.has_peak() + + if check_extra and not no_extra: p1 = output['parameters--p1'] p2 = output['parameters--p2'] @@ -115,7 +125,7 @@ def test_truth(): run('emcee', True, walkers=8, samples=100, do_truth=True) def test_fisher(): - run('fisher', False, check_extra=False) + run('fisher', False, check_extra=False, hints_peak=False) def test_grid(): run('grid', True, pp_extra=False, nsample_dimension=10) @@ -179,7 +189,7 @@ def test_star(): run('star', False, pp_extra=False, pp_2d=False) def test_test(): - run('test', False, can_postprocess=False) + run('test', False, can_postprocess=False, hints_peak=False) def test_list_sampler(): # test that the burn and thin parameters work