4
4
"""Provide interface to AFNI commands."""
5
5
from __future__ import (print_function , division , unicode_literals ,
6
6
absolute_import )
7
- from builtins import object , str
8
7
from future .utils import raise_from
9
8
10
9
import os
11
10
from sys import platform
12
11
from distutils import spawn
13
12
14
13
from ... import logging , LooseVersion
15
- from ...utils .filemanip import split_filename , fname_presuffix
16
-
14
+ from ...utils .filemanip import split_filename
17
15
from ..base import (CommandLine , traits , CommandLineInputSpec , isdefined , File ,
18
16
TraitedSpec , PackageInfo )
19
17
from ...external .due import BibTeX
23
21
24
22
25
23
class Info (PackageInfo ):
26
- """Handle afni output type and version information.
27
- """
24
+ """Handle afni output type and version information."""
25
+
28
26
__outputtype = 'AFNI'
29
27
ftypes = {'NIFTI' : '.nii' , 'AFNI' : '' , 'NIFTI_GZ' : '.nii.gz' }
30
28
version_cmd = 'afni --version'
31
29
32
30
@staticmethod
33
31
def parse_version (raw_info ):
32
+ """Check and parse AFNI's version."""
34
33
version_stamp = raw_info .split ('\n ' )[0 ].split ('Version ' )[1 ]
35
34
if version_stamp .startswith ('AFNI' ):
36
35
version_stamp = version_stamp .split ('AFNI_' )[1 ]
@@ -46,7 +45,8 @@ def parse_version(raw_info):
46
45
47
46
@classmethod
48
47
def output_type_to_ext (cls , outputtype ):
49
- """Get the file extension for the given output type.
48
+ """
49
+ Get the file extension for the given output type.
50
50
51
51
Parameters
52
52
----------
@@ -57,8 +57,8 @@ def output_type_to_ext(cls, outputtype):
57
57
-------
58
58
extension : str
59
59
The file extension for the output type.
60
- """
61
60
61
+ """
62
62
try :
63
63
return cls .ftypes [outputtype ]
64
64
except KeyError as e :
@@ -67,24 +67,28 @@ def output_type_to_ext(cls, outputtype):
67
67
68
68
@classmethod
69
69
def outputtype (cls ):
70
- """AFNI has no environment variables,
71
- Output filetypes get set in command line calls
72
- Nipype uses AFNI as default
70
+ """
71
+ Set default output filetype.
72
+
73
+ AFNI has no environment variables, Output filetypes get set in command line calls
74
+ Nipype uses ``AFNI`` as default
73
75
74
76
75
77
Returns
76
78
-------
77
79
None
80
+
78
81
"""
79
- # warn(('AFNI has no environment variable that sets filetype '
80
- # 'Nipype uses NIFTI_GZ as default'))
81
82
return 'AFNI'
82
83
83
84
@staticmethod
84
85
def standard_image (img_name ):
85
- '''Grab an image from the standard location.
86
+ """
87
+ Grab an image from the standard location.
86
88
87
- Could be made more fancy to allow for more relocatability'''
89
+ Could be made more fancy to allow for more relocatability
90
+
91
+ """
88
92
clout = CommandLine (
89
93
'which afni' ,
90
94
ignore_exception = True ,
@@ -101,6 +105,7 @@ def standard_image(img_name):
101
105
class AFNICommandBase (CommandLine ):
102
106
"""
103
107
A base class to fix a linking problem in OSX and afni.
108
+
104
109
See http://afni.nimh.nih.gov/afni/community/board/read.php?1,145346,145347#msg-145347
105
110
"""
106
111
@@ -127,7 +132,8 @@ class AFNICommandOutputSpec(TraitedSpec):
127
132
128
133
129
134
class AFNICommand (AFNICommandBase ):
130
- """Shared options for several AFNI commands """
135
+ """Shared options for several AFNI commands."""
136
+
131
137
input_spec = AFNICommandInputSpec
132
138
_outputtype = None
133
139
@@ -162,6 +168,7 @@ class AFNICommand(AFNICommandBase):
162
168
163
169
@property
164
170
def num_threads (self ):
171
+ """Get number of threads."""
165
172
return self .inputs .num_threads
166
173
167
174
@num_threads .setter
@@ -170,20 +177,21 @@ def num_threads(self, value):
170
177
171
178
@classmethod
172
179
def set_default_output_type (cls , outputtype ):
173
- """Set the default output type for AFNI classes.
180
+ """
181
+ Set the default output type for AFNI classes.
174
182
175
183
This method is used to set the default output type for all afni
176
184
subclasses. However, setting this will not update the output
177
185
type for any existing instances. For these, assign the
178
186
<instance>.inputs.outputtype.
179
187
"""
180
-
181
188
if outputtype in Info .ftypes :
182
189
cls ._outputtype = outputtype
183
190
else :
184
191
raise AttributeError ('Invalid AFNI outputtype: %s' % outputtype )
185
192
186
193
def __init__ (self , ** inputs ):
194
+ """Instantiate an AFNI command tool wrapper."""
187
195
super (AFNICommand , self ).__init__ (** inputs )
188
196
self .inputs .on_trait_change (self ._output_update , 'outputtype' )
189
197
@@ -199,13 +207,16 @@ def __init__(self, **inputs):
199
207
self ._output_update ()
200
208
201
209
def _nthreads_update (self ):
202
- """Update environment with new number of threads"""
210
+ """Update environment with new number of threads. """
203
211
self .inputs .environ ['OMP_NUM_THREADS' ] = '%d' % self .inputs .num_threads
204
212
205
213
def _output_update (self ):
206
- """ i think? updates class private attribute based on instance input
207
- in fsl also updates ENVIRON variable....not valid in afni
208
- as it uses no environment variables
214
+ """
215
+ Update the internal property with the provided input.
216
+
217
+ i think? updates class private attribute based on instance input
218
+ in fsl also updates ENVIRON variable....not valid in afni
219
+ as it uses no environment variables
209
220
"""
210
221
self ._outputtype = self .inputs .outputtype
211
222
@@ -226,59 +237,9 @@ def _list_outputs(self):
226
237
outputs [name ] = outputs [name ] + "+orig.BRIK"
227
238
return outputs
228
239
229
- def _gen_fname (self ,
230
- basename ,
231
- cwd = None ,
232
- suffix = None ,
233
- change_ext = True ,
234
- ext = None ):
235
- """Generate a filename based on the given parameters.
236
-
237
- The filename will take the form: cwd/basename<suffix><ext>.
238
- If change_ext is True, it will use the extentions specified in
239
- <instance>intputs.output_type.
240
-
241
- Parameters
242
- ----------
243
- basename : str
244
- Filename to base the new filename on.
245
- cwd : str
246
- Path to prefix to the new filename. (default is os.getcwd())
247
- suffix : str
248
- Suffix to add to the `basename`. (defaults is '' )
249
- change_ext : bool
250
- Flag to change the filename extension to the FSL output type.
251
- (default True)
252
-
253
- Returns
254
- -------
255
- fname : str
256
- New filename based on given parameters.
257
-
258
- """
259
-
260
- if basename == '' :
261
- msg = 'Unable to generate filename for command %s. ' % self .cmd
262
- msg += 'basename is not set!'
263
- raise ValueError (msg )
264
- if cwd is None :
265
- cwd = os .getcwd ()
266
- if ext is None :
267
- ext = Info .output_type_to_ext (self .inputs .outputtype )
268
- if change_ext :
269
- if suffix :
270
- suffix = '' .join ((suffix , ext ))
271
- else :
272
- suffix = ext
273
- if suffix is None :
274
- suffix = ''
275
- fname = fname_presuffix (
276
- basename , suffix = suffix , use_ext = False , newpath = cwd )
277
- return fname
278
-
279
240
280
241
def no_afni ():
281
- """ Checks if AFNI is available """
242
+ """Check whether AFNI is not available. """
282
243
if Info .version () is None :
283
244
return True
284
245
return False
@@ -292,8 +253,11 @@ class AFNIPythonCommandInputSpec(CommandLineInputSpec):
292
253
293
254
294
255
class AFNIPythonCommand (AFNICommand ):
256
+ """A subtype of AFNI command line for Python scripts."""
257
+
295
258
@property
296
259
def cmd (self ):
260
+ """Revise the command path."""
297
261
orig_cmd = super (AFNIPythonCommand , self ).cmd
298
262
found = spawn .find_executable (orig_cmd )
299
263
return found if found is not None else orig_cmd
0 commit comments