Skip to content

Commit 5b02964

Browse files
committed
FSL FNIRT: handle multiple intensity map inputs and outputs
1 parent 7d4d5e4 commit 5b02964

File tree

2 files changed

+67
-32
lines changed

2 files changed

+67
-32
lines changed

nipype/interfaces/fsl/preprocess.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from ..base import (TraitedSpec, File, InputMultiPath,
2626
OutputMultiPath, Undefined, traits,
2727
isdefined)
28-
from .base import FSLCommand, FSLCommandInputSpec
28+
from .base import FSLCommand, FSLCommandInputSpec, Info
2929

3030

3131
class BETInputSpec(FSLCommandInputSpec):
@@ -750,10 +750,12 @@ class FNIRTInputSpec(FSLCommandInputSpec):
750750
desc='name of file containing affine transform')
751751
inwarp_file = File(exists=True, argstr='--inwarp=%s',
752752
desc='name of file containing initial non-linear warps')
753-
in_intensitymap_file = File(exists=True, argstr='--intin=%s',
754-
desc=('name of file/files containing initial '
755-
'intensity maping usually generated by '
756-
'previous fnirt run'))
753+
in_intensitymap_file = traits.List(File, exists=True, argstr='--intin=%s',
754+
copyfiles=False, minlen=1, maxlen=2,
755+
desc=('name of file/files containing '
756+
'initial intensity mapping '
757+
'usually generated by previous '
758+
'fnirt run'))
757759
fieldcoeff_file = traits.Either(
758760
traits.Bool, File, argstr='--cout=%s',
759761
desc='name of output file with field coefficients or true')
@@ -907,8 +909,9 @@ class FNIRTOutputSpec(TraitedSpec):
907909
field_file = File(desc='file with warp field')
908910
jacobian_file = File(desc='file containing Jacobian of the field')
909911
modulatedref_file = File(desc='file containing intensity modulated --ref')
910-
out_intensitymap_file = File(
911-
desc='file containing info pertaining to intensity mapping')
912+
out_intensitymap_file = traits.List(
913+
File, minlen=2, maxlen=2,
914+
desc='files containing info pertaining to intensity mapping')
912915
log_file = File(desc='Name of log-file')
913916

914917

@@ -975,9 +978,23 @@ def _list_outputs(self):
975978
change_ext=change_ext)
976979
else:
977980
outputs[key] = os.path.abspath(inval)
981+
982+
if key == 'out_intensitymap_file' and isdefined(outputs[key]):
983+
basename = FNIRT.intensitymap_file_basename(outputs[key])
984+
outputs[key] = [
985+
outputs[key],
986+
'%s.txt' % basename,
987+
]
978988
return outputs
979989

980990
def _format_arg(self, name, spec, value):
991+
if name in ('in_intensitymap_file', 'out_intensitymap_file'):
992+
if name == 'out_intensitymap_file':
993+
value = self._list_outputs()[name]
994+
value = map(FNIRT.intensitymap_file_basename, value)
995+
assert len(set(value)) == 1, (
996+
'Found different basenames for {}: {}'.format(name, value))
997+
return spec.argstr % value[0]
981998
if name in list(self.filemap.keys()):
982999
return spec.argstr % self._list_outputs()[name]
9831000
return super(FNIRT, self)._format_arg(name, spec, value)
@@ -1005,6 +1022,17 @@ def write_config(self, configfile):
10051022
fid.write('%s\n' % (item))
10061023
fid.close()
10071024

1025+
@classmethod
1026+
def intensitymap_file_basename(cls, f):
1027+
"""Removes valid intensitymap extensions from `f`, returning a basename
1028+
that can refer to both intensitymap files.
1029+
"""
1030+
for ext in Info.ftypes.values() + ['.txt']:
1031+
if f.endswith(ext):
1032+
return f[:-len(ext)]
1033+
# TODO consider warning for this case
1034+
return f
1035+
10081036

