Skip to content

Commit 8baac42

Browse files
committed
moving to temporary directories, so tests can be run in readonly; adding conftest.py with datadir variable and some basic libraries (so you dont have to import them in doctests)
1 parent ce13ade commit 8baac42

File tree

8 files changed

+52
-39
lines changed

8 files changed

+52
-39
lines changed

conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
import numpy, os
3+
4+
@pytest.fixture(autouse=True)
5+
def add_np(doctest_namespace):
6+
doctest_namespace['np'] = numpy
7+
doctest_namespace['os'] = os
8+
9+
10+
filepath = os.path.dirname(os.path.realpath(__file__))
11+
datadir = os.path.realpath(os.path.join(filepath, 'nipype/testing/data'))
12+
doctest_namespace["datadir"] = datadir

nipype/interfaces/dcm2nii.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ class Dcm2nii(CommandLine):
7070
Examples
7171
========
7272
73+
>>> tmp = getfixture('tmpdir')
74+
>>> old = tmp.chdir() # changing to a temporary directory
7375
>>> from nipype.interfaces.dcm2nii import Dcm2nii
7476
>>> converter = Dcm2nii()
75-
>>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
77+
>>> converter.inputs.source_names = [os.path.join(datadir, 'functional_1.dcm'), os.path.join(datadir, 'functional_2.dcm')]
7678
>>> converter.inputs.gzip_output = True
7779
>>> converter.inputs.output_dir = '.'
78-
>>> converter.cmdline
79-
'dcm2nii -a y -c y -b config.ini -v y -d y -e y -g y -i n -n y -o . -p y -x n -f n functional_1.dcm'
80+
>>> converter.cmdline #doctest: +ELLIPSIS
81+
'dcm2nii -a y -c y -b config.ini -v y -d y -e y -g y -i n -n y -o . -p y -x n -f n ...functional_1.dcm'
82+
>>> os.chdir(old.strpath)
8083
"""
8184

8285
input_spec = Dcm2niiInputSpec

nipype/interfaces/fsl/tests/test_Level1Design_functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from ..model import Level1Design
55

66

7-
def test_level1design():
7+
def test_level1design(tmpdir):
8+
old = tmpdir.chdir()
89
l = Level1Design()
910
runinfo = dict(cond=[{'name': 'test_condition', 'onset': [0, 10],
1011
'duration':[10, 10]}],regress=[])
@@ -29,3 +30,5 @@ def test_level1design():
2930
do_tempfilter,
3031
key)
3132
assert "set fmri(convolve1) {0}".format(val) in output_txt
33+
34+
os.chdir(old.strpath)

nipype/interfaces/io.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,19 +2476,20 @@ class JSONFileGrabber(IOBase):
24762476
24772477
Example
24782478
-------
2479-
2479+
>>> tmp = getfixture('tmpdir')
2480+
>>> old = tmp.chdir() # changing to a temporary directory
24802481
>>> import pprint
24812482
>>> from nipype.interfaces.io import JSONFileGrabber
24822483
>>> jsonSource = JSONFileGrabber()
24832484
>>> jsonSource.inputs.defaults = {'param1': 'overrideMe', 'param3': 1.0}
24842485
>>> res = jsonSource.run()
24852486
>>> pprint.pprint(res.outputs.get())
24862487
{'param1': 'overrideMe', 'param3': 1.0}
2487-
>>> jsonSource.inputs.in_file = 'jsongrabber.txt'
2488+
>>> jsonSource.inputs.in_file = os.path.join(datadir, 'jsongrabber.txt')
24882489
>>> res = jsonSource.run()
24892490
>>> pprint.pprint(res.outputs.get()) # doctest:, +ELLIPSIS
24902491
{'param1': 'exampleStr', 'param2': 4, 'param3': 1.0}
2491-
2492+
>>> os.chdir(old.strpath)
24922493
24932494
"""
24942495
input_spec = JSONFileGrabberInputSpec

