33
44
55def 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
1718def 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):
2530def 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
4548def 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
91108def 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