-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_notebooks.py
65 lines (54 loc) · 2.12 KB
/
run_notebooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
# @Author: Theo Lemaire
# @Email: theo.lemaire@epfl.ch
# @Date: 2020-04-28 21:35:27
# @Last Modified by: Theo Lemaire
# @Last Modified time: 2020-05-02 14:28:47
''' Run all the notebooks to produce the data and figures. '''
import logging
import re
import glob
from argparse import ArgumentParser
from PySONIC.utils import logger
from notebook_runner import runNotebook
# Set logging level
logger.setLevel(logging.INFO)
# Gather all the notebooks in the directory
notebooks = sorted(list(glob.glob('*.ipynb')))
# Associate each notebook for a figure index
notebook_pattern = re.compile(f'^figure_([0-9]+).ipynb$')
notebooks = {int(notebook_pattern.match(n).group(1)): n for n in notebooks}
valid_indexes = list(notebooks.keys())
def main():
# Parse command line arguments
ap = ArgumentParser()
ap.add_argument(
'-f', '--figure', type=str, nargs='+', default='all', help='Figure index')
ap.add_argument(
'-s', '--save', default=False, action='store_true', help='Save in-place')
args = ap.parse_args()
figindexes = args.figure
save = args.save
try:
# Check validity of provided figure indexes
if figindexes == 'all' or figindexes == ['all']:
figindexes = valid_indexes
else:
if 'all' in figindexes:
raise ValueError('"all" cannot be provided with other figure indexes.')
figindexes = sorted([int(s) for s in figindexes])
for i in figindexes:
if i not in valid_indexes:
raise ValueError(
f'{i} is not a valid figure index. Options are {valid_indexes}.')
except Exception as e:
logger.error(e)
quit()
# Construct list of selected notebooks and execute them sequentially
logger.info(f'selected figures: {figindexes}')
selected_notebooks = [notebooks[i] for i in figindexes]
for notebook in selected_notebooks:
logger.info(f'running {notebook} notebook')
runNotebook(notebook, save=save)
if __name__ == '__main__':
main()