Skip to content
Snippets Groups Projects
Commit 5231e3b5 authored by Colm Talbot's avatar Colm Talbot
Browse files

Merge branch 'read-write-cosmological-prior' into 'master'

BUGFIX: make sure cosmological priors can be written and read

See merge request !1258
parents 26c84edf 2502c9bd
No related branches found
No related tags found
No related merge requests found
......@@ -222,18 +222,6 @@ class Prior(object):
else:
return f"{prior_module}.{prior_name}({args})"
@property
def _repr_dict(self):
"""
Get a dictionary containing the arguments needed to reproduce this object.
"""
property_names = {p for p in dir(self.__class__) if isinstance(getattr(self.__class__, p), property)}
subclass_args = infer_args_from_method(self.__init__)
dict_with_properties = self.__dict__.copy()
for key in property_names.intersection(subclass_args):
dict_with_properties[key] = getattr(self, key)
return {key: dict_with_properties[key] for key in subclass_args}
@property
def is_fixed(self):
"""
......
......@@ -292,20 +292,15 @@ class Cosmological(Interped):
else:
return cls._from_repr(string)
@property
def _repr_dict(self):
"""
Get a dictionary containing the arguments needed to reproduce this object.
"""
from astropy.cosmology.core import Cosmology
def get_instantiation_dict(self):
from astropy import units
dict_with_properties = super(Cosmological, self)._repr_dict
if isinstance(dict_with_properties['cosmology'], Cosmology):
if dict_with_properties['cosmology'].name is not None:
dict_with_properties['cosmology'] = dict_with_properties['cosmology'].name
if isinstance(dict_with_properties['unit'], units.Unit):
dict_with_properties['unit'] = dict_with_properties['unit'].to_string()
return dict_with_properties
from astropy.cosmology.realizations import available
instantiation_dict = super().get_instantiation_dict()
if self.cosmology.name in available:
instantiation_dict['cosmology'] = self.cosmology.name
if isinstance(self.unit, units.Unit):
instantiation_dict['unit'] = self.unit.to_string()
return instantiation_dict
class UniformComovingVolume(Cosmological):
......
......@@ -37,6 +37,14 @@ class TestBBHPriorDict(unittest.TestCase):
del self.bbh_prior_dict
del self.base_directory
def test_read_write_default_prior(self):
filename = "test_prior.prior"
self.bbh_prior_dict.to_file(outdir=".", label="test_prior")
new_prior = bilby.gw.prior.BBHPriorDict(filename=filename)
for key in self.bbh_prior_dict:
self.assertEqual(self.bbh_prior_dict[key], new_prior[key])
os.remove(filename)
def test_create_default_prior(self):
default = bilby.gw.prior.BBHPriorDict()
minima = all(
......
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