33# vi: set ft=python sts=4 ts=4 sw=4 et:
44from __future__ import print_function , division , unicode_literals , absolute_import
55from builtins import open
6-
76import os
8- from tempfile import mkdtemp
9- from shutil import rmtree
10-
11- import numpy as np
127
13- import nibabel as nif
148import 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
1713import 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"
0 commit comments