Commit 16e2d732 authored by Colm Talbot's avatar Colm Talbot Committed by Gregory Ashton

make cosmological priors safe to read/write to plain text

parent c1eb54ed
......@@ -606,15 +606,21 @@ class Prior(object):
str: A string representation of this instance
"""
subclass_args = infer_args_from_method(self.__init__)
prior_name = self.__class__.__name__
args = ', '.join(['{}={}'.format(key, repr(self._repr_dict[key])) for key in self._repr_dict])
return "{}({})".format(prior_name, args)
property_names = [p for p in dir(self.__class__) if isinstance(getattr(self.__class__, p), property)]
@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:
for key in property_names.intersection(subclass_args):
dict_with_properties[key] = getattr(self, key)
args = ', '.join(['{}={}'.format(key, repr(dict_with_properties[key])) for key in subclass_args])
return "{}({})".format(prior_name, args)
return {key: dict_with_properties[key] for key in subclass_args}
@property
def is_fixed(self):
......
......@@ -160,6 +160,19 @@ 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.
"""
dict_with_properties = super(Cosmological, self)._repr_dict
if isinstance(dict_with_properties['cosmology'], cosmo.core.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
class UniformComovingVolume(Cosmological):
......
......@@ -35,11 +35,16 @@ class TestPriorInstantiationWithoutOptionalPriors(unittest.TestCase):
self.assertIsNone(self.prior.rescale(1))
def test_base_repr(self):
"""
We compare that the strings contain all of the same characters in not
necessarily the same order as python2 doesn't conserve the order of the
arguments.
"""
self.prior = bilby.core.prior.Prior(name='test_name', latex_label='test_label', minimum=0, maximum=1,
boundary=None)
expected_string = "Prior(name='test_name', latex_label='test_label', unit=None, minimum=0, maximum=1, " \
"boundary=None)"
self.assertEqual(expected_string, self.prior.__repr__())
self.assertTrue(sorted(expected_string) == sorted(self.prior.__repr__()))
def test_base_prob(self):
self.assertTrue(np.isnan(self.prior.prob(5)))
......@@ -581,6 +586,11 @@ class TestPriorDict(unittest.TestCase):
self.assertDictEqual(expected, self.prior_set_from_file)
def test_to_file(self):
"""
We compare that the strings contain all of the same characters in not
necessarily the same order as python2 doesn't conserve the order of the
arguments.
"""
expected = ["length = DeltaFunction(peak=42, name='c', latex_label='c', unit='m')\n",
"speed = PowerLaw(alpha=3, minimum=1, maximum=2, name='b', latex_label='b', "
"unit='m/s', boundary=None)\n",
......@@ -589,7 +599,8 @@ class TestPriorDict(unittest.TestCase):
self.prior_set_from_dict.to_file(outdir='prior_files', label='to_file_test')
with open('prior_files/to_file_test.prior') as f:
for i, line in enumerate(f.readlines()):
self.assertTrue(line in expected)
self.assertTrue(any([
sorted(line) == sorted(expect) for expect in expected]))
def test_from_dict_with_string(self):
string_prior = "PowerLaw(name='b', alpha=3, minimum=1, maximum=2, unit='m/s', " \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment