1
1
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2
2
# vi: set ft=python sts=4 ts=4 sw=4 et:
3
- """The spm module provides basic functions for interfacing with SPM tools."""
3
+ """The spm module provides basic functions for interfacing with SPM tools.
4
+
5
+ In order to use the standalone MCR version of spm, you need to ensure that
6
+ the following commands are executed at the beginning of your script::
7
+
8
+ from nipype import spm
9
+ matlab_cmd = '/path/to/run_spm8.sh /path/to/Compiler_Runtime/v713/ script'
10
+ spm.SPMCommand.set_mlab_paths(matlab_cmd=matlab_cmd, use_mcr=True)
11
+
12
+ you can test by calling::
13
+
14
+ spm.SPMCommand().version
15
+ """
4
16
5
17
__docformat__ = 'restructuredtext'
6
18
@@ -111,7 +123,7 @@ class Info(object):
111
123
"""Handles SPM version information
112
124
"""
113
125
@staticmethod
114
- def version (matlab_cmd = None ):
126
+ def version (matlab_cmd = None , paths = None , use_mcr = None ):
115
127
"""Returns the path to the SPM directory in the Matlab path
116
128
If path not found, returns None.
117
129
@@ -135,6 +147,15 @@ def version(matlab_cmd=None):
135
147
except :
136
148
matlab_cmd = 'matlab -nodesktop -nosplash'
137
149
mlab = MatlabCommand (matlab_cmd = matlab_cmd )
150
+ mlab .inputs .mfile = False
151
+ if paths :
152
+ mlab .inputs .paths = paths
153
+ if use_mcr :
154
+ mlab .inputs .nodesktop = Undefined
155
+ mlab .inputs .nosplash = Undefined
156
+ mlab .inputs .single_comp_thread = Undefined
157
+ mlab .inputs .mfile = True
158
+ mlab .inputs .uses_mcr = True
138
159
mlab .inputs .script = """
139
160
if isempty(which('spm')),
140
161
throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
@@ -144,7 +165,6 @@ def version(matlab_cmd=None):
144
165
fprintf(1, 'NIPYPE path:%s|name:%s|release:%s', spm_path, name, version);
145
166
exit;
146
167
"""
147
- mlab .inputs .mfile = False
148
168
try :
149
169
out = mlab .run ()
150
170
except (IOError , RuntimeError ), e :
@@ -178,6 +198,9 @@ class SPMCommandInputSpec(BaseInterfaceInputSpec):
178
198
mfile = traits .Bool (True , desc = 'Run m-code using m-file' ,
179
199
usedefault = True )
180
200
use_mcr = traits .Bool (desc = 'Run m-code using SPM MCR' )
201
+ use_v8struct = traits .Bool (True , min_ver = '8' , usedefault = True ,
202
+ desc = ('Generate SPM8 and higher compatible jobs' )
203
+ )
181
204
182
205
183
206
class SPMCommand (BaseInterface ):
@@ -215,17 +238,25 @@ def _matlab_cmd_update(self):
215
238
# and can be set only during init
216
239
self .mlab = MatlabCommand (matlab_cmd = self .inputs .matlab_cmd ,
217
240
mfile = self .inputs .mfile ,
218
- paths = self .inputs .paths ,
219
- uses_mcr = self .inputs .use_mcr )
241
+ paths = self .inputs .paths )
220
242
self .mlab .inputs .script_file = 'pyscript_%s.m' % \
221
243
self .__class__ .__name__ .split ('.' )[- 1 ].lower ()
222
244
if isdefined (self .inputs .use_mcr ) and self .inputs .use_mcr :
223
245
self .mlab .inputs .nodesktop = Undefined
224
246
self .mlab .inputs .nosplash = Undefined
247
+ self .mlab .inputs .single_comp_thread = Undefined
248
+ self .mlab .inputs .uses_mcr = True
249
+ self .mlab .inputs .mfile = True
225
250
226
251
@property
227
252
def version (self ):
228
- return Info .version ()
253
+ version_dict = Info .version (matlab_cmd = self ._matlab_cmd ,
254
+ paths = self ._paths ,
255
+ use_mcr = self ._use_mcr )
256
+ if version_dict :
257
+ return '.' .join ((version_dict ['name' ].split ('SPM' )[- 1 ],
258
+ version_dict ['release' ]))
259
+ return version_dict
229
260
230
261
@property
231
262
def jobtype (self ):
@@ -300,9 +331,10 @@ def _reformat_dict_for_savemat(self, contents):
300
331
301
332
Examples
302
333
--------
303
- >>> a = SPMCommand()._reformat_dict_for_savemat(dict(a=1,b=dict(c=2,d=3)))
304
- >>> print a
305
- [{'a': 1, 'b': [{'c': 2, 'd': 3}]}]
334
+ >>> a = SPMCommand()._reformat_dict_for_savemat(dict(a=1,
335
+ ... b=dict(c=2, d=3)))
336
+ >>> a == [{'a': 1, 'b': [{'c': 2, 'd': 3}]}]
337
+ True
306
338
307
339
"""
308
340
newdict = {}
@@ -401,48 +433,46 @@ def _make_matlab_command(self, contents, postscript=None):
401
433
mscript = """
402
434
%% Generated by nipype.interfaces.spm
403
435
if isempty(which('spm')),
404
- throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
436
+ throw(MException('SPMCheck:NotFound', 'SPM not in matlab path'));
405
437
end
406
- [name, ver ] = spm('ver');
407
- fprintf('SPM version: %s Release: %s\\ n',name, ver );
408
- fprintf('SPM path: %s\\ n',which('spm'));
438
+ [name, version ] = spm('ver');
439
+ fprintf('SPM version: %s Release: %s\\ n',name, version );
440
+ fprintf('SPM path: %s\\ n', which('spm'));
409
441
spm('Defaults','fMRI');
410
442
411
- if strcmp(spm('ver'),'SPM8'), spm_jobman('initcfg');end\n
443
+ if strcmp(name, 'SPM8') || strcmp(name, 'SPM12b'),
444
+ spm_jobman('initcfg');
445
+ spm_get_defaults('CmdLine', 1);
446
+ end\n
412
447
"""
413
448
if self .mlab .inputs .mfile :
414
- if self .jobname in ['st' , 'smooth' , 'preproc' , 'preproc8' ,
415
- 'fmri_spec' , 'fmri_est' , 'factorial_design' ,
416
- 'defs' , 'dicom' ]:
417
- # parentheses
418
- mscript += self ._generate_job ('jobs{1}.%s{1}.%s(1)' %
449
+ if isdefined (self .inputs .use_v8struct ) and self .inputs .use_v8struct :
450
+ mscript += self ._generate_job ('jobs{1}.spm.%s.%s' %
419
451
(self .jobtype , self .jobname ),
420
452
contents [0 ])
421
453
else :
422
- #curly brackets
423
- mscript += self ._generate_job ('jobs{1}.%s{1}.%s{1}' %
424
- (self .jobtype , self .jobname ),
425
- contents [0 ])
454
+ if self .jobname in ['st' , 'smooth' , 'preproc' , 'preproc8' ,
455
+ 'fmri_spec' , 'fmri_est' , 'factorial_design' ,
456
+ 'defs' ]:
457
+ # parentheses
458
+ mscript += self ._generate_job ('jobs{1}.%s{1}.%s(1)' %
459
+ (self .jobtype , self .jobname ),
460
+ contents [0 ])
461
+ else :
462
+ #curly brackets
463
+ mscript += self ._generate_job ('jobs{1}.%s{1}.%s{1}' %
464
+ (self .jobtype , self .jobname ),
465
+ contents [0 ])
426
466
else :
427
467
jobdef = {'jobs' : [{self .jobtype :
428
- [{self .jobname :self .reformat_dict_for_savemat
429
- (contents [0 ])}]}]}
468
+ [{self .jobname :
469
+ self .reformat_dict_for_savemat (contents [0 ])}]
470
+ }]}
430
471
savemat (os .path .join (cwd , 'pyjobs_%s.mat' % self .jobname ), jobdef )
431
472
mscript += "load pyjobs_%s;\n \n " % self .jobname
432
473
mscript += """
433
- if strcmp(spm('ver'),'SPM8'),
434
- jobs=spm_jobman('spm5tospm8',{jobs});
435
- end
436
- spm_jobman(\' run_nogui\' ,jobs);\n
474
+ spm_jobman(\' run\' , jobs);\n
437
475
"""
438
476
if postscript is not None :
439
477
mscript += postscript
440
478
return mscript
441
-
442
- @property
443
- def version (self ):
444
- version_dict = Info .version ()
445
- if version_dict :
446
- return '.' .join ((version_dict ['name' ].split ('SPM' )[- 1 ],
447
- version_dict ['release' ]))
448
- return version_dict
0 commit comments