Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • john-veitch/bilby
  • duncanmmacleod/bilby
  • colm.talbot/bilby
  • lscsoft/bilby
  • matthew-pitkin/bilby
  • salvatore-vitale/tupak
  • charlie.hoy/bilby
  • bfarr/bilby
  • virginia.demilio/bilby
  • vivien/bilby
  • eric-howell/bilby
  • sebastian-khan/bilby
  • rhys.green/bilby
  • moritz.huebner/bilby
  • joseph.mills/bilby
  • scott.coughlin/bilby
  • matthew.carney/bilby
  • hyungwon.lee/bilby
  • monica.rizzo/bilby
  • christopher-berry/bilby
  • lindsay.demarchi/bilby
  • kaushik.rao/bilby
  • charles.kimball/bilby
  • andrew.matas/bilby
  • juan.calderonbustillo/bilby
  • patrick-meyers/bilby
  • hannah.middleton/bilby
  • eve.chase/bilby
  • grant.meadors/bilby
  • khun.phukon/bilby
  • sumeet.kulkarni/bilby
  • daniel.reardon/bilby
  • cjhaster/bilby
  • sylvia.biscoveanu/bilby
  • james-clark/bilby
  • meg.millhouse/bilby
  • joshua.willis/bilby
  • nikhil.sarin/bilby
  • paul.easter/bilby
  • youngmin/bilby
  • daniel-williams/bilby
  • shanika.galaudage/bilby
  • bruce.edelman/bilby
  • avi.vajpeyi/bilby
  • isobel.romero-shaw/bilby
  • andrew.kim/bilby
  • dominika.zieba/bilby
  • jonathan.davies/bilby
  • marc.arene/bilby
  • srishti.tiwari/bilby-tidal-heating-eccentric
  • aditya.vijaykumar/bilby
  • michael.williams/bilby
  • cecilio.garcia-quiros/bilby
  • rory-smith/bilby
  • maite.mateu-lucena/bilby
  • wushichao/bilby
  • kaylee.desoto/bilby
  • brandon.piotrzkowski/bilby
  • rossella.gamba/bilby
  • hunter.gabbard/bilby
  • deep.chatterjee/bilby
  • tathagata.ghosh/bilby
  • arunava.mukherjee/bilby
  • philip.relton/bilby
  • reed.essick/bilby
  • pawan.gupta/bilby
  • francisco.hernandez/bilby
  • rhiannon.udall/bilby
  • leo.tsukada/bilby
  • will-farr/bilby
  • vijay.varma/bilby
  • jeremy.baier/bilby
  • joshua.brandt/bilby
  • ethan.payne/bilby
  • ka-lok.lo/bilby
  • antoni.ramos-buades/bilby
  • oliviastephany.wilk/bilby
  • jack.heinzel/bilby
  • samson.leong/bilby-psi4
  • viviana.caceres/bilby
  • nadia.qutob/bilby
  • michael-coughlin/bilby
  • hemantakumar.phurailatpam/bilby
  • boris.goncharov/bilby
  • sama.al-shammari/bilby
  • siqi.zhong/bilby
  • jocelyn-read/bilby
  • marc.penuliar/bilby
  • stephanie.letourneau/bilby
  • alexandresebastien.goettel/bilby
  • alec.gunny/bilby
  • serguei.ossokine/bilby
  • pratyusava.baral/bilby
  • sophie.hourihane/bilby
  • eunsub/bilby
  • james.hart/bilby
  • pratyusava.baral/bilby-tg
  • zhaozc/bilby
  • pratyusava.baral/bilby_SoG
  • tomasz.baka/bilby
  • nicogerardo.bers/bilby
  • soumen.roy/bilby
  • isaac.mcmahon/healpix-redundancy
  • asamakai.baker/bilby-frequency-dependent-antenna-pattern-functions
  • anna.puecher/bilby
  • pratyusava.baral/bilby-x-g
  • thibeau.wouters/bilby
  • christian.adamcewicz/bilby
  • raffi.enficiaud/bilby
