ColmTalbot / wcosmo

Backend agnostic gwcosmology tools
https://wcosmo.readthedocs.io/en/latest/
MIT License
2 stars 1 forks source link

Backend agnostic astropy-like cosmology

An efficient implementation of :code:astropy-like cosmology compatible with :code:numpy-like backends, e.g., :code:jax and :code:cupy.

There are two main features leading to superior efficiency to :code:astropy:

The primary limitations are:

Installation and contribution

:code:wcosmo can be installed via :code:conda-forge, :code:pypi or from source.

.. code-block:: console

$ mamba install -c conda-forge wcosmo
$ pip install wcosmo
$ pip install git+https://github.com/ColmTalbot/wcosmo.git

for development you should follow a standard fork-and-pull workflow.

Basic usage

To import an astropy-like cosmology

.. code-block:: python

>>> from wcosmo import FlatwCDM
>>> cosmology = FlatwCDM(H0=70, Om0=0.3, w0=-1)
>>> cosmology.luminosity_distance(1)

Explicit usage of :code:astropy units can be freely enabled/disabled. In this case, the values will have the default units for each method.

.. code-block:: python

>>> from wcosmo import FlatwCDM
>>> from wcosmo.utils import disable_units, enable_units
>>> cosmology = FlatwCDM(H0=70, Om0=0.3, w0=-1)

>>> disable_units()
>>> cosmology.luminosity_distance(1)
6607.657732077576

>>> enable_units()
>>> cosmology.luminosity_distance(1)
<Quantity 6607.65773208 Mpc>

GWPopulation ^^^^^^^^^^^^

The primary intention for this package is for use with :code:GWPopulation. This code is automatically used in :code:GWPopulation when using either :code:gwpopulation.experimental.cosmo_models.CosmoModel and/or :code:PowerLawRedshift

Changing backend ^^^^^^^^^^^^^^^^

The backend can be switched automatically using, e.g.,

.. code-block:: python

>>> import gwpopulation
>>> gwpopulation.backend.set_backend("jax")

Manual backend setting can be done as follows:

.. code-block:: python

>>> import jax.numpy as jnp
>>> from jax.scipy.linalg.toeplitz import toeplitz

>>> from wcosmo import wcosmo, utils
>>> wcosmo.xp = jnp
>>> utils.xp = jnp
>>> utils.toeplitz = toeplitz