Skip to content

Commit

Permalink
fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Nov 5, 2024
1 parent 88f5ee4 commit 14c3f2c
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions tests/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import numpy as np
import numpy.typing as npt

import pytest
import pytest_mock

from glass.points import (
effective_bias,
Expand All @@ -20,6 +18,8 @@
if typing.TYPE_CHECKING:
import collections.abc

import pytest_mock


def catpos(
pos: collections.abc.Generator[
Expand Down Expand Up @@ -53,17 +53,17 @@ def test_effective_bias(mocker: pytest_mock.MockerFixture) -> None:
z = np.linspace(0, 1, 10)
bz = np.zeros((10,))

np.testing.assert_allclose(effective_bias(z, bz, w), np.zeros((10,))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(effective_bias(z, bz, w), np.zeros((10,)))

z = np.zeros((10,))
bz = np.full_like(z, 0.5)

np.testing.assert_allclose(effective_bias(z, bz, w), np.zeros((10,))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(effective_bias(z, bz, w), np.zeros((10,)))

z = np.linspace(0, 1, 10)
bz = np.full_like(z, 0.5)

np.testing.assert_allclose(effective_bias(z, bz, w), 0.25) # type: ignore[no-untyped-call]
np.testing.assert_allclose(effective_bias(z, bz, w), 0.25)


def test_linear_bias(rng: np.random.Generator) -> None:
Expand All @@ -72,21 +72,21 @@ def test_linear_bias(rng: np.random.Generator) -> None:
delta = np.zeros((2, 2))
b = 2.0

np.testing.assert_allclose(linear_bias(delta, b), np.zeros((2, 2))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(linear_bias(delta, b), np.zeros((2, 2)))

# test with 0 b

delta = rng.normal(5, 1, size=(2, 2))
b = 0.0

np.testing.assert_allclose(linear_bias(delta, b), np.zeros((2, 2))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(linear_bias(delta, b), np.zeros((2, 2)))

# compare with original implementation

delta = rng.normal(5, 1, size=(2, 2))
b = 2.0

np.testing.assert_allclose(linear_bias(delta, b), b * delta) # type: ignore[no-untyped-call]
np.testing.assert_allclose(linear_bias(delta, b), b * delta)


def test_loglinear_bias(rng: np.random.Generator) -> None:
Expand All @@ -95,24 +95,24 @@ def test_loglinear_bias(rng: np.random.Generator) -> None:
delta = np.zeros((2, 2))
b = 2.0

np.testing.assert_allclose(loglinear_bias(delta, b), np.zeros((2, 2))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(loglinear_bias(delta, b), np.zeros((2, 2)))

# test with 0 b

delta = rng.normal(5, 1, size=(2, 2))
b = 0.0

np.testing.assert_allclose(loglinear_bias(delta, b), np.zeros((2, 2))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(loglinear_bias(delta, b), np.zeros((2, 2)))

# compare with numpy implementation

delta = rng.normal(5, 1, size=(2, 2))
b = 2.0

np.testing.assert_allclose(loglinear_bias(delta, b), np.expm1(b * np.log1p(delta))) # type: ignore[no-untyped-call]
np.testing.assert_allclose(loglinear_bias(delta, b), np.expm1(b * np.log1p(delta)))


def test_positions_from_delta(rng: np.random.Generator) -> None:
def test_positions_from_delta(rng: np.random.Generator) -> None: # noqa: PLR0915
# create maps that saturate the batching in the function
nside = 128
npix = 12 * nside**2
Expand All @@ -131,31 +131,31 @@ def test_positions_from_delta(rng: np.random.Generator) -> None:

# test with rng

lon, lat, cnt = catpos(positions_from_delta(ngal, delta, bias, vis, rng=rng)) # type: ignore[no-untyped-call]
lon, lat, cnt = catpos(positions_from_delta(ngal, delta, bias, vis, rng=rng))

assert isinstance(cnt, int)
assert lon.shape == lat.shape == (cnt,)

# case: Nons bias and callable bias model

lon, lat, cnt = catpos(
positions_from_delta(ngal, delta, None, vis, bias_model=lambda x: x) # type: ignore[no-untyped-call]
positions_from_delta(ngal, delta, None, vis, bias_model=lambda x: x)
)

assert isinstance(cnt, int)
assert lon.shape == lat.shape == (cnt,)

# case: None vis

lon, lat, cnt = catpos(positions_from_delta(ngal, delta, bias, None)) # type: ignore[no-untyped-call]
lon, lat, cnt = catpos(positions_from_delta(ngal, delta, bias, None))

assert isinstance(cnt, int)
assert lon.shape == lat.shape == (cnt,)

# case: remove monopole

lon, lat, cnt = catpos(
positions_from_delta(ngal, delta, bias, vis, remove_monopole=True) # type: ignore[no-untyped-call]
positions_from_delta(ngal, delta, bias, vis, remove_monopole=True)
)

assert isinstance(cnt, int)
Expand All @@ -164,7 +164,7 @@ def test_positions_from_delta(rng: np.random.Generator) -> None:
# case: negative delta

lon, lat, cnt = catpos(
positions_from_delta(ngal, np.linspace(-1, -1, 196608), None, vis) # type: ignore[no-untyped-call]
positions_from_delta(ngal, np.linspace(-1, -1, 196608), None, vis)
)

assert isinstance(cnt, int)
Expand All @@ -173,7 +173,7 @@ def test_positions_from_delta(rng: np.random.Generator) -> None:
# case: large delta

lon, lat, cnt = catpos(
positions_from_delta(ngal, rng.normal(100, 1, size=(196608,)), bias, vis) # type: ignore[no-untyped-call]
positions_from_delta(ngal, rng.normal(100, 1, size=(196608,)), bias, vis)
)

assert isinstance(cnt, int)
Expand Down Expand Up @@ -225,16 +225,17 @@ def test_positions_from_delta(rng: np.random.Generator) -> None:

vis[: vis.size // 2] = 0.0

lon, lat, cnt = catpos(positions_from_delta(ngal, delta, bias, vis)) # type: ignore[no-untyped-call]
lon, lat, cnt = catpos(positions_from_delta(ngal, delta, bias, vis))

assert isinstance(cnt, np.ndarray)
assert cnt.shape == (3, 2)
assert lon.shape == (cnt.sum(),)
assert lat.shape == (cnt.sum(),)

# test TypeError

with pytest.raises(TypeError, match="bias_model must be string or callable"):
next(positions_from_delta(ngal, delta, bias, vis, bias_model=0)) # type: ignore[no-untyped-call]
next(positions_from_delta(ngal, delta, bias, vis, bias_model=0)) # type: ignore[arg-type]


def test_uniform_positions(rng: np.random.Generator) -> None:
Expand All @@ -246,7 +247,7 @@ def test_uniform_positions(rng: np.random.Generator) -> None:

# test with rng

lon, lat, cnt = catpos(uniform_positions(ngal, rng=rng)) # type: ignore[no-untyped-call]
lon, lat, cnt = catpos(uniform_positions(ngal, rng=rng))

assert isinstance(cnt, int)
assert lon.shape == lat.shape == (cnt,)
Expand Down

0 comments on commit 14c3f2c

Please sign in to comment.