Skip to content

Commit 7bb4129

Browse files
author
Ben Cipollini
committed
TST: use system extension info
1 parent 5702da1 commit 7bb4129

File tree

3 files changed

+69
-67
lines changed

3 files changed

+69
-67
lines changed

nipype/interfaces/fsl/tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_fsloutputtype():
2525

2626

2727
def test_outputtype_to_ext():
28-
for ftype, ext in list(fsl.Info.ftypes.items()):
28+
for ftype, ext in fsl.Info.ftypes.items():
2929
res = fsl.Info.output_type_to_ext(ftype)
3030
yield assert_equal, res, ext
3131

nipype/interfaces/fsl/tests/test_maths.py

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313
from nipype.testing import (assert_equal, assert_raises, skipif)
1414
from nipype.interfaces.base import Undefined
1515
import nipype.interfaces.fsl.maths as fsl
16-
from nipype.interfaces.fsl import no_fsl
16+
from nipype.interfaces.fsl import no_fsl, Info
1717

1818

1919
def create_files_in_directory():
2020
testdir = os.path.realpath(mkdtemp())
2121
origdir = os.getcwd()
2222
os.chdir(testdir)
2323

24-
ftype = os.environ["FSLOUTPUTTYPE"]
25-
os.environ["FSLOUTPUTTYPE"] = "NIFTI"
26-
2724
filelist = ['a.nii','b.nii']
2825
for f in filelist:
2926
hdr = nb.Nifti1Header()
@@ -32,18 +29,19 @@ def create_files_in_directory():
3229
img = np.random.random(shape)
3330
nb.save(nb.Nifti1Image(img,np.eye(4),hdr),
3431
os.path.join(testdir,f))
35-
return filelist, testdir, origdir, ftype
3632

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):
3837
if os.path.exists(testdir):
3938
rmtree(testdir)
4039
os.chdir(origdir)
41-
os.environ["FSLOUTPUTTYPE"] = ftype
4240

4341

4442
@skipif(no_fsl)
4543
def test_maths_base():
46-
files, testdir, origdir, ftype = create_files_in_directory()
44+
files, testdir, origdir, out_ext = create_files_in_directory()
4745

4846
# Get some fslmaths
4947
maths = fsl.MathsCommand()
@@ -56,33 +54,34 @@ def test_maths_base():
5654

5755
# Set an in file
5856
maths.inputs.in_file = "a.nii"
57+
out_file = "a_maths%s" % out_ext
5958

6059
# 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)
6261

6362
# Now test that we can set the various data types
6463
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"
6867
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)
7069
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)
7473
yield assert_equal, foobar.cmdline, duo_cmdline%(dtype, dtype)
7574

7675
# Test that we can ask for an outfile name
7776
maths.inputs.out_file = "b.nii"
7877
yield assert_equal, maths.cmdline, "fslmaths a.nii b.nii"
7978

8079
# Clean up our mess
81-
clean_directory(testdir, origdir, ftype)
80+
clean_directory(testdir, origdir)
8281

8382
@skipif(no_fsl)
8483
def test_changedt():
85-
files, testdir, origdir, ftype = create_files_in_directory()
84+
files, testdir, origdir, out_ext = create_files_in_directory()
8685

8786
# Get some fslmaths
8887
cdt = fsl.ChangeDataType()
@@ -108,11 +107,11 @@ def test_changedt():
108107
yield assert_equal, foo.cmdline, cmdline%dtype
109108

110109
# Clean up our mess
111-
clean_directory(testdir, origdir, ftype)
110+
clean_directory(testdir, origdir)
112111

113112
@skipif(no_fsl)
114113
def test_threshold():
115-
files, testdir, origdir, ftype = create_files_in_directory()
114+
files, testdir, origdir, out_ext = create_files_in_directory()
116115

117116
# Get the command
118117
thresh = fsl.Threshold(in_file="a.nii",out_file="b.nii")
@@ -142,12 +141,12 @@ def test_threshold():
142141
yield assert_equal, thresh.cmdline, cmdline%("-uthrP "+val)
143142

144143
# Clean up our mess
145-
clean_directory(testdir, origdir, ftype)
144+
clean_directory(testdir, origdir)
146145

147146

148147
@skipif(no_fsl)
149148
def test_meanimage():
150-
files, testdir, origdir, ftype = create_files_in_directory()
149+
files, testdir, origdir, out_ext = create_files_in_directory()
151150

152151
# Get the command
153152
meaner = fsl.MeanImage(in_file="a.nii",out_file="b.nii")
@@ -166,14 +165,14 @@ def test_meanimage():
166165

167166
# Test the auto naming
168167
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)
170169

171170
# Clean up our mess
172-
clean_directory(testdir, origdir, ftype)
171+
clean_directory(testdir, origdir)
173172

174173
@skipif(no_fsl)
175174
def test_maximage():
176-
files, testdir, origdir, ftype = create_files_in_directory()
175+
files, testdir, origdir, out_ext = create_files_in_directory()
177176

178177
# Get the command
179178
maxer = fsl.MaxImage(in_file="a.nii",out_file="b.nii")
@@ -192,14 +191,14 @@ def test_maximage():
192191

193192
# Test the auto naming
194193
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)
196195

197196
# Clean up our mess
198-
clean_directory(testdir, origdir, ftype)
197+
clean_directory(testdir, origdir)
199198

200199
@skipif(no_fsl)
201200
def test_smooth():
202-
files, testdir, origdir, ftype = create_files_in_directory()
201+
files, testdir, origdir, out_ext = create_files_in_directory()
203202

