From 2872f4bd8d8bc048f20787ff98d9b90c847ae431 Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Wed, 30 Jan 2019 15:00:13 -0800
Subject: [PATCH] Converts the read_in_result function to a class method

The function remains, but just uses the class method.
---
 bilby/core/result.py | 61 ++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 28 deletions(-)

diff --git a/bilby/core/result.py b/bilby/core/result.py
index 0b96f48a..691812db 100644
--- a/bilby/core/result.py
+++ b/bilby/core/result.py
@@ -37,34 +37,8 @@ def result_file_name(outdir, label):
 
 
 def read_in_result(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 Result(**deepdish.io.load(filename))
-    else:
-        raise IOError("No result '{}' found".format(filename))
+    """ Wrapper to bilby.core.result.Result.from_hdf5 """
+    return Result.from_hdf5(filename=filename, outdir=outdir, label=label)
 
 
 class Result(object):
@@ -156,6 +130,37 @@ class Result(object):
         self.prior_values = 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):
         """Print a summary """
         if getattr(self, 'posterior', None) is not None:
-- 
GitLab