Commit 514b1bd9 authored by Moritz Huebner's avatar Moritz Huebner
Browse files

Merge branch 'fix_distance_prior_maximum' into 'master'

Fixed bugs with changing max/min of priors within Cosmological  and Interped classes

See merge request !815
parents 3289bf61 9a87e205
Pipeline #141923 passed with stages
in 7 minutes and 21 seconds
......@@ -44,6 +44,8 @@ class Interped(Prior):
"""
self.xx = xx
self.min_limit = min(xx)
self.max_limit = max(xx)
self._yy = yy
self.YY = None
self.probability_density = None
......@@ -97,6 +99,8 @@ class Interped(Prior):
Updates the prior distribution if minimum is set to a different value.
Yields an error if value is set below instantiated x-array minimum.
Returns
-------
float: Minimum of the prior distribution
......@@ -106,6 +110,8 @@ class Interped(Prior):
@minimum.setter
def minimum(self, minimum):
if minimum < self.min_limit:
raise ValueError('Minimum cannot be set below {}.'.format(round(self.min_limit, 2)))
self._minimum = minimum
if '_maximum' in self.__dict__ and self._maximum < np.inf:
self._update_instance()
......@@ -116,6 +122,8 @@ class Interped(Prior):
Updates the prior distribution if maximum is set to a different value.
Yields an error if value is set above instantiated x-array maximum.
Returns
-------
float: Maximum of the prior distribution
......@@ -125,6 +133,8 @@ class Interped(Prior):
@maximum.setter
def maximum(self, maximum):
if maximum > self.max_limit:
raise ValueError('Maximum cannot be set above {}.'.format(round(self.max_limit, 2)))
self._maximum = maximum
if '_minimum' in self.__dict__ and self._minimum < np.inf:
self._update_instance()
......
......@@ -139,7 +139,10 @@ class Cosmological(Interped):
@minimum.setter
def minimum(self, minimum):
self._set_limit(value=minimum, limit_dict=self._minimum)
if (self.name in self._minimum) and (minimum < self.minimum):
self._set_limit(value=minimum, limit_dict=self._minimum, recalculate_array=True)
else:
self._set_limit(value=minimum, limit_dict=self._minimum)
@property
def maximum(self):
......@@ -147,11 +150,14 @@ class Cosmological(Interped):
@maximum.setter
def maximum(self, maximum):
self._set_limit(value=maximum, limit_dict=self._maximum)
if (self.name in self._maximum) and (maximum > self.maximum):
self._set_limit(value=maximum, limit_dict=self._maximum, recalculate_array=True)
else:
self._set_limit(value=maximum, limit_dict=self._maximum)
def _set_limit(self, value, limit_dict):
def _set_limit(self, value, limit_dict, recalculate_array=False):
"""
Set either the limits for redshift luminosity and comoving distances
Set either of the limits for redshift, luminosity, and comoving distances
Parameters
----------
......@@ -159,6 +165,8 @@ class Cosmological(Interped):
Limit value in current class' parameter
limit_dict: dict
The limit dictionary to modify in place
recalculate_array: boolean
Determines if the distance arrays are recalculated
"""
cosmology = get_cosmology(self.cosmology)
limit_dict[self.name] = value
......@@ -185,6 +193,13 @@ class Cosmological(Interped):
limit_dict['luminosity_distance'] = (
cosmology.luminosity_distance(limit_dict['redshift']).value
)
if recalculate_array:
if self.name == 'redshift':
self.xx, self.yy = self._get_redshift_arrays()
elif self.name == 'comoving_distance':
self.xx, self.yy = self._get_comoving_distance_arrays()
elif self.name == 'luminosity_distance':
self.xx, self.yy = self._get_luminosity_distance_arrays()
try:
self._update_instance()
except (AttributeError, KeyError):
......
......@@ -458,6 +458,14 @@ class TestUniformComovingVolumePrior(unittest.TestCase):
)
self.assertEqual(prior.maximum, 10000)
def test_increase_maximum(self):
prior = bilby.gw.prior.UniformComovingVolume(
minimum=10, maximum=10000, name="luminosity_distance"
)
prior.maximum = 20000
prior_sample = prior.sample(5000)
self.assertGreater(np.mean(prior_sample), 10000)
def test_zero_minimum_works(self):
prior = bilby.gw.prior.UniformComovingVolume(
minimum=0, maximum=10000, name="luminosity_distance"
......
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