Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed May 14, 2023
1 parent e0e14d8 commit 200cd94
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 72 deletions.
16 changes: 8 additions & 8 deletions src/cosmology/compat/classy/_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ def Omega_gamma0(self) -> Array:

def Omega_gamma(self, z: InputT, /) -> Array:
"""Redshift-dependent photon density parameter."""
return self.Omega_gamma0 * (z + 1.0) ** 4 / self.h_over_h0(z) ** 2
return self.Omega_gamma0 * (z + 1.0) ** 4 / self.H_over_H0(z) ** 2

# ----------------------------------------------
# HasCriticalDensity
# CriticalDensity

@property
def critical_density0(self) -> Array:
Expand All @@ -202,7 +202,7 @@ def critical_density(self, z: InputT, /) -> Array:
return np.array(3e6 * self.H(z) ** 2 / (8 * np.pi * constants.G))

# ----------------------------------------------
# HasHubbleParameter
# HubbleParameter

@property
def H0(self) -> Array:
Expand Down Expand Up @@ -249,7 +249,7 @@ def T_cmb0(self) -> Array:

def T_cmb(self, z: InputT, /) -> Array:
"""Temperature of the CMB at redshift ``z``."""
return self.Tcmb0 * (z + 1)
return self.T_cmb0 * (z + 1)

# ----------------------------------------------
# Time
Expand Down Expand Up @@ -328,7 +328,7 @@ def _comoving_volume_flat(self, z: InputT, /) -> Array:

def _comoving_volume_positive(self, z: InputT, /) -> Array:
dh = self.hubble_distance
x = self.comoving_transverse_distance(z) / dh
x = self.transverse_comoving_distance(z) / dh
term1 = 4.0 * np.pi * dh**3 / (2.0 * self.Omega_k0)
term2 = x * np.sqrt(1 + self.Omega_k0 * (x) ** 2)
term3 = np.sqrt(np.abs(self.Omega_k0)) * x
Expand All @@ -339,7 +339,7 @@ def _comoving_volume_positive(self, z: InputT, /) -> Array:

