Skip to content

Commit c74ac17

Browse files
Enable API reference docs to show accessor methods (#44)
* Install cupy-core and xarray The cupy-core package from conda-forge does not bring in CUDA dependencies and thus can be installed on CPU-only CI services. Xref conda-forge/cupy-feedstock#229 * Re-enable building of API page in docs/conf.py See usage instructions in https://sphinx-autosummary-accessors.readthedocs.io/en/stable/usage.html. Also need to `import cupy_xarray` to fix `exception: no module named xarray.DataArray.cupy` error following xarray-contrib/sphinx-autosummary-accessors#107. * Undocument Dataset.cupy.get Fixes `AttributeError: type object 'CupyDatasetAccessor' has no attribute 'get'` * Pip install cupy_xarray on readthedocs * Remove numpydoc and use napolean only * Put editable install in ci/doc.yml so no need to pip install separately Xref https://docs.readthedocs.io/en/stable/faq.html#i-need-to-install-a-package-in-a-environment-with-pinned-versions * Add minimal docstrings to accessor methods and attributes Adding some docstrings to the is_cupy, as_cupy and as_numpy methods, so that the API docs page looks like center-aligned. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a563f2d commit c74ac17

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ instance/
7070

7171
# Sphinx documentation
7272
docs/_build/
73+
docs/generated/
7374

7475
# PyBuilder
7576
.pybuilder/

ci/doc.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ name: cupy-xarray-doc
22
channels:
33
- conda-forge
44
dependencies:
5+
- cupy-core
56
- pip
67
- python=3.10
78
- sphinx
89
- sphinx-design
910
- sphinx-copybutton
1011
- sphinx-autosummary-accessors
11-
- numpydoc
1212
- ipython
1313
- ipykernel
1414
- ipywidgets
1515
- furo
1616
- myst-nb
17-
# - cupy
18-
# - xarray
17+
- xarray
18+
- pip:
19+
# relative to this file. Needs to be editable to be accepted.
20+
- --editable ..

cupy_xarray/accessors.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ def __init__(self, da):
2323

2424
@property
2525
def is_cupy(self):
26-
"""bool: The underlying data is a cupy array."""
26+
"""
27+
Check to see if the underlying array is a cupy array.
28+
29+
Returns
30+
-------
31+
is_cupy: bool
32+
Whether the underlying data is a cupy array.
33+
"""
2734
if isinstance(self.da.data, dask_array_type):
2835
return isinstance(self.da.data._meta, cp.ndarray)
2936
return isinstance(self.da.data, cp.ndarray)
@@ -75,7 +82,6 @@ def as_numpy(self):
7582
-------
7683
da: DataArray
7784
DataArray with underlying data cast to numpy.
78-
7985
"""
8086
if self.is_cupy:
8187
if isinstance(self.da.data, dask_array_type):
@@ -113,13 +119,27 @@ def __init__(self, ds):
113119

114120
@property
115121
def is_cupy(self):
122+
"""
123+
Check to see if the underlying array is a cupy array.
124+
125+
Returns
126+
-------
127+
is_cupy: bool
128+
Whether the underlying data is a cupy array.
129+
"""
116130
return all([da.cupy.is_cupy for da in self.ds.data_vars.values()])
117131

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

122139
def as_numpy(self):
140+
"""
141+
Converts the Dataset's underlying array type from cupy to numpy.
142+
"""
123143
if self.is_cupy:
124144
data_vars = {var: da.cupy.as_numpy() for var, da in self.ds.data_vars.items()}
125145
return Dataset(

docs/api.rst

-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,3 @@ Methods
5151

5252
Dataset.cupy.as_cupy
5353
Dataset.cupy.as_numpy
54-
Dataset.cupy.get

docs/conf.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
# -- Project information -----------------------------------------------------
77
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
88

9-
# import cupy_xarray
109
import sphinx_autosummary_accessors
1110

11+
import cupy_xarray # noqa: F401
12+
1213
project = "cupy-xarray"
1314
copyright = "2023, cupy-xarray developers"
1415
author = "cupy-xarray developers"
@@ -24,10 +25,9 @@
2425
"sphinx.ext.doctest",
2526
"sphinx.ext.intersphinx",
2627
"sphinx.ext.extlinks",
27-
"numpydoc",
28-
# "sphinx_autosummary_accessors",
29-
"IPython.sphinxext.ipython_directive",
3028
"sphinx.ext.napoleon",
29+
"sphinx_autosummary_accessors",
30+
"IPython.sphinxext.ipython_directive",
3131
"myst_nb",
3232
# "nbsphinx",
3333
"sphinx_copybutton",
@@ -41,7 +41,7 @@
4141
}
4242

4343
templates_path = ["_templates", sphinx_autosummary_accessors.templates_path]
44-
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "api.rst"]
44+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
4545

4646
# -- Options for HTML output -------------------------------------------------
4747
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

docs/source/contributing.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ Contributing documentation
138138
==========================
139139

140140
We greatly appreciate documentation improvements. The docs are built from the docstrings
141-
in the code and the docs in the ``doc`` directory.
141+
in the code and the docs in the ``docs`` directory.
142142

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

146146
conda env create --file ci/doc.yml
147-
conda activate cupy-xarray-docs
147+
conda activate cupy-xarray-doc
148148

149149
You can then build the documentation using::
150150

0 commit comments

Comments
 (0)