Skip to content

Commit bdd34bb

Browse files
committed
Add sphinx-gallery based plot gallery to docs
Add a gallery page with copy-able example code and rendered plots, similar to matplotlib's plot_types page. Examples use real Visium breast cancer tissue data alongside the synthetic blobs dataset. Closes scverse/2026_04_hackathon_padua#20 Made-with: Cursor
1 parent f7a0f14 commit bdd34bb

19 files changed

Lines changed: 272 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ __pycache__/
2222
# docs
2323
/docs/generated/
2424
/docs/_build/
25+
/docs/auto_gallery/
26+
/docs/sg_execution_times.rst
27+
/docs/gallery/**/data/
28+
/docs/gallery/**/__pycache__/
2529

2630
# IDEs
2731
/.idea/

docs/conf.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"sphinx.ext.intersphinx",
5757
"sphinx.ext.autosummary",
5858
"sphinx.ext.napoleon",
59+
"sphinx_gallery.gen_gallery",
5960
"sphinxcontrib.bibtex",
6061
"sphinxcontrib.katex",
6162
"sphinx_autodoc_typehints",
@@ -118,6 +119,9 @@
118119
"tutorials/notebooks/README.md",
119120
"tutorials/notebooks/references.md",
120121
"tutorials/notebooks/notebooks/paper_reproducibility/*",
122+
"gallery",
123+
"auto_gallery/**/*.ipynb",
124+
"auto_gallery/**/*.py",
121125
]
122126

123127

@@ -157,3 +161,18 @@
157161
# you can add an exception to this list.
158162
("py:class", "igraph.Graph"),
159163
]
164+
165+
# -- Sphinx-Gallery configuration -------------------------------------------
166+
167+
sys.path.insert(0, str(HERE / "gallery"))
168+
169+
sphinx_gallery_conf = {
170+
"examples_dirs": ["gallery"],
171+
"gallery_dirs": ["auto_gallery"],
172+
"filename_pattern": r"/plot_",
173+
"ignore_pattern": r"(__init__|_helpers)\.py",
174+
"image_scrapers": ("matplotlib",),
175+
"matplotlib_animations": True,
176+
"within_subsection_order": "FileNameSortKey",
177+
"download_all_examples": True,
178+
}

docs/gallery/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Gallery
2+
=======
3+
4+
Examples demonstrating the plotting capabilities of ``spatialdata-plot``.

docs/gallery/_helpers.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Shared data loaders for gallery examples."""
2+
3+
from __future__ import annotations
4+
5+
import warnings
6+
7+
import numpy as np
8+
import scanpy as sc
9+
import spatialdata as sd
10+
from spatialdata.models import Image2DModel, ShapesModel, TableModel
11+
12+
13+
def load_visium_breast_cancer() -> sd.SpatialData:
14+
"""Load Visium breast cancer tissue as a SpatialData object.
15+
16+
Uses ``scanpy.datasets.visium_sge`` which caches the download
17+
via :mod:`pooch`, so the data is only fetched once.
18+
"""
19+
with warnings.catch_warnings():
20+
warnings.simplefilter("ignore")
21+
adata = sc.datasets.visium_sge(
22+
sample_id="V1_Breast_Cancer_Block_A_Section_1",
23+
)
24+
25+
sample = list(adata.uns["spatial"].keys())[0]
26+
meta = adata.uns["spatial"][sample]
27+
sf = meta["scalefactors"]["tissue_hires_scalef"]
28+
29+
image = Image2DModel.parse(
30+
np.moveaxis(meta["images"]["hires"], -1, 0),
31+
dims=("c", "y", "x"),
32+
)
33+
34+
radius = meta["scalefactors"]["spot_diameter_fullres"] * sf / 2
35+
circles = ShapesModel.parse(
36+
adata.obsm["spatial"] * sf,
37+
geometry=0,
38+
radius=radius,
39+
index=adata.obs_names,
40+
)
41+
42+
adata.obs["region"] = "spots"
43+
adata.obs["region"] = adata.obs["region"].astype("category")
44+
adata.obs["instance_key"] = adata.obs_names
45+
table = TableModel.parse(
46+
adata,
47+
region="spots",
48+
region_key="region",
49+
instance_key="instance_key",
50+
)
51+
52+
table.var_names_make_unique()
53+
sc.pp.normalize_total(table)
54+
sc.pp.log1p(table)
55+
56+
return sd.SpatialData(
57+
images={"tissue": image},
58+
shapes={"spots": circles},
59+
tables={"table": table},
60+
)

docs/gallery/basic/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Basic
2+
-----
3+
4+
Rendering individual spatial element types.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Render tissue image
3+
===================
4+
5+
Render an H&E tissue image from a Visium experiment.
6+
"""
7+
8+
from _helpers import load_visium_breast_cancer
9+
10+
import spatialdata_plot # noqa: F401
11+
12+
sdata = load_visium_breast_cancer()
13+
14+
sdata.pl.render_images("tissue").pl.show()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Render labels
3+
=============
4+
5+
Render a cell segmentation mask.
6+
"""
7+
8+
import spatialdata as sd
9+
import spatialdata_plot # noqa: F401
10+
11+
sdata = sd.datasets.blobs()
12+
13+
sdata.pl.render_labels("blobs_labels").pl.show()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Render points
3+
=============
4+
5+
Render transcript detections as points.
6+
"""
7+
8+
import spatialdata as sd
9+
import spatialdata_plot # noqa: F401
10+
11+
sdata = sd.datasets.blobs()
12+
13+
sdata.pl.render_points("blobs_points", size=3).pl.show()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Render spots
3+
============
4+
5+
Render Visium spot shapes on their own.
6+
"""
7+
8+
from _helpers import load_visium_breast_cancer
9+
10+
import spatialdata_plot # noqa: F401
11+
12+
sdata = load_visium_breast_cancer()
13+
14+
sdata.pl.render_shapes("spots").pl.show()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Customization
2+
-------------
3+
4+
Styling options: colormaps, outlines, contours, and alpha.

0 commit comments

Comments
 (0)