Skip to content

Commit cf3fd76

Browse files
committed
Merge pull request #574 from satra/fix/name_source
Fix/name source fix for #573
2 parents 97f3050 + b1bd43c commit cf3fd76

File tree

4 files changed

+45
-97
lines changed

4 files changed

+45
-97
lines changed

nipype/interfaces/afni/base.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -194,50 +194,17 @@ def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True, prefix
194194

195195
class AFNICommandInputSpec(AFNIBaseCommandInputSpec):
196196
out_file = File("%s_afni", desc='output image file name',
197-
argstr='-prefix %s', xor=['out_file', 'prefix', 'suffix'], name_source="in_file", usedefault=True)
198-
prefix = traits.Str(
199-
desc='output image prefix', deprecated='0.8', new_name="out_file")
200-
suffix = traits.Str(
201-
desc='output image suffix', deprecated='0.8', new_name="out_file")
197+
argstr='-prefix %s', xor=['out_file', 'prefix', 'suffix'],
198+
name_source="in_file", usedefault=True)
202199

203200

204201
class AFNICommand(AFNIBaseCommand):
205202
input_spec = AFNICommandInputSpec
206203

207-
def _gen_filename(self, name):
208-
trait_spec = self.inputs.trait(name)
209-
if name == "out_file" and (isdefined(self.inputs.prefix) or isdefined(self.inputs.suffix)):
210-
suffix = ''
211-
prefix = ''
212-
if isdefined(self.inputs.prefix):
213-
prefix = self.inputs.prefix
214-
if isdefined(self.inputs.suffix):
215-
suffix = self.inputs.suffix
216-
217-
_, base, _ = split_filename(
218-
getattr(self.inputs, trait_spec.name_source))
219-
return self._gen_fname(basename=base, prefix=prefix, suffix=suffix, cwd=os.getcwd())
220-
221-
else:
222-
if getattr(self.inputs, name):
223-
return os.path.join(
224-
os.getcwd(),super(AFNICommand, self)._gen_filename(name))
225-
226204
def _overload_extension(self, value):
227205
path, base, _ = split_filename(value)
228206
return os.path.join(path, base + Info.outputtype_to_ext(self.inputs.outputtype))
229207

230-
def _list_outputs(self):
231-
metadata = dict(name_source=lambda t: t is not None)
232-
out_names = self.inputs.traits(**metadata).keys()
233-
if out_names:
234-
outputs = self.output_spec().get()
235-
for name in out_names:
236-
out = self._gen_filename(name)
237-
if out and isdefined(out):
238-
outputs[name] = os.path.abspath(out)
239-
return outputs
240-
241208

