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
paddyroddy authored Nov 18, 2024
2 parents 5bb94b3 + d6c8cdb commit ecbac74
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 116 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
uses: actions/checkout@v4

- name: Replace stable version with the release version
if: github.event_name == 'release' && github.event.action == 'published'
run: |-
# store the release tag
tag="${{ github.ref_name }}"
Expand All @@ -36,6 +37,9 @@ jobs:
--expression "$url_pattern" \
${{ github.workspace }}/README.md
# store the tag since the git checkout is now dirty
echo "SETUPTOOLS_SCM_PRETEND_VERSION=${tag}" >> $GITHUB_ENV
- name: Build SDist and wheel
run: pipx run build

Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
Expand Down Expand Up @@ -79,7 +78,7 @@ jobs:
- name: Coveralls finished
uses: coverallsapp/github-action@v2
with:
carryforward: run-3.9,run-3.10,run-3.11,run-3.12,run-3.13
carryforward: run-3.10,run-3.11,run-3.12,run-3.13
parallel-finished: true

build:
Expand Down
2 changes: 1 addition & 1 deletion examples/1-basic/photoz.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"tomo_nz = glass.tomo_nz_gausserr(z, dndz, phz_sigma_0, zbins)\n",
"tomo_nz *= ARCMIN2_SPHERE * (z[-1] - z[0]) / 40\n",
"\n",
"for (z1, z2), nz in zip(zbins, tomo_nz):\n",
"for (z1, z2), nz in zip(zbins, tomo_nz, strict=False):\n",
" plt.hist(\n",
" ztrue[(z1 <= zphot) & (zphot < z2)],\n",
" bins=40,\n",
Expand Down
10 changes: 4 additions & 6 deletions glass/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def broadcast_leading_axes(
shapes.append(s[:i])
trails.append(s[i:])
dims = np.broadcast_shapes(*shapes)
arrs = (np.broadcast_to(a, dims + t) for (a, _), t in zip(args, trails))
arrs = (
np.broadcast_to(a, dims + t) for (a, _), t in zip(args, trails, strict=False)
)
return (dims, *arrs)


Expand Down Expand Up @@ -164,11 +166,7 @@ def trapz_product(
y = np.interp(x, *f)
for f_ in ff:
y *= np.interp(x, *f_)
return np.trapz( # type: ignore[attr-defined, no-any-return]
y,
x,
axis=axis,
)
return np.trapezoid(y, x, axis=axis) # type: ignore[no-any-return]


def cumtrapz(
Expand Down
2 changes: 1 addition & 1 deletion glass/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from gaussiancl import gaussiancl

Cls = collections.abc.Sequence[
typing.Union[npt.NDArray[np.float64], collections.abc.Sequence[float]]
npt.NDArray[np.float64] | collections.abc.Sequence[float]
]


Expand Down
12 changes: 1 addition & 11 deletions glass/lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,7 @@ def add_window(self, delta: npt.NDArray[np.float64], w: RadialWindow) -> None:
"""
zsrc = w.zeff
lens_weight = float(
np.trapz( # type: ignore[attr-defined]
w.wa,
w.za,
)
/ np.interp(
zsrc,
w.za,
w.wa,
)
)
lens_weight = float(np.trapezoid(w.wa, w.za) / np.interp(zsrc, w.za, w.wa))

self.add_plane(delta, zsrc, lens_weight)

Expand Down
17 changes: 5 additions & 12 deletions glass/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from __future__ import annotations

import itertools
import math

import healpy as hp
Expand Down Expand Up @@ -119,11 +120,7 @@ def gaussian_nz(
sigma = np.reshape(sigma, np.shape(sigma) + (1,) * np.ndim(z))

nz = np.exp(-(((z - mean) / sigma) ** 2) / 2)
nz /= np.trapz( # type: ignore[attr-defined]
nz,
z,
axis=-1,
)[..., np.newaxis]
nz /= np.trapezoid(nz, z, axis=-1)[..., np.newaxis]

if norm is not None:
nz *= norm
Expand Down Expand Up @@ -184,11 +181,7 @@ def smail_nz(
beta = np.asanyarray(beta)[..., np.newaxis]

pz = z**alpha * np.exp(-alpha / beta * (z / z_mode) ** beta)
pz /= np.trapz( # type: ignore[attr-defined]
pz,
z,
axis=-1,
)[..., np.newaxis]
pz /= np.trapezoid(pz, z, axis=-1)[..., np.newaxis]

if norm is not None:
pz *= norm
Expand Down Expand Up @@ -238,7 +231,7 @@ def fixed_zbins(
msg = "exactly one of nbins and dz must be given"
raise ValueError(msg)

return list(zip(zbinedges, zbinedges[1:]))
return list(itertools.pairwise(zbinedges))


def equal_dens_zbins(
Expand Down Expand Up @@ -274,7 +267,7 @@ def equal_dens_zbins(
cuml_nz /= cuml_nz[[-1]]
zbinedges = np.interp(np.linspace(0, 1, nbins + 1), cuml_nz, z)

return list(zip(zbinedges, zbinedges[1:]))
return list(itertools.pairwise(zbinedges))


def tomo_nz_gausserr(
Expand Down
7 changes: 2 additions & 5 deletions glass/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ def effective_bias(
\;.
"""
norm = np.trapz( # type: ignore[attr-defined]
w.wa,
w.za,
)
return trapz_product((z, bz), (w.za, w.wa)) / norm # type: ignore[no-any-return]
norm = np.trapezoid(w.wa, w.za)
return trapz_product((z, bz), (w.za, w.wa)) / norm


def linear_bias(
Expand Down
73 changes: 13 additions & 60 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from __future__ import annotations

import collections.abc
import itertools
import typing
import warnings

Expand All @@ -58,7 +59,7 @@
if typing.TYPE_CHECKING:
from cosmology import Cosmology

ArrayLike1D = typing.Union[collections.abc.Sequence[float], npt.NDArray[np.float64]]
ArrayLike1D = collections.abc.Sequence[float] | npt.NDArray[np.float64]
WeightFunc = typing.Callable[[ArrayLike1D], npt.NDArray[np.float64]]


Expand Down Expand Up @@ -232,17 +233,11 @@ def tophat_windows(
wht: WeightFunc
wht = weight if weight is not None else np.ones_like
ws = []
for zmin, zmax in zip(zbins, zbins[1:]):
for zmin, zmax in itertools.pairwise(zbins):
n = max(round((zmax - zmin) / dz), 2)
z = np.linspace(zmin, zmax, n)
w = wht(z)
zeff = np.trapz( # type: ignore[attr-defined]
w * z,
z,
) / np.trapz( # type: ignore[attr-defined]
w,
z,
)
zeff = np.trapezoid(w * z, z) / np.trapezoid(w, z)
ws.append(RadialWindow(z, w, zeff))
return ws

Expand Down Expand Up @@ -296,7 +291,7 @@ def linear_windows(
warnings.warn("first triangular window does not start at z=0", stacklevel=2)

ws = []
for zmin, zmid, zmax in zip(zgrid, zgrid[1:], zgrid[2:]):
for zmin, zmid, zmax in zip(zgrid, zgrid[1:], zgrid[2:], strict=False):
n = max(round((zmid - zmin) / dz), 2) - 1
m = max(round((zmax - zmid) / dz), 2)
z = np.concatenate(
Expand Down Expand Up @@ -361,7 +356,7 @@ def cubic_windows(
warnings.warn("first cubic spline window does not start at z=0", stacklevel=2)

ws = []
for zmin, zmid, zmax in zip(zgrid, zgrid[1:], zgrid[2:]):
for zmin, zmid, zmax in zip(zgrid, zgrid[1:], zgrid[2:], strict=False):
n = max(round((zmid - zmin) / dz), 2) - 1
m = max(round((zmax - zmid) / dz), 2)
z = np.concatenate(
Expand Down Expand Up @@ -580,11 +575,7 @@ def partition_lstsq(

# create the window function matrix
a = np.array([np.interp(zp, za, wa, left=0.0, right=0.0) for za, wa, _ in shells])
a /= np.trapz( # type: ignore[attr-defined]
a,
zp,
axis=-1,
)[..., None]
a /= np.trapezoid(a, zp, axis=-1)[..., None]
a = a * dz

# create the target vector of distribution values
Expand All @@ -594,20 +585,7 @@ def partition_lstsq(
# append a constraint for the integral
mult = 1 / sumtol
a = np.concatenate([a, mult * np.ones((len(shells), 1))], axis=-1)
b = np.concatenate(
[
b,
mult
* np.reshape(
np.trapz( # type: ignore[attr-defined]
fz,
z,
),
(*dims, 1),
),
],
axis=-1,
)
b = np.concatenate([b, mult * np.reshape(np.trapezoid(fz, z), (*dims, 1))], axis=-1)

# now a is a matrix of shape (len(shells), len(zp) + 1)
# and b is a matrix of shape (*dims, len(zp) + 1)
Expand Down Expand Up @@ -672,11 +650,7 @@ def partition_nnls(
for za, wa, _ in shells
],
)
a /= np.trapz( # type: ignore[attr-defined]
a,
zp,
axis=-1,
)[..., None]
a /= np.trapezoid(a, zp, axis=-1)[..., None]
a = a * dz

# create the target vector of distribution values
Expand All @@ -686,20 +660,7 @@ def partition_nnls(
# append a constraint for the integral
mult = 1 / sumtol
a = np.concatenate([a, mult * np.ones((len(shells), 1))], axis=-1)
b = np.concatenate(
[
b,
mult
* np.reshape(
np.trapz( # type: ignore[attr-defined]
fz,
z,
),
(*dims, 1),
),
],
axis=-1,
)
b = np.concatenate([b, mult * np.reshape(np.trapezoid(fz, z), (*dims, 1))], axis=-1)

# now a is a matrix of shape (len(shells), len(zp) + 1)
# and b is a matrix of shape (*dims, len(zp) + 1)
Expand Down Expand Up @@ -743,11 +704,7 @@ def partition_restrict(
part = np.empty((len(shells),) + np.shape(fz)[:-1])
for i, w in enumerate(shells):
zr, fr = restrict(z, fz, w)
part[i] = np.trapz( # type: ignore[attr-defined]
fr,
zr,
axis=-1,
)
part[i] = np.trapezoid(fr, zr, axis=-1)
return part


Expand Down Expand Up @@ -879,15 +836,11 @@ def combine(
* np.interp(
z,
shell.za,
shell.wa
/ np.trapz( # type: ignore[attr-defined]
shell.wa,
shell.za,
),
shell.wa / np.trapezoid(shell.wa, shell.za),
left=0.0,
right=0.0,
)
for shell, weight in zip(shells, weights)
for shell, weight in zip(shells, weights, strict=False)
],
axis=0,
)
7 changes: 6 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
nox.options.reuse_existing_virtualenvs = True
nox.options.sessions = ["lint", "tests"]

ALL_PYTHON = ["3.9", "3.10", "3.11", "3.12", "3.13"]
ALL_PYTHON = [
"3.10",
"3.11",
"3.12",
"3.13",
]


@nox.session
Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ classifiers = [
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand All @@ -25,7 +24,7 @@ dependencies = [
"gaussiancl>=2022.10.21",
"healpix>=2022.11.1",
"healpy>=1.15.0",
"numpy>=1.22.0",
"numpy>=2.1.1",
]
description = "Generator for Large Scale Structure"
dynamic = [
Expand All @@ -36,7 +35,7 @@ maintainers = [
]
name = "glass"
readme = "README.md"
requires-python = ">=3.9"
requires-python = ">=3.10"
license.file = "LICENSE"

[project.optional-dependencies]
Expand Down Expand Up @@ -142,7 +141,6 @@ lint.ignore = [
"D203", # one-blank-line-before-class
"D212", # blank-line-before-class
"ISC001", # single-line-implicit-string-concatenation (ruff-format recommended)
"NPY201", # TODO: numpy2-deprecation
"RUF003", # ambiguous-unicode-character-comment
]
lint.isort = {known-first-party = [
Expand Down
4 changes: 2 additions & 2 deletions tests/test_lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_multi_plane_matrix(

deltas = rng.random((len(shells), 10))
kappas = []
for shell, delta in zip(shells, deltas):
for shell, delta in zip(shells, deltas, strict=False):
convergence.add_window(delta, shell)
if convergence.kappa is not None:
kappas.append(convergence.kappa.copy())
Expand All @@ -133,7 +133,7 @@ def test_multi_plane_weights(
deltas = rng.random((len(shells), 10))
weights = rng.random((len(shells), 3))
kappa = 0
for shell, delta, weight in zip(shells, deltas, weights):
for shell, delta, weight in zip(shells, deltas, weights, strict=False):
convergence.add_window(delta, shell)
kappa = kappa + weight[..., None] * convergence.kappa
kappa /= weights.sum(axis=0)[..., None]
Expand Down
Loading

0 comments on commit ecbac74

Please sign in to comment.