@@ -437,8 +437,7 @@ class EstimateContrast(SPMCommand):
437
437
_jobname = "con"
438
438
439
439
def _make_matlab_command (self , _ ):
440
- """validates spm options and generates job structure
441
- """
440
+ """Validate spm options and generate job structure."""
442
441
contrasts = []
443
442
cname = []
444
443
for i , cont in enumerate (self .inputs .contrasts ):
@@ -457,80 +456,80 @@ def _make_matlab_command(self, _):
457
456
contrasts [i ].weights = cont [3 ]
458
457
if len (cont ) >= 5 :
459
458
contrasts [i ].sessions = cont [4 ]
460
- script = "% generated by nipype.interfaces.spm\n "
461
- script += "spm_defaults;\n "
462
- script += "jobs{1}.stats{1}.con.spmmat = {'%s'};\n " % self .inputs .spm_mat_file
463
- script += "load(jobs{1}.stats{1}.con.spmmat{:});\n "
464
- script += "SPM.swd = '%s';\n " % os .getcwd ()
465
- script += "save(jobs{1}.stats{1}.con.spmmat{:},'SPM');\n "
466
- script += "names = SPM.xX.name;\n "
459
+ script = ["""\
460
+ % generated by nipype.interfaces.spm
461
+ spm_defaults;
462
+ jobs{1}.stats{1}.con.spmmat = {'%s'};
463
+ load(jobs{1}.stats{1}.con.spmmat{:});
464
+ SPM.swd = '%s';
465
+ save(jobs{1}.stats{1}.con.spmmat{:},'SPM');
466
+ names = SPM.xX.name;
467
+ """ % (self .inputs .spm_mat_file , os .getcwd ())]
467
468
# get names for columns
468
469
if isdefined (self .inputs .group_contrast ) and self .inputs .group_contrast :
469
- script += "condnames=names;\n "
470
+ script += [ "condnames=names;" ]
470
471
else :
471
472
if self .inputs .use_derivs :
472
- script += r"pat = 'Sn\([0-9]*\) (.*)';\n"
473
+ script += [ r"pat = 'Sn\([0-9]*\) (.*)';" ]
473
474
else :
474
- script += (
475
- r"pat = 'Sn\([0-9]*\) (.*)\*bf\(1\)|Sn\([0-9]*\) "
476
- r".*\*bf\([2-9]\)|Sn\([0-9]*\) (.*)';"
477
- "\n "
478
- )
479
- script += "t = regexp(names,pat,'tokens');\n "
475
+ script += [r"""\
476
+ pat = 'Sn\([0-9]*\) (.*)\*bf\(1\)|Sn\([0-9]*\) .*\*bf\([2-9]\)|Sn\([0-9]*\) (.*)';""" ]
477
+
478
+ script += ["t = regexp(names,pat,'tokens');" ]
480
479
# get sessidx for columns
481
- script += r"pat1 = 'Sn\(([0-9].*)\)\s.*';\n"
482
- script += "t1 = regexp(names,pat1,'tokens');\n "
483
- script += (
484
- "for i0=1:numel(t),condnames{i0}='';condsess(i0)=0;if "
485
- "~isempty(t{i0}{1}),condnames{i0} = t{i0}{1}{1};"
486
- "condsess(i0)=str2num(t1{i0}{1}{1});end;end;\n "
487
- )
480
+ script += [r"pat1 = 'Sn\(([0-9].*)\)\s.*';" ]
481
+ script += ["t1 = regexp(names,pat1,'tokens');" ]
482
+ script += ["""\
483
+ for i0=1:numel(t)
484
+ condnames{i0}='';
485
+ condsess(i0)=0;
486
+ if ~isempty(t{i0}{1})
487
+ condnames{i0} = t{i0}{1}{1};
488
+ condsess(i0)=str2num(t1{i0}{1}{1});
489
+ end;
490
+ end;
491
+ """ ]
492
+
488
493
# BUILD CONTRAST SESSION STRUCTURE
489
494
for i , contrast in enumerate (contrasts ):
490
495
if contrast .stat == "T" :
491
- script += "consess{%d}.tcon.name = '%s';\n " % (i + 1 , contrast .name )
492
- script += "consess{%d}.tcon.convec = zeros(1,numel(names));\n " % (i + 1 )
496
+ script += [ "consess{%d}.tcon.name = '%s';" % (i + 1 , contrast .name )]
497
+ script += [ "consess{%d}.tcon.convec = zeros(1,numel(names));" % (i + 1 )]
493
498
for c0 , cond in enumerate (contrast .conditions ):
494
- script += "idx = strmatch('%s',condnames,'exact');\n " % ( cond )
495
- script += (
496
- " if isempty(idx), throw(MException("
497
- " 'CondName:Chk', sprintf('Condition %%s not "
498
- "found in design','%s'))); end;\n "
499
- ) % cond
499
+ script += [ "idx = strmatch('%s',condnames,'exact');" % cond ]
500
+ script += [ """ \
501
+ if isempty(idx)
502
+ throw(MException( 'CondName:Chk', sprintf('Condition %%s not found in design','%s')));
503
+ end;
504
+ """ % cond ]
500
505
if contrast .sessions :
501
506
for sno , sw in enumerate (contrast .sessions ):
502
- script += "sidx = find(condsess(idx)==%d);\n " % (sno + 1 )
503
- script += "consess{%d}.tcon.convec(idx(sidx)) = %f;\n " % (
507
+ script += [ "sidx = find(condsess(idx)==%d);" % (sno + 1 )]
508
+ script += [ "consess{%d}.tcon.convec(idx(sidx)) = %f;" % (
504
509
i + 1 ,
505
510
sw * contrast .weights [c0 ],
506
- )
511
+ )]
507
512
else :
508
- script += "consess{%d}.tcon.convec(idx) = %f;\n " % (
513
+ script += [ "consess{%d}.tcon.convec(idx) = %f;" % (
509
514
i + 1 ,
510
515
contrast .weights [c0 ],
511
- )
516
+ )]
512
517
for i , contrast in enumerate (contrasts ):
513
518
if contrast .stat == "F" :
514
- script += "consess{%d}.fcon.name = '%s';\n " % (i + 1 , contrast .name )
519
+ script += [ "consess{%d}.fcon.name = '%s';" % (i + 1 , contrast .name )]
515
520
for cl0 , fcont in enumerate (contrast .conditions ):
516
- try :
517
- tidx = cname .index (fcont [0 ])
518
- except :
519
- Exception (
520
- "Contrast Estimate: could not get index of"
521
- " T contrast. probably not defined prior "
522
- "to the F contrasts"
523
- )
524
- script += (
525
- "consess{%d}.fcon.convec{%d} = consess{%d}.tcon.convec;\n "
526
- ) % (i + 1 , cl0 + 1 , tidx + 1 )
527
- script += "jobs{1}.stats{1}.con.consess = consess;\n "
528
- script += (
529
- "if strcmp(spm('ver'),'SPM8'), spm_jobman('initcfg');"
530
- "jobs=spm_jobman('spm5tospm8',{jobs});end\n "
531
- )
532
- script += "spm_jobman('run',jobs);"
533
- return script
521
+ tidx = cname .index (fcont [0 ])
522
+ script += ["consess{%d}.fcon.convec{%d} = consess{%d}.tcon.convec;" %
523
+ (i + 1 , cl0 + 1 , tidx + 1 )]
524
+ script += ["jobs{1}.stats{1}.con.consess = consess;" ]
525
+ script += ["""\
526
+ if strcmp(spm('ver'),'SPM8')
527
+ spm_jobman('initcfg');
528
+ jobs=spm_jobman('spm5tospm8',{jobs});
529
+ end;
530
+ """ ]
531
+ script += ["spm_jobman('run',jobs);" ]
532
+ return "\n " .join (script )
534
533
535
534
def _list_outputs (self ):
536
535
import scipy .io as sio
0 commit comments