Skip to content
Snippets Groups Projects
Commit 630e3eaf authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Merge branch 'dynesty-hotfix' into 'master'

Hotfix for dynesty bounds

See merge request !604
parents eae16e8a 2e967e14
No related branches found
No related tags found
1 merge request!604Hotfix for dynesty bounds
Pipeline #81237 passed with warnings
......@@ -10,7 +10,7 @@ import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame
from ..utils import logger, check_directory_exists_and_if_not_mkdir
from ..utils import logger, check_directory_exists_and_if_not_mkdir, reflect
from .base_sampler import Sampler, NestedSampler
......@@ -216,6 +216,7 @@ class Dynesty(NestedSampler):
def run_sampler(self):
import dynesty
logger.info("Using dynesty version {}".format(dynesty.__version__))
if self.kwargs['live_points'] is None:
self.kwargs['live_points'] = (
self.get_initial_points_from_prior(
......@@ -509,4 +510,5 @@ class Dynesty(NestedSampler):
|theta| - 1 (i.e. wrap around).
"""
theta[self._reflective] = reflect(theta[self._reflective])
return self.priors.rescale(self._search_parameter_keys, theta)
......@@ -1004,6 +1004,33 @@ def decode_astropy_quantity(dct):
return dct
def reflect(u):
"""
Iteratively reflect a number until it is contained in [0, 1].
This is for priors with a reflective boundary condition, all numbers in the set `u = 2n +/- x` should be mapped to x.
For the `+` case we just take `u % 1`.
For the `-` case we take `1 - (u % 1)`.
E.g., -0.9, 1.1, and 2.9 should all map to 0.9.
Parameters
----------
u: array-like
The array of points to map to the unit cube
Returns
-------
u: array-like
The input array, modified in place.
"""
idxs_even = np.mod(u, 2) < 1
u[idxs_even] = np.mod(u[idxs_even], 1)
u[~idxs_even] = 1 - np.mod(u[~idxs_even], 1)
return u
class IllegalDurationAndSamplingFrequencyException(Exception):
pass
......
......@@ -165,5 +165,37 @@ class TestTimeAndFrequencyArrays(unittest.TestCase):
starting_time=0)
class TestReflect(unittest.TestCase):
def test_in_range(self):
xprime = np.array([0.1, 0.5, 0.9])
x = np.array([0.1, 0.5, 0.9])
self.assertTrue(
np.testing.assert_allclose(utils.reflect(xprime), x) is None)
def test_in_one_to_two(self):
xprime = np.array([1.1, 1.5, 1.9])
x = np.array([0.9, 0.5, 0.1])
self.assertTrue(
np.testing.assert_allclose(utils.reflect(xprime), x) is None)
def test_in_two_to_three(self):
xprime = np.array([2.1, 2.5, 2.9])
x = np.array([0.1, 0.5, 0.9])
self.assertTrue(
np.testing.assert_allclose(utils.reflect(xprime), x) is None)
def test_in_minus_one_to_zero(self):
xprime = np.array([-0.9, -0.5, -0.1])
x = np.array([0.9, 0.5, 0.1])
self.assertTrue(
np.testing.assert_allclose(utils.reflect(xprime), x) is None)
def test_in_minus_two_to_minus_one(self):
xprime = np.array([-1.9, -1.5, -1.1])
x = np.array([0.1, 0.5, 0.9])
self.assertTrue(
np.testing.assert_allclose(utils.reflect(xprime), x) is None)
if __name__ == '__main__':
unittest.main()
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