Skip to content

Commit

Permalink
use pytest fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Sep 19, 2024
1 parent a2771ce commit e6b3be7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
10 changes: 8 additions & 2 deletions tests/core/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
HAVE_SCIPY = True


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

return np.random.default_rng(seed=42)


@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

rng = np.random.default_rng(seed=42)
a = rng.standard_normal((100, 20))
b = rng.standard_normal((100,))

Expand Down
14 changes: 8 additions & 6 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,13 +72,12 @@ 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
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)

Expand All @@ -90,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 @@ -100,7 +104,6 @@ def test_multi_plane_matrix(shells, cosmo):

convergence = MultiPlaneConvergence(cosmo)

rng = np.random.default_rng(seed=42)
deltas = rng.random((len(shells), 10))
kappas = []
for shell, delta in zip(shells, deltas):
Expand All @@ -110,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 @@ -121,7 +124,6 @@ def test_multi_plane_weights(shells, cosmo):

convergence = MultiPlaneConvergence(cosmo)

rng = np.random.default_rng(seed=42)
deltas = rng.random((len(shells), 10))
weights = rng.random((len(shells), 3))
kappa = 0
Expand Down
9 changes: 7 additions & 2 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,12 +103,11 @@ 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):
rng = np.random.default_rng(seed=42)
counts = rng.random(cshape)
bias = None if bshape is None else rng.random(bshape)

Expand Down

0 comments on commit e6b3be7

Please sign in to comment.