Skip to content

Commit 4ff3a51

Browse files
committed
fix: revise some r-strings in SPM's EstimateContrast
By removing some space concatenation of strings, some of them were r-strings and the concatenated one contained ``\n``, effectively escaping the special return-carriage. Instead of concatenating strings, the interface now accumulates the lines in a list that is joined in the end.
1 parent 3b14b1d commit 4ff3a51

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

nipype/interfaces/spm/model.py

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ class EstimateContrast(SPMCommand):
437437
_jobname = "con"
438438

439439
def _make_matlab_command(self, _):
440-
"""validates spm options and generates job structure
441-
"""
440+
"""Validate spm options and generate job structure."""
442441
contrasts = []
443442
cname = []
444443
for i, cont in enumerate(self.inputs.contrasts):
@@ -457,80 +456,80 @@ def _make_matlab_command(self, _):
457456
contrasts[i].weights = cont[3]
458457
if len(cont) >= 5:
459458
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())]
467468
# get names for columns
468469
if isdefined(self.inputs.group_contrast) and self.inputs.group_contrast:
469-
script += "condnames=names;\n"
470+
script += ["condnames=names;"]
470471
else:
471472
if self.inputs.use_derivs:
472-
script += r"pat = 'Sn\([0-9]*\) (.*)';\n"
473+
script += [r"pat = 'Sn\([0-9]*\) (.*)';"]
473474
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');"]
480479
# 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+
488493
# BUILD CONTRAST SESSION STRUCTURE
489494
for i, contrast in enumerate(contrasts):
490495
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)]
493498
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]
500505
if contrast.sessions:
501506
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;" % (
504509
i + 1,
505510
sw * contrast.weights[c0],
506-
)
511+
)]
507512
else:
508-
script += "consess{%d}.tcon.convec(idx) = %f;\n" % (
513+
script += ["consess{%d}.tcon.convec(idx) = %f;" % (
509514
i + 1,
510515
contrast.weights[c0],
511-
)
516+
)]
512517
for i, contrast in enumerate(contrasts):
513518
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)]
515520
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)
534533

535534
def _list_outputs(self):
536535
import scipy.io as sio

0 commit comments

Comments
 (0)