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-271: add tests for glass.observations #436

Merged
merged 37 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8e59628
gh-271: add tests for `glass.observations`
paddyroddy Nov 18, 2024
3f32e3f
Merge branch 'main' into paddy/issue-271
paddyroddy Nov 18, 2024
a5cb68a
Add docstrings to all unit tests
paddyroddy Nov 18, 2024
94650cb
Add first raise test
paddyroddy Nov 19, 2024
1bdf918
Add more raises tests
paddyroddy Nov 19, 2024
31d85ab
Add comments
paddyroddy Nov 19, 2024
368e291
Merge branch 'main' into paddy/issue-271
paddyroddy Nov 20, 2024
5172f3d
Fix typing
paddyroddy Nov 20, 2024
70b0567
Check norm
paddyroddy Nov 20, 2024
b7e65af
Testing `gaussian_nz`
paddyroddy Nov 20, 2024
bd0c345
Add test for the norm
paddyroddy Nov 20, 2024
98eb537
Fix types
paddyroddy Nov 20, 2024
67423c3
Fix imports
paddyroddy Nov 20, 2024
1d1c6a3
Use `_like`
paddyroddy Nov 20, 2024
e9ef0bb
Add tests for `tomo_nz_gausserr`
paddyroddy Nov 20, 2024
78f6419
Merge branch 'main' into paddy/issue-271
paddyroddy Nov 20, 2024
2f01a81
Use checks
paddyroddy Nov 20, 2024
fd4ea18
Check shape output
paddyroddy Nov 20, 2024
206d04c
Fixed zbins
paddyroddy Nov 20, 2024
5da3f65
Merge branch 'main' into paddy/issue-271
paddyroddy Nov 21, 2024
e7b4302
Remove `# type: ignore[attr-defined]`
paddyroddy Nov 21, 2024
af76e03
Add `nbins` test
paddyroddy Nov 21, 2024
d0ac6af
gh-447: make sure `dz` gives the same results as `nbins`
paddyroddy Nov 21, 2024
5bcf90e
Merge branch 'paddy/issue-447' into paddy/issue-271
paddyroddy Nov 21, 2024
23b36da
Fix tests
paddyroddy Nov 21, 2024
64f1c2c
Use `np.nextafter`
paddyroddy Nov 21, 2024
f497ce9
Merge branch 'paddy/issue-447' into paddy/issue-271
paddyroddy Nov 21, 2024
11ec4d6
Check the `dz=0.3` case
paddyroddy Nov 21, 2024
f8509f3
Check shape
paddyroddy Nov 21, 2024
acc29f6
No rotation
paddyroddy Nov 21, 2024
6bd1bcf
Merge branch 'main' into paddy/issue-271
paddyroddy Nov 21, 2024
688b567
Linting
paddyroddy Nov 21, 2024
b69d9be
Add extra raises
paddyroddy Nov 21, 2024
e6046cd
Improve sphinx
paddyroddy Nov 21, 2024
f68bed8
Use vars
paddyroddy Nov 21, 2024
037e111
Reuse 0.3
paddyroddy Nov 22, 2024
9c5e22c
Merge branch 'main' into paddy/issue-271
paddyroddy Nov 25, 2024
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
10 changes: 5 additions & 5 deletions glass/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def gaussian_nz(
mean: float | npt.NDArray[np.float64],
sigma: float | npt.NDArray[np.float64],
*,
norm: npt.NDArray[np.float64] | None = None,
norm: float | npt.NDArray[np.float64] | None = None,
) -> npt.NDArray[np.float64]:
"""
Gaussian redshift distribution.
Expand Down Expand Up @@ -130,11 +130,11 @@ def gaussian_nz(

def smail_nz(
z: npt.NDArray[np.float64],
z_mode: npt.NDArray[np.float64],
alpha: npt.NDArray[np.float64],
beta: npt.NDArray[np.float64],
z_mode: float | npt.NDArray[np.float64],
alpha: float | npt.NDArray[np.float64],
beta: float | npt.NDArray[np.float64],
*,
norm: npt.NDArray[np.float64] | None = None,
norm: float | npt.NDArray[np.float64] | None = None,
) -> npt.NDArray[np.float64]:
r"""
Redshift distribution following Smail et al. (1994).
Expand Down
144 changes: 144 additions & 0 deletions tests/test_observations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import numpy as np
import pytest

from glass import (
equal_dens_zbins,
fixed_zbins,
gaussian_nz,
smail_nz,
tomo_nz_gausserr,
vmap_galactic_ecliptic,
)


def test_vmap_galactic_ecliptic() -> None:
"""Add unit tests for :func:`vmap_galactic_ecliptic`."""
n_side = 4

# check shape

