Skip to content

Commit ff28376

Browse files
authored
📝 Initialize Python documentation page (#35)
* ➕ Add jupyter-book Create beautiful, publication-quality books and documents from computational content! * 🌱 Initialize Jupyter Book 2 with MyST Starting with a minimal Jupyter Book initialized with `jupyter book init`. Changed the `myst.yml` to use a proper title and description. * 📝 Use sphinx-ext-mystmd to generate autodoc API reference Move myst.yml to docs/ folder, and create a Sphinx configuration file at docs/conf.py. Docs are now built using `cd docs/ && sphinx-build --builder myst . _build/myst-asts && jupyter book start`. Extensions used: - `myst_parser` - Because the index page and API docs page in MyST instead of ReST - `sphinx_ext_mystmd` - Allows Sphinx to build an `api.myst.json` file that can then be used by MyST-MD - `sphinx.ext.autodoc` - Regular Sphinx extension to pull in documentation from docstrings - `sphinx.ext.napolean` - Support for NumPy style docstrings Note: needed to use {eval-rst} directive in API docs because https://myst-parser.readthedocs.io/en/latest/syntax/code_and_apis.html#sphinx-ext-autodoc says `sphinx.ext.autodoc` generates ReST only, and I didn't want to use `sphinx-autodoc2`. * 💬 Indent doctest examples to render them as code blocks Need to add a sentence, and then indent the code block by 4 spaces to it into a literal block (couldn't figure out syntax highlighting yet). Also taking the chance to properly update the docstrings, mentioning that read_geotiff only supports float32, and documenting the xy_coords method with more details. * 💚 Don't run rustdoc tests on pyo3 modules Fixes error with `cargo test` trying to run doctests in src/python/adapters.rs that are meant for Python, not Rust. Xref https://users.rust-lang.org/t/disabling-rustdoc-tests-for-module/50110/2 * 🎨 Use sphinx-ext-mystmd patch to unindent doctest blocks Figured out how to get doctest_block nodes working with a one-line change at jupyter-book/sphinx-ext-mystmd#2. Partially reverts 5d0ff56.
1 parent 8894135 commit ff28376

File tree

8 files changed

+100
-12
lines changed

8 files changed

+100
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ venv/
2828
*.egg-info/
2929
.installed.cfg
3030
*.egg
31+
32+
# MyST build outputs
33+
_build

docs/api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# API Reference
2+
3+
These are the Python docs for `cog3pio`.
4+
5+
## Classes
6+
7+
```{eval-rst}
8+
.. autoclass:: cog3pio.CogReader
9+
:members:
10+
:special-members: __dlpack__, __dlpack_device__
11+
```
12+
13+
14+
## Functions
15+
16+
```{eval-rst}
17+
.. autofunction:: cog3pio.read_geotiff
18+
```

docs/conf.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# General configurations.
2+
needs_sphinx = "6.2"
3+
extensions = [
4+
"myst_parser",
5+
"sphinx_ext_mystmd",
6+
"sphinx.ext.autodoc",
7+
"sphinx.ext.napoleon",
8+
]
9+
# Options for source files.
10+
exclude_patterns = [
11+
"_build",
12+
]
13+
# Options for napoleon.
14+
# https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#module-sphinx.ext.napoleon
15+
napoleon_use_admonition_for_examples = True
16+
napoleon_use_rtype = False
17+
napoleon_use_ivar = True

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ⚙️ *cog3pio* - Cloud-optimized GeoTIFF ... Parallel I/O

docs/myst.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# See docs at: https://mystmd.org/guide/frontmatter
2+
version: 1
3+
project:
4+
id: 0c0b30a3-67fd-4260-bea5-1d77fb40395d
5+
title: "cog3pio"
6+
description: "Cloud-optimized GeoTIFF ... Parallel I/O"
7+
# keywords: []
8+
# authors: []
9+
github: https://github.com/weiji14/cog3pio
10+
# To autogenerate a Table of Contents, run "jupyter book init --write-toc"
11+
toc:
12+
# Auto-generated by `myst init --write-toc`
13+
- file: index.md
14+
- pattern: _build/myst-asts/api.myst.json
15+
16+
site:
17+
template: book-theme
18+
# options:
19+
# favicon: favicon.ico
20+
# logo: site_logo.png

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ benchmark = [
2525
"pytest-codspeed",
2626
"rioxarray",
2727
]
28+
docs = [
29+
"jupyter-book>=2.0.0a0",
30+
"myst_parser",
31+
"sphinx",
32+
# https://github.com/jupyter-book/sphinx-ext-mystmd/pull/2
33+
"sphinx-ext-mystmd @ git+https://github.com/weiji14/sphinx-ext-mystmd@e995908b3a898b9c9d5d3fec4ff1478f1f4c1ccd",
34+
]
2835
tests = [
2936
"pytest",
3037
]

src/python/adapters.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ use crate::io::geotiff::{CogReader, read_geotiff};
2828
///
2929
/// Examples
3030
/// --------
31+
/// Read a GeoTIFF from a HTTP url into a numpy.ndarray:
32+
///
3133
/// >>> import numpy as np
3234
/// >>> from cog3pio import CogReader
33-
/// >>>
34-
/// >>> reader = CogReader(
35-
/// >>> path="https://github.com/rasterio/rasterio/raw/1.3.9/tests/data/float32.tif"
36-
/// >>> )
37-
/// >>> array: np.ndarray = reader.data()
35+
/// ...
36+
/// >>> cog = CogReader(
37+
/// ... path="https://github.com/rasterio/rasterio/raw/refs/tags/1.4.3/tests/data/RGBA.uint16.tif"
38+
/// ...)
39+
/// >>> array: np.ndarray = np.from_dlpack(cog)
3840
/// >>> array.shape
39-
/// >>> (1, 12, 13)
41+
/// (4, 411, 634)
4042
/// >>> array.dtype
41-
/// >>> dtype('float32')
43+
/// dtype('uint16')
4244
#[pyclass]
4345
#[pyo3(name = "CogReader")]
4446
struct PyCogReader {
@@ -86,7 +88,17 @@ impl PyCogReader {
8688
(device.device_type as i32, device.device_id)
8789
}
8890

89-
/// Get x and y coordinates as numpy.ndarray
91+
/// Get list of x and y coordinates.
92+
///
93+
/// Determined based on an Affine transformation matrix built from the
94+
/// `ModelPixelScaleTag` and `ModelTiepointTag` TIFF tags. Note that non-zero
95+
/// rotation (set by `ModelTransformationTag` is currently unsupported.
96+
///
97+
/// Returns
98+
/// -------
99+
/// coords : (np.ndarray, np.ndarray)
100+
/// A tuple (x_coords, y_coords) of np.ndarray objects representing the GeoTIFF's
101+
/// x- and y-coordinates.
90102
#[allow(clippy::type_complexity)]
91103
fn xy_coords<'py>(
92104
&mut self,
@@ -134,7 +146,7 @@ fn path_to_stream(path: &str) -> PyResult<Cursor<Bytes>> {
134146
Ok(stream)
135147
}
136148

137-
/// Read a GeoTIFF file from a path on disk or a url into an ndarray
149+
/// Read a GeoTIFF file from a path on disk or a url into an ndarray.
138150
///
139151
/// Parameters
140152
/// ----------
@@ -146,12 +158,21 @@ fn path_to_stream(path: &str) -> PyResult<Cursor<Bytes>> {
146158
/// array : np.ndarray
147159
/// 3D array of shape (band, height, width) containing the GeoTIFF pixel data.
148160
///
161+
/// Raises
162+
/// ------
163+
/// ValueError
164+
/// If a TIFF which has a non-float32 dtype is passed in. Please use
165+
/// `cog3pio.CogReader` for reading TIFFs with other dtypes (e.g. uint16).
166+
///
149167
/// Examples
150168
/// --------
151-
/// from cog3pio import read_geotiff
169+
/// Read a GeoTIFF from a HTTP url into a numpy.ndarray:
152170
///
153-
/// array = read_geotiff("https://github.com/pka/georaster/raw/v0.1.0/data/tiff/float32.tif")
154-
/// assert array.shape == (20, 20)
171+
/// >>> from cog3pio import read_geotiff
172+
/// ...
173+
/// >>> array = read_geotiff("https://github.com/pka/georaster/raw/v0.2.0/data/tiff/float32.tif")
174+
/// >>> array.shape
175+
/// (1, 20, 20)
155176
#[pyfunction]
156177
#[pyo3(name = "read_geotiff")]
157178
fn read_geotiff_py<'py>(path: &str, py: Python<'py>) -> PyResult<Bound<'py, PyArray3<f32>>> {

src/python/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/// Adapter interface from Rust to Python
2+
#[cfg(not(doctest))]
23
pub mod adapters;

0 commit comments

Comments
 (0)