From 40f13c5046071cb1373400d5b29651f76e1476ff Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Wed, 18 Sep 2024 12:00:28 +0100 Subject: [PATCH] gh-208: remove legacy random number generation --- tests/core/test_algorithm.py | 5 +++-- tests/test_lensing.py | 17 ++++++++++------- tests/test_points.py | 5 +++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tests/core/test_algorithm.py b/tests/core/test_algorithm.py index a25b1d39..c47fabf0 100644 --- a/tests/core/test_algorithm.py +++ b/tests/core/test_algorithm.py @@ -16,8 +16,9 @@ def test_nnls(): from scipy.optimize import nnls as nnls_scipy from glass.core.algorithm import nnls as nnls_glass - a = np.random.randn(100, 20) - b = np.random.randn(100) + rng = np.random.default_rng(seed=42) + a = rng.standard_normal((100, 20)) + b = rng.standard_normal((100,)) x_glass = nnls_glass(a, b) x_scipy, _ = nnls_scipy(a, b) diff --git a/tests/test_lensing.py b/tests/test_lensing.py index d1c60270..d267f10e 100644 --- a/tests/test_lensing.py +++ b/tests/test_lensing.py @@ -75,11 +75,12 @@ def test_deflect_many(): from glass.lensing import deflect n = 1000 - abs_alpha = np.random.uniform(0, 2 * np.pi, size=n) - arg_alpha = np.random.uniform(-np.pi, np.pi, size=n) + rng = np.random.default_rng(seed=42) + abs_alpha = rng.uniform(0, 2 * np.pi, size=n) + arg_alpha = rng.uniform(-np.pi, np.pi, size=n) - lon_ = np.degrees(np.random.uniform(-np.pi, np.pi, size=n)) - lat_ = np.degrees(np.arcsin(np.random.uniform(-1, 1, size=n))) + lon_ = np.degrees(rng.uniform(-np.pi, np.pi, size=n)) + lat_ = np.degrees(np.arcsin(rng.uniform(-1, 1, size=n))) lon, lat = deflect(lon_, lat_, abs_alpha * np.exp(1j * arg_alpha)) @@ -101,7 +102,8 @@ def test_multi_plane_matrix(shells, cosmo): convergence = MultiPlaneConvergence(cosmo) - deltas = np.random.rand(len(shells), 10) + rng = np.random.default_rng(seed=42) + deltas = rng.random((len(shells), 10)) kappas = [] for shell, delta in zip(shells, deltas): convergence.add_window(delta, shell) @@ -121,8 +123,9 @@ def test_multi_plane_weights(shells, cosmo): convergence = MultiPlaneConvergence(cosmo) - deltas = np.random.rand(len(shells), 10) - weights = np.random.rand(len(shells), 3) + rng = np.random.default_rng(seed=42) + deltas = rng.random((len(shells), 10)) + weights = rng.random((len(shells), 3)) kappa = 0 for shell, delta, weight in zip(shells, deltas, weights): convergence.add_window(delta, shell) diff --git a/tests/test_points.py b/tests/test_points.py index 9247ec02..0ce36bb6 100644 --- a/tests/test_points.py +++ b/tests/test_points.py @@ -102,8 +102,9 @@ def test_position_weights(): for bshape in None, (), (100,), (100, 1): for cshape in (100,), (100, 50), (100, 3, 2): - counts = np.random.rand(*cshape) - bias = None if bshape is None else np.random.rand(*bshape) + rng = np.random.default_rng(seed=42) + counts = rng.random(cshape) + bias = None if bshape is None else rng.random(bshape) weights = position_weights(counts, bias)