Skip to content

Commit cea21cd

Browse files
authored
add coordinate criteria table in docs (#147)
* add coordinate criteria table in docs * add criteria in index * add regex table and what's new * fix make docstring
1 parent aa93aba commit cea21cd

File tree

10 files changed

+145
-5
lines changed

10 files changed

+145
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ instance/
6969
.scrapy
7070

7171
# Sphinx documentation
72-
docs/_build/
72+
doc/_build/
73+
doc/generated/
74+
cf_xarray/tests/_build/
7375

7476
# PyBuilder
7577
target/

cf_xarray/scripts/make_doc.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
from pandas import DataFrame
6+
7+
from cf_xarray.accessor import _AXIS_NAMES, _COORD_NAMES, coordinate_criteria, regex
8+
9+
10+
def main():
11+
"""
12+
Make all additional files needed to build the documentations.
13+
"""
14+
15+
make_criteria_csv()
16+
make_regex_csv()
17+
18+
19+
def make_criteria_csv():
20+
"""
21+
Make criteria tables:
22+
_build/csv/{all,axes,coords}_criteria.csv
23+
"""
24+
25+
csv_dir = "_build/csv"
26+
os.makedirs(csv_dir, exist_ok=True)
27+
28+
# Criteria tables
29+
df = DataFrame.from_dict(coordinate_criteria)
30+
df = df.dropna(1, how="all")
31+
df = df.applymap(lambda x: ", ".join(sorted(x)) if isinstance(x, tuple) else x)
32+
df = df.sort_index(0).sort_index(1)
33+
34+
# All criteria
35+
df.to_csv(os.path.join(csv_dir, "all_criteria.csv"))
36+
37+
# Axes and coordinates
38+
for keys, name in zip([_AXIS_NAMES, _COORD_NAMES], ["axes", "coords"]):
39+
subdf = df.loc[sorted(keys)].dropna(1, how="all")
40+
subdf = subdf.dropna(1, how="all").transpose()
41+
subdf.to_csv(os.path.join(csv_dir, f"{name}_criteria.csv"))
42+
43+
44+
def make_regex_csv():
45+
"""
46+
Make regex tables:
47+
_build/csv/all_regex.csv
48+
"""
49+
50+
csv_dir = "_build/csv"
51+
os.makedirs(csv_dir, exist_ok=True)
52+
df = DataFrame(regex, index=[0])
53+
df = df.applymap(lambda x: f"``{x}``")
54+
df = df.sort_index(1).transpose()
55+
df.to_csv(os.path.join(csv_dir, "all_regex.csv"), header=False)
56+
57+
58+
if __name__ == "__main__":
59+
main()

cf_xarray/tests/test_scripts.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
3+
from cf_xarray.scripts import make_doc
4+
5+
6+
def remove_if_exists(paths):
7+
paths = [paths] if isinstance(paths, str) else paths
8+
for path in paths:
9+
if os.path.exists(path):
10+
os.remove(path)
11+
12+
13+
def test_make_doc():
14+
15+
# Create/remove files from tests/,
16+
# always return to original working directory
17+
owd = os.getcwd()
18+
os.chdir(os.path.dirname(__file__))
19+
try:
20+
names = [
21+
"axes_criteria",
22+
"coords_criteria",
23+
"all_criteria",
24+
"all_regex",
25+
]
26+
tables_to_check = [f"_build/csv/{name}.csv" for name in names]
27+
remove_if_exists(tables_to_check)
28+
29+
make_doc.main()
30+
assert all(os.path.exists(path) for path in tables_to_check)
31+
finally:
32+
os.chdir(owd)

doc/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import sphinx_autosummary_accessors
1919

2020
import cf_xarray # noqa
21+
from cf_xarray.scripts import make_doc
22+
23+
make_doc.main()
2124

2225
# If extensions (or modules to document with autodoc) are in another directory,
2326
# add these directories to sys.path here. If the directory is relative to the

doc/contributing.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ This dictionary contains criteria for identifying axis and coords using CF attri
3232

3333
~accessor.coordinate_criteria
3434

35+
.. csv-table::
36+
:file: _build/csv/all_criteria.csv
37+
:header-rows: 1
38+
:stub-columns: 1
39+
3540
Classes
3641
~~~~~~~
3742

doc/criteria.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. currentmodule:: xarray
2+
3+
CF Criteria
4+
-----------
5+
6+
Attributes
7+
~~~~~~~~~~
8+
Criteria for identifying variables using CF attributes.
9+
10+
Axes
11+
====
12+
13+
.. csv-table::
14+
:file: _build/csv/axes_criteria.csv
15+
:header-rows: 1
16+
:stub-columns: 1
17+
18+
Coordinates
19+
===========
20+
21+
.. csv-table::
22+
:file: _build/csv/coords_criteria.csv
23+
:header-rows: 1
24+
:stub-columns: 1
25+
26+
27+
Names
28+
~~~~~
29+
Regex used by :meth:`DataArray.cf.guess_coord_axis` and :meth:`Dataset.cf.guess_coord_axis` for identifying variables using their names.
30+
31+
.. csv-table::
32+
:file: _build/csv/all_regex.csv
33+
:stub-columns: 1
34+

doc/examples/introduction.ipynb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@
184184
"cell_type": "markdown",
185185
"metadata": {},
186186
"source": [
187-
"## What attributes have been discovered?\n"
187+
"## What attributes have been discovered?\n",
188+
"\n",
189+
"The criteria for identifying variables using CF attributes are listed\n",
190+
"[here](../criteria.rst).\n"
188191
]
189192
},
190193
{
@@ -1028,7 +1031,7 @@
10281031
"name": "python",
10291032
"nbconvert_exporter": "python",
10301033
"pygments_lexer": "ipython3",
1031-
"version": "3.7.8"
1034+
"version": "3.7.3"
10321035
},
10331036
"toc": {
10341037
"base_numbering": 1,

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Table of contents
3939
:maxdepth: 2
4040

4141
examples/introduction
42+
criteria
4243
whats-new
4344
roadmap
4445
contributing

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ What's New
66
v0.4.1 (unreleased)
77
===================
88

9+
- Added scripts to document CF criteria with tables. By `Mattia Almansi`_.
910
- Support for ``.drop()``, ``.drop_vars()``, ``.drop_sel()``, ``.drop_dims()``, ``.set_coords()``, ``.reset_coords()``. By `Mattia Almansi`_.
1011
- Support for using ``standard_name`` in more functions. (:pr:`128`) By `Deepak Cherian`_
1112
- Allow ``DataArray.cf[]`` with standard names. By `Deepak Cherian`_

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ test = pytest
116116
nobeep = True
117117

118118
[rstcheck]
119-
ignore_roles=pr,issue
120-
ignore_directives=ipython,autodata
119+
ignore_roles=pr,issue,meth
120+
ignore_directives=ipython,autodata,csv-table
121121
ignore_messages=(is not referenced\.$)

0 commit comments

Comments
 (0)