Skip to content

Commit

Permalink
Merge branch 'main' into saransh/test_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp authored Nov 22, 2024
2 parents 3fc9f26 + 6fbeb97 commit e11deb8
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 39 deletions.
56 changes: 55 additions & 1 deletion glass/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
"""GLASS package."""

__all__ = [
"cls2cov",
"combine",
"cubic_windows",
"deflect",
"density_weight",
"discretized_cls",
"distance_grid",
"distance_weight",
"effective_bias",
"effective_cls",
"ellipticity_gaussian",
"ellipticity_intnorm",
"ellipticity_ryden04",
"equal_dens_zbins",
"fixed_zbins",
"from_convergence",
"galaxy_shear",
"gaussian_nz",
"gaussian_phz",
"generate_gaussian",
"generate_lognormal",
"getcl",
"iternorm",
"linear_bias",
"linear_windows",
"load_cls",
"loglinear_bias",
"lognormal_gls",
"multalm",
"multi_plane_matrix",
"multi_plane_weights",
"MultiPlaneConvergence",
"partition",
"position_weights",
"positions_from_delta",
"RadialWindow",
"redshift_grid",
"redshifts",
"redshifts_from_nz",
"restrict",
"save_cls",
"shear_from_convergence",
"smail_nz",
"tomo_nz_gausserr",
"tophat_windows",
"transform_cls",
"triaxial_axis_ratio",
"uniform_positions",
"vmap_galactic_ecliptic",
"volume_weight",
"write_catalog",
]


import contextlib
from importlib.metadata import PackageNotFoundError

