...
 
Commits (79)
......@@ -423,7 +423,8 @@ class Prior(object):
maximum: float, optional
Maximum of the domain, default=np.inf
periodic_boundary: bool, optional
Whether or not the boundary condition is periodic. Not available in all samplers.
Whether or not the boundary condition is periodic.
Currently implemented in cpnest, dynesty and pymultinest.
"""
self.name = name
self.latex_label = latex_label
......
......@@ -494,7 +494,8 @@ class NestedSampler(Sampler):
class MCMCSampler(Sampler):
nwalkers_equiv_kwargs = ['nwalker', 'nwalkers', 'draws']
nwalkers_equiv_kwargs = ['nwalker', 'nwalkers', 'draws', 'Niter']
nburn_equiv_kwargs = ['burn', 'nburn']
def print_nburn_logging_info(self):
""" Prints logging info as to how nburn was calculated """
......
......@@ -71,7 +71,7 @@ class Dynesty(NestedSampler):
If true, resume run from checkpoint (if available)
"""
default_kwargs = dict(bound='multi', sample='rwalk',
verbose=True,
verbose=True, periodic=None,
check_point_delta_t=600, nlive=500,
first_update=None,
npdim=None, rstate=None, queue_size=None, pool=None,
......@@ -95,6 +95,7 @@ class Dynesty(NestedSampler):
self.n_check_point = n_check_point
self.check_point = check_point
self.resume = resume
self._apply_dynesty_boundaries()
if self.n_check_point is None:
# If the log_likelihood_eval_time is not calculable then
# check_point is set to False.
......@@ -173,6 +174,13 @@ class Dynesty(NestedSampler):
sys.stderr.write(print_str)
sys.stderr.flush()
def _apply_dynesty_boundaries(self):
if self.kwargs['periodic'] is None:
self.kwargs['periodic'] = []
for i, value in enumerate(self.priors.values()):
if value.periodic_boundary:
self.kwargs['periodic'].append(i)
def run_sampler(self):
import dynesty
self.sampler = dynesty.NestedSampler(
......
......@@ -78,7 +78,7 @@ class PTMCMCSampler(MCMCSampler):
def _translate_kwargs(self, kwargs):
if 'Niter' not in kwargs:
for equiv in self.nsteps_equiv_kwargs:
for equiv in self.nwalkers_equiv_kwargs:
if equiv in kwargs:
kwargs['Niter'] = kwargs.pop(equiv)
if 'burn' not in kwargs:
......
......@@ -46,6 +46,14 @@ class Pymultinest(NestedSampler):
context=0, write_output=True, log_zero=-1e100,
max_iter=0, init_MPI=False, dump_callback=None)
def __init__(self, likelihood, priors, outdir='outdir', label='label', use_ratio=False, plot=False,
skip_import_verification=False, **kwargs):
NestedSampler.__init__(self, likelihood=likelihood, priors=priors, outdir=outdir, label=label,
use_ratio=use_ratio, plot=plot,
skip_import_verification=skip_import_verification,
**kwargs)
self._apply_multinest_boundaries()
def _translate_kwargs(self, kwargs):
if 'n_live_points' not in kwargs:
for equiv in self.npoints_equiv_kwargs:
......@@ -75,6 +83,15 @@ class Pymultinest(NestedSampler):
self.kwargs['outputfiles_basename'])
NestedSampler._verify_kwargs_against_default_kwargs(self)
def _apply_multinest_boundaries(self):
if self.kwargs['wrapped_params'] is None:
self.kwargs['wrapped_params'] = []
for param, value in self.priors.items():
if value.periodic_boundary:
self.kwargs['wrapped_params'].append(1)
else:
self.kwargs['wrapped_params'].append(0)
def run_sampler(self):
import pymultinest
self._verify_kwargs_against_default_kwargs()
......
......@@ -132,7 +132,9 @@ class TestDynesty(unittest.TestCase):
def setUp(self):
self.likelihood = MagicMock()
self.priors = dict()
self.priors = bilby.core.prior.PriorDict()
self.priors['a'] = bilby.core.prior.Prior(periodic_boundary=True)
self.priors['b'] = bilby.core.prior.Prior(periodic_boundary=False)
self.sampler = bilby.core.sampler.Dynesty(self.likelihood, self.priors,
outdir='outdir', label='label',
use_ratio=False, plot=False,
......@@ -144,7 +146,7 @@ class TestDynesty(unittest.TestCase):
del self.sampler
def test_default_kwargs(self):
expected = dict(bound='multi', sample='rwalk', verbose=True,
expected = dict(bound='multi', sample='rwalk', periodic=None, verbose=True,
check_point_delta_t=600, nlive=500, first_update=None,
npdim=None, rstate=None, queue_size=None, pool=None,
use_pool=None, live_points=None, logl_args=None, logl_kwargs=None,
......@@ -152,12 +154,18 @@ class TestDynesty(unittest.TestCase):
enlarge=None, bootstrap=None, vol_dec=0.5, vol_check=2.0,
facc=0.5, slices=5, dlogz=0.1, maxiter=None, maxcall=None,
logl_max=np.inf, add_live=True, print_progress=True, save_bounds=True,
walks=0, update_interval=300, print_func='func')
walks=10, update_interval=300, print_func='func')
self.sampler.kwargs['print_func'] = 'func' # set this manually as this is not testable otherwise
self.assertListEqual([0], self.sampler.kwargs['periodic']) # Check this separately
self.sampler.kwargs['periodic'] = None # The dict comparison can't handle lists
for key in self.sampler.kwargs.keys():
print(key)
print(expected[key])
print(self.sampler.kwargs[key])
self.assertDictEqual(expected, self.sampler.kwargs)
def test_translate_kwargs(self):
expected = dict(bound='multi', sample='rwalk', verbose=True,
expected = dict(bound='multi', sample='rwalk', periodic=[0], verbose=True,
check_point_delta_t=600, nlive=250, first_update=None,
npdim=None, rstate=None, queue_size=None, pool=None,
use_pool=None, live_points=None, logl_args=None, logl_kwargs=None,
......@@ -165,7 +173,7 @@ class TestDynesty(unittest.TestCase):
enlarge=None, bootstrap=None, vol_dec=0.5, vol_check=2.0,
facc=0.5, slices=5, dlogz=0.1, maxiter=None, maxcall=None,
logl_max=np.inf, add_live=True, print_progress=True, save_bounds=True,
walks=0, update_interval=300, print_func='func')
walks=10, update_interval=300, print_func='func')
for equiv in bilby.core.sampler.base_sampler.NestedSampler.npoints_equiv_kwargs:
new_kwargs = self.sampler.kwargs.copy()
......@@ -385,7 +393,9 @@ class TestPymultinest(unittest.TestCase):
def setUp(self):
self.likelihood = MagicMock()
self.priors = dict()
self.priors = bilby.core.prior.PriorDict()
self.priors['a'] = bilby.core.prior.Prior(periodic_boundary=True)
self.priors['b'] = bilby.core.prior.Prior(periodic_boundary=False)
self.sampler = bilby.core.sampler.Pymultinest(self.likelihood, self.priors,
outdir='outdir', label='label',
use_ratio=False, plot=False,
......@@ -408,6 +418,8 @@ class TestPymultinest(unittest.TestCase):
max_modes=100, mode_tolerance=-1e90, seed=-1,
context=0, write_output=True, log_zero=-1e100,
max_iter=0, init_MPI=False, dump_callback=None)
self.assertListEqual([1, 0], self.sampler.kwargs['wrapped_params']) # Check this separately
self.sampler.kwargs['wrapped_params'] = None # The dict comparison can't handle lists
self.assertDictEqual(expected, self.sampler.kwargs)
def test_translate_kwargs(self):
......@@ -425,6 +437,7 @@ class TestPymultinest(unittest.TestCase):
for equiv in bilby.core.sampler.base_sampler.NestedSampler.npoints_equiv_kwargs:
new_kwargs = self.sampler.kwargs.copy()
del new_kwargs['n_live_points']
new_kwargs['wrapped_params'] = None # The dict comparison can't handle lists
new_kwargs[equiv] = 123
self.sampler.kwargs = new_kwargs
self.assertDictEqual(expected, self.sampler.kwargs)
......@@ -444,8 +457,9 @@ class TestRunningSamplers(unittest.TestCase):
self.likelihood = bilby.likelihood.GaussianLikelihood(
self.x, self.y, self.model, self.sigma)
self.priors = dict(
m=bilby.core.prior.Uniform(0, 5), c=bilby.core.prior.Uniform(-2, 2))
self.priors = bilby.core.prior.PriorDict()
self.priors['m'] = bilby.core.prior.Uniform(0, 5, periodic_boundary=False)
self.priors['c'] = bilby.core.prior.Uniform(-2, 2, periodic_boundary=False)
bilby.core.utils.check_directory_exists_and_if_not_mkdir('outdir')
def tearDown(self):
......