Skip to content

Commit

Permalink
[DEP][DOC]: 696 fix random seed documentation (#28)
Browse files Browse the repository at this point in the history
* BUG: fix typo in ln_i0

* TST: Add GitHub action testing (#24)

* TST: Create unit-tests.yml

* TST: try explicit version

* TST: Remove unnecessary setup python

* TST: disable precommit tests

* TST: typo

* TST: add pre-commit tests

* TST: try installing jupyter for pre-commit

* TST: add install/import tests

* TST: just install basic version

* TST: typo fixes

* TST: done run executable tests on windows

* TST: another attempt to disable executable tests for windows

* TST: remove executable tests

* DOC: add documentation job

* DOC: make sure to pull tags for docs job

* BLD: upload docs as an artifact

* DEP: Update documentation and examples to reflect changes to random seed

* DEP,DOC: Updated doc string to ask for np generator instance instead of legacy generator

* [DEP][DOC]: updates from review, typo fix, removal of unnecessary `rng` import

* [?]: Removed metadata from jupyter notebooks

* Pre-commit fixes

* pre-commit re-run

I realised that pre-commit didn't check any files because there were no logged chagnes. So I ran pre-commit on everything using "pre-commit run --all-files".

This only brought up relevant changes from myself, with the exceptions of:
"bilby/bilby_mcmc/sampler.py:525:38: E226 missing whitespace around arithmetic operator"
which is outside the remit of this merge request.

* Adding Sean to the authors for the CI

* Update bilby/core/sampler/dynesty_utils.py

Co-authored-by: Colm Talbot <[email protected]>

* Update bilby/core/sampler/dynesty_utils.py

Co-authored-by: Colm Talbot <[email protected]>

---------

Co-authored-by: Colm Talbot <[email protected]>
Co-authored-by: Colm Talbot <[email protected]>
Co-authored-by: Sean Hibbit <[email protected]>
Co-authored-by: Sean Hibbitt <[email protected]>
  • Loading branch information
5 people authored Oct 23, 2024
1 parent 133174a commit 7d23cc4
Show file tree
Hide file tree
Showing 42 changed files with 198 additions and 92 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ Martin White
Peter Tsun-Ho Pang
Alexandre Sebastien Goettel
Ann-Kristin Malz
Sean Hibbitt
10 changes: 6 additions & 4 deletions bilby/core/sampler/dynesty_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,9 @@ def propose_differetial_evolution(
The number of dimensions to run the differential evolution over, the
first :code:`n_cluster` dimensions are used. The rest are randomly
sampled from the prior.
rstate: np.random.RandomState
The random state to use to generate random numbers.
rstate: numpy.random.Generator
The numpy generator instance used for random number generation.
Consider using built in `bilby.core.utils.random.rng`.
mix: float
The fraction of proposed points that should follow the specified scale
rather than mode hopping. :code:`default=0.5`
Expand Down Expand Up @@ -656,8 +657,9 @@ def propose_volumetric(
The number of dimensions to run the differential evolution over, the
first :code:`n_cluster` dimensions are used. The rest are randomly
sampled from the prior.
rstate: np.random.RandomState
The random state to use to generate random numbers.
rstate: numpy.random.Generator
The numpy generator instance used for random number generation.
Consider using built in `bilby.core.utils.random.rng`.
Returns
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_bilby_mcmc"
Expand All @@ -31,8 +35,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.likelihood import GaussianLikelihood
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_pymc"
Expand All @@ -33,7 +37,7 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import numpy as np
import pymc as pm
from bilby.core.sampler.pymc import Pymc
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_pymc_custom_likelihood"
Expand All @@ -37,7 +41,7 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
7 changes: 5 additions & 2 deletions examples/core_examples/gaussian_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"""
import bilby
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "gaussian_example"
Expand All @@ -16,8 +20,7 @@
# waveform_generator to make the signal.

# Making simulated data: in this case, we consider just a Gaussian

data = np.random.normal(3, 4, 100)
data = rng.normal(3, 4, 100)


class SimpleGaussianLikelihood(bilby.Likelihood):
Expand Down
11 changes: 7 additions & 4 deletions examples/core_examples/gaussian_process_celerite_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.prior import Uniform
from bilby.core.utils.random import rng, seed

# sets seed of bilby's generator "rng" to "123"
seed(123)

# In this example we show how we can use the `celerite` package within `bilby`.
# We begin by synthesizing some data and then use a simple Gaussian Process
Expand Down Expand Up @@ -44,9 +48,8 @@ def linear_function(x, a, b):

# For the data creation, we leave a gap in the middle of the time series
# to see how the Gaussian Process model can interpolate the data.
# We fix the seed to ensure reproducibility.
# The seed has been fixed to ensure reproducibility (see line 11)

np.random.seed(42)
times = np.linspace(0, 40, 100)
times = np.append(times, np.linspace(60, 100, 100))
dt = times[1] - times[0]
Expand All @@ -56,7 +59,7 @@ def linear_function(x, a, b):
amplitude
* np.sin(2 * np.pi * times / period)
* np.exp(-((times - 50) ** 2) / 2 / width**2)
+ np.random.normal(scale=jitter, size=len(times))
+ rng.normal(scale=jitter, size=len(times))
+ linear_function(x=times, a=slope, b=offset)
)

Expand Down Expand Up @@ -184,7 +187,7 @@ def linear_function(x, a, b):

# Plot the mean model for ten other posterior samples.
samples = [
result.posterior.iloc[np.random.randint(len(result.posterior))] for _ in range(10)
result.posterior.iloc[rng.integers(len(result.posterior))] for _ in range(10)
]
for sample in samples:
likelihood.set_parameters(sample)
Expand Down
13 changes: 7 additions & 6 deletions examples/core_examples/gaussian_process_george_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.prior import Uniform
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# In this example we show how we can use the `george` package within
# `bilby`. We begin by synthesizing some data and then use a simple Gaussian
Expand Down Expand Up @@ -42,9 +46,8 @@ def linear_function(x, a, b):

# For the data creation, we leave a gap in the middle of the time series to
# see how the Gaussian Process model can interpolate the data. We fix the
# seed to ensure reproducibility.

np.random.seed(42)

times = np.linspace(0, 40, 100)
times = np.append(times, np.linspace(60, 100, 100))
dt = times[1] - times[0]
Expand All @@ -54,7 +57,7 @@ def linear_function(x, a, b):
amplitude
* np.sin(2 * np.pi * times / period)
* np.exp(-((times - 50) ** 2) / 2 / width**2)
+ np.random.normal(scale=jitter, size=len(times))
+ rng.normal(scale=jitter, size=len(times))
+ linear_function(x=times, a=slope, b=offset)
)

Expand Down Expand Up @@ -162,9 +165,7 @@ def linear_function(x, a, b):
plt.plot(x, trend, color="green", label="Mean")

# Plot the mean model for ten other posterior samples.
samples = [
result.posterior.iloc[np.random.randint(len(result.posterior))] for _ in range(10)
]
samples = [result.posterior.iloc[rng.integer(len(result.posterior))] for _ in range(10)]
for sample in samples:
likelihood.set_parameters(sample)
if not isinstance(likelihood.mean_model, (float, int)):
Expand Down
8 changes: 6 additions & 2 deletions examples/core_examples/grid_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_grid"
Expand All @@ -31,8 +35,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
10 changes: 7 additions & 3 deletions examples/core_examples/hyper_parameter_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
from bilby.core.result import make_pp_plot
from bilby.core.sampler import run_sampler
from bilby.core.utils import check_directory_exists_and_if_not_mkdir
from bilby.core.utils.random import rng, seed
from bilby.hyper.likelihood import HyperparameterLikelihood

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

outdir = "outdir"
check_directory_exists_and_if_not_mkdir(outdir)

Expand All @@ -35,11 +39,11 @@ def model(x, c0, c1):
# Make the sample sets
results = list()
for i in range(Nevents):
c0 = np.random.normal(true_mu_c0, true_sigma_c0)
c1 = np.random.uniform(-1, 1)
c0 = rng.normal(true_mu_c0, true_sigma_c0)
c1 = rng.uniform(-1, 1)
injection_parameters = dict(c0=c0, c1=c1)

data = model(x, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(x, **injection_parameters) + rng.normal(0, sigma, N)
line = ax1.plot(x, data, "-x", label=labels[i])

likelihood = GaussianLikelihood(x, data, model, sigma)
Expand Down
8 changes: 6 additions & 2 deletions examples/core_examples/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression"
Expand All @@ -31,8 +35,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/linear_regression_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_grid"
Expand All @@ -34,7 +38,7 @@ def model(time, m, c):
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = 3.0
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/linear_regression_unknown_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_unknown_noise"
Expand All @@ -32,7 +36,7 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
11 changes: 7 additions & 4 deletions examples/core_examples/linear_regression_with_Fisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import copy

import bilby
from bilby.core.utils.random import rng, seed

# sets seed of bilby's generator "rng" to "123"
seed(123)

import numpy as np

# A few simple setup steps
outdir = "outdir"

np.random.seed(123)


# First, we define our "signal model", in this case a simple linear function
def model(time, m, c):
Expand All @@ -33,8 +36,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# Now lets instantiate a version of our GaussianLikelihood, giving it
# the time, data and signal model
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/occam_factor_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "occam_factor"
Expand All @@ -44,7 +48,7 @@
N = 100
time = np.linspace(0, 1, N)
coeffs = [1, 2, 3]
data = np.polyval(coeffs, time) + np.random.normal(0, sigma, N)
data = np.polyval(coeffs, time) + rng.normal(0, sigma, N)

fig, ax = plt.subplots()
ax.plot(time, data, "o", label="data", color="C0")
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/radioactive_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import numpy as np
from bilby.core.likelihood import PoissonLikelihood
from bilby.core.prior import LogUniform
from bilby.core.utils.random import rng, seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "radioactive_decay"
Expand Down Expand Up @@ -61,7 +65,7 @@ def decay_rate(delta_t, halflife, n_init):

rates = decay_rate(delta_t, **injection_parameters)
# get radioactive counts
counts = np.random.poisson(rates)
counts = rng.poisson(rates)
theoretical = decay_rate(delta_t, **injection_parameters)

# We quickly plot the data to check it looks sensible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

import bilby
import gwinc
import numpy as np
from bilby.core.utils.random import seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# Set the duration and sampling frequency of the data segment that we're going
# to inject the signal into
Expand All @@ -22,9 +25,6 @@
label = "australian_detector"
bilby.core.utils.setup_logger(outdir=outdir, label=label)

# Set up a random seed for result reproducibility. This is optional!
np.random.seed(88170232)

# create a new detector using a PyGwinc sensitivity curve
curve = gwinc.load_budget("Aplus").run()

Expand Down
Loading

0 comments on commit 7d23cc4

Please sign in to comment.