Skip to content

Enable API reference docs to show accessor methods #44

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

Merged
merged 12 commits into from
Jun 21, 2024
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ instance/

# Sphinx documentation
docs/_build/
docs/generated/

# PyBuilder
.pybuilder/
Expand Down
8 changes: 5 additions & 3 deletions ci/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ name: cupy-xarray-doc
channels:
- conda-forge
dependencies:
- cupy-core
- pip
- python=3.10
- sphinx
- sphinx-design
- sphinx-copybutton
- sphinx-autosummary-accessors
- numpydoc
- ipython
- ipykernel
- ipywidgets
- furo
- myst-nb
# - cupy
# - xarray
- xarray
- pip:
# relative to this file. Needs to be editable to be accepted.
- --editable ..
24 changes: 22 additions & 2 deletions cupy_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ def __init__(self, da):

@property
def is_cupy(self):
"""bool: The underlying data is a cupy array."""
"""
Check to see if the underlying array is a cupy array.

Returns
-------
is_cupy: bool
Whether the underlying data is a cupy array.
"""
if isinstance(self.da.data, dask_array_type):
return isinstance(self.da.data._meta, cp.ndarray)
return isinstance(self.da.data, cp.ndarray)
Expand Down Expand Up @@ -75,7 +82,6 @@ def as_numpy(self):
-------
da: DataArray
DataArray with underlying data cast to numpy.

"""
if self.is_cupy:
if isinstance(self.da.data, dask_array_type):
Expand Down Expand Up @@ -113,13 +119,27 @@ def __init__(self, ds):

@property
def is_cupy(self):
"""
Check to see if the underlying array is a cupy array.

Returns
-------
is_cupy: bool
Whether the underlying data is a cupy array.
"""
return all([da.cupy.is_cupy for da in self.ds.data_vars.values()])

def as_cupy(self):
"""
Convert the Dataset's underlying array type to cupy.
"""
data_vars = {var: da.as_cupy() for var, da in self.ds.data_vars.items()}
return Dataset(data_vars=data_vars, coords=self.ds.coords, attrs=self.ds.attrs)

def as_numpy(self):
"""
Converts the Dataset's underlying array type from cupy to numpy.
"""
if self.is_cupy:
data_vars = {var: da.cupy.as_numpy() for var, da in self.ds.data_vars.items()}
return Dataset(
Expand Down
1 change: 0 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ Methods

Dataset.cupy.as_cupy
Dataset.cupy.as_numpy
Dataset.cupy.get
10 changes: 5 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

# import cupy_xarray
import sphinx_autosummary_accessors

import cupy_xarray # noqa: F401

project = "cupy-xarray"
copyright = "2023, cupy-xarray developers"
author = "cupy-xarray developers"
Expand All @@ -24,10 +25,9 @@
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.extlinks",
"numpydoc",
# "sphinx_autosummary_accessors",
"IPython.sphinxext.ipython_directive",
"sphinx.ext.napoleon",
"sphinx_autosummary_accessors",
"IPython.sphinxext.ipython_directive",
"myst_nb",
# "nbsphinx",
"sphinx_copybutton",
Expand All @@ -41,7 +41,7 @@
}

templates_path = ["_templates", sphinx_autosummary_accessors.templates_path]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "api.rst"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
Expand Down
4 changes: 2 additions & 2 deletions docs/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ Contributing documentation
==========================

We greatly appreciate documentation improvements. The docs are built from the docstrings
in the code and the docs in the ``doc`` directory.
in the code and the docs in the ``docs`` directory.

To build the documentation, you will need to requirements listed in ``ci/doc.yml``.
You can create an environment for building the documentation using::

conda env create --file ci/doc.yml
conda activate cupy-xarray-docs
conda activate cupy-xarray-doc

You can then build the documentation using::

Expand Down
Loading