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
Commits on Source (13)
Showing
with 285 additions and 74 deletions
......@@ -75,6 +75,10 @@ basic-3.10:
<<: *test-python
image: python:3.10
basic-3.11:
<<: *test-python
image: python:3.11
.test-samplers-import: &test-samplers-import
stage: initial
script:
......@@ -82,14 +86,18 @@ basic-3.10:
- *list-env
- python test/test_samplers_import.py
import-samplers-3.9:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
# import-samplers-3.9:
# <<: *test-samplers-import
# image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
import-samplers-3.10:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
import-samplers-3.11:
<<: *test-samplers-import
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
.precommits: &precommits
stage: initial
script:
......@@ -98,16 +106,15 @@ import-samplers-3.10:
- pip install --upgrade pip
- pip --cache-dir=$CACHE_DIR install --upgrade bilby
- pip --cache-dir=$CACHE_DIR install .
- pip --cache-dir=$CACHE_DIR install pre-commit
# Run precommits (flake8, spellcheck, isort, no merge conflicts, etc)
- pre-commit run --all-files --verbose --show-diff-on-failure
precommits-py3.9:
<<: *precommits
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
variables:
CACHE_DIR: ".pip39"
PYVERSION: "python39"
# precommits-py3.9:
# <<: *precommits
# image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
# variables:
# CACHE_DIR: ".pip39"
# PYVERSION: "python39"
precommits-py3.10:
<<: *precommits
......@@ -135,10 +142,10 @@ install:
- pytest --cov=bilby --durations 10
python-3.9:
<<: *unit-test
needs: ["basic-3.9", "precommits-py3.9"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
# python-3.9:
# <<: *unit-test
# needs: ["basic-3.9", "precommits-py3.9"]
# image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
python-3.10:
<<: *unit-test
......@@ -157,6 +164,11 @@ python-3.10:
- htmlcov/
expire_in: 30 days
python-3.11:
<<: *unit-test
needs: ["basic-3.11"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
.test-sampler: &test-sampler
stage: test
script:
......@@ -164,16 +176,21 @@ python-3.10:
- *list-env
- pytest test/integration/sampler_run_test.py --durations 10 -v
python-3.9-samplers:
<<: *test-sampler
needs: ["basic-3.9", "precommits-py3.9"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
# python-3.9-samplers:
# <<: *test-sampler
# needs: ["basic-3.9", "precommits-py3.9"]
# image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
python-3.10-samplers:
<<: *test-sampler
needs: ["basic-3.10", "precommits-py3.10"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
python-3.11-samplers:
<<: *test-sampler
needs: ["basic-3.11"]
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
integration-tests-python-3.10:
stage: test
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
......@@ -195,24 +212,27 @@ integration-tests-python-3.10:
- *list-env
- pytest test/gw/plot_test.py
plotting-python-3.9:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
needs: ["basic-3.9", "precommits-py3.9"]
# plotting-python-3.9:
# <<: *plotting
# image: containers.ligo.org/lscsoft/bilby/v2-bilby-python39
# needs: ["basic-3.9", "precommits-py3.9"]
plotting-python-3.10:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
needs: ["basic-3.10", "precommits-py3.10"]
plotting-python-3.11:
<<: *plotting
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python311
needs: ["basic-3.11"]
# ------------------- Docs stage -------------------------------------------
docs:
stage: docs
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
before_script:
- conda install -c conda-forge pandoc ipython jupyter nbconvert
- python -m pip install ipykernel
- python -m ipykernel install
script:
# Make the documentation
......@@ -263,16 +283,22 @@ pages:
- docker image tag v3-bilby-$PYVERSION containers.ligo.org/lscsoft/bilby/v2-bilby-$PYVERSION:latest
- docker image push containers.ligo.org/lscsoft/bilby/v2-bilby-$PYVERSION:latest
build-python39-container:
<<: *build-container
variables:
PYVERSION: "python39"
# disable building python39 container due to dependency issues
# build-python39-container:
# <<: *build-container
# variables:
# PYVERSION: "python39"
build-python310-container:
<<: *build-container
variables:
PYVERSION: "python310"
build-python311-container:
<<: *build-container
variables:
PYVERSION: "python311"
pypi-release:
stage: deploy
image: containers.ligo.org/lscsoft/bilby/v2-bilby-python310
......@@ -280,7 +306,6 @@ pypi-release:
TWINE_USERNAME: $PYPI_USERNAME
TWINE_PASSWORD: $PYPI_PASSWORD
before_script:
- python -m pip install twine setuptools_scm build
- python -m build --sdist --wheel --outdir dist/ .
script:
- twine upload dist/*
......
......@@ -57,6 +57,7 @@ Michael Puerrer
Michael Williams
Monica Rizzo
Moritz Huebner
Nico Gerardo Bers
Nicola De Lillo
Nikhil Sarin
Nirban Bose
......@@ -80,6 +81,7 @@ Sharan Banagiri
Shichao Wu
Simon Stevenson
Soichiro Morisaki
Soumen Roy
Stephen R Green
Sumeet Kulkarni
Sylvia Biscoveanu
......
# All notable changes will be documented in this file
## [2.2.3] 2024-02-24
Version 2.2.3 release of Bilby
This is a bugfix release
There are also a number of testing/infrastructure updates.
### Changes
- Fix a bug when the specified maximum frequency is too low for the multibanding likelihood (!1279)
- Allow the `DirichletElement` prior to be pickled (!1312)
- Add the ability to change the pool size when resuming a `dynesty` job (!1315)
- Fix how the random seed is passed to `dynesty` (!1319)
## [2.2.2] 2023-11-29
Version 2.2.2 release of Bilby
......
......@@ -372,6 +372,7 @@ class DirichletElement(ConditionalBeta):
label + str(ii) for ii in range(order)
]
self.__class__.__name__ = 'DirichletElement'
self.__class__.__qualname__ = 'DirichletElement'
def dirichlet_condition(self, reference_parms, **kwargs):
remaining = 1 - sum(
......
......@@ -123,6 +123,9 @@ class Dynesty(NestedSampler):
The proposal methods to use during MCMC. This can be some combination
of :code:`"diff", "volumetric"`. See the dynesty guide in the Bilby docs
for more details. default=:code:`["diff"]`.
rstate: numpy.random.Generator (None)
Instance of a numpy random generator for generating random numbers.
Also see :code:`seed` in 'Other Parameters'.
Other Parameters
================
......@@ -143,8 +146,13 @@ class Dynesty(NestedSampler):
has no impact on the sampling.
dlogz: float, (0.1)
Stopping criteria
seed: int (None)
Use to seed the random number generator if :code:`rstate` is not
specified.
"""
sampling_seed_key = "seed"
@property
def _dynesty_init_kwargs(self):
params = inspect.signature(self.sampler_init).parameters
......@@ -176,6 +184,7 @@ class Dynesty(NestedSampler):
def default_kwargs(self):
kwargs = self._dynesty_init_kwargs
kwargs.update(self._dynesty_sampler_kwargs)
kwargs["seed"] = None
return kwargs
def __init__(
......@@ -265,6 +274,14 @@ class Dynesty(NestedSampler):
for equiv in self.npool_equiv_kwargs:
if equiv in kwargs:
kwargs["queue_size"] = kwargs.pop(equiv)
if "seed" in kwargs:
seed = kwargs.get("seed")
if "rstate" not in kwargs:
kwargs["rstate"] = np.random.default_rng(seed)
else:
logger.warning(
"Kwargs contain both 'rstate' and 'seed', ignoring 'seed'."
)
def _verify_kwargs_against_default_kwargs(self):
if not self.kwargs["walks"]:
......@@ -604,6 +621,7 @@ class Dynesty(NestedSampler):
sampling_time_s=self.sampling_time.seconds,
ncores=self.kwargs.get("queue_size", 1),
)
self.kwargs["rstate"] = None
def _update_sampling_time(self):
end_time = datetime.datetime.now()
......@@ -717,6 +735,7 @@ class Dynesty(NestedSampler):
self.sampler.nqueue = -1
self.start_time = self.sampler.kwargs.pop("start_time")
self.sampling_time = self.sampler.kwargs.pop("sampling_time")
self.sampler.queue_size = self.kwargs["queue_size"]
self.sampler.pool = self.pool
if self.pool is not None:
self.sampler.M = self.pool.map
......
......@@ -607,7 +607,7 @@ class Interferometer(object):
Returns
=======
float: The matched filter signal to noise ratio squared
complex: The matched filter signal to noise ratio
"""
return gwutils.matched_filter_snr(
......
......@@ -517,7 +517,7 @@ class MBGravitationalWaveTransient(GravitationalWaveTransient):
for ifo in self.interferometers:
logger.info("Pre-computing linear coefficients for {}".format(ifo.name))
fddata = np.zeros(N // 2 + 1, dtype=complex)
fddata[:len(ifo.frequency_domain_strain)][ifo.frequency_mask] += \
fddata[:len(ifo.frequency_domain_strain)][ifo.frequency_mask[:len(fddata)]] += \
ifo.frequency_domain_strain[ifo.frequency_mask] / ifo.power_spectral_density_array[ifo.frequency_mask]
for b in range(self.number_of_bands):
Ks, Ke = self.Ks_Ke[b]
......@@ -606,7 +606,7 @@ class MBGravitationalWaveTransient(GravitationalWaveTransient):
for ifo in self.interferometers:
logger.info("Pre-computing quadratic coefficients for {}".format(ifo.name))
full_inv_psds = np.zeros(N // 2 + 1)
full_inv_psds[:len(ifo.power_spectral_density_array)][ifo.frequency_mask] = (
full_inv_psds[:len(ifo.power_spectral_density_array)][ifo.frequency_mask[:len(full_inv_psds)]] = (
1 / ifo.power_spectral_density_array[ifo.frequency_mask]
)
for b in range(self.number_of_bands):
......
......@@ -398,7 +398,7 @@ def read_frame_file(file_name, start_time, end_time, resample=None, channel=None
strain = TimeSeries.read(source=file_name, channel=channel, start=start_time, end=end_time, **kwargs)
loaded = True
logger.info('Successfully loaded {}.'.format(channel))
except RuntimeError:
except (RuntimeError, ValueError):
logger.warning('Channel {} not found. Trying preset channel names'.format(channel))
if loaded is False:
......@@ -418,7 +418,7 @@ def read_frame_file(file_name, start_time, end_time, resample=None, channel=None
**kwargs)
loaded = True
logger.info('Successfully read strain data for channel {}.'.format(channel))
except RuntimeError:
except (RuntimeError, ValueError):
pass
if loaded:
......
......@@ -13,17 +13,6 @@ RUN /bin/bash -c "source activate ${{conda_env}}"
RUN mamba info
RUN python --version
# 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)
# Add the ROQ data to the image
RUN mkdir roq_basis \
&& cd roq_basis \
......
......@@ -6,6 +6,7 @@ channels:
dependencies:
- pip
- setuptools
- setuptools_scm
- matplotlib
- numpy
- scipy
......@@ -57,5 +58,15 @@ dependencies:
- jaxlib>=0.4
- jax>=0.4
- numba>0.53.1
- make
- pre-commit
- pandoc
- ipython
- jupyter
- nbconvert
- twine
- pip:
- autodoc
- ipykernel
- build
- nflows
......@@ -15,17 +15,6 @@ RUN /bin/bash -c "source activate ${conda_env}"
RUN mamba info
RUN python --version
# 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)
# 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
......@@ -15,17 +15,6 @@ RUN /bin/bash -c "source activate ${conda_env}"
RUN mamba info
RUN python --version
# 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)
# Add the ROQ data to the image
RUN mkdir roq_basis \
&& cd roq_basis \
......
......@@ -3,7 +3,7 @@ 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")
for python_major_version, python_minor_version in python_versions:
......
......@@ -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
......
......@@ -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",
],
......
......@@ -5,6 +5,7 @@ from unittest import mock
import numpy as np
import pandas as pd
import pickle
import bilby
......@@ -472,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()