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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions tests/core/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
HAVE_SCIPY = True


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

return np.random.default_rng(seed=42)


Comment on lines +13 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to conftest.py so it only needs to be defined once?

@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
25 changes: 15 additions & 10 deletions tests/test_lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import pytest


@pytest.fixture
def rng():
return np.random.default_rng(seed=42)


@pytest.fixture
def shells():
from glass.shells import RadialWindow
Expand Down Expand Up @@ -67,17 +72,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 +94,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 +104,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 +113,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 +124,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
12 changes: 9 additions & 3 deletions tests/test_points.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import numpy as np
import numpy.testing as npt
import pytest


@pytest.fixture
def rng():
return np.random.default_rng(seed=42)


def catpos(pos):
Expand Down Expand Up @@ -97,13 +103,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