From 18d03f8710b977b95247c32152aed27af5a1a997 Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Tue, 17 Sep 2024 08:53:53 +0100 Subject: [PATCH 1/2] add test of getdist postprocessing and fix it --- cosmosis/postprocessing/statistics.py | 23 +++++++++++-- cosmosis/test/test_samplers.py | 49 ++++++++++++++------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/cosmosis/postprocessing/statistics.py b/cosmosis/postprocessing/statistics.py index bd885325..b6f984c7 100644 --- a/cosmosis/postprocessing/statistics.py +++ b/cosmosis/postprocessing/statistics.py @@ -281,7 +281,20 @@ def get_gdobj(self): def compute_basic_statsgd_col(self, gdc, col): dens1d = gdc.get1DDensity(col,writeDataToFile=False) if dens1d is None: - return [np.nan for i in range(9)] + return [np.nan for i in range(11)] + + + post = self.reduced_col("post") + + try: + like = self.reduced_col("like") + except ValueError: + like = post - self.reduced_col("prior") + + data1 = self.reduced_col(col) + maxlike = data1[like.argmax()] + maxpost = data1[post.argmax()] + def func(x): return -dens1d.Prob(x) @@ -298,6 +311,8 @@ def func(x): dens1d.getLimits([0.68])[1], dens1d.getLimits([0.95])[0], dens1d.getLimits([0.95])[1], + maxlike, + maxpost, ] return results @@ -753,8 +768,12 @@ def compute_basic_statsgd_col(self, gdc, col): if dens1d is None: return [np.nan for i in range(11)] - like = self.reduced_col("like") post = self.reduced_col("post") + try: + like = self.reduced_col("like") + except ValueError: + like = post - self.reduced_col("prior") + data1 = self.reduced_col(col) maxlike = data1[like.argmax()] maxpost = data1[post.argmax()] diff --git a/cosmosis/test/test_samplers.py b/cosmosis/test/test_samplers.py index 7e6c3a80..cb20b192 100644 --- a/cosmosis/test/test_samplers.py +++ b/cosmosis/test/test_samplers.py @@ -80,31 +80,32 @@ def run(name, check_prior, check_extra=True, can_postprocess=True, do_truth=Fals if can_postprocess: pp_class = postprocessor_for_sampler(name) print(pp_class) - with tempfile.TemporaryDirectory() as dirname: - truth_file = values.name if do_truth else None - pp = pp_class(output, "Chain", 0, outdir=dirname, prefix=name, truth=truth_file, fatal_errors=True) - pp_files = pp.run() - pp.finalize() - pp.save() - for p in pp_files: - print(p) - postprocess_files = ['parameters--p1', 'parameters--p2'] - if pp_2d: - postprocess_files.append('2D_parameters--p2_parameters--p1') - if check_extra and pp_extra and not no_extra: - postprocess_files.append('parameters--p3') + for getdist in [True, False]: + with tempfile.TemporaryDirectory() as dirname: + truth_file = values.name if do_truth else None + pp = pp_class(output, "Chain", 0, outdir=dirname, prefix=name, truth=truth_file, fatal_errors=True, getdist=getdist) + pp_files = pp.run() + pp.finalize() + pp.save() + for p in pp_files: + print(p) + postprocess_files = ['parameters--p1', 'parameters--p2'] if pp_2d: - postprocess_files += ['2D_parameters--p3_parameters--p2', '2D_parameters--p3_parameters--p1'] - for p in postprocess_files: - filename = f"{dirname}{os.path.sep}{name}_{p}.png" - print("WANT ", filename) - assert filename in pp_files - assert os.path.exists(filename) - for p in os.listdir(dirname): - p = os.path.join(dirname, p) - if p.endswith(".txt"): - Table.read(p, format='ascii.commented_header') - print(f"Read file {p} as a table") + postprocess_files.append('2D_parameters--p2_parameters--p1') + if check_extra and pp_extra and not no_extra: + postprocess_files.append('parameters--p3') + if pp_2d: + postprocess_files += ['2D_parameters--p3_parameters--p2', '2D_parameters--p3_parameters--p1'] + for p in postprocess_files: + filename = f"{dirname}{os.path.sep}{name}_{p}.png" + print("WANT ", filename) + assert filename in pp_files + assert os.path.exists(filename) + for p in os.listdir(dirname): + p = os.path.join(dirname, p) + if p.endswith(".txt"): + Table.read(p, format='ascii.commented_header') + print(f"Read file {p} as a table") return output From c30692cb86aedd684e8171b8c81d01e16990e502 Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Tue, 17 Sep 2024 09:03:20 +0100 Subject: [PATCH 2/2] add getdist to test suite --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0799a944..014811d3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,7 +85,7 @@ jobs: run: | python -m pip install --upgrade pip pip install -v . - pip install --no-binary=mpi4py mpi4py astropy pytest pytest-cov + pip install --no-binary=mpi4py mpi4py astropy pytest pytest-cov getdist - name: Install PocoMC if: matrix.python-version != '3.7'