diff --git a/bilby/core/prior/base.py b/bilby/core/prior/base.py index db5880da1a3cf77e67aff29fe9a8bdf2f2e69892..802d5f6f12f0ed15e44b76514809832c406f16f3 100644 --- a/bilby/core/prior/base.py +++ b/bilby/core/prior/base.py @@ -68,6 +68,9 @@ class Prior(object): if sorted(self.__dict__.keys()) != sorted(other.__dict__.keys()): return False for key in self.__dict__: + if key == "least_recently_sampled": + # ignore sample drawn from prior in comparison + continue if type(self.__dict__[key]) is np.ndarray: if not np.array_equal(self.__dict__[key], other.__dict__[key]): return False diff --git a/bilby/core/result.py b/bilby/core/result.py index d967138c77ba014af15f5ebc26e6338b6afcaafb..96432e066e99acd63f78ce7b28fb16debe417b33 100644 --- a/bilby/core/result.py +++ b/bilby/core/result.py @@ -23,7 +23,8 @@ from .utils import ( check_directory_exists_and_if_not_mkdir, latex_plot_format, safe_save_figure, BilbyJsonEncoder, load_json, - move_old_file, get_version_information + move_old_file, get_version_information, + decode_bilby_json, ) from .prior import Prior, PriorDict, DeltaFunction @@ -358,10 +359,27 @@ 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: + priordict = PriorDict() + for key, value in dictionary["priors"].items(): + if key not in ["__module__", "__name__", "__prior_dict__"]: + priordict[key] = decode_bilby_json(value) + dictionary["priors"] = priordict + 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 +627,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