Skip to content

Commit 29ba1b2

Browse files
authored
Merge pull request #1765 from djarecka/fixtures
Fixtures
2 parents 2d4726f + e9dc5a7 commit 29ba1b2

File tree

10 files changed

+196
-337
lines changed

10 files changed

+196
-337
lines changed

nipype/interfaces/freesurfer/tests/test_preprocess.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,16 @@
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
import os
5-
from shutil import rmtree
6-
import nibabel as nif
7-
import numpy as np
8-
from tempfile import mkdtemp
9-
import pytest
10-
import nipype.interfaces.freesurfer as freesurfer
11-
125

13-
@pytest.fixture()
14-
def create_files_in_directory(request):
15-
outdir = os.path.realpath(mkdtemp())
16-
cwd = os.getcwd()
17-
os.chdir(outdir)
18-
filelist = ['a.nii', 'b.nii']
19-
for f in filelist:
20-
hdr = nif.Nifti1Header()
21-
shape = (3, 3, 3, 4)
22-
hdr.set_data_shape(shape)
23-
img = np.random.random(shape)
24-
nif.save(nif.Nifti1Image(img, np.eye(4), hdr),
25-
os.path.join(outdir, f))
26-
27-
def clean_directory():
28-
if os.path.exists(outdir):
29-
rmtree(outdir)
30-
os.chdir(cwd)
6+
import pytest
7+
from nipype.testing.fixtures import create_files_in_directory
318

32-
request.addfinalizer(clean_directory)
33-
return (filelist, outdir, cwd)
9+
import nipype.interfaces.freesurfer as freesurfer
3410

3511

3612
@pytest.mark.skipif(freesurfer.no_freesurfer(), reason="freesurfer is not installed")
3713
def test_robustregister(create_files_in_directory):
38-
filelist, outdir, cwd = create_files_in_directory
14+
filelist, outdir = create_files_in_directory
3915

4016
reg = freesurfer.RobustRegister()
4117

@@ -62,7 +38,7 @@ def test_robustregister(create_files_in_directory):
6238

6339
@pytest.mark.skipif(freesurfer.no_freesurfer(), reason="freesurfer is not installed")
6440
def test_fitmsparams(create_files_in_directory):
65-
filelist, outdir, cwd = create_files_in_directory
41+
filelist, outdir = create_files_in_directory
6642

6743
fit = freesurfer.FitMSParams()
6844

@@ -85,7 +61,7 @@ def test_fitmsparams(create_files_in_directory):
8561

8662
@pytest.mark.skipif(freesurfer.no_freesurfer(), reason="freesurfer is not installed")
8763
def test_synthesizeflash(create_files_in_directory):
88-
filelist, outdir, cwd = create_files_in_directory
64+
filelist, outdir = create_files_in_directory
8965

9066
syn = freesurfer.SynthesizeFLASH()
9167

nipype/interfaces/freesurfer/tests/test_utils.py

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,18 @@
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
from __future__ import print_function, division, unicode_literals, absolute_import
55
from builtins import open
6-
76
import os
8-
from tempfile import mkdtemp
9-
from shutil import rmtree
10-
11-
import numpy as np
127

13-
import nibabel as nif
148
import pytest
15-
from nipype.interfaces.base import TraitError
9+
from nipype.testing.fixtures import (create_files_in_directory_plus_dummy_file,
10+
create_surf_file_in_directory)
1611

12+
from nipype.interfaces.base import TraitError
1713
import nipype.interfaces.freesurfer as fs
1814

1915

20-
@pytest.fixture()
21-
def create_files_in_directory(request):
22-
outdir = os.path.realpath(mkdtemp())
23-
cwd = os.getcwd()
24-
os.chdir(outdir)
25-
filelist = ['a.nii', 'b.nii']
26-
for f in filelist:
27-
hdr = nif.Nifti1Header()
28-
shape = (3, 3, 3, 4)
29-
hdr.set_data_shape(shape)
30-
img = np.random.random(shape)
31-
nif.save(nif.Nifti1Image(img, np.eye(4), hdr),
32-
os.path.join(outdir, f))
33-
with open(os.path.join(outdir, 'reg.dat'), 'wt') as fp:
34-
fp.write('dummy file')
35-
filelist.append('reg.dat')
36-
37-
def clean_directory():
38-
if os.path.exists(outdir):
39-
rmtree(outdir)
40-
os.chdir(cwd)
41-
42-
request.addfinalizer(clean_directory)
43-
return (filelist, outdir, cwd)
44-
45-
46-
@pytest.fixture()
47-
def create_surf_file(request):
48-
outdir = os.path.realpath(mkdtemp())
49-
cwd = os.getcwd()
50-
os.chdir(outdir)
51-
surf = 'lh.a.nii'
52-
hdr = nif.Nifti1Header()
53-
shape = (1, 100, 1)
54-
hdr.set_data_shape(shape)
55-
img = np.random.random(shape)
56-
nif.save(nif.Nifti1Image(img, np.eye(4), hdr),
57-
os.path.join(outdir, surf))
58-
59-
def clean_directory():
60-
if os.path.exists(outdir):
61-
rmtree(outdir)
62-
os.chdir(cwd)
63-
64-
request.addfinalizer(clean_directory)
65-
return (surf, outdir, cwd)
66-
67-
6816
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
69-
def test_sample2surf(create_files_in_directory):
17+
def test_sample2surf(create_files_in_directory_plus_dummy_file):
7018

