Skip to content

Commit d8f6c52

Browse files
committed
Merge pull request #693 from chrisfilo/enh/afni_refactor
Enh/afni refactor
2 parents a95d2db + 75b7024 commit d8f6c52

File tree

4 files changed

+120
-186
lines changed

4 files changed

+120
-186
lines changed

CHANGES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Next release
55
* ENH: New interfaces: spm.ResliceToReference, FuzzyOverlap, afni.AFNItoNIFTI
66
spm.DicomImport, P2PDistance
77
* ENH: W3C PROV support with optional RDF export built into Nipype
8-
* ENH: Added support for Simple Linux Utility Resource Management (SLURM)
8+
* ENH: Added support for Simple Linux Utility Resource Management (SLURM)
9+
* ENHL AFNI interfaces refactor, prefix, suffix are replaced by "flexible_%s_templates"
910

1011
* ENH: Several new interfaces related to Camino were added:
1112
- camino.SFPICOCalibData

nipype/interfaces/afni/base.py

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@
66
import os
77
import warnings
88

9-
from ...utils.filemanip import fname_presuffix, split_filename
9+
from ...utils.filemanip import split_filename
1010
from ..base import (
1111
CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec)
1212

1313
warn = warnings.warn
1414
warnings.filterwarnings('always', category=UserWarning)
1515

16-
###################################
17-
#
18-
# NEW_AFNI base class
19-
#
20-
###################################
21-
2216

2317
class Info(object):
2418
"""Handle afni output type and version information.
2519
"""
2620
__outputtype = 'AFNI'
2721
ftypes = {'NIFTI': '.nii',
28-
'AFNI': '+orig.BRIK',
22+
'AFNI': '',
2923
'NIFTI_GZ': '.nii.gz'}
3024

3125
@staticmethod
@@ -98,25 +92,26 @@ def standard_image(img_name):
9892
return os.path.join(basedir, img_name)
9993

10094

101-
class AFNIBaseCommandInputSpec(CommandLineInputSpec):
95+
class AFNICommandInputSpec(CommandLineInputSpec):
10296
outputtype = traits.Enum('AFNI', Info.ftypes.keys(),
10397
desc='AFNI output filetype')
98+
out_file = File(name_template="%s_afni", desc='output image file name',
99+
argstr='-prefix %s',
100+
name_source=["in_file"])
101+
102+
class AFNICommandOutputSpec(TraitedSpec):
103+
out_file = File(desc='output file',
104+
exists=True)
104105

105-
class AFNITraitedSpec(AFNIBaseCommandInputSpec):
106-
pass
107-
108-
109-
class AFNIBaseCommand(CommandLine):
110-
"""General support for AFNI commands. Every AFNI command accepts 'outputtype' input. For example:
111-
afni.Threedsetup(outputtype='NIFTI_GZ')
112-
"""
113106

114-
input_spec = AFNIBaseCommandInputSpec
107+
class AFNICommand(CommandLine):
108+
109+
input_spec = AFNICommandInputSpec
115110
_outputtype = None
116111

117112

118113
def __init__(self, **inputs):
119-
super(AFNIBaseCommand, self).__init__(**inputs)
114+
super(AFNICommand, self).__init__(**inputs)
120115
self.inputs.on_trait_change(self._output_update, 'outputtype')
121116

122117
if self._outputtype is None:
@@ -148,64 +143,19 @@ def set_default_output_type(cls, outputtype):
148143
cls._outputtype = outputtype
149144
else:
150145
raise AttributeError('Invalid AFNI outputtype: %s' % outputtype)
151-
152-
def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True, prefix=''):
153-
"""Generate a filename based on the given parameters.
154-
155-
The filename will take the form: cwd/basename<suffix><ext>.
156-
If change_ext is True, it will use the extensions specified in
157-
<instance>inputs.outputtype.
158-
159-
Parameters
160-
----------
161-
basename : str
162-
Filename to base the new filename on.
163-
cwd : str
164-
Path to prefix to the new filename. (default is os.getcwd())
165-
suffix : str
166-
Suffix to add to the `basename`. (default is '_fsl')
167-
change_ext : bool
168-
Flag to change the filename extension to the FSL output type.
169-
(default True)
170-
171-
Returns
172-
-------
173-
fname : str
174-
New filename based on given parameters.
175-
176-
"""
177-
178-
if basename == '':
179-
msg = 'Unable to generate filename for command %s. ' % self.cmd
180-
msg += 'basename is not set!'
181-
raise ValueError(msg)
182-
if cwd is None:
183-
cwd = os.getcwd()
184-
ext = Info.outputtype_to_ext(self.inputs.outputtype)
185-
if change_ext:
186-
if suffix:
187-
suffix = ''.join((suffix, ext))
188-
else:
189-
suffix = ext
190-
fname = fname_presuffix(basename, suffix=suffix,
191-
use_ext=False, newpath=cwd, prefix=prefix)
192-
return fname
193-
194-
195-
class AFNICommandInputSpec(AFNIBaseCommandInputSpec):
196-
out_file = File("%s_afni", desc='output image file name',
197-
argstr='-prefix %s', xor=['out_file', 'prefix', 'suffix'],
198-
name_source="in_file", usedefault=True)
199-
200-
201-
class AFNICommand(AFNIBaseCommand):
202-
input_spec = AFNICommandInputSpec
203-
146+
204147
def _overload_extension(self, value):
205148
path, base, _ = split_filename(value)
206149
return os.path.join(path, base + Info.outputtype_to_ext(self.inputs.outputtype))
207150

208-
209-
class AFNICommandOutputSpec(TraitedSpec):
210-
out_file = File(desc='output file',
211-
exists=True)
151+
def _list_outputs(self):
152+
outputs = super(AFNICommand, self)._list_outputs()
153+
metadata = dict(name_source=lambda t: t is not None)
154+
out_names = self.inputs.traits(**metadata).keys()
155+
if out_names:
156+
for name in out_names:
157+
if outputs[name]:
158+
_,_,ext = split_filename(outputs[name])
159+
if ext == "":
160+
outputs[name] = outputs[name] + "+orig.BRIK"
161+
return outputs

0 commit comments

Comments
 (0)