Skip to content

Commit a15d724

Browse files
author
Joseph Hamman
committed
add docs for accessors
1 parent 4b75f15 commit a15d724

File tree

8 files changed

+74
-22
lines changed

8 files changed

+74
-22
lines changed

doc/api.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
.. _api:
12

23
API reference
34
-------------
45

56
This page provides an auto-generated summary of Xbatcher's API.
67

8+
Core
9+
====
10+
711
.. autoclass:: xbatcher.BatchGenerator
812
:members:
13+
14+
Dataset.batch and DataArray.batch
15+
=================================
16+
17+
.. currentmodule:: xarray
18+
19+
.. autosummary::
20+
:toctree: generated/
21+
:template: autosummary/accessor_method.rst
22+
23+
Dataset.batch.generator
24+
DataArray.batch.generator

doc/conf.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import os
1616
import sys
1717

18+
import sphinx_autosummary_accessors
19+
1820
import xbatcher
1921

2022
# If extensions (or modules to document with autodoc) are in another directory,
@@ -47,6 +49,7 @@
4749
'nbsphinx',
4850
'IPython.sphinxext.ipython_directive',
4951
'IPython.sphinxext.ipython_console_highlighting',
52+
'sphinx_autosummary_accessors',
5053
]
5154

5255
# never execute notebooks: avoids lots of expensive imports on rtd
@@ -66,14 +69,16 @@ def setup(app):
6669

6770

6871
# link to github issues
69-
extlinks = {'issue': ('https://github.com/xbatcher/xbatcher/issues/%s', 'GH')}
72+
extlinks = {
73+
'issue': ('https://github.com/pangeo-data/xbatcher/issues/%s', 'GH')
74+
}
7075

7176
autosummary_generate = True
7277
numpydoc_class_members_toctree = False
7378
numpydoc_show_class_members = False
7479

7580
# Add any paths that contain templates here, relative to this directory.
76-
templates_path = ['_templates']
81+
templates_path = ['_templates', sphinx_autosummary_accessors.templates_path]
7782

7883
# The suffix of source filenames.
7984
source_suffix = '.rst'
@@ -86,7 +91,7 @@ def setup(app):
8691

8792
# General information about the project.
8893
project = u'xbatcher'
89-
copyright = u'2016, xbatcher developers'
94+
copyright = u'2021, xbatcher developers'
9095

9196
# The version info for the project you're documenting, acts as replacement for
9297
# |version| and |release|, also used in various other places throughout the

doc/environment.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
name: xbatcher
22
dependencies:
3-
- python=3.6
4-
- xarray
3+
- python=3.7
54
- dask
5+
- docrep
6+
- ipython
7+
- matplotlib
8+
- nbsphinx
69
- numpy
7-
- pytest
810
- numpydoc
11+
- pytest
912
- sphinx
10-
- ipython
11-
- matplotlib
12-
- pip:
13-
- docrep
14-
- nbsphinx
13+
- sphinx-autosummary-accessors
14+
- xarray

doc/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ and we want to create batches along the time dimension. We can do it like this
3939
# actually feed to machine learning library
4040
batch
4141
42+
or via a built-in `Xarray accessor <http://xarray.pydata.org/en/stable/internals/extending-xarray.html#extending-xarray>`_:
43+
44+
.. ipython:: python
45+
46+
import xbatcher
47+
48+
for batch in da.batch.generator({'time': 10}):
49+
pass
50+
# actually feed to machine learning library
51+
batch
52+
4253
.. toctree::
4354
:maxdepth: 2
4455
:caption: Contents:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ select = B,C,E,F,W,T4,B9
77

88
[isort]
99
known_first_party=xbatcher
10-
known_third_party=numpy,pytest,setuptools,xarray
10+
known_third_party=numpy,pytest,setuptools,sphinx_autosummary_accessors,xarray
1111
multi_line_output=3
1212
include_trailing_comma=True
1313
force_grid_wrap=0

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
'Intended Audience :: Science/Research',
1717
'Programming Language :: Python',
1818
'Programming Language :: Python :: 3',
19-
'Programming Language :: Python :: 3.5',
20-
'Programming Language :: Python :: 3.6',
19+
'Programming Language :: Python :: 3.7',
20+
'Programming Language :: Python :: 3.8',
21+
'Programming Language :: Python :: 3.9',
2122
'Topic :: Scientific/Engineering',
2223
]
2324

xbatcher/accessors.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33
from .generators import BatchGenerator
44

55

6-
@xr.register_dataarray_accessor("batch")
7-
@xr.register_dataset_accessor("batch")
6+
@xr.register_dataarray_accessor('batch')
7+
@xr.register_dataset_accessor('batch')
88
class BatchAccessor:
99
def __init__(self, xarray_obj):
10+
'''
11+
Batch accessor returning a BatchGenerator object via the `generator method`
12+
'''
1013
self._obj = xarray_obj
1114

1215
def generator(self, *args, **kwargs):
16+
'''
17+
Return a BatchGenerator via the batch accessor
18+
19+
Parameters
20+
----------
21+
*args : iterable
22+
Positional arguments to pass to the `BatchGenerator` constructor.
23+
**kwargs : dict
24+
Keyword arguments to pass to the `BatchGenerator` constructor.
25+
'''
1326
return BatchGenerator(self._obj, *args, **kwargs)

xbatcher/tests/test_accessors.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import xarray as xr
21
import numpy as np
32
import pytest
3+
import xarray as xr
44

5+
import xbatcher # noqa: F401
56
from xbatcher import BatchGenerator
6-
import xbatcher
77

88

99
@pytest.fixture(scope='module')
1010
def sample_ds_3d():
1111
shape = (10, 50, 100)
12-
ds = xr.Dataset({'foo': (['time', 'y', 'x'], np.random.rand(*shape)),
13-
'bar': (['time', 'y', 'x'], np.random.randint(0, 10, shape))},
14-
{'x': (['x'], np.arange(shape[-1])),
15-
'y': (['y'], np.arange(shape[-2]))})
12+
ds = xr.Dataset(
13+
{
14+
'foo': (['time', 'y', 'x'], np.random.rand(*shape)),
15+
'bar': (['time', 'y', 'x'], np.random.randint(0, 10, shape)),
16+
},
17+
{
18+
'x': (['x'], np.arange(shape[-1])),
19+
'y': (['y'], np.arange(shape[-2])),
20+
},
21+
)
1622
return ds
1723

1824

0 commit comments

Comments
 (0)