Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-208: remove legacy random number generation #241

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest


@pytest.fixture
def rng():
import numpy as np

return np.random.default_rng(seed=42)
6 changes: 3 additions & 3 deletions tests/core/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@


@pytest.mark.skipif(not HAVE_SCIPY, reason="test requires SciPy")
def test_nnls():
def test_nnls(rng):
import numpy as np
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)
a = rng.standard_normal((100, 20))
b = rng.standard_normal((100,))

x_glass = nnls_glass(a, b)
x_scipy, _ = nnls_scipy(a, b)
Expand Down
20 changes: 10 additions & 10 deletions tests/test_lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ def alpha(re, im):
assert np.allclose([lon, lat], [d, 0.0])


def test_deflect_many():
def test_deflect_many(rng):
import healpix

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)
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))

Expand All @@ -89,7 +89,7 @@ def test_deflect_many():
npt.assert_allclose(dotp, np.cos(abs_alpha))


def test_multi_plane_matrix(shells, cosmo):
def test_multi_plane_matrix(shells, cosmo, rng):
from glass.lensing import MultiPlaneConvergence, multi_plane_matrix

mat = multi_plane_matrix(shells, cosmo)
Expand All @@ -99,7 +99,7 @@ def test_multi_plane_matrix(shells, cosmo):

convergence = MultiPlaneConvergence(cosmo)

deltas = np.random.rand(len(shells), 10)
deltas = rng.random((len(shells), 10))
kappas = []
for shell, delta in zip(shells, deltas):
convergence.add_window(delta, shell)
Expand All @@ -108,7 +108,7 @@ def test_multi_plane_matrix(shells, cosmo):
npt.assert_allclose(mat @ deltas, kappas)


def test_multi_plane_weights(shells, cosmo):
def test_multi_plane_weights(shells, cosmo, rng):
from glass.lensing import MultiPlaneConvergence, multi_plane_weights

w_in = np.eye(len(shells))
Expand All @@ -119,8 +119,8 @@ def test_multi_plane_weights(shells, cosmo):

convergence = MultiPlaneConvergence(cosmo)

deltas = np.random.rand(len(shells), 10)
weights = np.random.rand(len(shells), 3)
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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def test_uniform_positions():
assert lon.shape == lat.shape == (cnt.sum(),)


def test_position_weights():
def test_position_weights(rng):
from glass.points import 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)
counts = rng.random(cshape)
bias = None if bshape is None else rng.random(bshape)

weights = position_weights(counts, bias)

Expand Down