-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.py
130 lines (106 loc) · 3.76 KB
/
build.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import papermill as pm
from pathlib import Path
import logging
import subprocess
import yaml
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--parameters_file", action="store",
help="Path to the YAML parameters file")
args = parser.parse_args()
logger = logging.getLogger()
def print_notebooks_to_build(workflow_stages):
"""
pretty-print the list of notebooks that will be considered
Parameters
----------
workflow_stages: Dict[str, List[Path]]
dictionary of workflow stage name mapped to list of notebooks within it.
"""
print("======== BUILDING: ===========")
for directory, files in workflow_stages.items():
print(directory, ":")
for nb in files:
print(" - ", nb)
def build_notebooks(stage, notebook_paths, build_dir, parameters={}):
"""
Build a list of notebooks
Parameters
----------
stage: str
name of stage (just for identification purposes)
notebook_paths: List[Path]
list of paths to notebooks
build_dir: Path
path to top-level of output directory
"""
logger.info("Building Stage '%s'", stage)
for path in notebook_paths:
output_path = build_dir/path
output_path.parents[0].mkdir(parents=True, exist_ok=True)
logger.debug("Created %s", path.parents[0])
try:
pm.execute_notebook(
str(path),
str(output_path),
parameters = parameters,
)
except:
logging.debug("Notebook {} not run".format(path))
def convert_notebooks(stage, notebook_paths, build_dir, fmt='html'):
"""
Convert notebooks into another format using nbconvert
Parameters
----------
stage: str
name of stage (just for identification purposes)
notebook_paths: List[Path]
list of paths to notebooks
build_dir: Path
path to top-level of output directory
fmt: str
output format supported by nbconvert
"""
logger.info("Converting notebooks in stage {} into {}".format(stage, fmt))
for path in notebook_paths:
cmd = "jupyter nbconvert --to {} {}".format(fmt, build_dir/path)
try:
subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)
logger.info("Converted {} in {}".format(path.parents[0], fmt))
except:
logger.debug(("{} not converted in {}".format(path.parents[0], fmt)))
def read_parameters_file(filename):
"""
Read a YAML file with parameters for the notebooks
Parameters
----------
filename: str
path to the file
Returns
-------
dictionnary of the parameters
"""
with open(filename) as file:
parameters = yaml.load(file, Loader=yaml.SafeLoader)
return parameters
if __name__ == '__main__':
build_dir = Path("BUILD")
build_dir.mkdir(exist_ok=True)
logging_filename = build_dir/"logs.txt"
logging.basicConfig(level=logging.DEBUG,
filename=logging_filename,
filemode="w",
)
logging.info("Build directory:", build_dir.resolve())
dirs_to_build = ['Preparation', 'Benchmarks', 'Summaries']
workflow_stages = {x: [path for path in Path(f'./{x}').rglob("*.ipynb")
if '.ipynb_checkpoints' not in path.parts]
for x in dirs_to_build}
print_notebooks_to_build(workflow_stages)
if args.parameters_file is not None:
parameters = read_parameters_file(args.parameters_file)
else:
parameters = {}
for stage, paths in workflow_stages.items():
build_notebooks(stage, paths, build_dir, parameters=parameters)
convert_notebooks(stage, paths, build_dir)