Skip to content
Snippets Groups Projects
Commit be6ddfe2 authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Merge branch 'convert-read_in_result-to-classmethod' into 'master'

Converts the read_in_result function to a class method

See merge request !345
parents 97974605 2872f4bd
No related branches found
No related tags found
No related merge requests found
...@@ -37,34 +37,8 @@ def result_file_name(outdir, label): ...@@ -37,34 +37,8 @@ def result_file_name(outdir, label):
def read_in_result(filename=None, outdir=None, label=None): def read_in_result(filename=None, outdir=None, label=None):
""" Read in a saved .h5 data file """ Wrapper to bilby.core.result.Result.from_hdf5 """
return Result.from_hdf5(filename=filename, outdir=outdir, label=label)
Parameters
----------
filename: str
If given, try to load from this filename
outdir, label: str
If given, use the default naming convention for saved results file
Returns
-------
result: bilby.core.result.Result
Raises
-------
ValueError: If no filename is given and either outdir or label is None
If no bilby.core.result.Result is found in the path
"""
if filename is None:
if (outdir is None) and (label is None):
raise ValueError("No information given to load file")
else:
filename = result_file_name(outdir, label)
if os.path.isfile(filename):
return Result(**deepdish.io.load(filename))
else:
raise IOError("No result '{}' found".format(filename))
class Result(object): class Result(object):
...@@ -156,6 +130,37 @@ class Result(object): ...@@ -156,6 +130,37 @@ class Result(object):
self.prior_values = None self.prior_values = None
self._kde = None self._kde = None
@classmethod
def from_hdf5(cls, filename=None, outdir=None, label=None):
""" Read in a saved .h5 data file
Parameters
----------
filename: str
If given, try to load from this filename
outdir, label: str
If given, use the default naming convention for saved results file
Returns
-------
result: bilby.core.result.Result
Raises
-------
ValueError: If no filename is given and either outdir or label is None
If no bilby.core.result.Result is found in the path
"""
if filename is None:
if (outdir is None) and (label is None):
raise ValueError("No information given to load file")
else:
filename = result_file_name(outdir, label)
if os.path.isfile(filename):
return cls(**deepdish.io.load(filename))
else:
raise IOError("No result '{}' found".format(filename))
def __str__(self): def __str__(self):
"""Print a summary """ """Print a summary """
if getattr(self, 'posterior', None) is not None: if getattr(self, 'posterior', None) is not None:
......
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