Skip to content

Commit

Permalink
gh-355: consistent use of typing and collections.abc (#356)
Browse files Browse the repository at this point in the history
Fixes #355, simplifying #308, xref #350. Change all import of
`typing`/`collections.abc` to `import <x>` rather than `from <x> import`
syntax. This is useful because it is straightforward to see at a glance
where the `import` is coming from. Further, we have both
`numpy.random.Generator` and `collections.abc.Generator` in use - so it
separates them. I've also moved everything out of the `TYPE_CHECKING`
block, except from where `ruff` says we should.
  • Loading branch information
paddyroddy authored Oct 14, 2024
1 parent 8ad8831 commit c15af39
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 53 deletions.
6 changes: 1 addition & 5 deletions glass/core/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING

import numpy as np

if TYPE_CHECKING:
import numpy.typing as npt
import numpy.typing as npt


def nnls(
Expand Down
28 changes: 16 additions & 12 deletions glass/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,30 @@

from __future__ import annotations

import collections.abc
import typing
import warnings
from collections.abc import Generator, Iterable, Sequence
from typing import Any, Callable, Optional, Union

import healpy as hp
import numpy as np
import numpy.typing as npt
from gaussiancl import gaussiancl

# types
Size = Optional[Union[int, tuple[int, ...]]]
Iternorm = tuple[Optional[int], npt.NDArray, npt.NDArray]
ClTransform = Union[str, Callable[[npt.NDArray], npt.NDArray]]
Cls = Sequence[Union[npt.NDArray, Sequence[float]]]
Size = typing.Optional[typing.Union[int, tuple[int, ...]]]
Iternorm = tuple[typing.Optional[int], npt.NDArray, npt.NDArray]
ClTransform = typing.Union[str, typing.Callable[[npt.NDArray], npt.NDArray]]
Cls = collections.abc.Sequence[
typing.Union[npt.NDArray, collections.abc.Sequence[float]]
]
Alms = npt.NDArray


def iternorm(
k: int,
cov: Iterable[npt.NDArray],
cov: collections.abc.Iterable[npt.NDArray],
size: Size = None,
) -> Generator[Iternorm, None, None]:
) -> collections.abc.Generator[Iternorm, None, None]:
"""Return the vector a and variance sigma^2 for iterative normal sampling."""
n: tuple[int, ...]
if size is None:
Expand Down Expand Up @@ -105,7 +107,9 @@ def iternorm(
yield j, a, s


def cls2cov(cls: Cls, nl: int, nf: int, nc: int) -> Generator[npt.NDArray, None, None]:
def cls2cov(
cls: Cls, nl: int, nf: int, nc: int
) -> collections.abc.Generator[npt.NDArray, None, None]:
"""Return array of cls as a covariance matrix for iterative sampling."""
cov = np.zeros((nl, nc + 1))
end = 0
Expand Down Expand Up @@ -134,7 +138,7 @@ def multalm(alm: Alms, bl: npt.NDArray, *, inplace: bool = False) -> Alms:
return out


def transform_cls(cls: Cls, tfm: ClTransform, pars: tuple[Any, ...] = ()) -> Cls:
def transform_cls(cls: Cls, tfm: ClTransform, pars: tuple[typing.Any, ...] = ()) -> Cls:
"""Transform Cls to Gaussian Cls."""
gls = []
for cl in cls:
Expand Down Expand Up @@ -212,7 +216,7 @@ def generate_gaussian(
*,
ncorr: int | None = None,
rng: np.random.Generator | None = None,
) -> Generator[npt.NDArray, None, None]:
) -> collections.abc.Generator[npt.NDArray, None, None]:
"""
Sample Gaussian random fields from Cls iteratively.
Expand Down Expand Up @@ -298,7 +302,7 @@ def generate_lognormal(
*,
ncorr: int | None = None,
rng: np.random.Generator | None = None,
) -> Generator[npt.NDArray, None, None]:
) -> collections.abc.Generator[npt.NDArray, None, None]:
"""Sample lognormal random fields from Gaussian Cls iteratively."""
for i, m in enumerate(generate_gaussian(gls, nside, ncorr=ncorr, rng=rng)):
# compute the variance of the auto-correlation
Expand Down
11 changes: 5 additions & 6 deletions glass/galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@

from __future__ import annotations

import typing
import warnings
from typing import TYPE_CHECKING

if TYPE_CHECKING:
import numpy.typing as npt

from glass.shells import RadialWindow

import healpix
import numpy as np
import numpy.typing as npt

from glass.core.array import broadcast_leading_axes, cumtrapz

if typing.TYPE_CHECKING:
from glass.shells import RadialWindow


def redshifts(
n: int | npt.ArrayLike,
Expand Down
13 changes: 6 additions & 7 deletions glass/lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@

from __future__ import annotations

from typing import TYPE_CHECKING
import typing

import healpy as hp
import numpy as np
import numpy.typing as npt

if TYPE_CHECKING:
from collections.abc import Sequence

import numpy.typing as npt
if typing.TYPE_CHECKING:
import collections.abc

from cosmology import Cosmology

Expand Down Expand Up @@ -363,7 +362,7 @@ def wlens(self) -> float:


def multi_plane_matrix(
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
cosmo: Cosmology,
) -> npt.ArrayLike:
"""Compute the matrix of lensing contributions from each shell."""
Expand All @@ -377,7 +376,7 @@ def multi_plane_matrix(

def multi_plane_weights(
weights: npt.ArrayLike,
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
cosmo: Cosmology,
) -> npt.ArrayLike:
"""
Expand Down
5 changes: 1 addition & 4 deletions glass/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@
from __future__ import annotations

import math
from typing import TYPE_CHECKING

import healpy as hp
import numpy as np
import numpy.typing as npt

from glass.core.array import cumtrapz

if TYPE_CHECKING:
import numpy.typing as npt


def vmap_galactic_ecliptic(
nside: int,
Expand Down
6 changes: 1 addition & 5 deletions glass/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING

import numpy as np

if TYPE_CHECKING:
import numpy.typing as npt
import numpy.typing as npt


def triaxial_axis_ratio(zeta, xi, size=None, *, rng=None):
Expand Down
26 changes: 13 additions & 13 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@

from __future__ import annotations

import collections.abc
import typing
import warnings
from collections.abc import Sequence
from typing import TYPE_CHECKING, Callable, NamedTuple, Union

import numpy as np
import numpy.typing as npt

from glass.core.array import ndinterp

if TYPE_CHECKING:
if typing.TYPE_CHECKING:
from cosmology import Cosmology

# types
ArrayLike1D = Union[Sequence[float], npt.NDArray]
WeightFunc = Callable[[ArrayLike1D], npt.NDArray]
ArrayLike1D = typing.Union[collections.abc.Sequence[float], npt.NDArray]
WeightFunc = typing.Callable[[ArrayLike1D], npt.NDArray]


def distance_weight(z: npt.ArrayLike, cosmo: Cosmology) -> npt.NDArray:
Expand All @@ -76,7 +76,7 @@ def density_weight(z: npt.ArrayLike, cosmo: Cosmology) -> npt.NDArray:
return cosmo.rho_m_z(z) * cosmo.xm(z) ** 2 / cosmo.ef(z)


class RadialWindow(NamedTuple):
class RadialWindow(typing.NamedTuple):
"""
A radial window, defined by a window function.
Expand Down Expand Up @@ -120,8 +120,8 @@ class RadialWindow(NamedTuple):
"""

za: Sequence[float]
wa: Sequence[float]
za: collections.abc.Sequence[float]
wa: collections.abc.Sequence[float]
zeff: float


Expand Down Expand Up @@ -338,7 +338,7 @@ def restrict(
def partition(
z: npt.ArrayLike,
fz: npt.ArrayLike,
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
*,
method: str = "nnls",
) -> npt.ArrayLike:
Expand Down Expand Up @@ -449,7 +449,7 @@ def partition(
def partition_lstsq(
z: npt.ArrayLike,
fz: npt.ArrayLike,
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
*,
sumtol: float = 0.01,
) -> npt.ArrayLike:
Expand Down Expand Up @@ -495,7 +495,7 @@ def partition_lstsq(
def partition_nnls(
z: npt.ArrayLike,
fz: npt.ArrayLike,
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
*,
sumtol: float = 0.01,
) -> npt.ArrayLike:
Expand Down Expand Up @@ -556,7 +556,7 @@ def partition_nnls(
def partition_restrict(
z: npt.ArrayLike,
fz: npt.ArrayLike,
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
) -> npt.ArrayLike:
"""Partition by restriction and integration."""
part = np.empty((len(shells),) + np.shape(fz)[:-1])
Expand Down Expand Up @@ -594,7 +594,7 @@ def distance_grid(cosmo, zmin, zmax, *, dx=None, num=None):
def combine(
z: npt.ArrayLike,
weights: npt.ArrayLike,
shells: Sequence[RadialWindow],
shells: collections.abc.Sequence[RadialWindow],
) -> npt.ArrayLike:
r"""
Evaluate a linear combination of window functions.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Issues = "https://github.com/glass-dev/glass/issues"

[tool.coverage]
report = {exclude_also = [
"if TYPE_CHECKING:",
"if typing.TYPE_CHECKING:",
], omit = [
"glass/_version.py",
], skip_covered = true, sort = "cover"}
Expand Down

0 comments on commit c15af39

Please sign in to comment.