def _comoving_volume_negative(self, z: InputT, /) -> Array:
dh = self.hubble_distance
x = self.comoving_transverse_distance(z) / dh
x = self.transverse_comoving_distance(z) / dh
term1 = 4.0 * np.pi * dh**3 / (2.0 * self.Omega_k0)
term2 = x * np.sqrt(1 + self.Omega_k0 * (x) ** 2)
term3 = np.sqrt(np.abs(self.Omega_k0)) * x
Expand Down Expand Up @@ -386,8 +386,8 @@ def differential_comoving_volume(self, z: InputT, /) -> Array:
"""
return (
self.comoving_transverse_distance(z) / self.hubble_distance
) ** 2 / self.h_over_h0(z)
self.transverse_comoving_distance(z) / self.hubble_distance
) ** 2 / self.H_over_H0(z)

# ----------------------------------------------
# Proper
Expand Down
50 changes: 25 additions & 25 deletions tests/classy/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from hypothesis import given

from cosmology.api import (
HasBaryonComponent,
HasDarkEnergyComponent,
HasDarkMatterComponent,
HasGlobalCurvatureComponent,
HasMatterComponent,
HasNeutrinoComponent,
HasPhotonComponent,
HasTotalComponent,
BaryonComponent,
CurvatureComponent,
DarkEnergyComponent,
DarkMatterComponent,
MatterComponent,
NeutrinoComponent,
PhotonComponent,
TotalComponent,
)

from .conftest import z_arr_st
Expand All @@ -24,13 +24,13 @@
################################################################################


class HasTotalComponent_Test:
class TotalComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasTotalComponent)
assert isinstance(wrapper, TotalComponent)

def test_Omega_tot0(self, wrapper, cosmo):
"""Test that the wrapper has the same Otot0 as the wrapped object."""
Expand All @@ -46,13 +46,13 @@ def test_Omega_tot(self, wrapper, vcosmo, z):
assert isinstance(omega, np.ndarray)


class HasGlobalCurvatureComponent_Test:
class CurvatureComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasGlobalCurvatureComponent)
assert isinstance(wrapper, CurvatureComponent)

def test_Omega_k0(self, wrapper, cosmo):
"""Test that the wrapper has the same Omega_k0 as the wrapped object."""
Expand All @@ -68,13 +68,13 @@ def test_Omega_k(self, wrapper, vcosmo, z):
assert isinstance(omega, np.ndarray)


class HasMatterComponent_Test:
class MatterComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasMatterComponent)
assert isinstance(wrapper, MatterComponent)

def test_Omega_m0(self, wrapper, cosmo):
"""Test that the wrapper has the same Om0 as the wrapped object."""
Expand All @@ -89,13 +89,13 @@ def test_Omega_m(self, wrapper, vcosmo, z):
assert isinstance(omega, np.ndarray)


class HasBaryonComponent_Test:
class BaryonComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasBaryonComponent)
assert isinstance(wrapper, BaryonComponent)

def test_Omega_b0(self, wrapper, cosmo):
"""Test that the wrapper has the same Omega_b0 as the wrapped object."""
Expand All @@ -110,15 +110,15 @@ def test_Omega_b(self, wrapper, cosmo, z):
assert isinstance(omega, np.ndarray)


class HasNeutrinoComponent_Test:
class NeutrinoComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

# FIXME: this should be an instance
with pytest.raises(NotImplementedError):
assert isinstance(wrapper, HasNeutrinoComponent)
assert isinstance(wrapper, NeutrinoComponent)

@pytest.mark.xfail(reason="TODO")
def test_Omega_nu0(self, wrapper, cosmo):
Expand All @@ -144,13 +144,13 @@ def test_Omega_nu(self, wrapper, vcosmo, z):
assert isinstance(omega, np.ndarray)


class HasDarkEnergyComponent_Test:
class DarkEnergyComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasDarkEnergyComponent)
assert isinstance(wrapper, DarkEnergyComponent)

def test_Omega_de0(self, wrapper, cosmo):
"""Test that the wrapper has the same Omega_de0 as the wrapped object."""
Expand All @@ -166,14 +166,14 @@ def test_Omega_de(self, wrapper, vcosmo, z):
assert isinstance(omega, np.ndarray)


class HasDarkMatterComponent_Test:
class DarkMatterComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

# FIXME: should be a HasDarkMatterComponent instance
assert isinstance(wrapper, HasDarkMatterComponent)
# FIXME: should be a DarkMatterComponent instance
assert isinstance(wrapper, DarkMatterComponent)

def test_Omega_dm0(self, wrapper, cosmo):
"""Test that the wrapper has the same Omega_dm0 as the wrapped object."""
Expand All @@ -189,13 +189,13 @@ def test_Omega_dm(self, wrapper, vcosmo, z):
assert isinstance(omega, np.ndarray)


class HasPhotonComponent_Test:
class PhotonComponent_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasPhotonComponent)
assert isinstance(wrapper, PhotonComponent)

def test_Omega_gamma0(self, wrapper, cosmo):
"""Test that the wrapper has the same Omega_gamma0 as the wrapped object."""
Expand Down
22 changes: 11 additions & 11 deletions tests/classy/test_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from hypothesis import given, settings

from cosmology.api import HasDistanceMeasures
from cosmology.api import DistanceMeasures

from .conftest import z_arr_st

Expand All @@ -15,13 +15,13 @@
################################################################################


class HasDistanceMeasures_Test:
class DistanceMeasures_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasDistanceMeasures)
assert isinstance(wrapper, DistanceMeasures)

# =========================================================================

Expand All @@ -37,15 +37,15 @@ def test_scale_factor(self, wrapper, cosmo, z):
assert np.array_equal(a, 1 / (1 + z))
assert isinstance(a, np.ndarray)

def test_Tcmb0(self, wrapper, cosmo):
def test_T_cmb0(self, wrapper, cosmo):
"""Test that the wrapper has the same Tcmb0 as the wrapped object."""
assert wrapper.Tcmb0 == cosmo.T_cmb()
assert isinstance(wrapper.Tcmb0, np.ndarray)
assert wrapper.T_cmb0 == cosmo.T_cmb()
assert isinstance(wrapper.T_cmb0, np.ndarray)

@given(z_arr_st())
def test_Tcmb(self, wrapper, cosmo, z):
def test_T_cmb(self, wrapper, cosmo, z):
"""Test that the wrapper's Tcmb is the same as the wrapped object's."""
T = wrapper.Tcmb(z)
T = wrapper.T_cmb(z)
assert np.array_equal(T, cosmo.T_cmb() * (1 + z))
assert isinstance(T, np.ndarray)

Expand Down Expand Up @@ -76,9 +76,9 @@ def test_comoving_distance(self, wrapper, vcosmo, z):
@pytest.mark.xfail(reason="TODO")
@settings(deadline=500)
@given(z_arr_st(min_value=np.float32(0.1), max_value=np.float32(1e5)))
def test_comoving_transverse_distance(self, wrapper, vcosmo, z):
"""Test the wrapper's comoving_transverse_distance."""
d = wrapper.comoving_transverse_distance(z)
def test_transverse_comoving_distance(self, wrapper, vcosmo, z):
"""Test the wrapper's transverse_comoving_distance."""
d = wrapper.transverse_comoving_distance(z)
assert np.allclose(d, vcosmo.angular_diameter_distance(z) * (1 + z))
assert isinstance(d, np.ndarray)

