From ac422cd7b72d6d2f17ee0351b3381b4c184203bb Mon Sep 17 00:00:00 2001 From: Colm Talbot <colm.talbot@ligo.org> Date: Fri, 21 May 2021 02:24:02 +0000 Subject: [PATCH] Resolve "Get rid off `Prior.test_valid_for_rescaling`?" --- bilby/core/prior/analytical.py | 17 ----------------- bilby/core/prior/base.py | 17 ----------------- bilby/core/prior/interpolated.py | 1 - bilby/core/prior/joint.py | 1 - bilby/gw/prior.py | 1 - examples/tutorials/making_priors.ipynb | 1 - test/core/prior/prior_test.py | 5 ----- 7 files changed, 43 deletions(-) diff --git a/bilby/core/prior/analytical.py b/bilby/core/prior/analytical.py index 722d4553f..96b8a9e92 100644 --- a/bilby/core/prior/analytical.py +++ b/bilby/core/prior/analytical.py @@ -40,7 +40,6 @@ class DeltaFunction(Prior): ======= float: Rescaled probability, equivalent to peak """ - self.test_valid_for_rescaling(val) return self.peak * val ** 0 def prob(self, val): @@ -105,7 +104,6 @@ class PowerLaw(Prior): ======= Union[float, array_like]: Rescaled probability """ - self.test_valid_for_rescaling(val) if self.alpha == -1: return self.minimum * np.exp(val * np.log(self.maximum / self.minimum)) else: @@ -206,7 +204,6 @@ class Uniform(Prior): ======= Union[float, array_like]: Rescaled probability """ - self.test_valid_for_rescaling(val) return self.minimum + val * (self.maximum - self.minimum) def prob(self, val): @@ -314,7 +311,6 @@ class SymmetricLogUniform(Prior): ======= Union[float, array_like]: Rescaled probability """ - self.test_valid_for_rescaling(val) if isinstance(val, (float, int)): if val < 0.5: return -self.maximum * np.exp(-2 * val * np.log(self.maximum / self.minimum)) @@ -401,7 +397,6 @@ class Cosine(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) norm = 1 / (np.sin(self.maximum) - np.sin(self.minimum)) return np.arcsin(val / norm + np.sin(self.minimum)) @@ -456,7 +451,6 @@ class Sine(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) norm = 1 / (np.cos(self.minimum) - np.cos(self.maximum)) return np.arccos(np.cos(self.minimum) - val / norm) @@ -515,7 +509,6 @@ class Gaussian(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) return self.mu + erfinv(2 * val - 1) * 2 ** 0.5 * self.sigma def prob(self, val): @@ -602,7 +595,6 @@ class TruncatedGaussian(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) return erfinv(2 * val * self.normalisation + erf( (self.minimum - self.mu) / 2 ** 0.5 / self.sigma)) * 2 ** 0.5 * self.sigma + self.mu @@ -695,7 +687,6 @@ class LogNormal(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) return np.exp(self.mu + np.sqrt(2 * self.sigma ** 2) * erfinv(2 * val - 1)) def prob(self, val): @@ -790,7 +781,6 @@ class Exponential(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) return -self.mu * log1p(-val) def prob(self, val): @@ -887,7 +877,6 @@ class StudentT(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) if isinstance(val, (float, int)): if val == 0: rescaled = -np.inf @@ -977,7 +966,6 @@ class Beta(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) return btdtri(self.alpha, self.beta, val) * (self.maximum - self.minimum) + self.minimum def prob(self, val): @@ -1070,7 +1058,6 @@ class Logistic(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) if isinstance(val, (float, int)): if val == 0: rescaled = -np.inf @@ -1151,7 +1138,6 @@ class Cauchy(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) rescaled = self.alpha + self.beta * np.tan(np.pi * (val - 0.5)) if isinstance(val, (float, int)): if val == 1: @@ -1233,7 +1219,6 @@ class Gamma(Prior): This maps to the inverse CDF. This has been analytically solved for this case. """ - self.test_valid_for_rescaling(val) return gammaincinv(self.k, val) * self.theta def prob(self, val): @@ -1385,8 +1370,6 @@ class FermiDirac(Prior): .. [1] M. Pitkin, M. Isi, J. Veitch & G. Woan, `arXiv:1705.08978v1 <https:arxiv.org/abs/1705.08978v1>`_, 2017. """ - self.test_valid_for_rescaling(val) - inv = (-np.exp(-1. * self.r) + (1. + np.exp(self.r)) ** -val + np.exp(-1. * self.r) * (1. + np.exp(self.r)) ** -val) diff --git a/bilby/core/prior/base.py b/bilby/core/prior/base.py index 023f1609f..3edeec29b 100644 --- a/bilby/core/prior/base.py +++ b/bilby/core/prior/base.py @@ -202,23 +202,6 @@ class Prior(object): """ return (val >= self.minimum) & (val <= self.maximum) - @staticmethod - def test_valid_for_rescaling(val): - """Test if 0 < val < 1 - - Parameters - ========== - val: Union[float, int, array_like] - - Raises - ======= - ValueError: If val is not between 0 and 1 - """ - valarray = np.atleast_1d(val) - tests = (valarray < 0) + (valarray > 1) - if np.any(tests): - raise ValueError("Number to be rescaled should be in [0, 1]") - def __repr__(self): """Overrides the special method __repr__. diff --git a/bilby/core/prior/interpolated.py b/bilby/core/prior/interpolated.py index ece311a5a..187e8a60a 100644 --- a/bilby/core/prior/interpolated.py +++ b/bilby/core/prior/interpolated.py @@ -86,7 +86,6 @@ class Interped(Prior): This maps to the inverse CDF. This is done using interpolation. """ - self.test_valid_for_rescaling(val) rescaled = self.inverse_cumulative_distribution(val) if rescaled.shape == (): rescaled = float(rescaled) diff --git a/bilby/core/prior/joint.py b/bilby/core/prior/joint.py index a10a0add2..393a32b9c 100644 --- a/bilby/core/prior/joint.py +++ b/bilby/core/prior/joint.py @@ -710,7 +710,6 @@ class JointPrior(Prior): A sample from the prior paramter. """ - self.test_valid_for_rescaling(val) self.dist.rescale_parameters[self.name] = val if self.dist.filled_rescale(): diff --git a/bilby/gw/prior.py b/bilby/gw/prior.py index 739cbf6a0..f9b6c182b 100644 --- a/bilby/gw/prior.py +++ b/bilby/gw/prior.py @@ -369,7 +369,6 @@ class UniformInComponentsMassRatio(Prior): return (self._integral(val) - self._integral(self.minimum)) / self.norm def rescale(self, val): - self.test_valid_for_rescaling(val) resc = self.icdf(val) if resc.ndim == 0: return resc.item() diff --git a/examples/tutorials/making_priors.ipynb b/examples/tutorials/making_priors.ipynb index 7f9d5719e..8daef3bc1 100644 --- a/examples/tutorials/making_priors.ipynb +++ b/examples/tutorials/making_priors.ipynb @@ -127,7 +127,6 @@ " self.alpha = alpha\n", " \n", " def rescale(self, val):\n", - " bilby.prior.Prior.test_valid_for_rescaling(val)\n", " return np.log((np.exp(self.alpha * self.maximum) - np.exp(self.alpha * self.minimum)) * val\n", " + np.exp(self.alpha * self.minimum)) / self.alpha\n", " \n", diff --git a/test/core/prior/prior_test.py b/test/core/prior/prior_test.py index 6afdc9933..924194543 100644 --- a/test/core/prior/prior_test.py +++ b/test/core/prior/prior_test.py @@ -239,11 +239,6 @@ class TestPriorClasses(unittest.TestCase): all((many_samples >= prior.minimum) & (many_samples <= prior.maximum)) ) - def test_out_of_bounds_rescaling(self): - """Test the the rescaling works as expected.""" - for prior in self.priors: - self.assertRaises(ValueError, lambda: prior.rescale(-1)) - def test_least_recently_sampled(self): for prior in self.priors: least_recently_sampled_expected = prior.sample() -- GitLab