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-384: rename all internal functions to trapezoid over trapz #392

Open
wants to merge 9 commits into
base: paddy/issue-163
Choose a base branch
from
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 cumtrapezoid(
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, cumtrapezoid

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 = cumtrapezoid(nz_out[k], z_out[k], dtype=float)
cdf /= cdf[-1]

# sample redshifts and store result
Expand Down
4 changes: 2 additions & 2 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 cumtrapezoid


def vmap_galactic_ecliptic(
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 = cumtrapezoid(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
22 changes: 11 additions & 11 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,
cumtrapezoid,
ndinterp,
trapz_product,
trapezoid_product,
)

# check if scipy is available for testing
Expand Down Expand Up @@ -144,20 +144,20 @@ 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:
def test_cumtrapezoid() -> None:
from scipy.integrate import cumulative_trapezoid

# 1D f and x
Expand All @@ -167,18 +167,18 @@ def test_cumtrapz() -> None:

# default dtype (int - not supported by scipy)

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

# explicit dtype (float)

glass_ct = cumtrapz(f, x, dtype=float)
glass_ct = cumtrapezoid(f, x, dtype=float)
scipy_ct = 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,)))
result = cumtrapezoid(f, x, dtype=float, out=np.zeros((4,)))
scipy_ct = cumulative_trapezoid(f, x, initial=0)
np.testing.assert_allclose(result, scipy_ct)

Expand All @@ -189,17 +189,17 @@ def test_cumtrapz() -> None:

# default dtype (int - not supported by scipy)

glass_ct = cumtrapz(f, x)
glass_ct = cumtrapezoid(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)
glass_ct = cumtrapezoid(f, x, dtype=float)
scipy_ct = 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)))
glass_ct = cumtrapezoid(f, x, dtype=float, out=np.zeros((2, 4)))
scipy_ct = cumulative_trapezoid(f, x, initial=0)
np.testing.assert_allclose(glass_ct, scipy_ct)