3
3
4
4
5
5
def clear_directory (directory ):
6
+ """Clears the given directory, deleting all files and subfolders."""
6
7
if os .path .exists (directory ):
7
8
for filename in os .listdir (directory ):
8
9
file_path = os .path .join (directory , filename )
@@ -15,6 +16,10 @@ def clear_directory(directory):
15
16
16
17
17
18
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
+ """
18
23
for nb in notebooks :
19
24
source_path = os .path .join (examples_dir , f'{ nb } .ipynb' )
20
25
target_path = os .path .join (target_dir , f'{ nb } .ipynb' )
@@ -25,87 +30,101 @@ def copy_notebooks(notebooks, examples_dir, target_dir):
25
30
def find_notebooks (directory ):
26
31
"""
27
32
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:
29
34
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).
31
36
"""
32
37
notebooks = []
33
38
for root , _ , files in os .walk (directory ):
34
39
for file in files :
35
40
if file .endswith (".ipynb" ):
36
41
path = os .path .join (root , file )
37
42
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' , '' ))
42
45
return notebooks
43
46
44
47
45
48
def group_notebooks_by_top_dir (notebooks ):
46
49
"""
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:
49
53
{
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
+ ...
53
57
}
54
58
"""
55
59
grouped : dict [str , list [str ]] = {}
56
60
for nb in notebooks :
57
61
parts = nb .split ('/' )
58
- top_dir = parts [0 ] # The directory before the first slash
62
+ top_dir = parts [0 ]
59
63
grouped .setdefault (top_dir , []).append (nb )
60
64
return grouped
61
65
62
66
63
- def generate_rst (notebooks , target_dir , output_file ):
67
+ def generate_rst (notebooks , output_file ):
64
68
"""
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 .
67
71
"""
68
72
grouped = group_notebooks_by_top_dir (notebooks )
69
73
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
+
70
85
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 " )
71
89
f .write ("Jupyter Notebook Examples by Model\n " )
72
- f .write ("----------------------------------\n \n " )
90
+ f .write ("------------------------------------ \n \n " )
73
91
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.
76
93
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 " )
78
98
f .write (".. toctree::\n " )
79
99
f .write (" :maxdepth: 2\n \n " )
80
-
81
- # Write each notebook path
82
100
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.
84
102
nb_path = os .path .join (
85
- ' notebooks' , nb ).replace (os .path .sep , '/' )
103
+ " notebooks" , nb ).replace (os .path .sep , '/' )
86
104
f .write (f" { nb_path } \n " )
87
-
88
- f .write ("\n " ) # blank line between sections
105
+ f .write ("\n " )
89
106
90
107
91
108
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" ))
94
112
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" )
97
115
98
116
# Clear the target directory before re-copying notebooks
99
117
clear_directory (target_dir )
100
118
119
+ # Find all notebooks in the examples directory
101
120
notebooks = find_notebooks (examples_dir )
102
121
103
- # Copy all notebooks from examples/ to docs/source/notebooks/
122
+ # Copy notebooks from examples/ to docs/source/notebooks/
104
123
copy_notebooks (notebooks , examples_dir , target_dir )
105
124
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 )
109
128
print (f"Generated { output_rst } " )
110
129
111
130
0 commit comments