242209
class AFNICommandOutputSpec(TraitedSpec):
243210
out_file = File(desc='output file',

nipype/interfaces/afni/preprocess.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class To3D(AFNICommand):
8181
>>> To3D.inputs.filetype = "anat"
8282
>>> To3D.inputs.outputtype = "NIFTI"
8383
>>> To3D.cmdline #doctest: +ELLIPSIS
84-
'to3d -datum float -anat -prefix .../dicomdir.nii ./*.dcm'
84+
'to3d -datum float -anat -prefix dicomdir.nii ./*.dcm'
8585
>>> res = To3D.run() #doctest: +SKIP
8686
8787
"""
@@ -361,7 +361,7 @@ class AutoTcorrelate(AFNICommand):
361361
>>> corr.inputs.mask = 'mask.nii'
362362
>>> corr.inputs.mask_only_targets = True
363363
>>> corr.cmdline # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
364-
'3dAutoTcorrelate -eta2 -mask mask.nii -mask_only_targets -prefix ...my_similarity_matrix.1D -polort -1 functional.nii'
364+
'3dAutoTcorrelate -eta2 -mask mask.nii -mask_only_targets -prefix my_similarity_matrix.1D -polort -1 functional.nii'
365365
>>> res = corr.run() # doctest: +SKIP
366366
"""
367367
input_spec = AutoTcorrelateInputSpec
@@ -374,9 +374,6 @@ def _overload_extension(self, value):
374374
ext = ext + ".1D"
375375
return os.path.join(path, base + ext)
376376

377-
def _gen_filename(self, name):
378-
return os.path.abspath(super(AutoTcorrelate, self)._gen_filename(name))
379-
380377

381378
class TStatInputSpec(AFNICommandInputSpec):
382379
in_file = File(desc='input file to 3dTstat',
@@ -1163,6 +1160,7 @@ class MaskaveInputSpec(AFNICommandInputSpec):
11631160
mandatory=True,
11641161
exists=True)
11651162
out_file = File("%s_maskave.1D", desc='output image file name',
1163+
keep_extension=True,
11661164
argstr="> %s", name_source="in_file", usedefault=True, position=-1)
11671165
mask = File(desc='matrix to align input file',
11681166
argstr='-mask %s',
@@ -1558,7 +1556,7 @@ class Calc(AFNICommand):
15581556
>>> calc.inputs.out_file = 'functional_calc.nii.gz'
15591557
>>> calc.inputs.outputtype = "NIFTI"
15601558
>>> calc.cmdline #doctest: +ELLIPSIS
1561-
'3dcalc -a functional.nii -b functional2.nii -expr "a*b" -prefix .../functional_calc.nii'
1559+
'3dcalc -a functional.nii -b functional2.nii -expr "a*b" -prefix functional_calc.nii'
15621560
15631561
"""
15641562

@@ -1591,13 +1589,9 @@ class BlurInMaskInputSpec(AFNICommandInputSpec):
15911589
position=1,
15921590
mandatory=True,
15931591
exists=True)
1594-
out_file = File(
1595-
'%s_blur',
1596-
desc='output to the file',
1597-
argstr='-prefix %s',
1598-
name_source='in_file',
1599-
position=-1,
1600-
genfile=True)
1592+
out_file = File('%s_blur', desc='output to the file', argstr='-prefix %s',
1593+
name_source='in_file', position=-1, genfile=True,
1594+
usedefault=True)
16011595
mask = File(
16021596
desc='Mask dataset, if desired. Blurring will occur only within the mask. Voxels NOT in the mask will be set to zero in the output.',
16031597
argstr='-mask %s')
@@ -1635,6 +1629,8 @@ class BlurInMask(AFNICommand):
16351629
>>> bim.inputs.in_file = 'functional.nii'
16361630
>>> bim.inputs.mask = 'mask.nii'
16371631
>>> bim.inputs.fwhm = 5.0
1632+
>>> bim.cmdline #doctest: +ELLIPSIS
1633+
'3dBlurInMask -input functional.nii -FWHM 5.000000 -mask mask.nii -prefix .../functional_blur+orig.BRIK'
16381634
>>> res = bim.run() # doctest: +SKIP
16391635
16401636
"""
@@ -1643,19 +1639,6 @@ class BlurInMask(AFNICommand):
16431639
input_spec = BlurInMaskInputSpec
16441640
output_spec = AFNICommandOutputSpec
16451641

1646-
def _list_outputs(self):
1647-
outputs = self.output_spec().get()
1648-
if not isdefined(self.inputs.out_file):
1649-
outputs['out_file'] = self._gen_fname(self.inputs.in_file,
1650-
suffix=self.inputs.suffix)
1651-
else:
1652-
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
1653-
return outputs
1654-
1655-
def _gen_filename(self, name):
1656-
if name == 'out_file':
1657-
return self._list_outputs()[name]
1658-
16591642

16601643
class TCorrMapInputSpec(AFNIBaseCommandInputSpec):
16611644
in_file = File(exists=True, argstr='-input %s', mandatory=True)

nipype/interfaces/base.py

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def help(cls, returnhelp=False):
694694
if returnhelp:
695695
return allhelp
696696
else:
697-
print allhelp
697+
print(allhelp)
698698

699699
@classmethod
700700
def _get_trait_desc(self, inputs, name, spec):
@@ -1218,7 +1218,7 @@ def help(cls, returnhelp=False):
12181218
if returnhelp:
12191219
return allhelp
12201220
else:
1221-
print allhelp
1221+
print(allhelp)
12221222

12231223
def _get_environ(self):
12241224
out_environ = {}
@@ -1326,49 +1326,45 @@ def _format_arg(self, name, trait_spec, value):
13261326
return sep.join([argstr % elt for elt in value])
13271327
else:
13281328
return argstr % sep.join(str(elt) for elt in value)
1329-
elif trait_spec.name_source:
1330-
return argstr % self._gen_filename(name)
13311329
else:
13321330
# Append options using format string.
13331331
return argstr % value
1334-
1335-
def _gen_filename(self, name):
1336-
trait_spec = self.inputs.trait(name)
1337-
value = getattr(self.inputs, name)
1338-
if isdefined(value):
1339-
if "%s" in value:
1340-
if isinstance(trait_spec.name_source, list):
1341-
for ns in trait_spec.name_source:
1342-
if isdefined(getattr(self.inputs, ns)):
1343-
name_source = ns
1344-
break
1345-
else:
1346-
name_source = trait_spec.name_source
1347-
if name_source.endswith(os.path.sep):
1348-
name_source = name_source[:-len(os.path.sep)]
1349-
_, base, _ = split_filename(getattr(self.inputs, name_source))
1350-
1351-
retval = value%base
1332+
1333+
def _filename_from_source(self, name):
1334+
trait_spec = self.inputs.trait(name)
1335+
retval = getattr(self.inputs, name)
1336+
if isdefined(retval):
1337+
if "%s" in retval:
1338+
if isinstance(trait_spec.name_source, list):
1339+
for ns in trait_spec.name_source:
1340+
if isdefined(getattr(self.inputs, ns)):
1341+
name_source = ns
1342+
break
13521343
else:
1353-
retval = value
1354-
else:
1355-
raise NotImplementedError
1356-
_,_,ext = split_filename(retval)
1357-
if trait_spec.overload_extension or not ext:
1358-
return self._overload_extension(retval)
1359-
else:
1344+
name_source = trait_spec.name_source
1345+
if name_source.endswith(os.path.sep):
1346+
name_source = name_source[:-len(os.path.sep)]
1347+
_, base, _ = split_filename(getattr(self.inputs, name_source))
1348+
retval = os.path.abspath(retval % base)
1349+
_, _, ext = split_filename(retval)
1350+
if trait_spec.keep_extension and ext:
13601351
return retval
1361-
1352+
return self._overload_extension(retval)
1353+
return retval
1354+
1355+
def _gen_filename(self, name):
1356+
raise NotImplementedError
1357+
13621358
def _overload_extension(self, value):
13631359
return value
1364-
1360+
13651361
def _list_outputs(self):
13661362
metadata = dict(name_source=lambda t: t is not None)
13671363
out_names = self.inputs.traits(**metadata).keys()
13681364
if out_names:
13691365
outputs = self.output_spec().get()
13701366
for name in out_names:
1371-
outputs[name] = os.path.abspath(self._gen_filename(name))
1367+
outputs[name] = os.path.abspath(self._filename_from_source(name))
13721368
return outputs
13731369

13741370
def _parse_inputs(self, skip=None):
@@ -1391,11 +1387,12 @@ def _parse_inputs(self, skip=None):
13911387
if skip and name in skip:
13921388
continue
13931389
value = getattr(self.inputs, name)
1394-
if not isdefined(value):
1395-
if spec.genfile or spec.name_source:
1390+
if spec.genfile or spec.name_source:
1391+
value = self._filename_from_source(name)
1392+
if not isdefined(value):
13961393
value = self._gen_filename(name)
1397-
else:
1398-
continue
1394+
if not isdefined(value):
1395+
continue
13991396
arg = self._format_arg(name, spec, value)
14001397
if arg is None:
14011398
continue

nipype/interfaces/freesurfer/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class MRISPreproc(FSCommand):
100100
def _list_outputs(self):
101101
outputs = self.output_spec().get()
102102
outfile = self.inputs.out_file
103+
outputs['out_file'] = outfile
103104
if not isdefined(outfile):
104105
outputs['out_file'] = os.path.join(os.getcwd(),
105106
'concat_%s_%s.mgz' % (self.inputs.hemi,

0 commit comments

Comments
 (0)