Skip to content

Commit c96b680

Browse files
committed
Consolidate examples documentation generation
- Removed redundant README inclusion from the examples page. - Updated the generation script to create a single, structured examples.rst file with a top-level header and model-specific toctree sections. - Added descriptive title mapping (e.g., "CRM" → "Consumer Resource Model (CRM)") for better clarity. - Ensured notebooks are copied into docs/source/notebooks and grouped by model for improved navigation.
1 parent 3a01346 commit c96b680

File tree

3 files changed

+120
-108
lines changed

3 files changed

+120
-108
lines changed

Diff for: docs/source/examples.rst

+68-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,73 @@
11
Examples
22
========
33

4-
Below is an overview of the examples, organized by model type or analysis type.
4+
Jupyter Notebook Examples by Model
5+
------------------------------------
56

6-
.. include:: ../../examples/README.rst
7-
:start-after: readme-begin
8-
:end-before: readme-end
7+
Consumer Resource Model (CRM)
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
10+
.. toctree::
11+
:maxdepth: 2
12+
13+
notebooks/CRM/examples-sim-CRM
14+
15+
Gaussian Processes (GP)
16+
~~~~~~~~~~~~~~~~~~~~~~~
17+
18+
.. toctree::
19+
:maxdepth: 2
20+
21+
notebooks/GP/examples-impute-GP
22+
notebooks/GP/examples-impute-GP_Stein
23+
24+
Multivariate Autoregressive Model (MVAR)
25+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26+
27+
.. toctree::
28+
:maxdepth: 2
29+
30+
notebooks/MVAR/examples-infer-MVAR
31+
notebooks/MVAR/examples-sim-MVAR
32+
33+
Multi-Model Analysis
34+
~~~~~~~~~~~~~~~~~~~~
35+
36+
.. toctree::
37+
:maxdepth: 2
38+
39+
notebooks/MultiModel/Herold/examples-Herold-VAR
40+
notebooks/MultiModel/Herold/examples-Herold-sVAR
41+
notebooks/MultiModel/Herold/examples_impute_data
42+
43+
Vector Autoregression (VAR)
44+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
46+
.. toctree::
47+
:maxdepth: 2
48+
49+
notebooks/VAR/examples-bayes-VAR
50+
notebooks/VAR/examples-sim-VAR
51+
52+
Generalized Lotka-Volterra (gLV)
53+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54+
55+
.. toctree::
56+
:maxdepth: 2
57+
58+
notebooks/gLV/examples-Rutter-Dekker
59+
notebooks/gLV/examples-Stein
60+
notebooks/gLV/examples-bayes-gLV
61+
notebooks/gLV/examples-lasso-gLV
62+
notebooks/gLV/examples-ridge-gLV
63+
notebooks/gLV/examples-sim-gLV
64+
65+
Generalized Metabolic Lotka-Volterra (gMLV)
66+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67+
68+
.. toctree::
69+
:maxdepth: 2
70+
71+
notebooks/gMLV/examples-ridge-lasso-gMLV
72+
notebooks/gMLV/examples-sim-gMLV
973

10-
.. include:: examples_auto.rst

Diff for: docs/source/examples_auto.rst

-70
This file was deleted.

Diff for: docs/source/generate_examples_rst.py

+52-33
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
def clear_directory(directory):
6+
"""Clears the given directory, deleting all files and subfolders."""
67
if os.path.exists(directory):
78
for filename in os.listdir(directory):
89
file_path = os.path.join(directory, filename)
@@ -15,6 +16,10 @@ def clear_directory(directory):
1516

1617

1718
def copy_notebooks(notebooks, examples_dir, target_dir):
19+
"""
20+
Copies each notebook from the examples directory to the target directory.
21+
The `notebooks` list contains paths relative to `examples_dir` (without the .ipynb extension).
22+
"""
1823
for nb in notebooks:
1924
source_path = os.path.join(examples_dir, f'{nb}.ipynb')
2025
target_path = os.path.join(target_dir, f'{nb}.ipynb')
@@ -25,87 +30,101 @@ def copy_notebooks(notebooks, examples_dir, target_dir):
2530
def find_notebooks(directory):
2631
"""
2732
Returns a list of notebook paths (with no file extension),
28-
relative to 'directory'. For example, if a notebook is:
33+
relative to `directory`. For example, if a notebook is:
2934
examples/gLV/examples-bayes-gLV.ipynb
30-
we return 'gLV/examples-bayes-gLV' (forward slashes).
35+
this function returns 'gLV/examples-bayes-gLV' (using forward slashes).
3136
"""
3237
notebooks = []
3338
for root, _, files in os.walk(directory):
3439
for file in files:
3540
if file.endswith(".ipynb"):
3641
path = os.path.join(root, file)
3742
relative_path = os.path.relpath(path, start=directory)
38-
notebooks.append(
39-
relative_path.replace(
40-
os.path.sep, '/').replace('.ipynb', '')
41-
)
43+
notebooks.append(relative_path.replace(
44+
os.path.sep, '/').replace('.ipynb', ''))
4245
return notebooks
4346

4447