vmap = vmap_galactic_ecliptic(n_side)
np.testing.assert_array_equal(len(vmap), 12 * n_side**2)

# no rotation

vmap = vmap_galactic_ecliptic(n_side, galactic=(0, 0), ecliptic=(0, 0))
np.testing.assert_array_equal(vmap, np.zeros_like(vmap))

# check errors raised

with pytest.raises(TypeError, match="galactic stripe must be a pair of numbers"):
vmap_galactic_ecliptic(n_side, galactic=(1,)) # type: ignore[arg-type]

with pytest.raises(TypeError, match="ecliptic stripe must be a pair of numbers"):
vmap_galactic_ecliptic(n_side, ecliptic=(1,)) # type: ignore[arg-type]

with pytest.raises(TypeError, match="galactic stripe must be a pair of numbers"):
vmap_galactic_ecliptic(n_side, galactic=(1, 2, 3)) # type: ignore[arg-type]

with pytest.raises(TypeError, match="ecliptic stripe must be a pair of numbers"):
vmap_galactic_ecliptic(n_side, ecliptic=(1, 2, 3)) # type: ignore[arg-type]


def test_gaussian_nz(rng: np.random.Generator) -> None:
"""Add unit tests for :func:`gaussian_nz`."""
mean = 0
sigma = 1
z = np.linspace(0, 1, 11)

# check passing in the norm

nz = gaussian_nz(z, mean, sigma, norm=0)
np.testing.assert_array_equal(nz, np.zeros_like(nz))

# check the value of each entry is close to the norm

norm = 1
nz = gaussian_nz(z, mean, sigma, norm=norm)
np.testing.assert_allclose(nz.sum() / nz.shape, norm, rtol=1e-2)

# check multidimensionality size

nz = gaussian_nz(
z,
np.tile(mean, z.shape),
np.tile(sigma, z.shape),
norm=rng.normal(size=z.shape),
)
np.testing.assert_array_equal(nz.shape, (len(z), len(z)))


def test_smail_nz() -> None:
"""Add unit tests for :func:`smail_nz`."""
alpha = 1
beta = 1
mode = 1
z = np.linspace(0, 1, 11)

# check passing in the norm

pz = smail_nz(z, mode, alpha, beta, norm=0)
np.testing.assert_array_equal(pz, np.zeros_like(pz))


def test_fixed_zbins() -> None:
"""Add unit tests for :func:`fixed_zbins`."""
zmin = 0
zmax = 1

# check nbins input

nbins = 5
expected_zbins = [(0.0, 0.2), (0.2, 0.4), (0.4, 0.6), (0.6, 0.8), (0.8, 1.0)]
zbins = fixed_zbins(zmin, zmax, nbins=nbins)
np.testing.assert_array_equal(len(zbins), nbins)
np.testing.assert_allclose(zbins, expected_zbins, rtol=1e-15)

# check dz input

dz = 0.2
zbins = fixed_zbins(zmin, zmax, dz=dz)
np.testing.assert_array_equal(len(zbins), np.ceil((zmax - zmin) / dz))
np.testing.assert_allclose(zbins, expected_zbins, rtol=1e-15)

# check dz for spacing which results in a max value above zmax

zbins = fixed_zbins(zmin, zmax, dz=0.3)
np.testing.assert_array_less(zmax, zbins[-1][1])

# check error raised

with pytest.raises(ValueError, match="exactly one of nbins and dz must be given"):
fixed_zbins(zmin, zmax, nbins=nbins, dz=dz)


def test_equal_dens_zbins() -> None:
"""Add unit tests for :func:`equal_dens_zbins`."""
z = np.linspace(0, 1, 11)
nbins = 5

# check expected zbins returned

expected_zbins = [(0.0, 0.2), (0.2, 0.4), (0.4, 0.6), (0.6, 0.8), (0.8, 1.0)]
zbins = equal_dens_zbins(z, np.ones_like(z), nbins)
np.testing.assert_allclose(zbins, expected_zbins, rtol=1e-15)

# check output shape

np.testing.assert_array_equal(len(zbins), nbins)


def test_tomo_nz_gausserr() -> None:
"""Add unit tests for :func:`tomo_nz_gausserr`."""
sigma_0 = 0.1
z = np.linspace(0, 1, 11)
zbins = [(0, 0.2), (0.2, 0.4), (0.4, 0.6), (0.6, 0.8), (0.8, 1.0)]

# check zeros returned

binned_nz = tomo_nz_gausserr(z, np.zeros_like(z), sigma_0, zbins)
np.testing.assert_array_equal(binned_nz, np.zeros_like(binned_nz))

# check the shape of the output

np.testing.assert_array_equal(binned_nz.shape, (len(zbins), len(z)))