204203
# Get the command
205204
smoother = fsl.IsotropicSmooth(in_file="a.nii",out_file="b.nii")
@@ -221,14 +220,14 @@ def test_smooth():
221220

222221
# Test automatic naming
223222
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))
225224

226225
# Clean up our mess
227-
clean_directory(testdir, origdir, ftype)
226+
clean_directory(testdir, origdir)
228227

229228
@skipif(no_fsl)
230229
def test_mask():
231-
files, testdir, origdir, ftype = create_files_in_directory()
230+
files, testdir, origdir, out_ext = create_files_in_directory()
232231

233232
# Get the command
234233
masker = fsl.ApplyMask(in_file="a.nii",out_file="c.nii")
@@ -245,15 +244,15 @@ def test_mask():
245244

246245
# Test auto name generation
247246
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)
249248

250249
# Clean up our mess
251-
clean_directory(testdir, origdir, ftype)
250+
clean_directory(testdir, origdir)
252251

253252

254253
@skipif(no_fsl)
255254
def test_dilation():
256-
files, testdir, origdir, ftype = create_files_in_directory()
255+
files, testdir, origdir, out_ext = create_files_in_directory()
257256

258257
# Get the command
259258
diller = fsl.DilateImage(in_file="a.nii",out_file="b.nii")
@@ -287,14 +286,14 @@ def test_dilation():
287286

288287
# Test that we don't need to request an out name
289288
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)
291290

292291
# Clean up our mess
293-
clean_directory(testdir, origdir, ftype)
292+
clean_directory(testdir, origdir)
294293

295294
@skipif(no_fsl)
296295
def test_erosion():
297-
files, testdir, origdir, ftype = create_files_in_directory()
296+
files, testdir, origdir, out_ext = create_files_in_directory()
298297

299298
# Get the command
300299
erode = fsl.ErodeImage(in_file="a.nii",out_file="b.nii")
@@ -311,14 +310,14 @@ def test_erosion():
311310

312311
# Test that we don't need to request an out name
313312
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)
315314

316315
# Clean up our mess
317-
clean_directory(testdir, origdir, ftype)
316+
clean_directory(testdir, origdir)
318317

319318
@skipif(no_fsl)
320319
def test_spatial_filter():
321-
files, testdir, origdir, ftype = create_files_in_directory()
320+
files, testdir, origdir, out_ext = create_files_in_directory()
322321

323322
# Get the command
324323
filter = fsl.SpatialFilter(in_file="a.nii",out_file="b.nii")
@@ -336,15 +335,15 @@ def test_spatial_filter():
336335

337336
# Test that we don't need to ask for an out name
338337
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)
340339

341340
# Clean up our mess
342-
clean_directory(testdir, origdir, ftype)
341+
clean_directory(testdir, origdir)
343342

344343

345344
@skipif(no_fsl)
346345
def test_unarymaths():
347-
files, testdir, origdir, ftype = create_files_in_directory()
346+
files, testdir, origdir, out_ext = create_files_in_directory()
348347

349348
# Get the command
350349
maths = fsl.UnaryMaths(in_file="a.nii",out_file="b.nii")
@@ -364,15 +363,15 @@ def test_unarymaths():
364363
# Test that we don't need to ask for an out file
365364
for op in ops:
366365
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)))
368367

369368
# Clean up our mess
370-
clean_directory(testdir, origdir, ftype)
369+
clean_directory(testdir, origdir)
371370

372371

373372
@skipif(no_fsl)
374373
def test_binarymaths():
375-
files, testdir, origdir, ftype = create_files_in_directory()
374+
files, testdir, origdir, out_ext = create_files_in_directory()
376375

377376
# Get the command
378377
maths = fsl.BinaryMaths(in_file="a.nii",out_file="c.nii")
@@ -396,18 +395,19 @@ def test_binarymaths():
396395
maths.inputs.operand_value = ent
397396
yield assert_equal, maths.cmdline, "fslmaths a.nii -%s %.8f c.nii"%(op, ent)
398397

398+
399399
# Test that we don't need to ask for an out file
400400
for op in ops:
401401
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))
403403

404404
# Clean up our mess
405-
clean_directory(testdir, origdir, ftype)
405+
clean_directory(testdir, origdir)
406406

407407

408408
@skipif(no_fsl)
409409
def test_multimaths():
410-
files, testdir, origdir, ftype = create_files_in_directory()
410+
files, testdir, origdir, out_ext = create_files_in_directory()
411411

412412
# Get the command
413413
maths = fsl.MultiImageMaths(in_file="a.nii",out_file="c.nii")
@@ -430,15 +430,15 @@ def test_multimaths():
430430
# Test that we don't need to ask for an out file
431431
maths = fsl.MultiImageMaths(in_file="a.nii", op_string="-add %s -mul 5", operand_files=["b.nii"])
432432
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)
434434

435435
# Clean up our mess
436-
clean_directory(testdir, origdir, ftype)
436+
clean_directory(testdir, origdir)
437437

438438

439439
@skipif(no_fsl)
440440
def test_tempfilt():
441-
files, testdir, origdir, ftype = create_files_in_directory()
441+
files, testdir, origdir, out_ext = create_files_in_directory()
442442

443443
# Get the command
444444
filt = fsl.TemporalFilter(in_file="a.nii",out_file="b.nii")
@@ -459,9 +459,9 @@ def test_tempfilt():
459459
# Test that we don't need to ask for an out file
460460
filt = fsl.TemporalFilter(in_file="a.nii", highpass_sigma = 64)
461461
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)
463463

464464
# Clean up our mess
465-
clean_directory(testdir, origdir, ftype)
465+
clean_directory(testdir, origdir)
466466

467467

0 commit comments

Comments
 (0)