Skip to content

Commit 45f2e07

Browse files
authored
Merge pull request miykael#91 from djarecka/nbval_test
Nbval test
2 parents ec3d005 + 5326d2b commit 45f2e07

File tree

5 files changed

+52
-65
lines changed

5 files changed

+52
-65
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 -v -s /home/neuro/nipype_tutorial/test_notebooks.py
21+
docker run -it --rm miykael/nipype_tutorial:$CIRCLE_BRANCH python /home/neuro/nipype_tutorial/test_notebooks.py

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ RUN export PATH="/opt/miniconda-latest/bin:$PATH" \
127127
nilearn \
128128
datalad[full] \
129129
nipy \
130-
duecredit" \
130+
duecredit \
131+
nbval" \
131132
&& rm -rf ~/.cache/pip/* \
132133
&& sync \
133134
&& sed -i '$isource activate neuro' $ND_ENTRYPOINT
@@ -231,6 +232,7 @@ RUN echo '{ \
231232
\n "nilearn", \
232233
\n "datalad[full]", \
233234
\n "nipy", \
235+
\n "nbval", \
234236
\n "duecredit" \
235237
\n ], \
236238
\n "create_env": "neuro", \

create_dockerfile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ docker run --rm kaczmarj/neurodocker:master generate docker\
1414
traits pandas matplotlib scikit-learn scikit-image seaborn nbformat nb_conda" \
1515
pip_install="https://github.com/nipy/nipype/tarball/master
1616
https://github.com/INCF/pybids/tarball/master
17-
nilearn datalad[full] nipy duecredit" \
17+
nilearn datalad[full] nipy duecredit nbval" \
1818
create_env="neuro" \
1919
activate=True \
2020
--run-bash "source activate neuro && jupyter nbextension enable exercise2/main && jupyter nbextension enable spellchecker/main" \

notebooks/introduction_jupyter-notebook.ipynb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
{
2828
"cell_type": "code",
2929
"execution_count": null,
30-
"metadata": {},
30+
"metadata": {
31+
"collapsed": true
32+
},
3133
"outputs": [],
3234
"source": [
3335
"import pandas as pd\n",
@@ -61,9 +63,12 @@
6163
{
6264
"cell_type": "code",
6365
"execution_count": null,
64-
"metadata": {},
66+
"metadata": {
67+
"collapsed": true
68+
},
6569
"outputs": [],
6670
"source": [
71+
"# NBVAL_SKIP\n",
6772
"# Use TAB completion for function info\n",
6873
"pd.read_csv("
6974
]
@@ -90,9 +95,12 @@
9095
{
9196
"cell_type": "code",
9297
"execution_count": null,
93-
"metadata": {},
98+
"metadata": {
99+
"collapsed": true
100+
},
94101
"outputs": [],
95102
"source": [
103+
"# NBVAL_SKIP\n",
96104
"# Use TAB completion to see possible function names\n",
97105
"pd.r"
98106
]
@@ -118,7 +126,9 @@
118126
{
119127
"cell_type": "code",
120128
"execution_count": null,
121-
"metadata": {},
129+
"metadata": {
130+
"collapsed": true
131+
},
122132
"outputs": [],
123133
"source": [
124134
"pd.read_csv?"
@@ -136,7 +146,9 @@
136146
{
137147
"cell_type": "code",
138148
"execution_count": null,
139-
"metadata": {},
149+
"metadata": {
150+
"collapsed": true
151+
},
140152
"outputs": [],
141153
"source": [
142154
"def print_10_nums():\n",
@@ -147,7 +159,9 @@
147159
{
148160
"cell_type": "code",
149161
"execution_count": null,
150-
"metadata": {},
162+
"metadata": {
163+
"collapsed": true
164+
},
151165
"outputs": [],
152166
"source": [
153167
"print_10_nums()"
@@ -217,7 +231,9 @@
217231
{
218232
"cell_type": "code",
219233
"execution_count": null,
220-
"metadata": {},
234+
"metadata": {
235+
"collapsed": true
236+
},
221237
"outputs": [],
222238
"source": [
223239
"%time result = sum([x for x in range(10**6)])"
@@ -235,7 +251,9 @@
235251
{
236252
"cell_type": "code",
237253
"execution_count": null,
238-
"metadata": {},
254+
"metadata": {
255+
"collapsed": true
256+
},
239257
"outputs": [],
240258
"source": [
241259
"%%latex\n",

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)