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 1 commit
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
6 changes: 3 additions & 3 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 All @@ -322,7 +322,7 @@ def kappa_ia_nla( # noqa: PLR0913
a_ia:
Intrinsic alignments amplitude.
cosmo:
Cosmology instance.
StandardCosmology instance.
z0:
Reference redshift for the redshift dependence.
eta:
Expand Down
14 changes: 7 additions & 7 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 @@ -270,7 +270,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 @@ -334,9 +334,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 @@ -378,7 +378,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 @@ -392,7 +392,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 All @@ -414,7 +414,7 @@ def multi_plane_weights(
shells:
Window functions of the shells.
cosmo:
Cosmology instance.
StandardCosmology instance.

"""
# ensure shape of weights ends with the number of shells
Expand Down
18 changes: 9 additions & 9 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,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 @@ -649,15 +649,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 @@ -20,7 +20,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 @@ -49,6 +49,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 @@ -151,6 +152,7 @@ lint.isort = {known-first-party = [
], sections = {"cosmo" = [
"camb",
"cosmology",
"cosmology.api",
]}}
lint.per-file-ignores = {"__init__.py" = [
"F401", # unused-import
Expand Down
8 changes: 4 additions & 4 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,7 +31,7 @@ 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:
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