From 613f6194f3cb521dd07019e2dde7f465af9bcd17 Mon Sep 17 00:00:00 2001
From: Matthew Pitkin <matthew.pitkin@ligo.org>
Date: Mon, 1 Jun 2020 14:36:02 +0100
Subject: [PATCH] Write/read bilby priors as a JSON dictionary to/from HDF5
 file output

---
 bilby/core/result.py | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/bilby/core/result.py b/bilby/core/result.py
index d967138c..db69507a 100644
--- a/bilby/core/result.py
+++ b/bilby/core/result.py
@@ -358,10 +358,25 @@ class Result(object):
 
         if os.path.isfile(filename):
             dictionary = deepdish.io.load(filename)
-            # Some versions of deepdish/pytables return the dictionanary as
+            # Some versions of deepdish/pytables return the dictionary as
             # a dictionary with a key 'data'
             if len(dictionary) == 1 and 'data' in dictionary:
                 dictionary = dictionary['data']
+
+            if "priors" in dictionary:
+                # parse priors from JSON string (allowing for backwards
+                # compatibility)
+                if not isinstance(dictionary["priors"], PriorDict):
+                    try:
+                        dictionary["priors"] = PriorDict._get_from_json_dict(
+                            dictionary["priors"]
+                        )
+                    except Exception as e:
+                        raise IOError(
+                            "Unable to parse priors from '{}':\n{}".format(
+                                filename, e,
+                            )
+                        )
             try:
                 if isinstance(dictionary.get('posterior', None), dict):
                     dictionary['posterior'] = pd.DataFrame(dictionary['posterior'])
@@ -609,8 +624,9 @@ class Result(object):
                     dictionary['sampler_kwargs'][key] = str(dictionary['sampler_kwargs'])
 
         try:
+            # convert priors to JSON dictionary for both JSON and hdf5 files
+            dictionary["priors"] = dictionary["priors"]._get_json_dict()
             if extension == 'json':
-                dictionary["priors"] = dictionary["priors"]._get_json_dict()
                 if gzip:
                     import gzip
                     # encode to a string
-- 
GitLab