4548
def group_notebooks_by_top_dir(notebooks):
4649
"""
47-
Takes a list of relative notebook paths like ['gLV/examples-bayes-gLV', 'CRM/examples-sim-CRM']
48-
and returns a dict grouping them by the top-level folder:
50+
Groups a list of relative notebook paths (e.g. 'gLV/examples-bayes-gLV')
51+
by their top-level folder.
52+
Returns a dictionary, for example:
4953
{
50-
'CRM': ['CRM/examples-sim-CRM'],
51-
'gLV': ['gLV/examples-bayes-gLV']
52-
...
54+
'CRM': ['CRM/examples-sim-CRM'],
55+
'gLV': ['gLV/examples-bayes-gLV', 'gLV/examples-sim-gLV'],
56+
...
5357
}
5458
"""
5559
grouped: dict[str, list[str]] = {}
5660
for nb in notebooks:
5761
parts = nb.split('/')
58-
top_dir = parts[0] # The directory before the first slash
62+
top_dir = parts[0]
5963
grouped.setdefault(top_dir, []).append(nb)
6064
return grouped
6165

6266

63-
def generate_rst(notebooks, target_dir, output_file):
67+
def generate_rst(notebooks, output_file):
6468
"""
65-
Generates 'examples_auto.rst', grouping notebooks by their top-level folder.
66-
Each folder gets its own subheading and toctree.
69+
Generates a single 'examples.rst' file that includes a top-level header and,
70+
for each model group, a section with a toctree of the notebooks.
6771
"""
6872
grouped = group_notebooks_by_top_dir(notebooks)
6973

74+
# Mapping from folder name to a more descriptive title
75+
MODEL_NAMES = {
76+
"CRM": "Consumer Resource Model (CRM)",
77+
"gLV": "Generalized Lotka-Volterra (gLV)",
78+
"gMLV": "Generalized Metabolic Lotka-Volterra (gMLV)",
79+
"GP": "Gaussian Processes (GP)",
80+
"MVAR": "Multivariate Autoregressive Model (MVAR)",
81+
"VAR": "Vector Autoregression (VAR)",
82+
"MultiModel": "Multi-Model Analysis"
83+
}
84+
7085
with open(output_file, 'w', encoding='utf-8') as f:
86+
# Write the top-level headers
87+
f.write("Examples\n")
88+
f.write("========\n\n")
7189
f.write("Jupyter Notebook Examples by Model\n")
72-
f.write("----------------------------------\n\n")
90+
f.write("------------------------------------\n\n")
7391

74-
# For each top-level folder, create a subheading and toctree
75-
# Sort the folder names so the output is consistent
92+
# For each top-level folder, generate a section with a toctree.
7693
for folder in sorted(grouped.keys()):
77-
f.write(folder + "\n" + "-" * len(folder) + "\n\n")
94+
title = MODEL_NAMES.get(folder, folder)
95+
# Use a level-3 header (using '~' as the underline character)
96+
f.write(f"{title}\n")
97+
f.write("~" * len(title) + "\n\n")
7898
f.write(".. toctree::\n")
7999
f.write(" :maxdepth: 2\n\n")
80-
81-
# Write each notebook path
82100
for nb in sorted(grouped[folder]):
83-
# notebooks are now in docs/source/notebooks
101+
# Assume notebooks are in the 'notebooks' subfolder relative to the .rst file.
84102
nb_path = os.path.join(
85-
'notebooks', nb).replace(os.path.sep, '/')
103+
"notebooks", nb).replace(os.path.sep, '/')
86104
f.write(f" {nb_path}\n")
87-
88-
f.write("\n") # blank line between sections
105+
f.write("\n")
89106

90107

91108
def main():
92-
# Path to your top-level examples directory
93-
examples_dir = os.path.abspath('../../examples')
109+
# Path to your top-level examples directory (adjust relative path as needed)
110+
examples_dir = os.path.abspath(os.path.join(
111+
os.path.dirname(__file__), "../../examples"))
94112

95-
# The target dir in docs/source where notebooks will be copied
96-
target_dir = os.path.join(os.path.dirname(__file__), 'notebooks')
113+
# The target directory in docs/source where notebooks will be copied
114+
target_dir = os.path.join(os.path.dirname(__file__), "notebooks")
97115

98116
# Clear the target directory before re-copying notebooks
99117
clear_directory(target_dir)
100118

119+
# Find all notebooks in the examples directory
101120
notebooks = find_notebooks(examples_dir)
102121

103-
# Copy all notebooks from examples/ to docs/source/notebooks/
122+
# Copy notebooks from examples/ to docs/source/notebooks/
104123
copy_notebooks(notebooks, examples_dir, target_dir)
105124

106-
# Generate 'examples_auto.rst' (instead of 'examples.rst')
107-
output_rst = os.path.join(os.path.dirname(__file__), 'examples_auto.rst')
108-
generate_rst(notebooks, target_dir, output_rst)
125+
# Generate 'examples.rst' in the same directory as this script
126+
output_rst = os.path.join(os.path.dirname(__file__), "examples.rst")
127+
generate_rst(notebooks, output_rst)
109128
print(f"Generated {output_rst}")
110129

111130

0 commit comments

Comments
 (0)