Skip to content

Commit

Permalink
Merge branch 'paddy/issue-358' into saransh/test_points
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp authored Nov 5, 2024
2 parents bb55c76 + 0df5a8c commit 88f5ee4
Show file tree
Hide file tree
Showing 30 changed files with 889 additions and 488 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ b42067f6776dcd763827000d585a88e071b78af3
f9bac62f497a7288aa71fd4dbd948945c37f854e
# applied ruff-linting - https://github.com/glass-dev/glass/pull/232
e58d8616c03b8447aba90996905a98b42f18ba0a
# added static typing - https://github.com/glass-dev/glass/pull/368
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist
.env
.coverage*
coverage*
.ipynb_checkpoints
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ repos:
args:
- --strict
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
rev: v0.7.2
hooks:
- id: ruff
- id: ruff-format
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.19.0
rev: 1.19.1
hooks:
- id: blacken-docs
additional_dependencies:
Expand All @@ -62,11 +62,11 @@ repos:
hooks:
- id: forbid-tabs
- repo: https://github.com/rhysd/actionlint
rev: v1.7.3
rev: v1.7.4
hooks:
- id: actionlint
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.12.1
rev: v1.13.0
hooks:
- id: mypy
additional_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

html_logo = "_static/logo.png"
html_favicon = "_static/favicon.ico"
html_css_files = [] # type: ignore[var-annotated]
html_css_files: list[str] = []


# -- Intersphinx -------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion examples/2-advanced/cosmic_shear.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@
"\n",
" # apply the shear fields to the ellipticities\n",
" gal_she = glass.galaxy_shear(\n",
" gal_lon, gal_lat, gal_eps, kappa_i, gamm1_i, gamm2_i\n",
" gal_lon,\n",
" gal_lat,\n",
" gal_eps,\n",
" kappa_i,\n",
" gamm1_i,\n",
" gamm2_i,\n",
" )\n",
"\n",
" # map the galaxy shears to a HEALPix map; this is opaque but works\n",
Expand Down
12 changes: 10 additions & 2 deletions examples/2-advanced/stage_4_galaxies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@
"\n",
" # generate galaxy positions from the matter density contrast\n",
" for gal_lon, gal_lat, gal_count in glass.positions_from_delta(\n",
" ngal[i], delta_i, bias, vis\n",
" ngal[i],\n",
" delta_i,\n",
" bias,\n",
" vis,\n",
" ):\n",
" # generate random redshifts over the given shell\n",
" gal_z = glass.redshifts(gal_count, ws[i])\n",
Expand All @@ -297,7 +300,12 @@
"\n",
" # apply the shear fields to the ellipticities\n",
" gal_she = glass.galaxy_shear(\n",
" gal_lon, gal_lat, gal_eps, kappa_i, gamm1_i, gamm2_i\n",
" gal_lon,\n",
" gal_lat,\n",
" gal_eps,\n",
" kappa_i,\n",
" gamm1_i,\n",
" gamm2_i,\n",
" )\n",
"\n",
" # make a mini-catalogue for the new rows\n",
Expand Down
6 changes: 3 additions & 3 deletions glass/core/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@


def nnls(
a: npt.ArrayLike,
b: npt.ArrayLike,
a: npt.NDArray[np.float64],
b: npt.NDArray[np.float64],
*,
tol: float = 0.0,
maxiter: int | None = None,
) -> npt.ArrayLike:
) -> npt.NDArray[np.float64]:
"""
Compute a non-negative least squares solution.
Expand Down
51 changes: 45 additions & 6 deletions glass/core/array.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
"""Module for array utilities."""

from __future__ import annotations

import typing
from functools import partial

import numpy as np
import numpy.typing as npt


def broadcast_first(*arrays): # type: ignore[no-untyped-def]
def broadcast_first(
*arrays: npt.NDArray[np.float64],
) -> tuple[npt.NDArray[np.float64], ...]:
"""Broadcast arrays, treating the first axis as common."""
arrays = tuple(np.moveaxis(a, 0, -1) if np.ndim(a) else a for a in arrays)
arrays = np.broadcast_arrays(*arrays)
return tuple(np.moveaxis(a, -1, 0) if np.ndim(a) else a for a in arrays)


def broadcast_leading_axes(*args): # type: ignore[no-untyped-def]
def broadcast_leading_axes(
*args: tuple[
float | npt.NDArray[np.float64],
int,
],
) -> tuple[
tuple[int, ...],
typing.Unpack[tuple[npt.NDArray[np.float64], ...]],
]:
"""
Broadcast all but the last N axes.
Expand Down Expand Up @@ -49,7 +63,15 @@ def broadcast_leading_axes(*args): # type: ignore[no-untyped-def]
return (dims, *arrs)


def ndinterp(x, xp, fp, axis=-1, left=None, right=None, period=None): # type: ignore[no-untyped-def] # noqa: PLR0913
def ndinterp( # noqa: PLR0913
x: float | npt.NDArray[np.float64],
xp: npt.NDArray[np.float64],
fp: npt.NDArray[np.float64],
axis: int = -1,
left: float | None = None,
right: float | None = None,
period: float | None = None,
) -> npt.NDArray[np.float64]:
"""Interpolate multi-dimensional array over axis."""
return np.apply_along_axis(
partial(np.interp, x, xp),
Expand All @@ -61,8 +83,16 @@ def ndinterp(x, xp, fp, axis=-1, left=None, right=None, period=None): # type: i
)


def trapz_product(f, *ff, axis=-1): # type: ignore[no-untyped-def]
def trapz_product(
f: tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]],
*ff: tuple[
npt.NDArray[np.float64],
npt.NDArray[np.float64],
],
axis: int = -1,
) -> npt.NDArray[np.float64]:
"""Trapezoidal rule for a product of functions."""
x: npt.NDArray[np.float64]
x, _ = f
for x_, _ in ff:
x = np.union1d(
Expand All @@ -72,10 +102,19 @@ def trapz_product(f, *ff, axis=-1): # type: ignore[no-untyped-def]
y = np.interp(x, *f)
for f_ in ff:
y *= np.interp(x, *f_)
return np.trapz(y, x, axis=axis) # type: ignore[attr-defined]
return np.trapz( # type: ignore[attr-defined]
y,
x,
axis=axis,
)


def cumtrapz(f, x, dtype=None, out=None): # type: ignore[no-untyped-def]
def cumtrapz(
f: npt.NDArray[np.int_] | npt.NDArray[np.float64],
x: npt.NDArray[np.int_] | npt.NDArray[np.float64],
dtype: npt.DTypeLike | None = None,
out: npt.NDArray[np.float64] | None = None,
) -> npt.NDArray[np.float64]:
"""Cumulative trapezoidal rule along last axis."""
if out is None:
out = np.empty_like(f, dtype=dtype)
Expand Down
4 changes: 2 additions & 2 deletions glass/ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""


def _extend_path(path, name) -> list: # type: ignore[no-untyped-def, type-arg]
def _extend_path(path: list[str], name: str) -> list[str]:
import os.path
from pkgutil import extend_path

Expand All @@ -17,7 +17,7 @@ def _extend_path(path, name) -> list: # type: ignore[no-untyped-def, type-arg]
filter(
os.path.isdir,
(os.path.join(p, _mod) for p in extend_path(path, _pkg)), # noqa: PTH118
)
),
)


Expand Down
Loading

0 comments on commit 88f5ee4

Please sign in to comment.