10091037
class ApplyWarpInputSpec(FSLCommandInputSpec):
10101038
in_file = File(exists=True, argstr='--in=%s',

nipype/interfaces/fsl/tests/test_preprocess.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def test_mcflirt(setup_flirt):
353353
def test_fnirt(setup_flirt):
354354

355355
tmpdir, infile, reffile = setup_flirt
356+
os.chdir(tmpdir)
356357
fnirt = fsl.FNIRT()
357358
assert fnirt.cmd == 'fnirt'
358359

@@ -404,60 +405,66 @@ def test_fnirt(setup_flirt):
404405
fnirt.run()
405406
fnirt.inputs.in_file = infile
406407
fnirt.inputs.ref_file = reffile
408+
infile_basename = fsl.FNIRT.intensitymap_file_basename(infile)
407409

408410
# test files
409-
opt_map = {
410-
'affine_file': ('--aff='),
411-
'inwarp_file': ('--inwarp='),
412-
'in_intensitymap_file': ('--intin='),
413-
'config_file': ('--config='),
414-
'refmask_file': ('--refmask='),
415-
'inmask_file': ('--inmask='),
416-
'field_file': ('--fout='),
417-
'jacobian_file': ('--jout='),
418-
'modulatedref_file': ('--refout='),
419-
'out_intensitymap_file': ('--intout='),
420-
'log_file': ('--logout=')}
421-
422-
for name, settings in list(opt_map.items()):
411+
opt_map = [
412+
('affine_file', '--aff=%s' % infile, infile),
413+
('inwarp_file', '--inwarp=%s' % infile, infile),
414+
('in_intensitymap_file', '--intin=%s' % infile_basename, [infile]),
415+
('in_intensitymap_file',
416+
'--intin=%s' % infile_basename,
417+
[infile, '%s.txt' % infile_basename]),
418+
('config_file', '--config=%s' % infile, infile),
419+
('refmask_file', '--refmask=%s' % infile, infile),
420+
('inmask_file', '--inmask=%s' % infile, infile),
421+
('field_file', '--fout=%s' % infile, infile),
422+
('jacobian_file', '--jout=%s' % infile, infile),
423+
('modulatedref_file', '--refout=%s' % infile, infile),
424+
('out_intensitymap_file',
425+
'--intout=%s_intmap' % infile_basename, True),
426+
('out_intensitymap_file', '--intout=%s' % infile_basename, infile),
427+
('log_file', '--logout=%s' % infile, infile)]
428+
429+
for (name, settings, arg) in opt_map:
423430
fnirt = fsl.FNIRT(in_file=infile,
424431
ref_file=reffile,
425-
**{name: infile})
432+
**{name: arg})
426433

427434
if name in ('config_file', 'affine_file', 'field_file'):
428-
cmd = 'fnirt %s%s --in=%s '\
435+
cmd = 'fnirt %s --in=%s '\
429436
'--logout=%s '\
430-
'--ref=%s --iout=%s' % (settings, infile, infile, log,
437+
'--ref=%s --iout=%s' % (settings, infile, log,
431438
reffile, iout)
432439
elif name in ('refmask_file'):
433440
cmd = 'fnirt --in=%s '\
434441
'--logout=%s --ref=%s '\
435-
'%s%s '\
442+
'%s '\
436443
'--iout=%s' % (infile, log,
437444
reffile,
438-
settings, infile,
445+
settings,
439446
iout)
440447
elif name in ('in_intensitymap_file', 'inwarp_file', 'inmask_file', 'jacobian_file'):
441448
cmd = 'fnirt --in=%s '\
442-
'%s%s '\
449+
'%s '\
443450
'--logout=%s --ref=%s '\
444451
'--iout=%s' % (infile,
445-
settings, infile,
452+
settings,
446453
log,
447454
reffile,
448455
iout)
449456
elif name in ('log_file'):
450457
cmd = 'fnirt --in=%s '\
451-
'%s%s --ref=%s '\
458+
'%s --ref=%s '\
452459
'--iout=%s' % (infile,
453-
settings, infile,
460+
settings,
454461
reffile,
455462
iout)
456463
else:
457464
cmd = 'fnirt --in=%s '\
458-
'--logout=%s %s%s '\
465+
'--logout=%s %s '\
459466
'--ref=%s --iout=%s' % (infile, log,
460-
settings, infile,
467+
settings,
461468
reffile, iout)
462469

463470
assert fnirt.cmdline == cmd

0 commit comments

Comments
 (0)