Skip to content

Commit

Permalink
add test of getdist postprocessing and fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
joezuntz committed Sep 17, 2024
1 parent e09492c commit 18d03f8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
23 changes: 21 additions & 2 deletions cosmosis/postprocessing/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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()]
Expand Down
49 changes: 25 additions & 24 deletions cosmosis/test/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 18d03f8

Please sign in to comment.