@@ -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,90 @@ 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
+ """\
461
+ %% generated by nipype.interfaces.spm
462
+ spm_defaults;
463
+ jobs{1}.stats{1}.con.spmmat = {'%s'};
464
+ load(jobs{1}.stats{1}.con.spmmat{:});
465
+ SPM.swd = '%s';
466
+ save(jobs{1}.stats{1}.con.spmmat{:},'SPM');
467
+ names = SPM.xX.name;"""
468
+ % (self .inputs .spm_mat_file , os .getcwd ())
469
+ ]
467
470
# get names for columns
468
471
if isdefined (self .inputs .group_contrast ) and self .inputs .group_contrast :
469
- script += "condnames=names;\n "
472
+ script += [ "condnames=names;" ]
470
473
else :
471
474
if self .inputs .use_derivs :
472
- script += r"pat = 'Sn\([0-9]*\) (.*)';\n"
475
+ script += [ r"pat = 'Sn\([0-9]*\) (.*)';" ]
473
476
else :
474
- script += (
477
+ script += [
475
478
r"pat = 'Sn\([0-9]*\) (.*)\*bf\(1\)|Sn\([0-9]*\) "
476
479
r".*\*bf\([2-9]\)|Sn\([0-9]*\) (.*)';"
477
- " \n "
478
- )
479
- script += "t = regexp(names,pat,'tokens');\n "
480
+ ]
481
+
482
+ script += [ "t = regexp(names,pat,'tokens');" ]
480
483
# 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
- )
484
+ script += [r"pat1 = 'Sn\(([0-9].*)\)\s.*';" ]
485
+ script += ["t1 = regexp(names,pat1,'tokens');" ]
486
+ script += [
487
+ """\
488
+ for i0=1:numel(t)
489
+ condnames{i0}='';
490
+ condsess(i0)=0;
491
+ if ~isempty(t{i0}{1})
492
+ condnames{i0} = t{i0}{1}{1};
493
+ condsess(i0)=str2num(t1{i0}{1}{1});
494
+ end;
495
+ end;"""
496
+ ]
497
+
488
498
# BUILD CONTRAST SESSION STRUCTURE
489
499
for i , contrast in enumerate (contrasts ):
490
500
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 )
501
+ script += [ "consess{%d}.tcon.name = '%s';" % (i + 1 , contrast .name )]
502
+ script += [ "consess{%d}.tcon.convec = zeros(1,numel(names));" % (i + 1 )]
493
503
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
504
+ script += ["idx = strmatch('%s',condnames,'exact');" % cond ]
505
+ script += [
506
+ """\
507
+ if isempty(idx)
508
+ throw(MException('CondName:Chk', sprintf('Condition %%s not found in design','%s')));
509
+ end;"""
510
+ % cond
511
+ ]
500
512
if contrast .sessions :
501
513
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 " % (
504
- i + 1 ,
505
- sw * contrast .weights [c0 ],
506
- )
514
+ script += [ "sidx = find(condsess(idx)==%d);" % (sno + 1 )]
515
+ script += [
516
+ "consess{%d}.tcon.convec(idx(sidx)) = %f;"
517
+ % ( i + 1 , sw * contrast .weights [c0 ],)
518
+ ]
507
519
else :
508
- script += "consess{%d}.tcon.convec(idx) = %f; \n " % (
509
- i + 1 ,
510
- contrast .weights [c0 ],
511
- )
520
+ script += [
521
+ "consess{%d}.tcon.convec(idx) = %f;"
522
+ % ( i + 1 , contrast .weights [c0 ],)
523
+ ]
512
524
for i , contrast in enumerate (contrasts ):
513
525
if contrast .stat == "F" :
514
- script += "consess{%d}.fcon.name = '%s';\n " % (i + 1 , contrast .name )
526
+ script += [ "consess{%d}.fcon.name = '%s';" % (i + 1 , contrast .name )]
515
527
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
528
+ tidx = cname .index (fcont [0 ])
529
+ script += [
530
+ "consess{%d}.fcon.convec{%d} = consess{%d}.tcon.convec;"
531
+ % (i + 1 , cl0 + 1 , tidx + 1 )
532
+ ]
533
+ script += ["jobs{1}.stats{1}.con.consess = consess;" ]
534
+ script += [
535
+ """\
536
+ if strcmp(spm('ver'),'SPM8')
537
+ spm_jobman('initcfg');
538
+ jobs=spm_jobman('spm5tospm8',{jobs});
539
+ end;"""
540
+ ]
541
+ script += ["spm_jobman('run',jobs);" ]
542
+ return "\n " .join (script )
534
543
535
544
def _list_outputs (self ):
536
545
import scipy .io as sio
0 commit comments