Skip to content

Commit d6c6ad3

Browse files
committed
WIP: rewrite test_notebook.py to use nbval flag
1 parent 6e0b9bf commit d6c6ad3

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
name: docker run
1919
no_output_timeout: 120m
2020
command: |
21-
docker run -it --rm miykael/nipype_tutorial:$CIRCLE_BRANCH pytest --nbval-lax --nbval-cell-timeout 7200 -v -s /home/neuro/nipype_tutorial/notebooks/basic_*.ipynb /home/neuro/nipype_tutorial/notebooks/introduction_*.ipynb /home/neuro/nipype_tutorial/notebooks/advanced_*.ipynb /home/neuro/nipype_tutorial/notebooks/example_preprocessing.ipynb /home/neuro/nipype_tutorial/notebooks/example_1stlevel.ipynb /home/neuro/nipype_tutorial/notebooks/example_normalize.ipynb /home/neuro/nipype_tutorial/notebooks/example_2ndlevel.ipynb /home/neuro/nipype_tutorial/notebooks/handson_preprocessing.ipynb /home/neuro/nipype_tutorial/notebooks/handson_analysis.ipynb
21+
docker run -it --rm miykael/nipype_tutorial:$CIRCLE_BRANCH python /home/neuro/nipype_tutorial/test_notebooks.py

test_notebooks.py

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,16 @@
1+
import os
2+
import time
13
from glob import glob
2-
import sys, os, time
3-
import pytest, pdb
4-
5-
import nbformat
6-
from nbconvert.preprocessors import ExecutePreprocessor
7-
from nbconvert.preprocessors.execute import CellExecutionError
8-
94

105
def test_version():
116
import nipype
127
print("nipype version: ", nipype.__version__)
138

149

15-
def _notebook_run(path):
16-
"""
17-
Execute a notebook via nbconvert and collect output.
18-
:returns (parsed nb object, execution errors)
19-
"""
20-
kernel_name = 'python%d' % sys.version_info[0]
21-
this_file_directory = os.path.dirname(__file__)
22-
errors = []
23-
24-
with open(path) as f:
25-
nb = nbformat.read(f, as_version=4)
26-
nb.metadata.get('kernelspec', {})['name'] = kernel_name
27-
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=7200) #, allow_errors=True
28-
29-
try:
30-
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
31-
32-
except CellExecutionError as e:
33-
if "TAB" in e.traceback:
34-
print(str(e.traceback).split("\n")[-2])
35-
else:
36-
raise e
37-
38-
return nb, errors
39-
40-
4110
def reduce_notebook_load(path):
4211
"""
4312
Changes the number of subjects in examples and hands-on to two,
44-
to reduce computation time on circleci.
13+
to reduce computation time on CircleCi.
4514
"""
4615

4716
path_short = path[:-6] + '_short.ipynb'
@@ -82,27 +51,25 @@ def reduce_notebook_load(path):
8251
return path_short
8352

8453

85-
Dir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "notebooks")
54+
if __name__ == '__main__':
8655

87-
@pytest.mark.parametrize("notebook",
88-
sorted(glob(os.path.join(Dir_path, "introduction_*.ipynb"))) +
89-
sorted(glob(os.path.join(Dir_path, "basic*.ipynb"))) +
90-
sorted(glob(os.path.join(Dir_path, "advanced*.ipynb"))) +
91-
[os.path.join(Dir_path, "example_preprocessing.ipynb"),
92-
os.path.join(Dir_path, "example_1stlevel.ipynb"),
93-
os.path.join(Dir_path, "example_normalize.ipynb"),
94-
os.path.join(Dir_path, "example_2ndlevel.ipynb"),
95-
os.path.join(Dir_path, "handson_preprocessing.ipynb"),
96-
os.path.join(Dir_path, "handson_analysis.ipynb")])
97-
98-
def test_notebooks(notebook):
9956
test_version()
10057

101-
if 'example' in notebook or 'handson' in notebook:
102-
notebook = reduce_notebook_load(notebook)
103-
print('Testing shortened notebook.')
58+
notebooks = sorted(glob("/home/neuro/nipype_tutorial/notebooks/introduction_*.ipynb")) + \
59+
sorted(glob("/home/neuro/nipype_tutorial/notebooks/basic*.ipynb")) + \
60+
sorted(glob("/home/neuro/nipype_tutorial/notebooks/advanced*.ipynb"))
61+
62+
for n in ["/home/neuro/nipype_tutorial/notebooks/example_preprocessing.ipynb",
63+
"/home/neuro/nipype_tutorial/notebooks/example_1stlevel.ipynb",
64+
"/home/neuro/nipype_tutorial/notebooks/example_normalize.ipynb",
65+
"/home/neuro/nipype_tutorial/notebooks/example_2ndlevel.ipynb",
66+
"/home/neuro/nipype_tutorial/notebooks/handson_preprocessing.ipynb",
67+
"/home/neuro/nipype_tutorial/notebooks/handson_analysis.ipynb"]:
68+
69+
print('Reducing: %s' % n)
70+
notebooks.append(reduce_notebook_load(n))
10471

105-
t0 = time.time()
106-
nb, errors = _notebook_run(notebook)
107-
print("time", time.time() - t0)
108-
assert errors == []
72+
for test in notebooks:
73+
t0 = time.time()
74+
os.system('pytest --nbval-lax --nbval-cell-timeout 7200 -v -s %s' % test)
75+
print("time", time.time() - t0)

0 commit comments

Comments
 (0)