7119
s2s = fs.SampleToSurface()
7220
# Test underlying command
@@ -76,7 +24,7 @@ def test_sample2surf(create_files_in_directory):
7624
with pytest.raises(ValueError): s2s.run()
7725

7826
# Create testing files
79-
files, cwd, oldwd = create_files_in_directory
27+
files, cwd = create_files_in_directory_plus_dummy_file
8028

8129
# Test input settings
8230
s2s.inputs.source_file = files[0]
@@ -107,7 +55,7 @@ def set_illegal_range():
10755

10856

10957
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
110-
def test_surfsmooth(create_surf_file):
58+
def test_surfsmooth(create_surf_file_in_directory):
11159

11260
smooth = fs.SurfaceSmooth()
11361

@@ -118,7 +66,7 @@ def test_surfsmooth(create_surf_file):
11866
with pytest.raises(ValueError): smooth.run()
11967

12068
# Create testing files
121-
surf, cwd, oldwd = create_surf_file
69+
surf, cwd = create_surf_file_in_directory
12270

12371
# Test input settings
12472
smooth.inputs.in_file = surf
@@ -139,7 +87,7 @@ def test_surfsmooth(create_surf_file):
13987

14088

14189
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
142-
def test_surfxfm(create_surf_file):
90+
def test_surfxfm(create_surf_file_in_directory):
14391

14492
xfm = fs.SurfaceTransform()
14593

@@ -150,7 +98,7 @@ def test_surfxfm(create_surf_file):
15098
with pytest.raises(ValueError): xfm.run()
15199

152100
# Create testing files
153-
surf, cwd, oldwd = create_surf_file
101+
surf, cwd = create_surf_file_in_directory
154102

155103
# Test input settings
156104
xfm.inputs.source_file = surf
@@ -170,7 +118,7 @@ def test_surfxfm(create_surf_file):
170118

171119

172120
@pytest.mark.skipif(fs.no_freesurfer(), reason="freesurfer is not installed")
173-
def test_surfshots(create_files_in_directory):
121+
def test_surfshots(create_files_in_directory_plus_dummy_file):
174122

175123
fotos = fs.SurfaceSnapshots()
176124

@@ -181,7 +129,7 @@ def test_surfshots(create_files_in_directory):
181129
with pytest.raises(ValueError): fotos.run()
182130

183131
# Create testing files
184-
files, cwd, oldwd = create_files_in_directory
132+
files, cwd = create_files_in_directory_plus_dummy_file
185133

186134
# Test input settins
187135
fotos.inputs.subject_id = "fsaverage"

nipype/interfaces/fsl/tests/test_dti.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,14 @@
33
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
44
# vi: set ft=python sts=4 ts=4 sw=4 et:
55
from builtins import open, range
6-
76
import os
87

9-
from tempfile import mkdtemp
10-
from shutil import rmtree
11-
12-
import numpy as np
13-
14-
import nibabel as nb
15-
168
import nipype.interfaces.fsl.dti as fsl
179
from nipype.interfaces.fsl import Info, no_fsl
1810
from nipype.interfaces.base import Undefined
1911

2012
import pytest
21-
22-
23-
@pytest.fixture(scope="module")
24-
def create_files_in_directory(request):
25-
outdir = os.path.realpath(mkdtemp())
26-
cwd = os.getcwd()
27-
os.chdir(outdir)
28-
filelist = ['a.nii', 'b.nii']
29-
for f in filelist:
30-
hdr = nb.Nifti1Header()
31-
shape = (3, 3, 3, 4)
32-
hdr.set_data_shape(shape)
33-
img = np.random.random(shape)
34-
nb.save(nb.Nifti1Image(img, np.eye(4), hdr),
35-
os.path.join(outdir, f))
36-
37-
def clean_directory():
38-
rmtree(outdir)
39-
os.chdir(cwd)
40-
41-
request.addfinalizer(clean_directory)
42-
return (filelist, outdir)
13+
from nipype.testing.fixtures import create_files_in_directory
4314

4415

4516
# test dtifit

nipype/interfaces/fsl/tests/test_epi.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,13 @@
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
import os
55

6-
from tempfile import mkdtemp
7-
from shutil import rmtree
8-
9-
import numpy as np
10-
11-
import nibabel as nb
12-
136
import pytest
7+
from nipype.testing.fixtures import create_files_in_directory
8+
149
import nipype.interfaces.fsl.epi as fsl
1510
from nipype.interfaces.fsl import no_fsl
1611

1712

18-
@pytest.fixture(scope="module")
19-
def create_files_in_directory(request):
20-
outdir = os.path.realpath(mkdtemp())
21-
cwd = os.getcwd()
22-
os.chdir(outdir)
23-
filelist = ['a.nii', 'b.nii']
24-
for f in filelist:
25-
hdr = nb.Nifti1Header()
26-
shape = (3, 3, 3, 4)
27-
hdr.set_data_shape(shape)
28-
img = np.random.random(shape)
29-
nb.save(nb.Nifti1Image(img, np.eye(4), hdr),
30-
os.path.join(outdir, f))
31-
32-
def clean_directory():
33-
rmtree(outdir)
34-
os.chdir(cwd)
35-
36-
request.addfinalizer(clean_directory)
37-
return (filelist, outdir)
38-
39-
4013
# test eddy_correct
4114
@pytest.mark.skipif(no_fsl(), reason="fsl is not installed")
4215
def test_eddy_correct2(create_files_in_directory):

0 commit comments

Comments
 (0)