forked from nipy/nipype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
1269 lines (1114 loc) · 42.1 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""The spm module provides basic functions for interfacing with matlab
and spm to access spm tools.
"""
# Standard library imports
import os
from glob import glob
# Third-party imports
import numpy as np
# Local imports
from ... import logging
from ...utils.filemanip import ensure_list, simplify_list, split_filename
from ..base import (
Bunch,
traits,
Tuple,
TraitedSpec,
File,
Directory,
OutputMultiPath,
InputMultiPath,
isdefined,
)
from .base import SPMCommand, SPMCommandInputSpec, scans_for_fnames, ImageFileSPM
__docformat__ = "restructuredtext"
iflogger = logging.getLogger("nipype.interface")
class Level1DesignInputSpec(SPMCommandInputSpec):
spm_mat_dir = Directory(
exists=True, field="dir", desc="directory to store SPM.mat file (opt)"
)
timing_units = traits.Enum(
"secs",
"scans",
field="timing.units",
desc="units for specification of onsets",
mandatory=True,
)
interscan_interval = traits.Float(
field="timing.RT", desc="Interscan interval in secs", mandatory=True
)
microtime_resolution = traits.Int(
field="timing.fmri_t", desc=("Number of time-bins per scan in secs (opt)")
)
microtime_onset = traits.Float(
field="timing.fmri_t0",
desc=("The onset/time-bin in seconds for alignment (opt)"),
)
session_info = traits.Any(
field="sess",
desc=("Session specific information generated by ``modelgen.SpecifyModel``"),
mandatory=True,
)
factor_info = traits.List(
traits.Dict(traits.Enum("name", "levels")),
field="fact",
desc=("Factor specific information file (opt)"),
)
bases = traits.Dict(
traits.Enum("hrf", "fourier", "fourier_han", "gamma", "fir"),
field="bases",
desc="""\
Dictionary names of the basis function to parameters:
* hrf
* derivs -- (2-element list) Model HRF Derivatives. No derivatives: [0,0],
Time derivatives : [1,0], Time and Dispersion derivatives: [1,1]
* fourier, fourier_han, gamma, or fir:
* length -- (int) Post-stimulus window length (in seconds)
* order -- (int) Number of basis functions
""",
mandatory=True,
)
volterra_expansion_order = traits.Enum(
1, 2, field="volt", desc=("Model interactions - no:1, yes:2")
)
global_intensity_normalization = traits.Enum(
"none",
"scaling",
field="global",
desc=("Global intensity normalization - scaling or none"),
)
mask_image = File(
exists=True, field="mask", desc="Image for explicitly masking the analysis"
)
mask_threshold = traits.Either(
traits.Enum("-Inf"),
traits.Float(),
desc="Thresholding for the mask",
default="-Inf",
usedefault=True,
)
model_serial_correlations = traits.Enum(
"AR(1)",
"FAST",
"none",
field="cvi",
desc=(
"Model serial correlations "
"AR(1), FAST or none. FAST "
"is available in SPM12"
),
)
flags = traits.Dict(
desc="Additional arguments to the job, e.g., a common SPM operation is to "
"modify the default masking threshold (mthresh)"
)
class Level1DesignOutputSpec(TraitedSpec):
spm_mat_file = File(exists=True, desc="SPM mat file")
class Level1Design(SPMCommand):
"""Generate an SPM design matrix
http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=59
Examples
--------
>>> level1design = Level1Design()
>>> level1design.inputs.timing_units = 'secs'
>>> level1design.inputs.interscan_interval = 2.5
>>> level1design.inputs.bases = {'hrf':{'derivs': [0,0]}}
>>> level1design.inputs.session_info = 'session_info.npz'
>>> level1design.inputs.flags = {'mthresh': 0.4}
>>> level1design.run() # doctest: +SKIP
"""
input_spec = Level1DesignInputSpec
output_spec = Level1DesignOutputSpec
_jobtype = "stats"
_jobname = "fmri_spec"
def _format_arg(self, opt, spec, val):
"""Convert input to appropriate format for spm"""
if opt in ["spm_mat_dir", "mask_image"]:
return np.array([str(val)], dtype=object)
if opt in ["session_info"]: # , 'factor_info']:
if isinstance(val, dict):
return [val]
else:
return val
return super()._format_arg(opt, spec, val)
def _parse_inputs(self):
"""validate spm realign options if set to None ignore"""
einputs = super()._parse_inputs(skip=("mask_threshold", "flags"))
if isdefined(self.inputs.flags):
einputs[0].update({flag: val for (flag, val) in self.inputs.flags.items()})
for sessinfo in einputs[0]["sess"]:
sessinfo["scans"] = scans_for_fnames(
ensure_list(sessinfo["scans"]), keep4d=False
)
if not isdefined(self.inputs.spm_mat_dir):
einputs[0]["dir"] = np.array([str(os.getcwd())], dtype=object)
return einputs
def _make_matlab_command(self, content):
"""validates spm options and generates job structure
if mfile is True uses matlab .m file
else generates a job structure and saves in .mat
"""
if isdefined(self.inputs.mask_image):
# SPM doesn't handle explicit masking properly, especially
# when you want to use the entire mask image
postscript = "load SPM;\n"
postscript += "SPM.xM.VM = spm_vol('%s');\n" % simplify_list(
self.inputs.mask_image
)
postscript += "SPM.xM.I = 0;\n"
postscript += "SPM.xM.T = [];\n"
postscript += (
"SPM.xM.TH = ones(size(SPM.xM.TH))*(%s);\n" % self.inputs.mask_threshold
)
postscript += "SPM.xM.xs = struct('Masking', 'explicit masking only');\n"
postscript += "save SPM SPM;\n"
else:
postscript = None
return super()._make_matlab_command(content, postscript=postscript)
def _list_outputs(self):
outputs = self._outputs().get()
spm = os.path.join(os.getcwd(), "SPM.mat")
outputs["spm_mat_file"] = spm
return outputs
class EstimateModelInputSpec(SPMCommandInputSpec):
spm_mat_file = File(
exists=True,
field="spmmat",
copyfile=True,
mandatory=True,
desc="Absolute path to SPM.mat",
)
estimation_method = traits.Dict(
traits.Enum("Classical", "Bayesian2", "Bayesian"),
field="method",
mandatory=True,
desc=("Dictionary of either Classical: 1, Bayesian: 1, or Bayesian2: 1 (dict)"),
)
write_residuals = traits.Bool(
field="write_residuals", desc="Write individual residual images"
)
flags = traits.Dict(desc="Additional arguments")
class EstimateModelOutputSpec(TraitedSpec):
mask_image = ImageFileSPM(exists=True, desc="binary mask to constrain estimation")
beta_images = OutputMultiPath(
ImageFileSPM(exists=True), desc="design parameter estimates"
)
residual_image = ImageFileSPM(
exists=True, desc="Mean-squared image of the residuals"
)
residual_images = OutputMultiPath(
ImageFileSPM(exists=True),
desc="individual residual images (requires `write_residuals`",
)
RPVimage = ImageFileSPM(exists=True, desc="Resels per voxel image")
spm_mat_file = File(exists=True, desc="Updated SPM mat file")
labels = ImageFileSPM(exists=True, desc="label file")
SDerror = OutputMultiPath(
ImageFileSPM(exists=True), desc="Images of the standard deviation of the error"
)
ARcoef = OutputMultiPath(
ImageFileSPM(exists=True), desc="Images of the AR coefficient"
)
Cbetas = OutputMultiPath(
ImageFileSPM(exists=True), desc="Images of the parameter posteriors"
)
SDbetas = OutputMultiPath(
ImageFileSPM(exists=True),
desc="Images of the standard deviation of parameter posteriors",
)
con_images = OutputMultiPath(
File(exists=True),
desc=(
"contrast images from a t-contrast "
"(created if factor_info used in Level1Design)"
),
)
spmT_images = OutputMultiPath(
File(exists=True),
desc=(
"stat images from a t-contrast"
"(created if factor_info used in Level1Design)"
),
)
ess_images = OutputMultiPath(
File(exists=True),
desc=(
"contrast images from an F-contrast"
"(created if factor_info used in Level1Design)"
),
)
spmF_images = OutputMultiPath(
File(exists=True),
desc=(
"stat images from an F-contrast"
"(created if factor_info used in Level1Design)"
),
)
class EstimateModel(SPMCommand):
"""Use spm_spm to estimate the parameters of a model
http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=69
Examples
--------
>>> est = EstimateModel()
>>> est.inputs.spm_mat_file = 'SPM.mat'
>>> est.inputs.estimation_method = {'Classical': 1}
>>> est.run() # doctest: +SKIP
"""
input_spec = EstimateModelInputSpec
output_spec = EstimateModelOutputSpec
_jobtype = "stats"
_jobname = "fmri_est"
def _format_arg(self, opt, spec, val):
"""Convert input to appropriate format for spm"""
if opt == "spm_mat_file":
return np.array([str(val)], dtype=object)
if opt == "estimation_method":
if isinstance(val, (str, bytes)):
return {f"{val}": 1}
else:
return val
return super()._format_arg(opt, spec, val)
def _parse_inputs(self):
"""validate spm realign options if set to None ignore"""
einputs = super()._parse_inputs(skip=("flags"))
if isdefined(self.inputs.flags):
einputs[0].update({flag: val for (flag, val) in self.inputs.flags.items()})
return einputs
def _list_outputs(self):
import scipy.io as sio
outputs = self._outputs().get()
pth = os.path.dirname(self.inputs.spm_mat_file)
outtype = "nii" if "12" in self.version.split(".")[0] else "img"
spm = sio.loadmat(self.inputs.spm_mat_file, struct_as_record=False)
betas = [vbeta.fname[0] for vbeta in spm["SPM"][0, 0].Vbeta[0]]
if (
"Bayesian" in self.inputs.estimation_method.keys()
or "Bayesian2" in self.inputs.estimation_method.keys()
):
outputs["labels"] = os.path.join(pth, f"labels.{outtype}")
outputs["SDerror"] = glob(os.path.join(pth, "Sess*_SDerror*"))
outputs["ARcoef"] = glob(os.path.join(pth, "Sess*_AR_*"))
if betas:
outputs["Cbetas"] = [os.path.join(pth, f"C{beta}") for beta in betas]
outputs["SDbetas"] = [os.path.join(pth, f"SD{beta}") for beta in betas]
if "Classical" in self.inputs.estimation_method.keys():
outputs["residual_image"] = os.path.join(pth, f"ResMS.{outtype}")
outputs["RPVimage"] = os.path.join(pth, f"RPV.{outtype}")
if self.inputs.write_residuals:
outputs["residual_images"] = glob(os.path.join(pth, "Res_*"))
if betas:
outputs["beta_images"] = [os.path.join(pth, beta) for beta in betas]
# When 'factor_info' is used in Level1Design
# spm automatically creates contrast
try:
contrast = [c.Vcon[0][0].fname[0] for c in spm["SPM"][0, 0].xCon[0]]
contrast_spm = [c.Vspm[0][0].fname[0] for c in spm["SPM"][0, 0].xCon[0]]
except Exception:
contrast = []
contrast_spm = []
if contrast:
outputs["con_images"] = [
os.path.join(pth, cont) for cont in contrast if 'con' in cont
]
outputs["ess_images"] = [
os.path.join(pth, cont) for cont in contrast if 'ess' in cont
]
if contrast_spm:
outputs["spmT_images"] = [
os.path.join(pth, cont) for cont in contrast_spm if 'spmT' in cont
]
outputs["spmF_images"] = [
os.path.join(pth, cont) for cont in contrast_spm if 'spmF' in cont
]
outputs["mask_image"] = os.path.join(pth, f"mask.{outtype}")
outputs["spm_mat_file"] = os.path.join(pth, "SPM.mat")
return outputs
class EstimateContrastInputSpec(SPMCommandInputSpec):
spm_mat_file = File(
exists=True,
field="spmmat",
desc="Absolute path to SPM.mat",
copyfile=True,
mandatory=True,
)
contrasts = traits.List(
traits.Either(
Tuple(
traits.Str,
traits.Enum("T"),
traits.List(traits.Str),
traits.List(traits.Float),
),
Tuple(
traits.Str,
traits.Enum("T"),
traits.List(traits.Str),
traits.List(traits.Float),
traits.List(traits.Float),
),
Tuple(
traits.Str,
traits.Enum("F"),
traits.List(
traits.Either(
Tuple(
traits.Str,
traits.Enum("T"),
traits.List(traits.Str),
traits.List(traits.Float),
),
Tuple(
traits.Str,
traits.Enum("T"),
traits.List(traits.Str),
traits.List(traits.Float),
traits.List(traits.Float),
),
)
),
),
),
desc="""List of contrasts with each contrast being a list of the form:
[('name', 'stat', [condition list], [weight list], [session list])]
If session list is None or not provided, all sessions are used. For
F contrasts, the condition list should contain previously defined
T-contrasts.""",
mandatory=True,
)
beta_images = InputMultiPath(
File(exists=True),
desc=("Parameter estimates of the design matrix"),
copyfile=False,
mandatory=True,
)
residual_image = File(
exists=True,
desc="Mean-squared image of the residuals",
copyfile=False,
mandatory=True,
)
use_derivs = traits.Bool(
desc="use derivatives for estimation", xor=["group_contrast"]
)
group_contrast = traits.Bool(desc="higher level contrast", xor=["use_derivs"])
class EstimateContrastOutputSpec(TraitedSpec):
con_images = OutputMultiPath(
File(exists=True), desc="contrast images from a t-contrast"
)
spmT_images = OutputMultiPath(
File(exists=True), desc="stat images from a t-contrast"
)
ess_images = OutputMultiPath(
File(exists=True), desc="contrast images from an F-contrast"
)
spmF_images = OutputMultiPath(
File(exists=True), desc="stat images from an F-contrast"
)
spm_mat_file = File(exists=True, desc="Updated SPM mat file")
class EstimateContrast(SPMCommand):
"""Use spm_contrasts to estimate contrasts of interest
Examples
--------
>>> import nipype.interfaces.spm as spm
>>> est = spm.EstimateContrast()
>>> est.inputs.spm_mat_file = 'SPM.mat'
>>> cont1 = ('Task>Baseline','T', ['Task-Odd','Task-Even'],[0.5,0.5])
>>> cont2 = ('Task-Odd>Task-Even','T', ['Task-Odd','Task-Even'],[1,-1])
>>> contrasts = [cont1,cont2]
>>> est.inputs.contrasts = contrasts
>>> est.run() # doctest: +SKIP
"""
input_spec = EstimateContrastInputSpec
output_spec = EstimateContrastOutputSpec
_jobtype = "stats"
_jobname = "con"
def _make_matlab_command(self, _):
"""Validate spm options and generate job structure."""
contrasts = []
cname = []
for i, cont in enumerate(self.inputs.contrasts):
cname.insert(i, cont[0])
contrasts.insert(
i,
Bunch(
name=cont[0],
stat=cont[1],
conditions=cont[2],
weights=None,
sessions=None,
),
)
if len(cont) >= 4:
contrasts[i].weights = cont[3]
if len(cont) >= 5:
contrasts[i].sessions = cont[4]
script = [
"""\
%% generated by nipype.interfaces.spm
spm_defaults;
jobs{1}.stats{1}.con.spmmat = {'%s'};
load(jobs{1}.stats{1}.con.spmmat{:});
SPM.swd = '%s';
save(jobs{1}.stats{1}.con.spmmat{:},'SPM');
names = SPM.xX.name;"""
% (self.inputs.spm_mat_file, os.getcwd())
]
# get names for columns
if isdefined(self.inputs.group_contrast) and self.inputs.group_contrast:
script += ["condnames=names;"]
else:
if self.inputs.use_derivs:
script += [r"pat = 'Sn\([0-9]*\) (.*)';"]
else:
script += [
r"pat = 'Sn\([0-9]*\) (.*)\*bf\(1\)|Sn\([0-9]*\) "
r".*\*bf\([2-9]\)|Sn\([0-9]*\) (.*)';"
]
script += ["t = regexp(names,pat,'tokens');"]
# get sessidx for columns
script += [r"pat1 = 'Sn\(([0-9].*)\)\s.*';"]
script += ["t1 = regexp(names,pat1,'tokens');"]
script += [
"""\
for i0=1:numel(t)
condnames{i0}='';
condsess(i0)=0;
if ~isempty(t{i0}{1})
condnames{i0} = t{i0}{1}{1};
condsess(i0)=str2num(t1{i0}{1}{1});
end;
end;"""
]
# BUILD CONTRAST SESSION STRUCTURE
for i, contrast in enumerate(contrasts):
if contrast.stat == "T":
script += ["consess{%d}.tcon.name = '%s';" % (i + 1, contrast.name)]
script += ["consess{%d}.tcon.convec = zeros(1,numel(names));" % (i + 1)]
for c0, cond in enumerate(contrast.conditions):
script += ["idx = strmatch('%s',condnames,'exact');" % cond]
script += [
"""\
if isempty(idx)
throw(MException('CondName:Chk', sprintf('Condition %%s not found in design','%s')));
end;"""
% cond
]
if contrast.sessions:
for sno, sw in enumerate(contrast.sessions):
script += ["sidx = find(condsess(idx)==%d);" % (sno + 1)]
script += [
"consess{%d}.tcon.convec(idx(sidx)) = %f;"
% (i + 1, sw * contrast.weights[c0])
]
else:
script += [
"consess{%d}.tcon.convec(idx) = %f;"
% (i + 1, contrast.weights[c0])
]
for i, contrast in enumerate(contrasts):
if contrast.stat == "F":
script += ["consess{%d}.fcon.name = '%s';" % (i + 1, contrast.name)]
for cl0, fcont in enumerate(contrast.conditions):
tidx = cname.index(fcont[0])
script += [
"consess{%d}.fcon.convec{%d} = consess{%d}.tcon.convec;"
% (i + 1, cl0 + 1, tidx + 1)
]
script += ["jobs{1}.stats{1}.con.consess = consess;"]
script += [
"""\
if strcmp(spm('ver'),'SPM8')
spm_jobman('initcfg');
jobs=spm_jobman('spm5tospm8',{jobs});
end;"""
]
script += ["spm_jobman('run',jobs);"]
return "\n".join(script)
def _list_outputs(self):
import scipy.io as sio
outputs = self._outputs().get()
pth, _ = os.path.split(self.inputs.spm_mat_file)
spm = sio.loadmat(self.inputs.spm_mat_file, struct_as_record=False)
con_images = []
spmT_images = []
for con in spm["SPM"][0, 0].xCon[0]:
con_images.append(str(os.path.join(pth, con.Vcon[0, 0].fname[0])))
spmT_images.append(str(os.path.join(pth, con.Vspm[0, 0].fname[0])))
if con_images:
outputs["con_images"] = con_images
outputs["spmT_images"] = spmT_images
spm12 = "12" in self.version.split(".")[0]
if spm12:
ess = glob(os.path.join(pth, "ess*.nii"))
else:
ess = glob(os.path.join(pth, "ess*.img"))
if len(ess) > 0:
outputs["ess_images"] = sorted(ess)
if spm12:
spmf = glob(os.path.join(pth, "spmF*.nii"))
else:
spmf = glob(os.path.join(pth, "spmF*.img"))
if len(spmf) > 0:
outputs["spmF_images"] = sorted(spmf)
outputs["spm_mat_file"] = self.inputs.spm_mat_file
return outputs
class ThresholdInputSpec(SPMCommandInputSpec):
spm_mat_file = File(
exists=True, desc="absolute path to SPM.mat", copyfile=True, mandatory=True
)
stat_image = File(exists=True, desc="stat image", copyfile=False, mandatory=True)
contrast_index = traits.Int(
mandatory=True, desc="which contrast in the SPM.mat to use"
)
use_fwe_correction = traits.Bool(
True,
usedefault=True,
desc=(
"whether to use FWE (Bonferroni) "
"correction for initial threshold "
"(height_threshold_type has to be "
"set to p-value)"
),
)
use_vox_fdr_correction = traits.Bool(
False,
usedefault=True,
desc=(
"whether to use voxel-based FDR "
"correction for initial threshold "
"(height_threshold_type has to be "
"set to q-value)"
),
)
use_topo_fdr = traits.Bool(
True,
usedefault=True,
desc=("whether to use FDR over cluster extent probabilities"),
)
height_threshold = traits.Float(
0.05,
usedefault=True,
desc=("value for initial thresholding (defining clusters)"),
)
height_threshold_type = traits.Enum(
"p-value",
"stat",
usedefault=True,
desc=("Is the cluster forming threshold a stat value or p-value?"),
)
extent_fdr_p_threshold = traits.Float(
0.05,
usedefault=True,
desc=("p threshold on FDR corrected cluster size probabilities"),
)
extent_threshold = traits.Int(
0, usedefault=True, desc="Minimum cluster size in voxels"
)
force_activation = traits.Bool(
False,
usedefault=True,
desc=(
"In case no clusters survive the "
"topological inference step this "
"will pick a culster with the highest "
"sum of t-values. Use with care."
),
)
class ThresholdOutputSpec(TraitedSpec):
thresholded_map = File(exists=True)
n_clusters = traits.Int()
pre_topo_fdr_map = File(exists=True)
pre_topo_n_clusters = traits.Int()
activation_forced = traits.Bool()
cluster_forming_thr = traits.Float()
class Threshold(SPMCommand):
"""Topological FDR thresholding based on cluster extent/size. Smoothness is
estimated from GLM residuals but is assumed to be the same for all of the
voxels.
Examples
--------
>>> thresh = Threshold()
>>> thresh.inputs.spm_mat_file = 'SPM.mat'
>>> thresh.inputs.stat_image = 'spmT_0001.img'
>>> thresh.inputs.contrast_index = 1
>>> thresh.inputs.extent_fdr_p_threshold = 0.05
>>> thresh.run() # doctest: +SKIP
"""
input_spec = ThresholdInputSpec
output_spec = ThresholdOutputSpec
def _gen_thresholded_map_filename(self):
_, fname, ext = split_filename(self.inputs.stat_image)
return os.path.abspath(fname + "_thr" + ext)
def _gen_pre_topo_map_filename(self):
_, fname, ext = split_filename(self.inputs.stat_image)
return os.path.abspath(fname + "_pre_topo_thr" + ext)
def _make_matlab_command(self, _):
script = "con_index = %d;\n" % self.inputs.contrast_index
script += "cluster_forming_thr = %f;\n" % self.inputs.height_threshold
if self.inputs.use_fwe_correction and self.inputs.use_vox_fdr_correction:
raise ValueError(
"'use_fwe_correction' and 'use_vox_fdr_correction' can't both be True"
)
if self.inputs.use_fwe_correction and not self.inputs.use_vox_fdr_correction:
script += "thresDesc = 'FWE';\n"
elif self.inputs.use_vox_fdr_correction and not self.inputs.use_fwe_correction:
script += "thresDesc = 'FDR';\n"
else:
script += "thresDesc = 'none';\n"
if self.inputs.use_topo_fdr:
script += "use_topo_fdr = 1;\n"
else:
script += "use_topo_fdr = 0;\n"
if self.inputs.force_activation:
script += "force_activation = 1;\n"
else:
script += "force_activation = 0;\n"
script += (
"cluster_extent_p_fdr_thr = %f;\n" % self.inputs.extent_fdr_p_threshold
)
script += "stat_filename = '%s';\n" % self.inputs.stat_image
script += "height_threshold_type = '%s';\n" % self.inputs.height_threshold_type
script += "extent_threshold = %d;\n" % self.inputs.extent_threshold
script += "load %s;\n" % self.inputs.spm_mat_file
script += """
FWHM = SPM.xVol.FWHM;
df = [SPM.xCon(con_index).eidf SPM.xX.erdf];
STAT = SPM.xCon(con_index).STAT;
VspmSv = cat(1,SPM.xCon(con_index).Vspm);
R = SPM.xVol.R;
S = SPM.xVol.S;
n = 1;
switch thresDesc
case 'FWE'
cluster_forming_thr = spm_uc(cluster_forming_thr,df,STAT,R,n,S);
case 'FDR'
cluster_forming_thr = spm_uc_FDR(cluster_forming_thr,df,STAT,n,VspmSv,0);
case 'none'
if strcmp(height_threshold_type, 'p-value')
cluster_forming_thr = spm_u(cluster_forming_thr^(1/n),df,STAT);
end
end
stat_map_vol = spm_vol(stat_filename);
[stat_map_data, stat_map_XYZmm] = spm_read_vols(stat_map_vol);
Z = stat_map_data(:)';
[x,y,z] = ind2sub(size(stat_map_data),(1:numel(stat_map_data))');
XYZ = cat(1, x', y', z');
XYZth = XYZ(:, Z >= cluster_forming_thr);
Zth = Z(Z >= cluster_forming_thr);
"""
script += (
"spm_write_filtered(Zth,XYZth,stat_map_vol.dim',"
"stat_map_vol.mat,'thresholded map', '%s');\n"
) % self._gen_pre_topo_map_filename()
script += """
max_size = 0;
max_size_index = 0;
th_nclusters = 0;
nclusters = 0;
if isempty(XYZth)
thresholded_XYZ = [];
thresholded_Z = [];
else
if use_topo_fdr
V2R = 1/prod(FWHM(stat_map_vol.dim > 1));
[uc,Pc,ue] = spm_uc_clusterFDR(cluster_extent_p_fdr_thr,df,STAT,R,n,Z,XYZ,V2R,cluster_forming_thr);
end
voxel_labels = spm_clusters(XYZth);
nclusters = max(voxel_labels);
thresholded_XYZ = [];
thresholded_Z = [];
for i = 1:nclusters
cluster_size = sum(voxel_labels==i);
if cluster_size > extent_threshold && (~use_topo_fdr || (cluster_size - uc) > -1)
thresholded_XYZ = cat(2, thresholded_XYZ, XYZth(:,voxel_labels == i));
thresholded_Z = cat(2, thresholded_Z, Zth(voxel_labels == i));
th_nclusters = th_nclusters + 1;
end
if force_activation
cluster_sum = sum(Zth(voxel_labels == i));
if cluster_sum > max_size
max_size = cluster_sum;
max_size_index = i;
end
end
end
end
activation_forced = 0;
if isempty(thresholded_XYZ)
if force_activation && max_size ~= 0
thresholded_XYZ = XYZth(:,voxel_labels == max_size_index);
thresholded_Z = Zth(voxel_labels == max_size_index);
th_nclusters = 1;
activation_forced = 1;
else
thresholded_Z = [0];
thresholded_XYZ = [1 1 1]';
th_nclusters = 0;
end
end
fprintf('activation_forced = %d\\n',activation_forced);
fprintf('pre_topo_n_clusters = %d\\n',nclusters);
fprintf('n_clusters = %d\\n',th_nclusters);
fprintf('cluster_forming_thr = %f\\n',cluster_forming_thr);
"""
script += (
"spm_write_filtered(thresholded_Z,thresholded_XYZ,"
"stat_map_vol.dim',stat_map_vol.mat,'thresholded map',"
" '%s');\n"
) % self._gen_thresholded_map_filename()
return script
def aggregate_outputs(self, runtime=None):
outputs = self._outputs()
outputs.thresholded_map = self._gen_thresholded_map_filename()
outputs.pre_topo_fdr_map = self._gen_pre_topo_map_filename()
for line in runtime.stdout.split("\n"):
if line.startswith("activation_forced = "):
outputs.activation_forced = (
line[len("activation_forced = ") :].strip() == "1"
)
elif line.startswith("n_clusters = "):
outputs.n_clusters = int(line[len("n_clusters = ") :].strip())
elif line.startswith("pre_topo_n_clusters = "):
outputs.pre_topo_n_clusters = int(
line[len("pre_topo_n_clusters = ") :].strip()
)
elif line.startswith("cluster_forming_thr = "):
outputs.cluster_forming_thr = float(
line[len("cluster_forming_thr = ") :].strip()
)
return outputs
def _list_outputs(self):
outputs = self._outputs().get()
outputs["thresholded_map"] = self._gen_thresholded_map_filename()
outputs["pre_topo_fdr_map"] = self._gen_pre_topo_map_filename()
return outputs
class ThresholdStatisticsInputSpec(SPMCommandInputSpec):
spm_mat_file = File(
exists=True, desc="absolute path to SPM.mat", copyfile=True, mandatory=True
)
stat_image = File(exists=True, desc="stat image", copyfile=False, mandatory=True)
contrast_index = traits.Int(
mandatory=True, desc="which contrast in the SPM.mat to use"
)
height_threshold = traits.Float(
desc=("stat value for initial thresholding (defining clusters)"), mandatory=True
)
extent_threshold = traits.Int(
0, usedefault=True, desc="Minimum cluster size in voxels"
)
class ThresholdStatisticsOutputSpec(TraitedSpec):
voxelwise_P_Bonf = traits.Float()
voxelwise_P_RF = traits.Float()
voxelwise_P_uncor = traits.Float()
voxelwise_P_FDR = traits.Float()
clusterwise_P_RF = traits.Float()
clusterwise_P_FDR = traits.Float()
class ThresholdStatistics(SPMCommand):
"""Given height and cluster size threshold calculate theoretical
probabilities concerning false positives
Examples
--------
>>> thresh = ThresholdStatistics()
>>> thresh.inputs.spm_mat_file = 'SPM.mat'
>>> thresh.inputs.stat_image = 'spmT_0001.img'
>>> thresh.inputs.contrast_index = 1
>>> thresh.inputs.height_threshold = 4.56
>>> thresh.run() # doctest: +SKIP
"""
input_spec = ThresholdStatisticsInputSpec
output_spec = ThresholdStatisticsOutputSpec
def _make_matlab_command(self, _):
script = "con_index = %d;\n" % self.inputs.contrast_index
script += "cluster_forming_thr = %f;\n" % self.inputs.height_threshold
script += "stat_filename = '%s';\n" % self.inputs.stat_image
script += "extent_threshold = %d;\n" % self.inputs.extent_threshold
script += "load '%s'\n" % self.inputs.spm_mat_file
script += """
FWHM = SPM.xVol.FWHM;
df = [SPM.xCon(con_index).eidf SPM.xX.erdf];
STAT = SPM.xCon(con_index).STAT;
R = SPM.xVol.R;
S = SPM.xVol.S;
n = 1;
voxelwise_P_Bonf = spm_P_Bonf(cluster_forming_thr,df,STAT,S,n)
voxelwise_P_RF = spm_P_RF(1,0,cluster_forming_thr,df,STAT,R,n)
stat_map_vol = spm_vol(stat_filename);
[stat_map_data, stat_map_XYZmm] = spm_read_vols(stat_map_vol);
Z = stat_map_data(:);
Zum = Z;
switch STAT
case 'Z'
VPs = (1-spm_Ncdf(Zum)).^n;
voxelwise_P_uncor = (1-spm_Ncdf(cluster_forming_thr)).^n
case 'T'
VPs = (1 - spm_Tcdf(Zum,df(2))).^n;
voxelwise_P_uncor = (1 - spm_Tcdf(cluster_forming_thr,df(2))).^n
case 'X'
VPs = (1-spm_Xcdf(Zum,df(2))).^n;
voxelwise_P_uncor = (1-spm_Xcdf(cluster_forming_thr,df(2))).^n
case 'F'
VPs = (1 - spm_Fcdf(Zum,df)).^n;
voxelwise_P_uncor = (1 - spm_Fcdf(cluster_forming_thr,df)).^n
end
VPs = sort(VPs);
voxelwise_P_FDR = spm_P_FDR(cluster_forming_thr,df,STAT,n,VPs)
V2R = 1/prod(FWHM(stat_map_vol.dim > 1));
clusterwise_P_RF = spm_P_RF(1,extent_threshold*V2R,cluster_forming_thr,df,STAT,R,n)
[x,y,z] = ind2sub(size(stat_map_data),(1:numel(stat_map_data))');
XYZ = cat(1, x', y', z');
[u, CPs, ue] = spm_uc_clusterFDR(0.05,df,STAT,R,n,Z,XYZ,V2R,cluster_forming_thr);
clusterwise_P_FDR = spm_P_clusterFDR(extent_threshold*V2R,df,STAT,R,n,cluster_forming_thr,CPs')
"""
return script
def aggregate_outputs(self, runtime=None, needed_outputs=None):
outputs = self._outputs()
cur_output = ""
for line in runtime.stdout.split("\n"):
if cur_output != "" and len(line.split()) != 0:
setattr(outputs, cur_output, float(line))
cur_output = ""
continue
if len(line.split()) != 0 and line.split()[0] in [
"clusterwise_P_FDR",
"clusterwise_P_RF",
"voxelwise_P_Bonf",
"voxelwise_P_FDR",
"voxelwise_P_RF",
"voxelwise_P_uncor",
]:
cur_output = line.split()[0]
continue
return outputs
class FactorialDesignInputSpec(SPMCommandInputSpec):
spm_mat_dir = Directory(
exists=True, field="dir", desc="directory to store SPM.mat file (opt)"
)