Skip to content

Commit

Permalink
Minor clean up to the results methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryAshton authored and cplb committed Apr 11, 2023
1 parent 8ed276d commit 89d547c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions bilby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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__
Expand Down
47 changes: 43 additions & 4 deletions bilby/core/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1696,7 +1724,7 @@ def to_arviz(self, prior=None):

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.
Expand All @@ -1705,8 +1733,15 @@ def __init__(self, results=None):
==========
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)

Expand Down Expand Up @@ -1737,9 +1772,11 @@ def combine(self, shuffle=False, consistency_level="error"):
----------
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
-------
Expand Down Expand Up @@ -1884,6 +1921,8 @@ def _error_or_warning_consistency(self, msg):
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")

Expand Down
9 changes: 1 addition & 8 deletions cli_bilby/bilby_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}:")
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 89d547c

Please sign in to comment.