6
6
import os
7
7
import warnings
8
8
9
- from ...utils .filemanip import fname_presuffix
10
- from ..base import (CommandLine , traits , CommandLineInputSpec , isdefined )
9
+ from ...utils .filemanip import fname_presuffix , split_filename
10
+ from ..base import (
11
+ CommandLine , traits , CommandLineInputSpec , isdefined , File , TraitedSpec )
11
12
12
13
warn = warnings .warn
13
14
warnings .filterwarnings ('always' , category = UserWarning )
18
19
#
19
20
###################################
20
21
22
+
21
23
class Info (object ):
22
24
"""Handle afni output type and version information.
23
25
"""
@@ -80,7 +82,6 @@ def outputtype(cls):
80
82
# 'Nipype uses NIFTI_GZ as default'))
81
83
return 'AFNI'
82
84
83
-
84
85
@staticmethod
85
86
def standard_image (img_name ):
86
87
'''Grab an image from the standard location.
@@ -95,23 +96,27 @@ def standard_image(img_name):
95
96
return os .path .join (basedir , img_name )
96
97
97
98
98
- class AFNITraitedSpec (CommandLineInputSpec ):
99
- outputtype = traits .Enum ('AFNI' , Info .ftypes .keys (),
100
- desc = 'AFNI output filetype' )
99
+ class AFNIBaseCommandInputSpec (CommandLineInputSpec ):
100
+ outputtype = traits .Enum ('AFNI' , Info .ftypes .keys (),
101
+ desc = 'AFNI output filetype' )
102
+
103
+ class AFNITraitedSpec (AFNIBaseCommandInputSpec ):
104
+ pass
101
105
102
106
103
- class AFNICommand (CommandLine ):
107
+ class AFNIBaseCommand (CommandLine ):
104
108
"""General support for AFNI commands. Every AFNI command accepts 'outputtype' input. For example:
105
109
afni.Threedsetup(outputtype='NIFTI_GZ')
106
110
"""
107
111
108
- input_spec = AFNITraitedSpec
112
+ input_spec = AFNIBaseCommandInputSpec
109
113
_outputtype = None
114
+
110
115
111
116
def __init__ (self , ** inputs ):
112
- super (AFNICommand , self ).__init__ (** inputs )
117
+ super (AFNIBaseCommand , self ).__init__ (** inputs )
113
118
self .inputs .on_trait_change (self ._output_update , 'outputtype' )
114
-
119
+
115
120
if self ._outputtype is None :
116
121
self ._outputtype = Info .outputtype ()
117
122
@@ -127,7 +132,6 @@ def _output_update(self):
127
132
"""
128
133
self ._outputtype = self .inputs .outputtype
129
134
130
-
131
135
@classmethod
132
136
def set_default_outputtype (cls , outputtype ):
133
137
"""Set the default output type for AFNI classes.
@@ -143,7 +147,7 @@ def set_default_outputtype(cls, outputtype):
143
147
else :
144
148
raise AttributeError ('Invalid AFNI outputtype: %s' % outputtype )
145
149
146
- def _gen_fname (self , basename , cwd = None , suffix = '_afni' , change_ext = True ):
150
+ def _gen_fname (self , basename , cwd = None , suffix = '_afni' , change_ext = True , prefix = '' ):
147
151
"""Generate a filename based on the given parameters.
148
152
149
153
The filename will take the form: cwd/basename<suffix><ext>.
@@ -181,9 +185,54 @@ def _gen_fname(self, basename, cwd=None, suffix='_afni', change_ext=True):
181
185
suffix = '' .join ((suffix , ext ))
182
186
else :
183
187
suffix = ext
184
- fname = fname_presuffix (basename , suffix = suffix ,
185
- use_ext = False , newpath = cwd )
188
+ fname = fname_presuffix (basename , suffix = suffix ,
189
+ use_ext = False , newpath = cwd , prefix = prefix )
186
190
return fname
187
191
188
192
193
+ class AFNICommandInputSpec (AFNIBaseCommandInputSpec ):
194
+ out_file = File ("%s_afni" , desc = 'output image file name' ,
195
+ argstr = '-prefix %s' , xor = ['out_file' , 'prefix' , 'suffix' ], name_source = "in_file" , usedefault = True )
196
+ prefix = traits .Str (
197
+ desc = 'output image prefix' , deprecated = '0.8' , new_name = "out_file" )
198
+ suffix = traits .Str (
199
+ desc = 'output image suffix' , deprecated = '0.8' , new_name = "out_file" )
189
200
201
+
202
+ class AFNICommand (AFNIBaseCommand ):
203
+ input_spec = AFNICommandInputSpec
204
+
205
+ def _gen_filename (self , name ):
206
+ trait_spec = self .inputs .trait (name )
207
+ if name == "out_file" and (isdefined (self .inputs .prefix ) or isdefined (self .inputs .suffix )):
208
+ suffix = ''
209
+ prefix = ''
210
+ if isdefined (self .inputs .prefix ):
211
+ prefix = self .inputs .prefix
212
+ if isdefined (self .inputs .suffix ):
213
+ suffix = self .inputs .suffix
214
+
215
+ _ , base , _ = split_filename (
216
+ getattr (self .inputs , trait_spec .name_source ))
217
+ return self ._gen_fname (basename = base , prefix = prefix , suffix = suffix , cwd = '' )
218
+ else :
219
+ return super (AFNICommand , self )._gen_filename (name )
220
+
221
+ def _overload_extension (self , value ):
222
+ path , base , _ = split_filename (value )
223
+ return os .path .join (path , base + Info .outputtype_to_ext (self .inputs .outputtype ))
224
+
225
+ def _list_outputs (self ):
226
+ metadata = dict (name_source = lambda t : t is not None )
227
+ out_names = self .inputs .traits (** metadata ).keys ()
228
+ if out_names :
229
+ outputs = self .output_spec ().get ()
230
+ for name in out_names :
231
+ out = self ._gen_filename (name )
232
+ outputs [name ] = os .path .abspath (out )
233
+ return outputs
234
+
235
+
236
+ class AFNICommandOutputSpec (TraitedSpec ):
237
+ out_file = File (desc = 'output file' ,
238
+ exists = True )
0 commit comments