Skip to content
Snippets Groups Projects
Commit 89d547c2 authored by Gregory Ashton's avatar Gregory Ashton Committed by Christopher Philip Luke Berry
Browse files

Minor clean up to the results methods

parent 8ed276d0
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@ from . import core, gw, hyper
from .core import utils, likelihood, prior, result, sampler
from .core.sampler import run_sampler
from .core.likelihood import Likelihood
from .core.result import read_in_result, read_in_result_list
try:
from ._version import version as __version__
......
......@@ -108,6 +108,34 @@ def read_in_result(filename=None, outdir=None, label=None, extension='json', gzi
return result
def read_in_result_list(filename_list, invalid="warning"):
""" Read in a set of results
Parameters
==========
filename_list: list
A list of filename paths
invalid: str (ignore, warning, error)
Behaviour if a file in filename_list is not a valid bilby result
Returns
-------
result_list: ResultList
A list of results
"""
results_list = []
for filename in filename_list:
try:
results_list.append(read_in_result(filename=filename))
except Exception as e:
msg = f"Failed to read in file {filename} due to exception {e}"
if invalid == "error":
raise ResultListError(msg)
elif invalid == "warning":
logger.warning(msg)
return ResultList(results_list)
def get_weights_for_reweighting(
result, new_likelihood=None, new_prior=None, old_likelihood=None,
old_prior=None, resume_file=None, n_checkpoint=5000, npool=1):
......@@ -1696,7 +1724,7 @@ class Result(object):
class ResultList(list):
def __init__(self, results=None):
def __init__(self, results=None, consistency_level="warning"):
""" A class to store a list of :class:`bilby.core.result.Result` objects
from equivalent runs on the same data. This provides methods for
outputting combined results.
......@@ -1705,8 +1733,15 @@ class ResultList(list):
==========
results: list
A list of `:class:`bilby.core.result.Result`.
consistency_level: str, [ignore, warning, error]
If warning, print a warning if inconsistencies are discovered
between the results. If error, raise an error if inconsistencies
are discovered between the results before combining. If ignore, do
nothing.
"""
super(ResultList, self).__init__()
self.consistency_level = consistency_level
for result in results:
self.append(result)
......@@ -1737,9 +1772,11 @@ class ResultList(list):
----------
shuffle: bool
If true, shuffle the samples when combining, otherwise they are concatenated.
consistency_level: str, [warning, error]
If warning, print a warning if inconsistencies are discovered between the results before combining.
If error, raise an error if inconsistencies are discovered between the results before combining.
consistency_level: str, [ignore, warning, error]
Overwrite the class level consistency_level. If warning, print a
warning if inconsistencies are discovered between the results. If
error, raise an error if inconsistencies are discovered between
the results before combining. If ignore, do nothing.
Returns
-------
......@@ -1884,6 +1921,8 @@ class ResultList(list):
raise ResultListError(msg)
elif self.consistency_level == "warning":
logger.warning(msg)
elif self.consistency_level == "ignore":
pass
else:
raise ValueError(f"Input consistency_level {self.consistency_level} not understood")
......
......@@ -129,13 +129,6 @@ def setup_command_line_args():
return args
def read_in_results(filename_list):
results_list = []
for filename in filename_list:
results_list.append(bilby.core.result.read_in_result(filename=filename))
return bilby.core.result.ResultList(results_list)
def print_bayes_factors(results_list):
for res in results_list:
print(f"For result {res.label}:")
......@@ -204,7 +197,7 @@ def save(result, args):
def main():
args = setup_command_line_args()
results_list = read_in_results(args.results)
results_list = bilby.core.result.read_in_result_list(args.results)
if args.save:
for result in results_list:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment