13
13
from nipype .testing import (assert_equal , assert_raises , skipif )
14
14
from nipype .interfaces .base import Undefined
15
15
import nipype .interfaces .fsl .maths as fsl
16
- from nipype .interfaces .fsl import no_fsl
16
+ from nipype .interfaces .fsl import no_fsl , Info
17
17
18
18
19
19
def create_files_in_directory ():
20
20
testdir = os .path .realpath (mkdtemp ())
21
21
origdir = os .getcwd ()
22
22
os .chdir (testdir )
23
23
24
- ftype = os .environ ["FSLOUTPUTTYPE" ]
25
- os .environ ["FSLOUTPUTTYPE" ] = "NIFTI"
26
-
27
24
filelist = ['a.nii' ,'b.nii' ]
28
25
for f in filelist :
29
26
hdr = nb .Nifti1Header ()
@@ -32,18 +29,19 @@ def create_files_in_directory():
32
29
img = np .random .random (shape )
33
30
nb .save (nb .Nifti1Image (img ,np .eye (4 ),hdr ),
34
31
os .path .join (testdir ,f ))
35
- return filelist , testdir , origdir , ftype
36
32
37
- def clean_directory (testdir , origdir , ftype ):
33
+ out_ext = Info .output_type_to_ext (Info .output_type ())
34
+ return filelist , testdir , origdir , out_ext
35
+
36
+ def clean_directory (testdir , origdir ):
38
37
if os .path .exists (testdir ):
39
38
rmtree (testdir )
40
39
os .chdir (origdir )
41
- os .environ ["FSLOUTPUTTYPE" ] = ftype
42
40
43
41
44
42
@skipif (no_fsl )
45
43
def test_maths_base ():
46
- files , testdir , origdir , ftype = create_files_in_directory ()
44
+ files , testdir , origdir , out_ext = create_files_in_directory ()
47
45
48
46
# Get some fslmaths
49
47
maths = fsl .MathsCommand ()
@@ -56,33 +54,34 @@ def test_maths_base():
56
54
57
55
# Set an in file
58
56
maths .inputs .in_file = "a.nii"
57
+ out_file = "a_maths%s" % out_ext
59
58
60
59
# Now test the most basic command line
61
- yield assert_equal , maths .cmdline , "fslmaths a.nii %s" % os .path .join (testdir , "a_maths.nii" )
60
+ yield assert_equal , maths .cmdline , "fslmaths a.nii %s" % os .path .join (testdir , out_file )
62
61
63
62
# Now test that we can set the various data types
64
63
dtypes = ["float" ,"char" ,"int" ,"short" ,"double" ,"input" ]
65
- int_cmdline = "fslmaths -dt %s a.nii " + os .path .join (testdir , "a_maths.nii" )
66
- out_cmdline = "fslmaths a.nii " + os .path .join (testdir , "a_maths.nii" ) + " -odt %s"
67
- duo_cmdline = "fslmaths -dt %s a.nii " + os .path .join (testdir , "a_maths.nii" ) + " -odt %s"
64
+ int_cmdline = "fslmaths -dt %s a.nii " + os .path .join (testdir , out_file )
65
+ out_cmdline = "fslmaths a.nii " + os .path .join (testdir , out_file ) + " -odt %s"
66
+ duo_cmdline = "fslmaths -dt %s a.nii " + os .path .join (testdir , out_file ) + " -odt %s"
68
67
for dtype in dtypes :
69
- foo = fsl .MathsCommand (in_file = "a.nii" ,internal_datatype = dtype )
68
+ foo = fsl .MathsCommand (in_file = "a.nii" , internal_datatype = dtype )
70
69
yield assert_equal , foo .cmdline , int_cmdline % dtype
71
- bar = fsl .MathsCommand (in_file = "a.nii" ,output_datatype = dtype )
72
- yield assert_equal , bar .cmdline , out_cmdline % dtype
73
- foobar = fsl .MathsCommand (in_file = "a.nii" ,internal_datatype = dtype ,output_datatype = dtype )
70
+ bar = fsl .MathsCommand (in_file = "a.nii" , output_datatype = dtype )
71
+ yield assert_equal , bar .cmdline , out_cmdline % dtype
72
+ foobar = fsl .MathsCommand (in_file = "a.nii" , internal_datatype = dtype , output_datatype = dtype )
74
73
yield assert_equal , foobar .cmdline , duo_cmdline % (dtype , dtype )
75
74
76
75
# Test that we can ask for an outfile name
77
76
maths .inputs .out_file = "b.nii"
78
77
yield assert_equal , maths .cmdline , "fslmaths a.nii b.nii"
79
78
80
79
# Clean up our mess
81
- clean_directory (testdir , origdir , ftype )
80
+ clean_directory (testdir , origdir )
82
81
83
82
@skipif (no_fsl )
84
83
def test_changedt ():
85
- files , testdir , origdir , ftype = create_files_in_directory ()
84
+ files , testdir , origdir , out_ext = create_files_in_directory ()
86
85
87
86
# Get some fslmaths
88
87
cdt = fsl .ChangeDataType ()
@@ -108,11 +107,11 @@ def test_changedt():
108
107
yield assert_equal , foo .cmdline , cmdline % dtype
109
108
110
109
# Clean up our mess
111
- clean_directory (testdir , origdir , ftype )
110
+ clean_directory (testdir , origdir )
112
111
113
112
@skipif (no_fsl )
114
113
def test_threshold ():
115
- files , testdir , origdir , ftype = create_files_in_directory ()
114
+ files , testdir , origdir , out_ext = create_files_in_directory ()
116
115
117
116
# Get the command
118
117
thresh = fsl .Threshold (in_file = "a.nii" ,out_file = "b.nii" )
@@ -142,12 +141,12 @@ def test_threshold():
142
141
yield assert_equal , thresh .cmdline , cmdline % ("-uthrP " + val )
143
142
144
143
# Clean up our mess
145
- clean_directory (testdir , origdir , ftype )
144
+ clean_directory (testdir , origdir )
146
145
147
146
148
147
@skipif (no_fsl )
149
148
def test_meanimage ():
150
- files , testdir , origdir , ftype = create_files_in_directory ()
149
+ files , testdir , origdir , out_ext = create_files_in_directory ()
151
150
152
151
# Get the command
153
152
meaner = fsl .MeanImage (in_file = "a.nii" ,out_file = "b.nii" )
@@ -166,14 +165,14 @@ def test_meanimage():
166
165
167
166
# Test the auto naming
168
167
meaner = fsl .MeanImage (in_file = "a.nii" )
169
- yield assert_equal , meaner .cmdline , "fslmaths a.nii -Tmean %s" % os .path .join (testdir , "a_mean.nii" )
168
+ yield assert_equal , meaner .cmdline , "fslmaths a.nii -Tmean %s" % os .path .join (testdir , "a_mean%s" % out_ext )
170
169
171
170
# Clean up our mess
172
- clean_directory (testdir , origdir , ftype )
171
+ clean_directory (testdir , origdir )
173
172
174
173
@skipif (no_fsl )
175
174
def test_maximage ():
176
- files , testdir , origdir , ftype = create_files_in_directory ()
175
+ files , testdir , origdir , out_ext = create_files_in_directory ()
177
176
178
177
# Get the command
179
178
maxer = fsl .MaxImage (in_file = "a.nii" ,out_file = "b.nii" )
@@ -192,14 +191,14 @@ def test_maximage():
192
191
193
192
# Test the auto naming
194
193
maxer = fsl .MaxImage (in_file = "a.nii" )
195
- yield assert_equal , maxer .cmdline , "fslmaths a.nii -Tmax %s" % os .path .join (testdir , "a_max.nii" )
194
+ yield assert_equal , maxer .cmdline , "fslmaths a.nii -Tmax %s" % os .path .join (testdir , "a_max%s" % out_ext )
196
195
197
196
# Clean up our mess
198
- clean_directory (testdir , origdir , ftype )
197
+ clean_directory (testdir , origdir )
199
198
200
199
@skipif (no_fsl )
201
200
def test_smooth ():
202
- files , testdir , origdir , ftype = create_files_in_directory ()
201
+ files , testdir , origdir , out_ext = create_files_in_directory ()
203
202
204
203
# Get the command
205
204
smoother = fsl .IsotropicSmooth (in_file = "a.nii" ,out_file = "b.nii" )
@@ -221,14 +220,14 @@ def test_smooth():
221
220
222
221
# Test automatic naming
223
222
smoother = fsl .IsotropicSmooth (in_file = "a.nii" , sigma = 5 )
224
- yield assert_equal , smoother .cmdline , "fslmaths a.nii -s %.5f %s" % (5 , os .path .join (testdir , "a_smooth.nii" ))
223
+ yield assert_equal , smoother .cmdline , "fslmaths a.nii -s %.5f %s" % (5 , os .path .join (testdir , "a_smooth%s" % out_ext ))
225
224
226
225
# Clean up our mess
227
- clean_directory (testdir , origdir , ftype )
226
+ clean_directory (testdir , origdir )
228
227
229
228
@skipif (no_fsl )
230
229
def test_mask ():
231
- files , testdir , origdir , ftype = create_files_in_directory ()
230
+ files , testdir , origdir , out_ext = create_files_in_directory ()
232
231
233
232
# Get the command
234
233
masker = fsl .ApplyMask (in_file = "a.nii" ,out_file = "c.nii" )
@@ -245,15 +244,15 @@ def test_mask():
245
244
246
245
# Test auto name generation
247
246
masker = fsl .ApplyMask (in_file = "a.nii" ,mask_file = "b.nii" )
248
- yield assert_equal , masker .cmdline , "fslmaths a.nii -mas b.nii " + os .path .join (testdir , "a_masked.nii" )
247
+ yield assert_equal , masker .cmdline , "fslmaths a.nii -mas b.nii " + os .path .join (testdir , "a_masked%s" % out_ext )
249
248
250
249
# Clean up our mess
251
- clean_directory (testdir , origdir , ftype )
250
+ clean_directory (testdir , origdir )
252
251
253
252
254
253
@skipif (no_fsl )
255
254
def test_dilation ():
256
- files , testdir , origdir , ftype = create_files_in_directory ()
255
+ files , testdir , origdir , out_ext = create_files_in_directory ()
257
256
258
257
# Get the command
259
258
diller = fsl .DilateImage (in_file = "a.nii" ,out_file = "b.nii" )
@@ -287,14 +286,14 @@ def test_dilation():
287
286
288
287
# Test that we don't need to request an out name
289
288
dil = fsl .DilateImage (in_file = "a.nii" , operation = "max" )
290
- yield assert_equal , dil .cmdline , "fslmaths a.nii -dilF %s" % os .path .join (testdir , "a_dil.nii" )
289
+ yield assert_equal , dil .cmdline , "fslmaths a.nii -dilF %s" % os .path .join (testdir , "a_dil%s" % out_ext )
291
290
292
291
# Clean up our mess
293
- clean_directory (testdir , origdir , ftype )
292
+ clean_directory (testdir , origdir )
294
293
295
294
@skipif (no_fsl )
296
295
def test_erosion ():
297
- files , testdir , origdir , ftype = create_files_in_directory ()
296
+ files , testdir , origdir , out_ext = create_files_in_directory ()
298
297
299
298
# Get the command
300
299
erode = fsl .ErodeImage (in_file = "a.nii" ,out_file = "b.nii" )
@@ -311,14 +310,14 @@ def test_erosion():
311
310
312
311
# Test that we don't need to request an out name
313
312
erode = fsl .ErodeImage (in_file = "a.nii" )
314
- yield assert_equal , erode .cmdline , "fslmaths a.nii -ero %s" % os .path .join (testdir , "a_ero.nii" )
313
+ yield assert_equal , erode .cmdline , "fslmaths a.nii -ero %s" % os .path .join (testdir , "a_ero%s" % out_ext )
315
314
316
315
# Clean up our mess
317
- clean_directory (testdir , origdir , ftype )
316
+ clean_directory (testdir , origdir )
318
317
319
318
@skipif (no_fsl )
320
319
def test_spatial_filter ():
321
- files , testdir , origdir , ftype = create_files_in_directory ()
320
+ files , testdir , origdir , out_ext = create_files_in_directory ()
322
321
323
322
# Get the command
324
323
filter = fsl .SpatialFilter (in_file = "a.nii" ,out_file = "b.nii" )
@@ -336,15 +335,15 @@ def test_spatial_filter():
336
335
337
336
# Test that we don't need to ask for an out name
338
337
filter = fsl .SpatialFilter (in_file = "a.nii" , operation = "mean" )
339
- yield assert_equal , filter .cmdline , "fslmaths a.nii -fmean %s" % os .path .join (testdir , "a_filt.nii" )
338
+ yield assert_equal , filter .cmdline , "fslmaths a.nii -fmean %s" % os .path .join (testdir , "a_filt%s" % out_ext )
340
339
341
340
# Clean up our mess
342
- clean_directory (testdir , origdir , ftype )
341
+ clean_directory (testdir , origdir )
343
342
344
343
345
344
@skipif (no_fsl )
346
345
def test_unarymaths ():
347
- files , testdir , origdir , ftype = create_files_in_directory ()
346
+ files , testdir , origdir , out_ext = create_files_in_directory ()
348
347
349
348
# Get the command
350
349
maths = fsl .UnaryMaths (in_file = "a.nii" ,out_file = "b.nii" )
@@ -364,15 +363,15 @@ def test_unarymaths():
364
363
# Test that we don't need to ask for an out file
365
364
for op in ops :
366
365
maths = fsl .UnaryMaths (in_file = "a.nii" , operation = op )
367
- yield assert_equal , maths .cmdline , "fslmaths a.nii -%s %s" % (op , os .path .join (testdir , "a_%s.nii" % op ))
366
+ yield assert_equal , maths .cmdline , "fslmaths a.nii -%s %s" % (op , os .path .join (testdir , "a_%s%s" % ( op , out_ext ) ))
368
367
369
368
# Clean up our mess
370
- clean_directory (testdir , origdir , ftype )
369
+ clean_directory (testdir , origdir )
371
370
372
371
373
372
@skipif (no_fsl )
374
373
def test_binarymaths ():
375
- files , testdir , origdir , ftype = create_files_in_directory ()
374
+ files , testdir , origdir , out_ext = create_files_in_directory ()
376
375
377
376
# Get the command
378
377
maths = fsl .BinaryMaths (in_file = "a.nii" ,out_file = "c.nii" )
@@ -396,18 +395,19 @@ def test_binarymaths():
396
395
maths .inputs .operand_value = ent
397
396
yield assert_equal , maths .cmdline , "fslmaths a.nii -%s %.8f c.nii" % (op , ent )
398
397
398
+
399
399
# Test that we don't need to ask for an out file
400
400
for op in ops :
401
401
maths = fsl .BinaryMaths (in_file = "a.nii" , operation = op , operand_file = "b.nii" )
402
- yield assert_equal , maths .cmdline , "fslmaths a.nii -%s b.nii %s" % (op ,os .path .join (testdir ,"a_maths.nii" ))
402
+ yield assert_equal , maths .cmdline , "fslmaths a.nii -%s b.nii %s" % (op ,os .path .join (testdir , "a_maths%s" % out_ext ))
403
403
404
404
# Clean up our mess
405
- clean_directory (testdir , origdir , ftype )
405
+ clean_directory (testdir , origdir )
406
406
407
407
408
408
@skipif (no_fsl )
409
409
def test_multimaths ():
410
- files , testdir , origdir , ftype = create_files_in_directory ()
410
+ files , testdir , origdir , out_ext = create_files_in_directory ()
411
411
412
412
# Get the command
413
413
maths = fsl .MultiImageMaths (in_file = "a.nii" ,out_file = "c.nii" )
@@ -430,15 +430,15 @@ def test_multimaths():
430
430
# Test that we don't need to ask for an out file
431
431
maths = fsl .MultiImageMaths (in_file = "a.nii" , op_string = "-add %s -mul 5" , operand_files = ["b.nii" ])
432
432
yield assert_equal , maths .cmdline , \
433
- "fslmaths a.nii -add b.nii -mul 5 %s" % os .path .join (testdir ,"a_maths.nii" )
433
+ "fslmaths a.nii -add b.nii -mul 5 %s" % os .path .join (testdir , "a_maths%s" % out_ext )
434
434
435
435
# Clean up our mess
436
- clean_directory (testdir , origdir , ftype )
436
+ clean_directory (testdir , origdir )
437
437
438
438
439
439
@skipif (no_fsl )
440
440
def test_tempfilt ():
441
- files , testdir , origdir , ftype = create_files_in_directory ()
441
+ files , testdir , origdir , out_ext = create_files_in_directory ()
442
442
443
443
# Get the command
444
444
filt = fsl .TemporalFilter (in_file = "a.nii" ,out_file = "b.nii" )
@@ -459,9 +459,9 @@ def test_tempfilt():
459
459
# Test that we don't need to ask for an out file
460
460
filt = fsl .TemporalFilter (in_file = "a.nii" , highpass_sigma = 64 )
461
461
yield assert_equal , filt .cmdline , \
462
- "fslmaths a.nii -bptf 64.000000 -1.000000 %s" % os .path .join (testdir ,"a_filt.nii" )
462
+ "fslmaths a.nii -bptf 64.000000 -1.000000 %s" % os .path .join (testdir ,"a_filt%s" % out_ext )
463
463
464
464
# Clean up our mess
465
- clean_directory (testdir , origdir , ftype )
465
+ clean_directory (testdir , origdir )
466
466
467
467
0 commit comments