From 2c83a76ab50c36eeceffcac6ee6a80f00c412bdb Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Wed, 30 Oct 2024 15:07:30 +0000 Subject: [PATCH 1/2] Rename `trapz_product` to `trapezoid_product` --- glass/core/array.py | 2 +- glass/points.py | 4 ++-- tests/core/test_array.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/glass/core/array.py b/glass/core/array.py index 182b7b98..e50dfbe2 100644 --- a/glass/core/array.py +++ b/glass/core/array.py @@ -85,7 +85,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], diff --git a/glass/points.py b/glass/points.py index 5f8df0aa..ecad9879 100644 --- a/glass/points.py +++ b/glass/points.py @@ -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 @@ -83,7 +83,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( diff --git a/tests/core/test_array.py b/tests/core/test_array.py index 8258b75e..0e99d344 100644 --- a/tests/core/test_array.py +++ b/tests/core/test_array.py @@ -9,7 +9,7 @@ broadcast_leading_axes, cumtrapz, ndinterp, - trapz_product, + trapezoid_product, ) # check if scipy is available for testing @@ -144,14 +144,14 @@ 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) From 32cc85eae04682a4708437a2a031911c5edffb2f Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Wed, 30 Oct 2024 15:09:53 +0000 Subject: [PATCH 2/2] Rename `cumtrapz` to `cumtrapezoid` --- glass/core/array.py | 2 +- glass/galaxies.py | 4 ++-- glass/observations.py | 4 ++-- tests/core/test_array.py | 16 ++++++++-------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/glass/core/array.py b/glass/core/array.py index e50dfbe2..60b1b11e 100644 --- a/glass/core/array.py +++ b/glass/core/array.py @@ -107,7 +107,7 @@ def trapezoid_product( return np.trapezoid(y, x, axis=axis) -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, diff --git a/glass/galaxies.py b/glass/galaxies.py index 15e8b73a..db4ccb15 100644 --- a/glass/galaxies.py +++ b/glass/galaxies.py @@ -30,7 +30,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 @@ -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 diff --git a/glass/observations.py b/glass/observations.py index e1cb988e..b106654e 100644 --- a/glass/observations.py +++ b/glass/observations.py @@ -35,7 +35,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( @@ -247,7 +247,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) diff --git a/tests/core/test_array.py b/tests/core/test_array.py index 0e99d344..508a4640 100644 --- a/tests/core/test_array.py +++ b/tests/core/test_array.py @@ -7,7 +7,7 @@ from glass.core.array import ( broadcast_first, broadcast_leading_axes, - cumtrapz, + cumtrapezoid, ndinterp, trapezoid_product, ) @@ -157,7 +157,7 @@ def test_trapezoid_product() -> None: @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 @@ -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) @@ -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)