Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-124: use cosmology.api over the cosmology package #397

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions glass/galaxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from glass.core.array import broadcast_leading_axes, cumtrapz

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

from glass.shells import RadialWindow

Expand Down Expand Up @@ -300,7 +300,7 @@ def kappa_ia_nla( # noqa: PLR0913
delta: npt.NDArray[np.float64],
zeff: float,
a_ia: float,
cosmo: Cosmology,
cosmo: StandardCosmology,
*,
z0: float = 0.0,
eta: float = 0.0,
Expand Down
12 changes: 6 additions & 6 deletions glass/lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
if typing.TYPE_CHECKING:
import collections.abc

from cosmology import Cosmology
from cosmology.api import StandardCosmology

from glass.shells import RadialWindow

Expand Down Expand Up @@ -269,7 +269,7 @@ def shear_from_convergence(
class MultiPlaneConvergence:
"""Compute convergence fields iteratively from multiple matter planes."""

def __init__(self, cosmo: Cosmology) -> None:
def __init__(self, cosmo: StandardCosmology) -> None:
"""Create a new instance to iteratively compute the convergence."""
self.cosmo = cosmo

Expand Down Expand Up @@ -333,9 +333,9 @@ def add_plane(
t = r13 / r12

# lensing weight of mass plane to be added
f = 3 * self.cosmo.omega_m / 2
f = 3 * self.cosmo.Omega_m0 / 2
f *= x2 * self.r23
f *= (1 + self.z2) / self.cosmo.ef(self.z2)
f *= (1 + self.z2) / self.cosmo.H_over_H0(self.z2)
f *= w2

# create kappa planes on first iteration
Expand Down Expand Up @@ -377,7 +377,7 @@ def wlens(self) -> float:

def multi_plane_matrix(
shells: collections.abc.Sequence[RadialWindow],
cosmo: Cosmology,
cosmo: StandardCosmology,
) -> npt.NDArray[np.float64]:
"""Compute the matrix of lensing contributions from each shell."""
mpc = MultiPlaneConvergence(cosmo)
Expand All @@ -391,7 +391,7 @@ def multi_plane_matrix(
def multi_plane_weights(
weights: npt.NDArray[np.float64],
shells: collections.abc.Sequence[RadialWindow],
cosmo: Cosmology,
cosmo: StandardCosmology,
) -> npt.NDArray[np.float64]:
"""
Compute effective weights for multi-plane convergence.
Expand Down
18 changes: 9 additions & 9 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,34 @@
from glass.core.array import ndinterp

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

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


def distance_weight(
z: npt.NDArray[np.float64],
cosmo: Cosmology,
cosmo: StandardCosmology,
) -> npt.NDArray[np.float64]:
"""Uniform weight in comoving distance."""
return 1 / cosmo.ef(z) # type: ignore[no-any-return]
return 1 / cosmo.H_over_H0(z) # type: ignore[no-any-return]


def volume_weight(
z: npt.NDArray[np.float64],
cosmo: Cosmology,
cosmo: StandardCosmology,
) -> npt.NDArray[np.float64]:
"""Uniform weight in comoving volume."""
return cosmo.xm(z) ** 2 / cosmo.ef(z) # type: ignore[no-any-return]
return cosmo.xm(z) ** 2 / cosmo.H_over_H0(z) # type: ignore[no-any-return]


def density_weight(
z: npt.NDArray[np.float64],
cosmo: Cosmology,
cosmo: StandardCosmology,
) -> npt.NDArray[np.float64]:
"""Uniform weight in matter density."""
return cosmo.rho_m_z(z) * cosmo.xm(z) ** 2 / cosmo.ef(z) # type: ignore[no-any-return]
return cosmo.rho_m_z(z) * cosmo.xm(z) ** 2 / cosmo.H_over_H0(z) # type: ignore[no-any-return]


class RadialWindow(typing.NamedTuple):
Expand Down Expand Up @@ -650,15 +650,15 @@ def redshift_grid(


def distance_grid(
cosmo: Cosmology,
cosmo: StandardCosmology,
zmin: float,
zmax: float,
*,
dx: float | None = None,
num: int | None = None,
) -> npt.NDArray[np.float64]:
"""Redshift grid with uniform spacing in comoving distance."""
xmin, xmax = cosmo.dc(zmin), cosmo.dc(zmax)
xmin, xmax = cosmo.comoving_distance(zmin), cosmo.comoving_distance(zmax)
if dx is not None and num is None:
x = np.arange(xmin, np.nextafter(xmax + dx, xmax), dx)
elif dx is None and num is not None:
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ classifiers = [
"Typing :: Typed",
]
dependencies = [
"cosmology>=2022.10.9",
"cosmology.api>=0.1.0",
"gaussiancl>=2022.10.21",
"healpix>=2022.11.1",
"healpy>=1.15.0",
Expand Down Expand Up @@ -50,6 +50,7 @@ docs = [
]
examples = [
"camb",
"cosmology",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosmology is still used in the notebooks currently

"glass.ext.camb",
"jupyter",
"matplotlib",
Expand Down Expand Up @@ -156,6 +157,7 @@ lint.isort = {known-first-party = [
], sections = {"cosmo" = [
"camb",
"cosmology",
"cosmology.api",
]}}
lint.per-file-ignores = {"__init__.py" = [
"F401", # unused-import
Expand Down
14 changes: 7 additions & 7 deletions tests/test_lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from glass.shells import RadialWindow

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


@pytest.fixture
Expand All @@ -31,14 +31,14 @@ def shells() -> list[RadialWindow]:


@pytest.fixture
def cosmo() -> Cosmology:
def cosmo() -> StandardCosmology:
class MockCosmology:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the mock to match the names from cosmology.api, hence the noqa

@property
def omega_m(self) -> float:
def Omega_m0(self) -> float: # noqa: N802
return 0.3

def ef(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
return (self.omega_m * (1 + z) ** 3 + 1 - self.omega_m) ** 0.5
def H_over_H0(self, z: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]: # noqa: N802
return (self.Omega_m0 * (1 + z) ** 3 + 1 - self.Omega_m0) ** 0.5

def xm(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no equivalently named function in cosmology.api. In practice, this would be

cosmology.api.Standard.Cosmology.transverse_comoving_distance(z) / (cosmology.api.CosmologyConstantsNamespace.c / cosmology.api.Standard.Cosmology.H0)

self,
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_deflect_many(rng: np.random.Generator) -> None:

def test_multi_plane_matrix(
shells: list[RadialWindow],
cosmo: Cosmology,
cosmo: StandardCosmology,
rng: np.random.Generator,
) -> None:
mat = multi_plane_matrix(shells, cosmo)
Expand All @@ -119,7 +119,7 @@ def test_multi_plane_matrix(

def test_multi_plane_weights(
shells: list[RadialWindow],
cosmo: Cosmology,
cosmo: StandardCosmology,
rng: np.random.Generator,
) -> None:
w_in = np.eye(len(shells))
Expand Down