From 6436d66dd603f462c2bb3292e6e373caa73b40ec Mon Sep 17 00:00:00 2001 From: Isobel Marguarethe Romero-Shaw <isobel.romero-shaw@ligo.org> Date: Wed, 15 Jan 2020 23:55:35 -0600 Subject: [PATCH] Raise ValueError if prior maximum <= prior minimum and prior is not a DeltaFunction --- bilby/core/prior/analytical.py | 2 +- bilby/core/prior/base.py | 11 ++++++++++- test/prior_test.py | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bilby/core/prior/analytical.py b/bilby/core/prior/analytical.py index 03fb71e15..4de79a382 100644 --- a/bilby/core/prior/analytical.py +++ b/bilby/core/prior/analytical.py @@ -25,7 +25,7 @@ class DeltaFunction(Prior): """ super(DeltaFunction, self).__init__(name=name, latex_label=latex_label, unit=unit, - minimum=peak, maximum=peak) + minimum=peak, maximum=peak, check_range_nonzero=False) self.peak = peak self._is_fixed = True diff --git a/bilby/core/prior/base.py b/bilby/core/prior/base.py index 820b4965d..29efe925d 100644 --- a/bilby/core/prior/base.py +++ b/bilby/core/prior/base.py @@ -15,7 +15,7 @@ class Prior(object): _default_latex_labels = {} def __init__(self, name=None, latex_label=None, unit=None, minimum=-np.inf, - maximum=np.inf, boundary=None): + maximum=np.inf, check_range_nonzero=True, boundary=None): """ Implements a Prior object Parameters @@ -30,15 +30,24 @@ class Prior(object): Minimum of the domain, default=-np.inf maximum: float, optional Maximum of the domain, default=np.inf + check_range_nonzero: boolean, optional + If True, checks that the prior range is non-zero boundary: str, optional The boundary condition of the prior, can be 'periodic', 'reflective' Currently implemented in cpnest, dynesty and pymultinest. """ + if check_range_nonzero and maximum <= minimum: + raise ValueError( + "maximum {} <= minimum {} for {} prior on {}".format( + maximum, minimum, type(self).__name__, name + ) + ) self.name = name self.latex_label = latex_label self.unit = unit self.minimum = minimum self.maximum = maximum + self.check_range_nonzero = check_range_nonzero self.least_recently_sampled = None self.boundary = boundary self._is_fixed = False diff --git a/test/prior_test.py b/test/prior_test.py index 8b2b9efe9..af0fbb909 100644 --- a/test/prior_test.py +++ b/test/prior_test.py @@ -42,9 +42,9 @@ class TestPriorInstantiationWithoutOptionalPriors(unittest.TestCase): arguments. """ self.prior = bilby.core.prior.Prior(name='test_name', latex_label='test_label', minimum=0, maximum=1, - boundary=None) + check_range_nonzero=True, boundary=None) expected_string = "Prior(name='test_name', latex_label='test_label', unit=None, minimum=0, maximum=1, " \ - "boundary=None)" + "check_range_nonzero=True, boundary=None)" self.assertTrue(sorted(expected_string) == sorted(self.prior.__repr__())) def test_base_prob(self): -- GitLab