Expand All @@ -19,7 +74,6 @@
transform_cls,
)
from glass.galaxies import (
_kappa_ia_nla,
galaxy_shear,
gaussian_phz,
redshifts,
Expand Down
4 changes: 2 additions & 2 deletions glass/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def ndinterp( # noqa: PLR0913
)


def trapz_product(
def trapezoid_product(
f: tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]],
*ff: tuple[
npt.NDArray[np.float64],
Expand Down Expand Up @@ -169,7 +169,7 @@ def trapz_product(
return np.trapezoid(y, x, axis=axis) # type: ignore[no-any-return]


def cumtrapz(
def cumulative_trapezoid(
f: npt.NDArray[np.int_] | npt.NDArray[np.float64],
x: npt.NDArray[np.int_] | npt.NDArray[np.float64],
dtype: npt.DTypeLike | None = None,
Expand Down
4 changes: 2 additions & 2 deletions glass/galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import numpy as np
import numpy.typing as npt

from glass.core.array import broadcast_leading_axes, cumtrapz
from glass.core.array import broadcast_leading_axes, cumulative_trapezoid

if typing.TYPE_CHECKING:
from cosmology import Cosmology
Expand Down Expand Up @@ -129,7 +129,7 @@ def redshifts_from_nz(
# go through extra dimensions; also works if dims is empty
for k in np.ndindex(dims):
# compute the CDF of each galaxy population
cdf = cumtrapz(nz_out[k], z_out[k], dtype=float)
cdf = cumulative_trapezoid(nz_out[k], z_out[k], dtype=float)
cdf /= cdf[-1]

# sample redshifts and store result
Expand Down
8 changes: 4 additions & 4 deletions glass/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import numpy as np
import numpy.typing as npt

from glass.core.array import cumtrapz
from glass.core.array import cumulative_trapezoid


def vmap_galactic_ecliptic(
Expand Down Expand Up @@ -225,8 +225,8 @@ def fixed_zbins(
"""
if nbins is not None and dz is None:
zbinedges = np.linspace(zmin, zmax, nbins + 1)
if nbins is None and dz is not None:
zbinedges = np.arange(zmin, zmax, dz)
elif nbins is None and dz is not None:
zbinedges = np.arange(zmin, np.nextafter(zmax + dz, zmax), dz)
else:
msg = "exactly one of nbins and dz must be given"
raise ValueError(msg)
Expand Down Expand Up @@ -263,7 +263,7 @@ def equal_dens_zbins(
# first compute the cumulative integral (by trapezoidal rule)
# then normalise: the first z is at CDF = 0, the last z at CDF = 1
# interpolate to find the z values at CDF = i/nbins for i = 0, ..., nbins
cuml_nz = cumtrapz(nz, z)
cuml_nz = cumulative_trapezoid(nz, z)
cuml_nz /= cuml_nz[[-1]]
zbinedges = np.interp(np.linspace(0, 1, nbins + 1), cuml_nz, z)

Expand Down
4 changes: 2 additions & 2 deletions glass/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import numpy as np
import numpy.typing as npt

from glass.core.array import broadcast_first, broadcast_leading_axes, trapz_product
from glass.core.array import broadcast_first, broadcast_leading_axes, trapezoid_product

if typing.TYPE_CHECKING:
import collections.abc
Expand Down Expand Up @@ -85,7 +85,7 @@ def effective_bias(
"""
norm = np.trapezoid(w.wa, w.za)
return trapz_product((z, bz), (w.za, w.wa)) / norm
return trapezoid_product((z, bz), (w.za, w.wa)) / norm


def linear_bias(
Expand Down
2 changes: 0 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

@pytest.fixture(scope="session")
def rng() -> np.random.Generator:
import numpy as np

return np.random.default_rng(seed=42)
32 changes: 16 additions & 16 deletions tests/core/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from glass.core.array import (
broadcast_first,
broadcast_leading_axes,
cumtrapz,
cumulative_trapezoid,
ndinterp,
trapz_product,
trapezoid_product,
)

# check if scipy is available for testing
Expand Down Expand Up @@ -144,21 +144,21 @@ def test_ndinterp() -> None:
)


def test_trapz_product() -> None:
def test_trapezoid_product() -> None:
x1 = np.linspace(0, 2, 100)
f1 = np.full_like(x1, 2.0)

x2 = np.linspace(1, 2, 10)
f2 = np.full_like(x2, 0.5)

s = trapz_product((x1, f1), (x2, f2))
s = trapezoid_product((x1, f1), (x2, f2))

np.testing.assert_allclose(s, 1.0)


@pytest.mark.skipif(not HAVE_SCIPY, reason="test requires SciPy")
def test_cumtrapz() -> None:
from scipy.integrate import cumulative_trapezoid
def test_cumulative_trapezoid() -> None:
import scipy.integrate as spi

# 1D f and x

Expand All @@ -167,19 +167,19 @@ def test_cumtrapz() -> None:

# default dtype (int - not supported by scipy)

glass_ct = cumtrapz(f, x)
glass_ct = cumulative_trapezoid(f, x)
np.testing.assert_allclose(glass_ct, np.array([0, 1, 4, 7]))

# explicit dtype (float)

glass_ct = cumtrapz(f, x, dtype=float)
scipy_ct = cumulative_trapezoid(f, x, initial=0)
glass_ct = cumulative_trapezoid(f, x, dtype=float)
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0)
np.testing.assert_allclose(glass_ct, scipy_ct)

# explicit return array

result = cumtrapz(f, x, dtype=float, out=np.zeros((4,)))
scipy_ct = cumulative_trapezoid(f, x, initial=0)
result = cumulative_trapezoid(f, x, dtype=float, out=np.zeros((4,)))
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0)
np.testing.assert_allclose(result, scipy_ct)

# 2D f and 1D x
Expand All @@ -189,17 +189,17 @@ def test_cumtrapz() -> None:

# default dtype (int - not supported by scipy)

glass_ct = cumtrapz(f, x)
glass_ct = cumulative_trapezoid(f, x)
np.testing.assert_allclose(glass_ct, np.array([[0, 2, 12, 31], [0, 2, 8, 17]]))

# explicit dtype (float)

glass_ct = cumtrapz(f, x, dtype=float)
scipy_ct = cumulative_trapezoid(f, x, initial=0)
glass_ct = cumulative_trapezoid(f, x, dtype=float)
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0)
np.testing.assert_allclose(glass_ct, scipy_ct)

# explicit return array

glass_ct = cumtrapz(f, x, dtype=float, out=np.zeros((2, 4)))
scipy_ct = cumulative_trapezoid(f, x, initial=0)
glass_ct = cumulative_trapezoid(f, x, dtype=float, out=np.zeros((2, 4)))
scipy_ct = spi.cumulative_trapezoid(f, x, initial=0)
np.testing.assert_allclose(glass_ct, scipy_ct)
2 changes: 1 addition & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
import pytest

from glass.fields import (
from glass import (
cls2cov,
discretized_cls,
effective_cls,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy.typing as npt
import pytest

from glass import user
from glass import write_catalog

# check if fitsio is available for testing
HAVE_FITSIO = importlib.util.find_spec("fitsio") is not None
Expand Down Expand Up @@ -37,7 +37,7 @@ def _test_append(
hdu.write(data, names=names, firstrow=hdu.get_nrows())

with (
user.write_catalog(tmp_path / filename_gfits, ext="CATALOG") as out,
write_catalog(tmp_path / filename_gfits, ext="CATALOG") as out,
fitsio.FITS(tmp_path / filename_tfits, "rw", clobber=True) as my_fits,
):
for i in range(my_max):
Expand Down Expand Up @@ -80,7 +80,7 @@ def raise_error(msg: str) -> None:
raise TestWriteError(msg)

try:
with user.write_catalog(tmp_path / filename, ext="CATALOG") as out:
with write_catalog(tmp_path / filename, ext="CATALOG") as out:
for i in range(my_max):
if i == except_int:
msg = "Unhandled exception"
Expand Down
7 changes: 6 additions & 1 deletion tests/test_galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import pytest
import pytest_mock

from glass.galaxies import galaxy_shear, gaussian_phz, redshifts, redshifts_from_nz
from glass import (
galaxy_shear,
gaussian_phz,
redshifts,
redshifts_from_nz,
)


def test_redshifts(mocker: pytest_mock.MockerFixture) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import numpy.typing as npt
import pytest

from glass.lensing import (
from glass import (
MultiPlaneConvergence,
RadialWindow,
deflect,
multi_plane_matrix,
multi_plane_weights,
)
from glass.shells import RadialWindow

if typing.TYPE_CHECKING:
from cosmology import Cosmology
Expand Down
2 changes: 1 addition & 1 deletion tests/test_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy.typing as npt
import pytest

from glass.points import (
from glass import (
effective_bias,
linear_bias,
loglinear_bias,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_shapes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from glass.shapes import (
from glass import (
ellipticity_gaussian,
ellipticity_intnorm,
ellipticity_ryden04,
Expand Down
7 changes: 6 additions & 1 deletion tests/test_shells.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import numpy as np
import pytest

from glass.shells import RadialWindow, partition, restrict, tophat_windows
from glass import (
RadialWindow,
partition,
restrict,
tophat_windows,
)


def test_tophat_windows() -> None:
Expand Down

0 comments on commit e11deb8

Please sign in to comment.