109 results
Show changes
Showing
with 344 additions and 260 deletions
FROM containers.ligo.org/docker/base:conda
LABEL name="bilby CI testing" \
maintainer="Gregory Ashton <gregory.ashton@ligo.org>"
RUN conda update -n base -c defaults conda
maintainer="Gregory Ashton <gregory.ashton@ligo.org>, Colm Talbot <colm.talbot@ligo.org>"
COPY env-template.yml env.yml
RUN echo " - python={python_major_version}.{python_minor_version}" >> env.yml
ENV conda_env python{python_major_version}{python_minor_version}
RUN conda create -n ${{conda_env}} python={python_major_version}.{python_minor_version}
RUN mamba env create -f env.yml -n ${{conda_env}}
RUN echo "source activate ${{conda_env}}" > ~/.bashrc
ENV PATH /opt/conda/envs/${{conda_env}}/bin:$PATH
RUN /bin/bash -c "source activate ${{conda_env}}"
RUN conda info
RUN mamba info
RUN python --version
# Install conda-installable programs
RUN conda install -n ${{conda_env}} -y matplotlib numpy scipy pandas astropy flake8
RUN conda install -n ${{conda_env}} -c anaconda coverage configargparse future dill
RUN conda install -n ${{conda_env}} -c conda-forge black pytest-cov deepdish arviz
# Install pip-requirements
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools coverage-badge parameterized
# Install documentation requirements
RUN pip install sphinx numpydoc nbsphinx sphinx_rtd_theme sphinx-tabs autodoc
# Install testing requirements
RUN conda install -n ${{conda_env}} -c conda-forge scikit-image celerite george
# Install dependencies and samplers
RUN pip install corner healpy cython tables
RUN conda install -n ${{conda_env}} {conda_samplers} -c conda-forge -c pytorch
# Install Polychord
RUN apt-get update --allow-releaseinfo-change
RUN apt-get install -y build-essential
RUN apt-get install -y libblas3 libblas-dev
RUN apt-get install -y liblapack3 liblapack-dev
RUN apt-get install -y libatlas3-base libatlas-base-dev
RUN apt-get install -y gfortran
RUN git clone https://github.com/PolyChord/PolyChordLite.git \
&& (cd PolyChordLite && python setup.py --no-mpi install)
# Install GW packages
RUN conda install -n ${{conda_env}} -c conda-forge python-lalsimulation bilby.cython pyseobnr
RUN pip install ligo-gracedb gwpy ligo.skymap
# Add the ROQ data to the image
RUN mkdir roq_basis \
&& cd roq_basis \
......
# This is a template yaml file for the test image
# The python version should be added before creating the env
channels:
- conda-forge
- defaults
dependencies:
- pip
- setuptools
- setuptools_scm
- matplotlib
- numpy
- scipy
- pandas
- astropy
- flake8
- anaconda
- coverage
- configargparse
- future
- dill
- black
- pytest-cov
- pytest-requires
- arviz
- parameterized
- scikit-image
- celerite
- george
- corner
- healpy
- cython
- pytables
- pytorch
- python-lalsimulation
- bilby.cython
- pyseobnr
- ligo-gracedb
- gwpy
- ligo.skymap
- sphinx
- numpydoc
- nbsphinx
- sphinx_rtd_theme
- sphinx-tabs
- dynesty
- emcee
- nestle
- ptemcee
- pymultinest
- ultranest
- cpnest
- kombine
- dnest4
- zeus-mcmc
- pytorch
- pymc>=5.9
- nessai
- ptmcmcsampler
- jaxlib>=0.4
- jax>=0.4
- numba>0.53.1
- make
- pre-commit
- pandoc
- ipython
- jupyter
- nbconvert
- twine
- glasflow
- pip:
- autodoc
- ipykernel
- build
......@@ -2,53 +2,19 @@
FROM containers.ligo.org/docker/base:conda
LABEL name="bilby CI testing" \
maintainer="Gregory Ashton <gregory.ashton@ligo.org>"
RUN conda update -n base -c defaults conda
maintainer="Gregory Ashton <gregory.ashton@ligo.org>, Colm Talbot <colm.talbot@ligo.org>"
COPY env-template.yml env.yml
RUN echo " - python=3.10" >> env.yml
ENV conda_env python310
RUN conda create -n ${conda_env} python=3.10
RUN mamba env create -f env.yml -n ${conda_env}
RUN echo "source activate ${conda_env}" > ~/.bashrc
ENV PATH /opt/conda/envs/${conda_env}/bin:$PATH
RUN /bin/bash -c "source activate ${conda_env}"
RUN conda info
RUN mamba info
RUN python --version
# Install conda-installable programs
RUN conda install -n ${conda_env} -y matplotlib numpy scipy pandas astropy flake8
RUN conda install -n ${conda_env} -c anaconda coverage configargparse future dill
RUN conda install -n ${conda_env} -c conda-forge black pytest-cov deepdish arviz
# Install pip-requirements
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools coverage-badge parameterized
# Install documentation requirements
RUN pip install sphinx numpydoc nbsphinx sphinx_rtd_theme sphinx-tabs autodoc
# Install testing requirements
RUN conda install -n ${conda_env} -c conda-forge scikit-image celerite george
# Install dependencies and samplers
RUN pip install corner healpy cython tables
RUN conda install -n ${conda_env} dynesty emcee nestle ptemcee pymultinest ultranest cpnest kombine dnest4 zeus-mcmc pytorch 'pymc>=4' nessai ptmcmcsampler -c conda-forge -c pytorch
# Install Polychord
RUN apt-get update --allow-releaseinfo-change
RUN apt-get install -y build-essential
RUN apt-get install -y libblas3 libblas-dev
RUN apt-get install -y liblapack3 liblapack-dev
RUN apt-get install -y libatlas3-base libatlas-base-dev
RUN apt-get install -y gfortran
RUN git clone https://github.com/PolyChord/PolyChordLite.git \
&& (cd PolyChordLite && python setup.py --no-mpi install)
# Install GW packages
RUN conda install -n ${conda_env} -c conda-forge python-lalsimulation bilby.cython pyseobnr
RUN pip install ligo-gracedb gwpy ligo.skymap
# Add the ROQ data to the image
RUN mkdir roq_basis \
&& cd roq_basis \
......
# This dockerfile is written automatically and should not be modified by hand.
FROM containers.ligo.org/docker/base:conda
LABEL name="bilby CI testing" \
maintainer="Gregory Ashton <gregory.ashton@ligo.org>, Colm Talbot <colm.talbot@ligo.org>"
COPY env-template.yml env.yml
RUN echo " - python=3.11" >> env.yml
ENV conda_env python311
RUN mamba env create -f env.yml -n ${conda_env}
RUN echo "source activate ${conda_env}" > ~/.bashrc
ENV PATH /opt/conda/envs/${conda_env}/bin:$PATH
RUN /bin/bash -c "source activate ${conda_env}"
RUN mamba info
RUN python --version
# Add the ROQ data to the image
RUN mkdir roq_basis \
&& cd roq_basis \
&& wget https://git.ligo.org/lscsoft/ROQ_data/raw/master/IMRPhenomPv2/4s/B_linear.npy \
&& wget https://git.ligo.org/lscsoft/ROQ_data/raw/master/IMRPhenomPv2/4s/B_quadratic.npy \
&& wget https://git.ligo.org/lscsoft/ROQ_data/raw/master/IMRPhenomPv2/4s/fnodes_linear.npy \
&& wget https://git.ligo.org/lscsoft/ROQ_data/raw/master/IMRPhenomPv2/4s/fnodes_quadratic.npy \
&& wget https://git.ligo.org/lscsoft/ROQ_data/raw/master/IMRPhenomPv2/4s/params.dat \
&& wget https://git.ligo.org/soichiro.morisaki/roq_basis/raw/main/IMRPhenomD/16s_nospins/basis_addcal.hdf5 \
&& wget https://git.ligo.org/soichiro.morisaki/roq_basis/raw/main/IMRPhenomD/16s_nospins/basis_multiband_addcal.hdf5
......@@ -2,53 +2,19 @@
FROM containers.ligo.org/docker/base:conda
LABEL name="bilby CI testing" \
maintainer="Gregory Ashton <gregory.ashton@ligo.org>"
RUN conda update -n base -c defaults conda
maintainer="Gregory Ashton <gregory.ashton@ligo.org>, Colm Talbot <colm.talbot@ligo.org>"
COPY env-template.yml env.yml
RUN echo " - python=3.9" >> env.yml
ENV conda_env python39
RUN conda create -n ${conda_env} python=3.9
RUN mamba env create -f env.yml -n ${conda_env}
RUN echo "source activate ${conda_env}" > ~/.bashrc
ENV PATH /opt/conda/envs/${conda_env}/bin:$PATH
RUN /bin/bash -c "source activate ${conda_env}"
RUN conda info
RUN mamba info
RUN python --version
# Install conda-installable programs
RUN conda install -n ${conda_env} -y matplotlib numpy scipy pandas astropy flake8
RUN conda install -n ${conda_env} -c anaconda coverage configargparse future dill
RUN conda install -n ${conda_env} -c conda-forge black pytest-cov deepdish arviz
# Install pip-requirements
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools coverage-badge parameterized
# Install documentation requirements
RUN pip install sphinx numpydoc nbsphinx sphinx_rtd_theme sphinx-tabs autodoc
# Install testing requirements
RUN conda install -n ${conda_env} -c conda-forge scikit-image celerite george
# Install dependencies and samplers
RUN pip install corner healpy cython tables
RUN conda install -n ${conda_env} dynesty emcee nestle ptemcee pymultinest ultranest cpnest kombine dnest4 zeus-mcmc pytorch 'pymc>=4' nessai ptmcmcsampler -c conda-forge -c pytorch
# Install Polychord
RUN apt-get update --allow-releaseinfo-change
RUN apt-get install -y build-essential
RUN apt-get install -y libblas3 libblas-dev
RUN apt-get install -y liblapack3 liblapack-dev
RUN apt-get install -y libatlas3-base libatlas-base-dev
RUN apt-get install -y gfortran
RUN git clone https://github.com/PolyChord/PolyChordLite.git \
&& (cd PolyChordLite && python setup.py --no-mpi install)
# Install GW packages
RUN conda install -n ${conda_env} -c conda-forge python-lalsimulation bilby.cython pyseobnr
RUN pip install ligo-gracedb gwpy ligo.skymap
# Add the ROQ data to the image
RUN mkdir roq_basis \
&& cd roq_basis \
......
......@@ -3,15 +3,9 @@ from datetime import date
with open("dockerfile-template", "r") as ff:
template = ff.read()
python_versions = [(3, 9), (3, 10)]
python_versions = [(3, 9), (3, 10), (3, 11)]
today = date.today().strftime("%Y%m%d")
samplers = [
"dynesty", "emcee", "nestle", "ptemcee", "pymultinest", "ultranest",
"cpnest", "kombine", "dnest4", "zeus-mcmc",
"pytorch", "'pymc>=4'", "nessai", "ptmcmcsampler",
]
for python_major_version, python_minor_version in python_versions:
key = f"python{python_major_version}{python_minor_version}"
with open(
......@@ -26,5 +20,4 @@ for python_major_version, python_minor_version in python_versions:
date=today,
python_major_version=python_major_version,
python_minor_version=python_minor_version,
conda_samplers=" ".join(samplers)
))
api/
......@@ -37,8 +37,8 @@ new point from the constrained prior. These can be specified using the
iterations of :code:`dynesty`. With this method, :code:`nact=2` often gives good
results, however, in some cases, a larger value may be required.
2. :code:`sample="acceptance-walk"`: with this method, the length of each MCMC
chain is predetermined. The specific length evolves during the run to yield an
2. :code:`sample="acceptance-walk"`: with this method, at each iteration all MCMC chains
are set to the same length. The specific length evolves during the run to yield an
average of :code:`naccept` accepted jumps during each chain. This method is well
suited to parallelised applications, as each MCMC chain will run for the same
amount of time. The value of :code:`naccept` should be tuned based on the
......
......@@ -10,7 +10,7 @@ Installation
$ conda install -c conda-forge bilby
Supported python versions: 3.8-3.10.
Supported python versions: 3.9-3.11.
.. tab:: Pip
......@@ -18,7 +18,7 @@ Installation
$ pip install bilby
Supported python versions: 3.8-3.10.
Supported python versions: 3.9-3.11.
This will install all requirements for running :code:`bilby` for general
......@@ -47,7 +47,7 @@ wave inference, please additionally run the following commands.
Install bilby from source
-------------------------
:code:`bilby` is developed and tested with Python 3.8-3.10. In the
:code:`bilby` is developed and tested with Python 3.9-3.11. In the
following, we assume you have a working python installation, `python pip
<https://packaging.python.org/tutorials/installing-packages/#use-pip-for-installing)>`_,
and `git <https://git-scm.com/>`_. See :ref:`installing-python` for our
......
......@@ -3,7 +3,7 @@ dynesty>=2.0.1
emcee
corner
numpy
matplotlib<3.8.0
matplotlib
scipy>=1.5
pandas
dill
......
......@@ -66,7 +66,7 @@ setup(
"bilby.gw.detector": ["noise_curves/*.txt", "detectors/*"],
"bilby.gw.eos": ["eos_tables/*.dat"],
},
python_requires=">=3.8",
python_requires=">=3.9",
install_requires=get_requirements(),
extras_require={
"gw": get_requirements("gw"),
......@@ -87,6 +87,7 @@ setup(
classifiers=[
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
......
......@@ -11,6 +11,7 @@ from bilby.bilby_mcmc.chain import Chain, Sample
from bilby.bilby_mcmc import proposals
from bilby.bilby_mcmc.utils import LOGLKEY, LOGPKEY
import numpy as np
import pytest
class GivenProposal(proposals.BaseProposal):
......@@ -164,36 +165,32 @@ class TestProposals(TestBaseProposals):
else:
print("Unable to test GMM as sklearn is not installed")
@pytest.mark.requires("glasflow")
def test_NF_proposal(self):
priors = self.create_priors()
chain = self.create_chain(10000)
if proposals.NormalizingFlowProposal.check_dependencies():
prop = proposals.NormalizingFlowProposal(priors, first_fit=10000)
prop.steps_since_refit = 9999
start = time.time()
p, w = prop(chain)
dt = time.time() - start
print(f"Training for {prop.__class__.__name__} took dt~{dt:0.2g} [s]")
self.assertTrue(prop.trained)
self.proposal_check(prop)
else:
print("nflows not installed, unable to test NormalizingFlowProposal")
prop = proposals.NormalizingFlowProposal(priors, first_fit=10000)
prop.steps_since_refit = 9999
start = time.time()
p, w = prop(chain)
dt = time.time() - start
print(f"Training for {prop.__class__.__name__} took dt~{dt:0.2g} [s]")
self.assertTrue(prop.trained)
self.proposal_check(prop)
@pytest.mark.requires("glasflow")
def test_NF_proposal_15D(self):
ndim = 15
priors = self.create_priors(ndim)
chain = self.create_chain(10000, ndim=ndim)
if proposals.NormalizingFlowProposal.check_dependencies():
prop = proposals.NormalizingFlowProposal(priors, first_fit=10000)
prop.steps_since_refit = 9999
start = time.time()
p, w = prop(chain)
dt = time.time() - start
print(f"Training for {prop.__class__.__name__} took dt~{dt:0.2g} [s]")
self.assertTrue(prop.trained)
self.proposal_check(prop, ndim=ndim)
else:
print("nflows not installed, unable to test NormalizingFlowProposal")
prop = proposals.NormalizingFlowProposal(priors, first_fit=10000)
prop.steps_since_refit = 9999
start = time.time()
p, w = prop(chain)
dt = time.time() - start
print(f"Training for {prop.__class__.__name__} took dt~{dt:0.2g} [s]")
self.assertTrue(prop.trained)
self.proposal_check(prop, ndim=ndim)
if __name__ == "__main__":
......
import pytest
def pytest_addoption(parser):
parser.addoption(
"--skip-roqs", action="store_true", default=False, help="Skip all tests that require ROQs"
)
def pytest_configure(config):
config.addinivalue_line("markers", "requires_roqs: mark a test that requires ROQs")
def pytest_collection_modifyitems(config, items):
if config.getoption("--skip-roqs"):
skip_roqs = pytest.mark.skip(reason="Skipping tests that require ROQs")
for item in items:
if "requires_roqs" in item.keywords:
item.add_marker(skip_roqs)
......@@ -5,6 +5,7 @@ from unittest import mock
import numpy as np
import pandas as pd
import pickle
import bilby
......@@ -412,6 +413,38 @@ class TestConditionalPriorDict(unittest.TestCase):
res = priors.rescale(["a", "b", "d", "c"], [0.5, 0.5, 0.5, 0.5])
print(res)
def test_subset_sampling(self):
def _tp_conditional_uniform(ref_params, period):
min_ref, max_ref = ref_params["minimum"], ref_params["maximum"]
max_ref = np.minimum(max_ref, min_ref + period)
return {"minimum": min_ref, "maximum": max_ref}
p0 = 68400.0
prior = bilby.core.prior.ConditionalPriorDict(
{
"tp": bilby.core.prior.ConditionalUniform(
condition_func=_tp_conditional_uniform, minimum=0, maximum=2 * p0
)
}
)
# ---------- 0. Sanity check: sample full prior
prior["period"] = p0
samples2d = prior.sample(1000)
assert samples2d["tp"].max() < p0
# ---------- 1. Subset sampling with external delta-prior
print("Test 1: Subset-sampling conditionals for fixed 'externals':")
prior["period"] = p0
samples1d = prior.sample_subset(["tp"], 1000)
self.assertLess(samples1d["tp"].max(), p0)
# ---------- 2. Subset sampling with external uniform prior
prior["period"] = bilby.core.prior.Uniform(minimum=p0, maximum=2 * p0)
print("Test 2: Subset-sampling conditionals for 'external' uncertainties:")
with self.assertRaises(bilby.core.prior.IllegalConditionsException):
prior.sample_subset(["tp"], 1000)
class TestDirichletPrior(unittest.TestCase):
......@@ -440,6 +473,10 @@ class TestDirichletPrior(unittest.TestCase):
test = bilby.core.prior.PriorDict.from_json(filename="priors/test_prior.json")
self.assertEqual(self.priors, test)
def test_pickle(self):
"""Assert can be pickled (needed for use with bilby_pipe)"""
pickle.dumps(self.priors)
if __name__ == "__main__":
unittest.main()
......@@ -751,5 +751,71 @@ class TestPPPlots(unittest.TestCase):
)
class SimpleGaussianLikelihood(bilby.core.likelihood.Likelihood):
def __init__(self, mean=0, sigma=1):
"""
A very simple Gaussian likelihood for testing
"""
from scipy.stats import norm
super().__init__(parameters=dict())
self.mean = mean
self.sigma = sigma
self.dist = norm(loc=mean, scale=sigma)
def log_likelihood(self):
return self.dist.logpdf(self.parameters["mu"])
class TestReweight(unittest.TestCase):
def setUp(self):
self.priors = bilby.core.prior.PriorDict(dict(
mu=bilby.core.prior.TruncatedNormal(0, 1, minimum=-5, maximum=5),
))
self.result = bilby.core.result.Result(
search_parameter_keys=list(self.priors.keys()),
priors=self.priors,
posterior=pd.DataFrame(self.priors.sample(1000)),
log_evidence=-np.log(10),
)
def _run_reweighting(self, sigma):
likelihood_1 = SimpleGaussianLikelihood()
likelihood_2 = SimpleGaussianLikelihood(sigma=sigma)
original_ln_likelihoods = list()
for ii in range(len(self.result.posterior)):
likelihood_1.parameters = self.result.posterior.iloc[ii]
original_ln_likelihoods.append(likelihood_1.log_likelihood())
self.result.posterior["log_prior"] = self.priors.ln_prob(self.result.posterior)
self.result.posterior["log_likelihood"] = original_ln_likelihoods
self.original_ln_likelihoods = original_ln_likelihoods
return bilby.core.result.reweight(
self.result, likelihood_1, likelihood_2, verbose_output=True
)
def test_reweight_same_likelihood_weights_1(self):
"""
When the likelihoods are the same, the weights should be 1.
"""
_, weights, _, _, _, _ = self._run_reweighting(sigma=1)
self.assertLess(min(abs(weights - 1)), 1e-10)
def test_reweight_different_likelihood_weights_correct(self):
"""
Test the known case where the target likelihood is a Gaussian with
sigma=0.5. The weights can be calculated analytically and the evidence
should be close to the original evidence within statistical error.
"""
from scipy.stats import norm
new, weights, _, _, _, _ = self._run_reweighting(sigma=0.5)
expected_weights = (
norm(0, 0.5).pdf(self.result.posterior["mu"])
/ norm(0, 1).pdf(self.result.posterior["mu"])
)
self.assertLess(min(abs(weights - expected_weights)), 1e-10)
self.assertLess(abs(new.log_evidence - self.result.log_evidence), 0.05)
self.assertNotEqual(new.log_evidence, self.result.log_evidence)
if __name__ == "__main__":
unittest.main()
......@@ -7,6 +7,7 @@ import numpy as np
import parameterized
from bilby.core.sampler import dynesty_utils
from scipy.stats import gamma, ks_1samp, uniform, powerlaw
import shutil
@define
......@@ -271,5 +272,78 @@ class TestEstimateNMCMC(unittest.TestCase):
self.assertAlmostEqual(estimated, expected)
class TestReproducibility(unittest.TestCase):
@staticmethod
def model(x, m, c):
return m * x + c
def setUp(self):
bilby.core.utils.random.seed(42)
bilby.core.utils.command_line_args.bilby_test_mode = False
rng = bilby.core.utils.random.rng
self.x = np.linspace(0, 1, 11)
self.injection_parameters = dict(m=0.5, c=0.2)
self.sigma = 0.1
self.y = self.model(self.x, **self.injection_parameters) + rng.normal(
0, self.sigma, len(self.x)
)
self.likelihood = bilby.likelihood.GaussianLikelihood(
self.x, self.y, self.model, self.sigma
)
self.priors = bilby.core.prior.PriorDict()
self.priors["m"] = bilby.core.prior.Uniform(0, 5, boundary="periodic")
self.priors["c"] = bilby.core.prior.Uniform(-2, 2, boundary="reflective")
# Evaluate prior once to ensure normalization constant have been set
theta = self.priors.sample()
self.priors.ln_prob(theta)
self._remove_tree()
bilby.core.utils.check_directory_exists_and_if_not_mkdir("outdir")
def tearDown(self):
del self.likelihood
del self.priors
bilby.core.utils.command_line_args.bilby_test_mode = False
self._remove_tree()
def _remove_tree(self):
try:
shutil.rmtree("outdir")
except OSError:
pass
def _run_sampler(self, **kwargs):
bilby.core.utils.random.seed(42)
return bilby.run_sampler(
likelihood=self.likelihood,
priors=self.priors,
sampler="dynesty",
save=False,
resume=False,
dlogz=1.0,
nlive=20,
**kwargs,
)
def test_reproducibility_seed(self):
res0 = self._run_sampler(seed=1234)
res1 = self._run_sampler(seed=1234)
assert res0.log_evidence == res1.log_evidence
def test_reproducibility_state(self):
rstate = np.random.default_rng(1234)
res0 = self._run_sampler(rstate=rstate)
rstate = np.random.default_rng(1234)
res1 = self._run_sampler(rstate=rstate)
assert res0.log_evidence == res1.log_evidence
def test_reproducibility_state_and_seed(self):
rstate = np.random.default_rng(1234)
res0 = self._run_sampler(rstate=rstate)
res1 = self._run_sampler(seed=1234)
assert res0.log_evidence == res1.log_evidence
if __name__ == "__main__":
unittest.main()
import unittest
from unittest.mock import MagicMock
import numpy as np
import bilby
class TestPolyChord(unittest.TestCase):
def setUp(self):
self.likelihood = MagicMock()
self.priors = bilby.core.prior.PriorDict(
dict(a=bilby.core.prior.Uniform(0, 1), b=bilby.core.prior.Uniform(0, 1))
)
self.sampler = bilby.core.sampler.PyPolyChord(
self.likelihood,
self.priors,
outdir="outdir",
label="polychord",
use_ratio=False,
plot=False,
skip_import_verification=True,
)
def tearDown(self):
del self.likelihood
del self.priors
del self.sampler
def test_default_kwargs(self):
expected = dict(
use_polychord_defaults=False,
nlive=self.sampler.ndim * 25,
num_repeats=self.sampler.ndim * 5,
nprior=-1,
do_clustering=True,
feedback=1,
precision_criterion=0.001,
logzero=-1e30,
max_ndead=-1,
boost_posterior=0.0,
posteriors=True,
equals=True,
cluster_posteriors=True,
write_resume=True,
write_paramnames=False,
read_resume=True,
write_stats=True,
write_live=True,
write_dead=True,
write_prior=True,
compression_factor=np.exp(-1),
base_dir="outdir",
file_root="polychord",
seed=-1,
grade_dims=list([self.sampler.ndim]),
grade_frac=list([1.0] * len([self.sampler.ndim])),
nlives={},
)
self.sampler._setup_dynamic_defaults()
self.assertDictEqual(expected, self.sampler.kwargs)
def test_translate_kwargs(self):
expected = dict(
use_polychord_defaults=False,
nlive=123,
num_repeats=self.sampler.ndim * 5,
nprior=-1,
do_clustering=True,
feedback=1,
precision_criterion=0.001,
logzero=-1e30,
max_ndead=-1,
boost_posterior=0.0,
posteriors=True,
equals=True,
cluster_posteriors=True,
write_resume=True,
write_paramnames=False,
read_resume=True,
write_stats=True,
write_live=True,
write_dead=True,
write_prior=True,
compression_factor=np.exp(-1),
base_dir="outdir",
file_root="polychord",
seed=-1,
grade_dims=list([self.sampler.ndim]),
grade_frac=list([1.0] * len([self.sampler.ndim])),
nlives={},
)
self.sampler._setup_dynamic_defaults()
for equiv in bilby.core.sampler.base_sampler.NestedSampler.npoints_equiv_kwargs:
new_kwargs = self.sampler.kwargs.copy()
del new_kwargs["nlive"]
new_kwargs[equiv] = 123
self.sampler.kwargs = new_kwargs
self.assertDictEqual(expected, self.sampler.kwargs)
if __name__ == "__main__":
unittest.main()
......@@ -864,8 +864,8 @@ class TestEquationOfStateConversions(unittest.TestCase):
self.mass_2_source_polytrope[i],
0
)
self.assertAlmostEqual(self.lambda_1_polytrope[i], lambda_1, places=3)
self.assertAlmostEqual(self.lambda_2_polytrope[i], lambda_2, places=3)
self.assertAlmostEqual(self.lambda_1_polytrope[i], lambda_1, places=2)
self.assertAlmostEqual(self.lambda_2_polytrope[i], lambda_2, places=1)
self.assertAlmostEqual(self.eos_check_polytrope[i], eos_check)
......