Skip to content

Commit

Permalink
gh-478: glass.core.array -> glass.arraytools (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp authored Feb 19, 2025
1 parent 41ff04f commit 84dff24
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 45 deletions.
2 changes: 1 addition & 1 deletion glass/core/array.py → glass/arraytools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Module for array utilities.""" # noqa: A005
"""Module for array utilities."""

from __future__ import annotations

Expand Down
10 changes: 0 additions & 10 deletions glass/core/__init__.py

This file was deleted.

6 changes: 3 additions & 3 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 glass
import glass.core.array
import glass.arraytools

if TYPE_CHECKING:
from numpy.typing import NDArray
Expand Down Expand Up @@ -117,7 +117,7 @@ def redshifts_from_nz(
rng = np.random.default_rng()

# bring inputs' leading axes into common shape
dims, *rest = glass.core.array.broadcast_leading_axes((count, 0), (z, 1), (nz, 1))
dims, *rest = glass.arraytools.broadcast_leading_axes((count, 0), (z, 1), (nz, 1))
count_out, z_out, nz_out = rest

# list of results for all dimensions
Expand All @@ -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 = glass.core.array.cumulative_trapezoid(nz_out[k], z_out[k], dtype=float)
cdf = glass.arraytools.cumulative_trapezoid(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 healpy as hp
import numpy as np

import glass.core.array
import glass.arraytools

if TYPE_CHECKING:
from numpy.typing import NDArray
Expand Down Expand Up @@ -261,7 +261,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 = glass.core.array.cumulative_trapezoid(nz, z)
cuml_nz = glass.arraytools.cumulative_trapezoid(nz, z)
cuml_nz /= cuml_nz[[-1]]
zbinedges = np.interp(np.linspace(0, 1, nbins + 1), cuml_nz, z)

Expand Down
8 changes: 4 additions & 4 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 glass
import glass.core.array
import glass.arraytools

if TYPE_CHECKING:
from collections.abc import Callable, Generator
Expand Down Expand Up @@ -85,7 +85,7 @@ def effective_bias(
"""
norm = np.trapezoid(w.wa, w.za)
return glass.core.array.trapezoid_product((z, bz), (w.za, w.wa)) / norm
return glass.arraytools.trapezoid_product((z, bz), (w.za, w.wa)) / norm


def linear_bias(
Expand Down Expand Up @@ -232,7 +232,7 @@ def positions_from_delta( # noqa: PLR0912, PLR0913, PLR0915
inputs.append((bias, 0))
if vis is not None:
inputs.append((vis, 1))
dims, *rest = glass.core.array.broadcast_leading_axes(*inputs)
dims, *rest = glass.arraytools.broadcast_leading_axes(*inputs)
ngal, delta, *rest = rest
if bias is not None:
bias, *rest = rest
Expand Down Expand Up @@ -409,7 +409,7 @@ def position_weights(
"""
# bring densities and bias into the same shape
if bias is not None:
densities, bias = glass.core.array.broadcast_first(densities, bias)
densities, bias = glass.arraytools.broadcast_first(densities, bias)
# normalise densities after shape has been fixed
densities = densities / np.sum(densities, axis=0)
# apply bias after normalisation
Expand Down
10 changes: 5 additions & 5 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import numpy as np

import glass.algorithm
import glass.core.array
import glass.arraytools

if TYPE_CHECKING:
from collections.abc import Callable, Sequence
Expand Down Expand Up @@ -411,9 +411,9 @@ def restrict(
"""
z_ = np.compress(np.greater(z, w.za[0]) & np.less(z, w.za[-1]), z)
zr = np.union1d(w.za, z_)
fr = glass.core.array.ndinterp(
fr = glass.arraytools.ndinterp(
zr, z, f, left=0.0, right=0.0
) * glass.core.array.ndinterp(zr, w.za, w.wa)
) * glass.arraytools.ndinterp(zr, w.za, w.wa)
return zr, fr


Expand Down Expand Up @@ -583,7 +583,7 @@ def partition_lstsq(
a = a * dz

# create the target vector of distribution values
b = glass.core.array.ndinterp(zp, z, fz, left=0.0, right=0.0)
b = glass.arraytools.ndinterp(zp, z, fz, left=0.0, right=0.0)
b = b * dz

# append a constraint for the integral
Expand Down Expand Up @@ -658,7 +658,7 @@ def partition_nnls(
a = a * dz

# create the target vector of distribution values
b = glass.core.array.ndinterp(zp, z, fz, left=0.0, right=0.0)
b = glass.arraytools.ndinterp(zp, z, fz, left=0.0, right=0.0)
b = b * dz

# append a constraint for the integral
Expand Down
Empty file removed tests/core/__init__.py
Empty file.
40 changes: 20 additions & 20 deletions tests/core/test_array.py → tests/test_arraytools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

import glass.core.array
import glass.arraytools

if TYPE_CHECKING:
from numpy.typing import NDArray
Expand All @@ -15,7 +15,7 @@ def test_broadcast_first() -> None:

# arrays with shape ((3, 4, 2)) and ((1, 2)) are passed
# to np.broadcast_arrays; hence it works
a_a, b_a = glass.core.array.broadcast_first(a, b)
a_a, b_a = glass.arraytools.broadcast_first(a, b)
assert a_a.shape == (2, 3, 4)
assert b_a.shape == (2, 3, 4)

Expand All @@ -29,7 +29,7 @@ def test_broadcast_first() -> None:
b = np.ones((5, 6))

with pytest.raises(ValueError, match="shape mismatch"):
glass.core.array.broadcast_first(a, b)
glass.arraytools.broadcast_first(a, b)

# plain np.broadcast_arrays will work
a_a, b_a = np.broadcast_arrays(a, b)
Expand All @@ -43,7 +43,7 @@ def test_broadcast_leading_axes() -> None:
b_in = np.zeros((4, 10))
c_in = np.zeros((3, 1, 5, 6))

dims, *rest = glass.core.array.broadcast_leading_axes(
dims, *rest = glass.arraytools.broadcast_leading_axes(
(a_in, 0), (b_in, 1), (c_in, 2)
)
a_out, b_out, c_out = rest
Expand All @@ -61,17 +61,17 @@ def test_ndinterp() -> None:
yp = np.array([1.1, 1.2, 1.3, 1.4, 1.5])

x: float | NDArray[np.float64] = 0.5
y = glass.core.array.ndinterp(x, xp, yp)
y = glass.arraytools.ndinterp(x, xp, yp)
assert np.shape(y) == ()
np.testing.assert_allclose(y, 1.15, atol=1e-15)

x = np.array([0.5, 1.5, 2.5])
y = glass.core.array.ndinterp(x, xp, yp)
y = glass.arraytools.ndinterp(x, xp, yp)
assert np.shape(y) == (3,)
np.testing.assert_allclose(y, [1.15, 1.25, 1.35], atol=1e-15)

x = np.array([[0.5, 1.5], [2.5, 3.5]])
y = glass.core.array.ndinterp(x, xp, yp)
y = glass.arraytools.ndinterp(x, xp, yp)
assert np.shape(y) == (2, 2)
np.testing.assert_allclose(y, [[1.15, 1.25], [1.35, 1.45]], atol=1e-15)

Expand All @@ -80,17 +80,17 @@ def test_ndinterp() -> None:
yp = np.array([[1.1, 1.2, 1.3, 1.4, 1.5], [2.1, 2.2, 2.3, 2.4, 2.5]])

x = 0.5
y = glass.core.array.ndinterp(x, xp, yp)
y = glass.arraytools.ndinterp(x, xp, yp)
assert np.shape(y) == (2,)
np.testing.assert_allclose(y, [1.15, 2.15], atol=1e-15)

x = np.array([0.5, 1.5, 2.5])
y = glass.core.array.ndinterp(x, xp, yp)
y = glass.arraytools.ndinterp(x, xp, yp)
assert np.shape(y) == (2, 3)
np.testing.assert_allclose(y, [[1.15, 1.25, 1.35], [2.15, 2.25, 2.35]], atol=1e-15)

x = np.array([[0.5, 1.5], [2.5, 3.5]])
y = glass.core.array.ndinterp(x, xp, yp)
y = glass.arraytools.ndinterp(x, xp, yp)
assert np.shape(y) == (2, 2, 2)
np.testing.assert_allclose(
y,
Expand All @@ -105,12 +105,12 @@ def test_ndinterp() -> None:
)

x = 0.5
y = glass.core.array.ndinterp(x, xp, yp, axis=1)
y = glass.arraytools.ndinterp(x, xp, yp, axis=1)
assert np.shape(y) == (2, 1)
np.testing.assert_allclose(y, [[1.15], [2.15]], atol=1e-15)

x = np.array([0.5, 1.5, 2.5])
y = glass.core.array.ndinterp(x, xp, yp, axis=1)
y = glass.arraytools.ndinterp(x, xp, yp, axis=1)
assert np.shape(y) == (2, 3, 1)
np.testing.assert_allclose(
y,
Expand All @@ -119,7 +119,7 @@ def test_ndinterp() -> None:
)

x = np.array([[0.5, 1.5, 2.5, 3.5], [3.5, 2.5, 1.5, 0.5], [0.5, 3.5, 1.5, 2.5]])
y = glass.core.array.ndinterp(x, xp, yp, axis=1)
y = glass.arraytools.ndinterp(x, xp, yp, axis=1)
assert np.shape(y) == (2, 3, 4, 1)
np.testing.assert_allclose(
y,
Expand All @@ -146,7 +146,7 @@ def test_trapezoid_product() -> None:
x2 = np.linspace(1, 2, 10)
f2 = np.full_like(x2, 0.5)

s = glass.core.array.trapezoid_product((x1, f1), (x2, f2))
s = glass.arraytools.trapezoid_product((x1, f1), (x2, f2))

np.testing.assert_allclose(s, 1.0)

Expand All @@ -159,18 +159,18 @@ def test_cumulative_trapezoid() -> None:

# default dtype (int)

ct = glass.core.array.cumulative_trapezoid(f, x)
ct = glass.arraytools.cumulative_trapezoid(f, x)
np.testing.assert_allclose(ct, np.array([0, 1, 4, 7]))

# explicit dtype (float)

ct = glass.core.array.cumulative_trapezoid(f, x, dtype=float)
ct = glass.arraytools.cumulative_trapezoid(f, x, dtype=float)
np.testing.assert_allclose(ct, np.array([0.0, 1.5, 4.0, 7.5]))

# explicit return array

out = np.zeros((4,))
ct = glass.core.array.cumulative_trapezoid(f, x, dtype=float, out=out)
ct = glass.arraytools.cumulative_trapezoid(f, x, dtype=float, out=out)
np.testing.assert_equal(ct, out)

# 2D f and 1D x
Expand All @@ -180,18 +180,18 @@ def test_cumulative_trapezoid() -> None:

# default dtype (int)

ct = glass.core.array.cumulative_trapezoid(f, x)
ct = glass.arraytools.cumulative_trapezoid(f, x)
np.testing.assert_allclose(ct, np.array([[0, 2, 12, 31], [0, 2, 8, 17]]))

# explicit dtype (float)

ct = glass.core.array.cumulative_trapezoid(f, x, dtype=float)
ct = glass.arraytools.cumulative_trapezoid(f, x, dtype=float)
np.testing.assert_allclose(
ct, np.array([[0.0, 2.5, 12.25, 31.0], [0.0, 2.5, 8.5, 17.5]])
)

# explicit return array

out = np.zeros((2, 4))
ct = glass.core.array.cumulative_trapezoid(f, x, dtype=float, out=out)
ct = glass.arraytools.cumulative_trapezoid(f, x, dtype=float, out=out)
np.testing.assert_equal(ct, out)

0 comments on commit 84dff24

Please sign in to comment.