Expand Down
14 changes: 7 additions & 7 deletions tests/classy/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
from hypothesis import given

from cosmology.api import HasCriticalDensity, HasHubbleParameter
from cosmology.api import CriticalDensity, HubbleParameter
from cosmology.compat.classy import constants

from .conftest import z_arr_st
Expand All @@ -15,13 +15,13 @@
################################################################################


class HasCriticalDensity_Test:
class CriticalDensity_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasCriticalDensity)
assert isinstance(wrapper, CriticalDensity)

def test_critical_density0(self, wrapper, cosmo):
"""
Expand All @@ -45,13 +45,13 @@ def test_critical_density(self, wrapper, vcosmo, z):
assert isinstance(rho, np.ndarray)


class HasHubbleParameter_Test:
class HubbleParameter_Test:
def test_wrapper_is_compliant(self, wrapper):
"""Test that AstropyCosmology is a BackgroundCosmologyWrapper."""
if hasattr(super(), "test_wrapper_is_compliant"):
super().test_wrapper_is_compliant(wrapper)

assert isinstance(wrapper, HasHubbleParameter)
assert isinstance(wrapper, HubbleParameter)

def test_H0(self, wrapper, cosmo):
"""Test that the wrapper has the same H0 as the wrapped object."""
Expand All @@ -77,8 +77,8 @@ def test_H(self, wrapper, vcosmo, z):
assert isinstance(H, np.ndarray)

@given(z_arr_st(min_value=0, max_value=1e10))
def test_h_over_h0(self, wrapper, vcosmo, z):
def test_H_over_H0(self, wrapper, vcosmo, z):
"""Test that the wrapper's efunc is the same as the wrapped object's."""
e = wrapper.h_over_h0(z)
e = wrapper.H_over_H0(z)
assert np.array_equal(e, vcosmo.Hubble(z) / vcosmo.Hubble(0))
assert isinstance(e, np.ndarray)
42 changes: 21 additions & 21 deletions tests/classy/test_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@
from cosmology.compat.classy import StandardCosmologyWrapper

from .test_components import (
HasBaryonComponent_Test,
HasDarkEnergyComponent_Test,
HasDarkMatterComponent_Test,
HasGlobalCurvatureComponent_Test,
HasMatterComponent_Test,
HasNeutrinoComponent_Test,
HasPhotonComponent_Test,
HasTotalComponent_Test,
BaryonComponent_Test,
CurvatureComponent_Test,
DarkEnergyComponent_Test,
DarkMatterComponent_Test,
MatterComponent_Test,
NeutrinoComponent_Test,
PhotonComponent_Test,
TotalComponent_Test,
)
from .test_core import Test_CosmologyWrapper
from .test_distances import HasDistanceMeasures_Test
from .test_extras import HasCriticalDensity_Test, HasHubbleParameter_Test
from .test_distances import DistanceMeasures_Test
from .test_extras import CriticalDensity_Test, HubbleParameter_Test

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


class Test_StandardCosmologyWrapper(
HasTotalComponent_Test,
HasGlobalCurvatureComponent_Test,
HasMatterComponent_Test,
HasBaryonComponent_Test,
HasNeutrinoComponent_Test,
HasDarkEnergyComponent_Test,
HasDarkMatterComponent_Test,
HasPhotonComponent_Test,
HasCriticalDensity_Test,
HasHubbleParameter_Test,
HasDistanceMeasures_Test,
TotalComponent_Test,
CurvatureComponent_Test,
MatterComponent_Test,
BaryonComponent_Test,
NeutrinoComponent_Test,
DarkEnergyComponent_Test,
DarkMatterComponent_Test,
PhotonComponent_Test,
CriticalDensity_Test,
HubbleParameter_Test,
DistanceMeasures_Test,
Test_CosmologyWrapper,
):
@pytest.fixture(scope="class")
Expand Down

0 comments on commit 200cd94

Please sign in to comment.