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

Hubble parametrized #31

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ repos:
- id: rst-inline-touching-normal
- id: text-unicode-replacement-char

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.254"
hooks:
- id: ruff
args:
- --fix

- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
Expand All @@ -44,13 +51,6 @@ repos:
- id: blacken-docs
additional_dependencies: [black]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.254"
hooks:
- id: ruff
args:
- --fix

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.0.1"
hooks:
Expand Down
3 changes: 3 additions & 0 deletions src/cosmology/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from cosmology.api._constants import CosmologyConstantsAPINamespace
from cosmology.api._core import CosmologyAPI
from cosmology.api._extras import HasHubbleParameter
from cosmology.api._namespace import CosmologyAPINamespace
from cosmology.api._standard import StandardCosmologyAPI
from cosmology.api.compat import (
Expand All @@ -31,6 +32,8 @@
"DarkEnergyComponent",
"DarkMatterComponent",
"PhotonComponent",
# parametrizations
"HasHubbleParameter",
# wrappers
"CosmologyWrapperAPI",
"StandardCosmologyWrapperAPI",
Expand Down
57 changes: 57 additions & 0 deletions src/cosmology/api/_extras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""The cosmology API."""

from __future__ import annotations

from typing import Protocol

from cosmology.api._array_api import ArrayT
from cosmology.api._core import CosmologyAPI

__all__: list[str] = []


class HasHubbleParameter(CosmologyAPI[ArrayT], Protocol):
r"""The cosmology has methods to retrieve the Hubble parameter :math:`H`."""

@property
def H0(self) -> ArrayT:
"""Hubble parameter at redshift 0 in km s-1 Mpc-1."""
...

@property
def hubble_distance(self) -> ArrayT:
"""Hubble distance in Mpc."""
...

@property
def hubble_time(self) -> ArrayT:
"""Hubble time in Gyr."""
...

def H(self, z: ArrayT | float, /) -> ArrayT:
"""Hubble parameter :math:`H(z)` in km s-1 Mpc-1.

Parameters
----------
z : Array
The redshift(s) at which to evaluate the Hubble parameter.

Returns
-------
Array
"""
...

def h_over_h0(self, z: ArrayT | float, /) -> ArrayT:
"""Standardised Hubble function :math:`E(z) = H(z)/H_0`.

Parameters
----------
z : Array
The redshift(s) at which to evaluate.

Returns
-------
Array
"""
...
73 changes: 2 additions & 71 deletions src/cosmology/api/_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NeutrinoComponent,
PhotonComponent,
)
from cosmology.api._extras import HasHubbleParameter

__all__: list[str] = []

Expand All @@ -28,6 +29,7 @@ class StandardCosmologyAPI(
MatterComponent[ArrayT],
DarkEnergyComponent[ArrayT],
GlobalCurvatureComponent[ArrayT],
HasHubbleParameter[ArrayT],
FriedmannLemaitreRobertsonWalker[ArrayT],
Protocol,
):
Expand All @@ -44,29 +46,6 @@ def Tcmb0(self) -> ArrayT:
"""Temperature of the CMB at redshift 0 in Kelvin."""
...

# ----------------------------------------------
# Hubble

@property
def H0(self) -> ArrayT:
"""Hubble function at redshift 0 in km s-1 Mpc-1."""
...

@property
def h(self) -> ArrayT:
r"""Dimensionless Hubble parameter, h = H_0 / (100 [km/sec/Mpc])."""
...

@property
def hubble_distance(self) -> ArrayT:
"""Hubble distance in Mpc."""
...

@property
def hubble_time(self) -> ArrayT:
"""Hubble time in Gyr."""
...

# ----------------------------------------------
# Omega

Expand Down Expand Up @@ -102,54 +81,6 @@ def Tcmb(self, z: ArrayT | float, /) -> ArrayT:
"""
...

# ----------------------------------------------
# Hubble

def H(self, z: ArrayT | float, /) -> ArrayT:
"""Hubble function :math:`H(z)` in km s-1 Mpc-1.

Parameters
----------
z : Array
The redshift(s) at which to evaluate the Hubble parameter.

Returns
-------
Array
"""
...

def efunc(self, z: ArrayT | float, /) -> ArrayT:
"""Standardised Hubble function :math:`E(z) = H(z)/H_0`.

Parameters
----------
z : Array
The redshift(s) at which to evaluate efunc.

Returns
-------
Array
"""
...

def inv_efunc(self, z: ArrayT | float, /) -> ArrayT:
"""Inverse of ``efunc``.

Parameters
----------
z : Array, positional-only
Input redshift.

Returns
-------
Array
"""
...

# ----------------------------------------------
# Omega

def Otot(self, z: ArrayT | float, /) -> ArrayT:
r"""Redshift-dependent total density parameter.

Expand Down
24 changes: 23 additions & 1 deletion tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
StandardCosmologyAPI,
)
from cosmology.api._array_api import Array
from cosmology.api._extras import HasHubbleParameter

CT = TypeVar("CT", bound=CosmologyAPI)

Expand Down Expand Up @@ -210,7 +211,6 @@ def neutrino_cls(


@pytest.fixture(scope="session")
@pytest.mark.parametrize("comp_cls", [PhotonComponent])
def photon_cls(
cosmology_cls: type[CosmologyAPI],
) -> type[PhotonComponent | CosmologyAPI]:
Expand Down Expand Up @@ -247,6 +247,28 @@ def darkenergy_cls(
)


# ==============================================================================
# Parametrizations API


@pytest.fixture(scope="session")
def hashubbleparamet_cls(
cosmology_cls: type[CosmologyAPI],
) -> type[HasHubbleParameter | CosmologyAPI]:
"""An example standard cosmology API class."""
comp_attrs = get_comp_attrs(HasHubbleParameter)
comp_meths = get_comp_meths(HasHubbleParameter)
fields = {"H0"}
return make_dataclass(
"ExampleHasHubbleParameter",
[(n, Array, field(default_factory=_default_one)) for n in fields],
bases=(cosmology_cls,),
namespace={n: property(_return_one) for n in comp_attrs - set(fields)}
| {n: _return_1arg for n in comp_meths},
frozen=True,
)


# ==============================================================================
# Background API

Expand Down
1 change: 1 addition & 0 deletions tests/api/test_extras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test the Cosmology API standard."""
64 changes: 64 additions & 0 deletions tests/api/test_extras/test_hubble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Test ``cosmology.api.HasHubbleParameter``."""

from __future__ import annotations

from dataclasses import field, make_dataclass

from cosmology.api import HasHubbleParameter
from cosmology.api._array_api import Array

from ..conftest import _default_one, _return_1arg

################################################################################
# TESTS
################################################################################


def test_noncompliant_hubbleparametrized():
"""
Test that a non-compliant instance is not a
`cosmology.api.HasHubbleParameter`.
"""
# Simple example: missing everything

class ExampleHasHubbleParameter:
pass

cosmo = ExampleHasHubbleParameter()

assert not isinstance(cosmo, HasHubbleParameter)

# TODO: more examples?


def test_compliant_hubbleparametrized(hashubbleparamet_cls):
"""
Test that a compliant instance is a
`cosmology.api.HasHubbleParameter`.
"""
ExampleHasHubbleParameter = make_dataclass(
"ExampleHasHubbleParameter",
[(n, Array, field(default_factory=_default_one)) for n in {"H0"}],
bases=(hashubbleparamet_cls,),
namespace={
"h": _default_one,
"hubble_time": _default_one,
"hubble_distance": _default_one,
"H": _return_1arg,
"efunc": _return_1arg,
"inv_efunc": _return_1arg,
},
frozen=True,
)

cosmo = ExampleHasHubbleParameter()

assert isinstance(cosmo, HasHubbleParameter)


def test_fixture(hashubbleparamet_cls):
"""
Test that the ``hashubbleparamet_cls`` fixture is a
`cosmology.api.HasHubbleParameter`.
"""
assert isinstance(hashubbleparamet_cls(), HasHubbleParameter)
8 changes: 6 additions & 2 deletions tests/api/test_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PhotonComponent,
)
from cosmology.api._core import CosmologyAPI
from cosmology.api._extras import HasHubbleParameter

from .conftest import _default_one, _return_1arg, _return_one

Expand All @@ -25,7 +26,7 @@
################################################################################


def test_noncompliant_cosmo():
def test_noncompliant_standard():
"""
Test that a non-compliant instance is not a
`cosmology.api.CosmologyAPI`.
Expand All @@ -42,7 +43,7 @@ class StandardCosmology:
# TODO: more examples?


def test_compliant_cosmo(cosmology_cls, standard_attrs, standard_meths):
def test_compliant_standard(cosmology_cls, standard_attrs, standard_meths):
"""
Test that an instance is `cosmology.api.StandardCosmologyAPI` even if it
doesn't inherit from `cosmology.api.StandardCosmologyAPI`.
Expand Down Expand Up @@ -73,6 +74,9 @@ def test_compliant_cosmo(cosmology_cls, standard_attrs, standard_meths):
assert isinstance(cosmo, NeutrinoComponent)
assert isinstance(cosmo, PhotonComponent)

# Check Parametrizations
assert isinstance(cosmo, HasHubbleParameter)

# Full Standard Cosmology
assert isinstance(cosmo, StandardCosmologyAPI)

Expand Down