nipype/interfaces/niftyreg/regutils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ class RegAverage(NiftyRegCommand):
391391
392392
Examples
393393
--------
394+
>>> tmp = getfixture('tmpdir')
395+
>>> old = tmp.chdir() # changing to temporary file
394396
>>> from nipype.interfaces import niftyreg
395397
>>> node = niftyreg.RegAverage()
396398
>>> one_file = 'im1.nii'
@@ -399,6 +401,7 @@ class RegAverage(NiftyRegCommand):
399401
>>> node.inputs.avg_files = [one_file, two_file, three_file]
400402
>>> node.cmdline # doctest: +ELLIPSIS
401403
'reg_average --cmd_file .../reg_average_cmd'
404+
>>> os.chdir(old.strpath)
402405
403406
"""
404407
_cmd = get_custom_path('reg_average')

nipype/interfaces/utility/base.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
"""
5-
Various utilities
6-
7-
Change directory to provide relative paths for doctests
8-
>>> import os
9-
>>> filepath = os.path.dirname(os.path.realpath(__file__))
10-
>>> datadir = os.path.realpath(os.path.join(filepath,
11-
... '../../testing/data'))
12-
>>> os.chdir(datadir)
13-
5+
# changing to temporary directories
6+
>>> tmp = getfixture('tmpdir')
7+
>>> old = tmp.chdir()
148
"""
159
from __future__ import print_function, division, unicode_literals, absolute_import
1610
from builtins import range
@@ -231,14 +225,14 @@ class Rename(IOBase):
231225
232226
>>> from nipype.interfaces.utility import Rename
233227
>>> rename1 = Rename()
234-
>>> rename1.inputs.in_file = "zstat1.nii.gz"
228+
>>> rename1.inputs.in_file = os.path.join(datadir, "zstat1.nii.gz") # datadir is a directory with exemplary files, defined in conftest.py
235229
>>> rename1.inputs.format_string = "Faces-Scenes.nii.gz"
236230
>>> res = rename1.run() # doctest: +SKIP
237231
>>> res.outputs.out_file # doctest: +SKIP
238232
'Faces-Scenes.nii.gz" # doctest: +SKIP
239233
240234
>>> rename2 = Rename(format_string="%(subject_id)s_func_run%(run)02d")
241-
>>> rename2.inputs.in_file = "functional.nii"
235+
>>> rename2.inputs.in_file = os.path.join(datadir, "functional.nii")
242236
>>> rename2.inputs.keep_ext = True
243237
>>> rename2.inputs.subject_id = "subj_201"
244238
>>> rename2.inputs.run = 2
@@ -247,7 +241,7 @@ class Rename(IOBase):
247241
'subj_201_func_run02.nii' # doctest: +SKIP
248242
249243
>>> rename3 = Rename(format_string="%(subject_id)s_%(seq)s_run%(run)02d.nii")
250-
>>> rename3.inputs.in_file = "func_epi_1_1.nii"
244+
>>> rename3.inputs.in_file = os.path.join(datadir, "func_epi_1_1.nii")
251245
>>> rename3.inputs.parse_string = "func_(?P<seq>\w*)_.*"
252246
>>> rename3.inputs.subject_id = "subj_201"
253247
>>> rename3.inputs.run = 2

nipype/interfaces/utility/wrappers.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
# -*- coding: utf-8 -*-
22
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
4-
"""Various utilities
5-
6-
Change directory to provide relative paths for doctests
7-
>>> import os
8-
>>> filepath = os.path.dirname(os.path.realpath(__file__))
9-
>>> datadir = os.path.realpath(os.path.join(filepath,
10-
... '../../testing/data'))
11-
>>> os.chdir(datadir)
12-
13-
144
"""
5+
# changing to temporary directories
6+
>>> tmp = getfixture('tmpdir')
7+
>>> old = tmp.chdir()
8+
"""
9+
1510
from __future__ import print_function, division, unicode_literals, absolute_import
1611

1712
from future import standard_library

nipype/pipeline/engine/tests/test_utils.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@
1818
from ..utils import merge_dict, clean_working_directory, write_workflow_prov
1919

2020

21-
def test_identitynode_removal():
21+
def test_identitynode_removal(tmpdir):
2222

2323
def test_function(arg1, arg2, arg3):
2424
import numpy as np
2525
return (np.array(arg1) + arg2 + arg3).tolist()
26+
27+
out_dir = tmpdir.strpath
2628

27-
wf = pe.Workflow(name="testidentity")
29+
wf = pe.Workflow(name="testidentity", base_dir=out_dir)
2830

29-
n1 = pe.Node(niu.IdentityInterface(fields=['a', 'b']), name='src')
31+
n1 = pe.Node(niu.IdentityInterface(fields=['a', 'b']), name='src', base_dir=out_dir,)
3032
n1.iterables = ('b', [0, 1, 2, 3])
3133
n1.inputs.a = [0, 1, 2, 3]
3234

33-
n2 = pe.Node(niu.Select(), name='selector')
35+
n2 = pe.Node(niu.Select(), name='selector', base_dir=out_dir,)
3436
wf.connect(n1, ('a', test_function, 1, -1), n2, 'inlist')
3537
wf.connect(n1, 'b', n2, 'index')
3638

37-
n3 = pe.Node(niu.IdentityInterface(fields=['c', 'd']), name='passer')
39+
n3 = pe.Node(niu.IdentityInterface(fields=['c', 'd']), name='passer', base_dir=out_dir,)
3840
n3.inputs.c = [1, 2, 3, 4]
3941
wf.connect(n2, 'out', n3, 'd')
4042

41-
n4 = pe.Node(niu.Select(), name='selector2')
43+
n4 = pe.Node(niu.Select(), name='selector2', base_dir=out_dir,)
4244
wf.connect(n3, ('c', test_function, 1, -1), n4, 'inlist')
4345
wf.connect(n3, 'd', n4, 'index')
4446

@@ -216,19 +218,19 @@ def test_function3(arg):
216218
n1 = pe.Node(niu.Function(input_names=['arg1'],
217219
output_names=['out_file1', 'out_file2', 'dir'],
218220
function=test_function),
219-
name='n1')
221+
name='n1', base_dir=out_dir)
220222
n1.inputs.arg1 = 1
221223

222224
n2 = pe.Node(niu.Function(input_names=['in_file', 'arg'],
223225
output_names=['out_file1', 'out_file2', 'n'],
224226
function=test_function2),
225-
name='n2')
227+
name='n2', base_dir=out_dir)
226228
n2.inputs.arg = 2
227229

228230
n3 = pe.Node(niu.Function(input_names=['arg'],
229231
output_names=['n'],
230232
function=test_function3),
231-
name='n3')
233+
name='n3', base_dir=out_dir)
232234

233235
wf = pe.Workflow(name="node_rem_test" + plugin, base_dir=out_dir)
234236
wf.connect(n1, "out_file1", n2, "in_file")
@@ -271,7 +273,7 @@ def test_function3(arg):
271273
n2.name,
272274
'file3.txt')) != remove_unnecessary_outputs
273275

274-
n4 = pe.Node(UtilsTestInterface(), name='n4')
276+
n4 = pe.Node(UtilsTestInterface(), name='n4', base_dir=out_dir)
275277
wf.connect(n2, "out_file1", n4, "in_file")
276278

277279
def pick_first(l):

0 commit comments

Comments
 (0)