Revert RNG switch to np.random.default_rng()

Restore use of the global np.random state in pseudo_Obs (misc.py) and the
prior id generation (fits.py), keeping seed behavior unchanged. The switch
to a module-local generator is out of scope for this lint-focused PR.
This commit is contained in:
Fabian Joswig 2026-06-18 10:49:11 +02:00
commit 9be9fb6767
3 changed files with 3 additions and 22 deletions

View file

@ -18,8 +18,6 @@ from odrpack import odr_fit
from .obs import Obs, cov_Obs, covariance, derived_observable, invert_corr_cov_cholesky
_rng = np.random.default_rng()
class Fit_result(Sequence):
"""Represents fit results.
@ -948,6 +946,6 @@ def _construct_prior_obs(i_prior, i_n):
return i_prior
elif isinstance(i_prior, str):
loc_val, loc_dval = _extract_val_and_dval(i_prior)
return cov_Obs(loc_val, loc_dval ** 2, '#prior' + str(i_n) + f"_{_rng.integers(2147483647):010d}")
return cov_Obs(loc_val, loc_dval ** 2, '#prior' + str(i_n) + f"_{np.random.randint(2147483647):010d}")
else:
raise TypeError("Prior entries need to be 'Obs' or 'str'.")

View file

@ -10,8 +10,6 @@ import scipy
from .obs import Obs
from .version import __version__
_rng = np.random.default_rng()
def print_config():
"""Print information about version of python, pyerrors and dependencies."""
@ -122,7 +120,7 @@ def pseudo_Obs(value, dvalue, name, samples=1000):
return Obs([np.zeros(samples) + value], [name])
else:
for _ in range(100):
deltas = [_rng.normal(0.0, dvalue * np.sqrt(samples), samples)]
deltas = [np.random.normal(0.0, dvalue * np.sqrt(samples), samples)]
deltas -= np.mean(deltas)
deltas *= dvalue / np.sqrt(np.var(deltas) / samples) / np.sqrt(1 + 3 / samples)
deltas += value
@ -165,7 +163,7 @@ def gen_correlated_data(means, cov, name, tau=0.5, samples=1000):
raise Exception('All integrated autocorrelations have to be >= 0.5.')
a = (2 * tau - 1) / (2 * tau + 1)
rand = _rng.multivariate_normal(np.zeros_like(means), cov * samples, samples)
rand = np.random.multivariate_normal(np.zeros_like(means), cov * samples, samples)
# Normalize samples such that sample variance matches input
norm = np.array([np.var(o, ddof=1) / samples for o in rand.T])

View file

@ -22,18 +22,3 @@ def test_obs_errorbar():
def test_print_config():
pe.print_config()
def test_pseudo_Obs_seed_independence():
# pseudo_Obs now uses a module-local np.random.default_rng() generator,
# so np.random.seed() no longer controls its output. The per-sample
# deltas therefore differ between successive calls even with a re-seed,
# though the normalized value / dvalue still match the requested inputs.
np.random.seed(0)
a = pe.pseudo_Obs(1.0, 0.1, "e")
np.random.seed(0)
b = pe.pseudo_Obs(1.0, 0.1, "e")
assert not np.allclose(a.deltas["e"], b.deltas["e"])
assert np.isclose(a.value, b.value)
assert np.isclose(a.dvalue, b.dvalue)