From ff8e81b944facfe0e08088333b8c94bf1b5dfd95 Mon Sep 17 00:00:00 2001 From: nstarman Date: Tue, 31 Jan 2023 13:48:19 -0500 Subject: [PATCH 1/3] make floats Signed-off-by: nstarman --- src/cosmology/api/background.py | 24 ++++++++++++------------ src/cosmology/api/components.py | 14 +++++++------- src/cosmology/api/standard.py | 10 +++++----- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/cosmology/api/background.py b/src/cosmology/api/background.py index d8ad848..7b92f31 100644 --- a/src/cosmology/api/background.py +++ b/src/cosmology/api/background.py @@ -68,14 +68,14 @@ def critical_density0(self) -> ArrayT: # ============================================================== # Methods - def scale_factor(self, z: ArrayT, /) -> ArrayT: + def scale_factor(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependenct scale factor. The scale factor is defined as :math:`a = a_0 / (1 + z)`. Parameters ---------- - z : Array, positional-only + z : Array or float, positional-only The redshift(s) at which to evaluate the scale factor. Returns @@ -84,7 +84,7 @@ def scale_factor(self, z: ArrayT, /) -> ArrayT: """ ... - def Otot(self, z: ArrayT, /) -> ArrayT: + def Otot(self, z: ArrayT | float, /) -> ArrayT: r"""Redshift-dependent total density parameter. Parameters @@ -98,14 +98,14 @@ def Otot(self, z: ArrayT, /) -> ArrayT: """ ... - def critical_density(self, z: ArrayT, /) -> ArrayT: + def critical_density(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent critical density in Msol Mpc-3.""" ... # ---------------------------------------------- # Time - def age(self, z: ArrayT, /) -> ArrayT: + def age(self, z: ArrayT | float, /) -> ArrayT: """Age of the universe in Gyr at redshift ``z``. Parameters @@ -119,7 +119,7 @@ def age(self, z: ArrayT, /) -> ArrayT: """ ... - def lookback_time(self, z: ArrayT, /) -> ArrayT: + def lookback_time(self, z: ArrayT | float, /) -> ArrayT: """Lookback time to redshift ``z`` in Gyr. The lookback time is the difference between the age of the Universe now @@ -139,7 +139,7 @@ def lookback_time(self, z: ArrayT, /) -> ArrayT: # ---------------------------------------------- # Comoving distance - def comoving_distance(self, z: ArrayT, /) -> ArrayT: + def comoving_distance(self, z: ArrayT | float, /) -> ArrayT: r"""Comoving line-of-sight distance :math:`d_c(z)` in Mpc. The comoving distance along the line-of-sight between two objects @@ -156,7 +156,7 @@ def comoving_distance(self, z: ArrayT, /) -> ArrayT: """ ... - def comoving_transverse_distance(self, z: ArrayT, /) -> ArrayT: + def comoving_transverse_distance(self, z: ArrayT | float, /) -> ArrayT: r"""Transverse comoving distance :math:`d_M(z)` in Mpc. This value is the transverse comoving distance at redshift ``z`` @@ -175,7 +175,7 @@ def comoving_transverse_distance(self, z: ArrayT, /) -> ArrayT: """ ... - def comoving_volume(self, z: ArrayT, /) -> ArrayT: + def comoving_volume(self, z: ArrayT | float, /) -> ArrayT: r"""Comoving volume in cubic Mpc. This is the volume of the universe encompassed by redshifts less than @@ -193,7 +193,7 @@ def comoving_volume(self, z: ArrayT, /) -> ArrayT: """ ... - def differential_comoving_volume(self, z: ArrayT, /) -> ArrayT: + def differential_comoving_volume(self, z: ArrayT | float, /) -> ArrayT: r"""Differential comoving volume in cubic Mpc per steradian. If :math:`V_c` is the comoving volume of a redshift slice with solid @@ -212,7 +212,7 @@ def differential_comoving_volume(self, z: ArrayT, /) -> ArrayT: # ---------------------------------------------- # Angular diameter distance - def angular_diameter_distance(self, z: ArrayT, /) -> ArrayT: + def angular_diameter_distance(self, z: ArrayT | float, /) -> ArrayT: """Angular diameter distance :math:`d_A(z)` in Mpc. This gives the proper (sometimes called 'physical') transverse @@ -239,7 +239,7 @@ def angular_diameter_distance(self, z: ArrayT, /) -> ArrayT: # ---------------------------------------------- # Luminosity distance - def luminosity_distance(self, z: ArrayT, /) -> ArrayT: + def luminosity_distance(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent luminosity distance in Mpc. This is the distance to use when converting between the bolometric flux diff --git a/src/cosmology/api/components.py b/src/cosmology/api/components.py index dd6caf3..87c179d 100644 --- a/src/cosmology/api/components.py +++ b/src/cosmology/api/components.py @@ -18,7 +18,7 @@ def Ok0(self) -> ArrayT: """Omega curvature; the effective curvature density/critical density at z=0.""" ... - def Ok(self, z: ArrayT, /) -> ArrayT: + def Ok(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent curvature density parameter. Parameters @@ -41,7 +41,7 @@ def Om0(self) -> ArrayT: """Omega matter; matter density/critical density at z=0.""" ... - def Om(self, z: ArrayT, /) -> ArrayT: + def Om(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent non-relativistic matter density parameter. Parameters @@ -69,7 +69,7 @@ def Ob0(self) -> ArrayT: """Omega baryon; baryon density/critical density at z=0.""" ... - def Ob(self, z: ArrayT, /) -> ArrayT: + def Ob(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent baryon density parameter. Parameters @@ -102,7 +102,7 @@ def m_nu(self) -> tuple[ArrayT, ...]: """Neutrino mass in eV.""" ... - def Onu(self, z: ArrayT, /) -> ArrayT: + def Onu(self, z: ArrayT | float, /) -> ArrayT: r"""Redshift-dependent neutrino density parameter. Parameters @@ -125,7 +125,7 @@ def Ode0(self) -> ArrayT: """Omega dark energy; dark energy density/critical density at z=0.""" ... - def Ode(self, z: ArrayT, /) -> ArrayT: + def Ode(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent dark energy density parameter. Parameters @@ -148,7 +148,7 @@ def Odm0(self) -> ArrayT: """Omega dark matter; dark matter density/critical density at z=0.""" ... - def Odm(self, z: ArrayT, /) -> ArrayT: + def Odm(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent dark matter density parameter. Parameters @@ -176,7 +176,7 @@ def Ogamma0(self) -> ArrayT: """Omega gamma; the density/critical density of photons at z=0.""" ... - def Ogamma(self, z: ArrayT, /) -> ArrayT: + def Ogamma(self, z: ArrayT | float, /) -> ArrayT: """Redshift-dependent photon density parameter. Parameters diff --git a/src/cosmology/api/standard.py b/src/cosmology/api/standard.py index fcf3f58..c505172 100644 --- a/src/cosmology/api/standard.py +++ b/src/cosmology/api/standard.py @@ -130,7 +130,7 @@ def Otot0(self) -> ArrayT: # ============================================================== # Methods - def Tcmb(self, z: ArrayT, /) -> ArrayT: + def Tcmb(self, z: ArrayT | float, /) -> ArrayT: """Temperature of the CMB at redshift z in Kelvin. Parameters @@ -147,7 +147,7 @@ def Tcmb(self, z: ArrayT, /) -> ArrayT: # ---------------------------------------------- # Hubble - def H(self, z: ArrayT, /) -> ArrayT: + def H(self, z: ArrayT | float, /) -> ArrayT: """Hubble function :math:`H(z)` in km s-1 Mpc-1. Parameters @@ -161,7 +161,7 @@ def H(self, z: ArrayT, /) -> ArrayT: """ ... - def efunc(self, z: ArrayT, /) -> ArrayT: + def efunc(self, z: ArrayT | float, /) -> ArrayT: """Standardised Hubble function :math:`E(z) = H(z)/H_0`. Parameters @@ -175,7 +175,7 @@ def efunc(self, z: ArrayT, /) -> ArrayT: """ ... - def inv_efunc(self, z: ArrayT, /) -> ArrayT: + def inv_efunc(self, z: ArrayT | float, /) -> ArrayT: """Inverse of ``efunc``. Parameters @@ -192,7 +192,7 @@ def inv_efunc(self, z: ArrayT, /) -> ArrayT: # ---------------------------------------------- # Omega - def Otot(self, z: ArrayT, /) -> ArrayT: + def Otot(self, z: ArrayT | float, /) -> ArrayT: r"""Redshift-dependent total density parameter. This is the sum of the matter, radiation, neutrino, dark energy, and From b2113d4e8e792982b71f4223c66fe1c9aa370ce9 Mon Sep 17 00:00:00 2001 From: nstarman Date: Thu, 9 Feb 2023 11:55:05 -0500 Subject: [PATCH 2/3] docs Signed-off-by: nstarman --- docs/index.rst | 5 +++ docs/tests/test_arrays.py | 82 +++++++++++++++++++++++++++++++++++++++ docs/typing.rst | 19 +++++++++ pyproject.toml | 6 ++- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 docs/tests/test_arrays.py create mode 100644 docs/typing.rst diff --git a/docs/index.rst b/docs/index.rst index 09fdeb5..d94a843 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -9,6 +9,11 @@ switch to e.g. a CAMB result instance if you happen to require the power spectra or transfer functions. +Details: + +- :ref:`api-static_typing` + + Contributors ============ diff --git a/docs/tests/test_arrays.py b/docs/tests/test_arrays.py new file mode 100644 index 0000000..42bd0a3 --- /dev/null +++ b/docs/tests/test_arrays.py @@ -0,0 +1,82 @@ +"""Test Array.""" + +import array +import sys +from enum import Enum, auto + + +class ArrayLibraries(Enum): + """Array libraries.""" + + array = auto() + + numpy = auto() + zarr = auto() + dask = auto() + xarray = auto() + + # ML + jax = auto() + torch = auto() + tensorflow = auto() + + +def get_array_from_library(name: ArrayLibraries) -> tuple[str, object]: # noqa: PLR0911 + """Import an array library.""" + + if name == ArrayLibraries.array: + vrsn = "{}.{}.{}".format(*sys.version_info[:3]) + + return vrsn, array.array("f", [1.0, 2.0, 3.0]) + + elif name == ArrayLibraries.numpy: + import numpy as np + + return np.__version__, np.linspace(0, 1, 10, dtype=np.float64) + + elif name == ArrayLibraries.jax: + import jax + import jax.numpy as jnp + + return jax.__version__, jnp.linspace(0, 1, 10) + + elif name == ArrayLibraries.torch: + import torch + + return torch.__version__, torch.linspace(0, 1, 10, dtype=torch.float64) + + elif name == ArrayLibraries.zarr: + import zarr + + return zarr.__version__, zarr.zeros((100, 100), chunks=(10, 10), dtype="f4")[:] + + elif name == ArrayLibraries.dask: + import dask + import dask.array as da + + return dask.__version__, da.linspace(0, 1, 10, dtype="float") + + elif name == ArrayLibraries.tensorflow: + import tensorflow as tf + + return tf.__version__, tf.linspace(0, 1, 10) + + elif name == ArrayLibraries.xarray: + import xarray as xr + + return xr.__version__, xr.DataArray([1.0, 2.0, 3.0]) + + +if __name__ == "__main__": + for library in ArrayLibraries: + # import the array library + vrsn, arr = get_array_from_library(library) + + # try adding a float + try: + _ = arr + 1.0 + _ = 1.0 + arr + except Exception: # noqa: BLE001 + print(f"{library.name}-{vrsn} failed to add a float") # noqa: T201 + else: + print(f"{library.name}-{vrsn} added a float") # noqa: T201 diff --git a/docs/typing.rst b/docs/typing.rst new file mode 100644 index 0000000..ac3b1c4 --- /dev/null +++ b/docs/typing.rst @@ -0,0 +1,19 @@ +.. _api-static_typing:.. _My target: + +############# +Static Typing +############# + + +.. csv-table:: Array + float :rst:dir:`csv-table` + :header: "Array Library", "Version", "Can add float" + + "NumPy", "1.24.1", "Yes" + "Zarr", "2.13.6", "Yes" + "Dask", "2023.1.1", "Yes" + "Xarray", "2023.2.0", "Yes" + "Jax", "0.4.3", "Yes" + "Torch", "1.13.1", "Yes" + "Tensorflow", "2.11.0", "Yes" + +Last tested 2023-02-09. diff --git a/pyproject.toml b/pyproject.toml index dcf31dd..13844aa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -109,12 +109,16 @@ warn_redundant_casts = true warn_unused_configs = true warn_unreachable = true - exclude = '''(^|/)tests/''' + exclude = '''(^|/)tests/|(^/)docs/''' [[tool.mypy.overrides]] module = "tests/*" ignore_errors = true + [[tool.mypy.overrides]] + module = "docs/*" + ignore_errors = true + [[tool.mypy.overrides]] module = "tomli.*" ignore_missing_imports = true From 527cc67a8e6cda0895d9b4eb74f85dfb3f0e2144 Mon Sep 17 00:00:00 2001 From: nstarman Date: Tue, 14 Feb 2023 12:08:55 -0500 Subject: [PATCH 3/3] add install page Signed-off-by: nstarman --- docs/conf.py | 3 ++- docs/index.rst | 14 ++++++++++++-- docs/install.rst | 35 +++++++++++++++++++++++++++++++++++ docs/typing.rst | 2 +- tox.ini | 12 ++++++++++++ 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 docs/install.rst diff --git a/docs/conf.py b/docs/conf.py index 8ce6447..c99a6d1 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -60,7 +60,6 @@ def get_authors() -> set[str]: extensions = [ "sphinx.ext.autosummary", "sphinx.ext.doctest", - "sphinx.ext.intersphinx", "sphinx_automodapi.automodapi", "matplotlib.sphinxext.plot_directive", "numpydoc", @@ -88,6 +87,8 @@ def get_authors() -> set[str]: # be used globally. rst_epilog = """ .. |author| replace:: {author} + +.. _Python: http://www.python.org """ intersphinx_mapping = { diff --git a/docs/index.rst b/docs/index.rst index d94a843..1ccb554 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -8,10 +8,20 @@ to start testing your code with e.g. an Astropy cosmology instance, and later switch to e.g. a CAMB result instance if you happen to require the power spectra or transfer functions. +.. toctree:: + :caption: Table of Contents + :name: toptoc + :maxdepth: 2 + :hidden: -Details: + install.rst -- :ref:`api-static_typing` + +.. toctree:: + :name: details + :maxdepth: 1 + + typing.rst Contributors diff --git a/docs/install.rst b/docs/install.rst new file mode 100644 index 0000000..ccad0f0 --- /dev/null +++ b/docs/install.rst @@ -0,0 +1,35 @@ +.. _cosmology-api-install: + +************ +Installation +************ + + +From Source: Cloning, Building, Installing +========================================== + +The latest development version of cosmology-api can be cloned from `GitHub +`_ using ``git`` + +.. code-block:: bash + + git clone https://github.com/cosmology-api/cosmology-api.git + +To build and install the project (from the root of the source tree, e.g., inside +the cloned ``cosmology-api`` directory) + +.. code-block:: bash + + python -m pip install [-e] . + + +Python Dependencies +=================== + +This package has the following dependencies: + +* `Python`_ >= 3.9 + +Explicit version requirements are specified in the project `pyproject.toml +`_. +``pip`` and ``conda`` should install and enforce these versions automatically. diff --git a/docs/typing.rst b/docs/typing.rst index ac3b1c4..3cc263a 100644 --- a/docs/typing.rst +++ b/docs/typing.rst @@ -1,4 +1,4 @@ -.. _api-static_typing:.. _My target: +.. _api-static_typing: ############# Static Typing diff --git a/tox.ini b/tox.ini index 6df2b45..94c4827 100644 --- a/tox.ini +++ b/tox.ini @@ -69,6 +69,18 @@ commands = cov-!double: pytest --pyargs {toxinidir}/tests --cov cosmology --cov-config={toxinidir}/pyproject.toml {posargs} cov: coverage xml -o {toxinidir}/coverage.xml + +# This lets developers use tox to build docs and ignores warnings. +# This is not used in CI; For that, we have RTD PR builder. +[testenv:build_docs] +changedir = docs +description = invoke sphinx-build to build the HTML docs +extras = docs +commands = + pip freeze + sphinx-build -b html . _build/html {posargs:-j auto} + + [testenv:codestyle] skip_install = true description = Run all style and file checks with pre-commit