forked from jsheunis/matlab-spm-scripts-jsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thePlotSpmPreproc.m
208 lines (195 loc) · 8.28 KB
/
thePlotSpmPreproc.m
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
function output = thePlotSpmPreproc(functional4D_fn, structural_fn, fwhm, spm_dir)
% Function to complete preprocessing of structural and functional data from
% a single subject for use in thePlotSpm.m
%
% Steps include coregistering structural image to first functional image,
% segmenting the coregistered structural image into tissue types, and
% reslicing the segments to the functional resolution image grid.
%
% Makes use of spm12 batch routines.
% If spm12 batch parameters are not explicitly set, defaults are assumed.
%
% INPUT:
% funcional4D_fn - filename of pre-real-time functional scan
% structural_fn - filename of T1-weighted structural scan
% fwhm - kernel size for smoothing operations
%
% OUTPUT:
% output - structure with filenames and data
%__________________________________________________________________________
% Copyright (C) Stephan Heunis
% Load data
f4D_img = spm_read_vols(spm_vol(functional4D_fn));
[Ni, Nj, Nk, Nt] = size(f4D_img);
% Declare output structure
output = struct;
% STEP 1 -- Realign (estimate and reslice) all functionals to first functional
disp('Step 1...');
spm('defaults','fmri');
spm_jobman('initcfg');
realign_estimate_reslice = struct;
% Data
fnms={};
for i = 1:Nt
fnms{i} = [functional4D_fn ',' num2str(i) ];
end
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.data={fnms'};
% Eoptions
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.sep = 4;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.interp = 2;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.weight = '';
% Roptions
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.which = [2 1];
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.interp = 4;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.mask = 1;
realign_estimate_reslice.matlabbatch{1}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
% Run
spm_jobman('run',realign_estimate_reslice.matlabbatch);
[d, f, e] = fileparts(functional4D_fn);
output.rfunctional_fn = [d filesep 'r' f e];
output.mp_fn = [d filesep 'rp_' f '.txt'];
output.MP = load(output.mp_fn);
disp('Step 1 - Done!');
% STEP 2 -- Coregister structural image to first dynamic image (estimate only)
disp('Step 2...');
spm('defaults','fmri');
spm_jobman('initcfg');
coreg_estimate = struct;
% Ref
coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.ref = {[functional4D_fn ',1']};
% Source
coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.source = {structural_fn};
% % Other
% coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estwrite.other = {};
% Eoptions
coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.tol = [0.02 0.02 0.02 0.001 0.001 0.001 0.01 0.01 0.01 0.001 0.001 0.001];
coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
% % Roptions
% coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.interp = 4;
% coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.wrap = [0 0 0];
% coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.mask = 0;
% coreg_estimate.matlabbatch{1}.spm.spatial.coreg.estwrite.roptions.prefix = 'r';
% Run
spm_jobman('run',coreg_estimate.matlabbatch);
disp('Step 2 - Done!');
% STEP 3 -- Segmentation of coregistered structural image into GM, WM, CSF, etc
% (with implicit warping to MNI space, saving forward and inverse transformations)
disp('Step 3...');
spm('defaults','fmri');
spm_jobman('initcfg');
segmentation = struct;
% Channel
segmentation.matlabbatch{1}.spm.spatial.preproc.channel.biasreg = 0.001;
segmentation.matlabbatch{1}.spm.spatial.preproc.channel.biasfwhm = 60;
segmentation.matlabbatch{1}.spm.spatial.preproc.channel.write = [0 1];
segmentation.matlabbatch{1}.spm.spatial.preproc.channel.vols = {structural_fn};
% Tissue
for t = 1:6
segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).tpm = {[spm_dir filesep 'tpm' filesep 'TPM.nii,' num2str(t)]};
segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).ngaus = t-1;
segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).native = [1 0];
segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(t).warped = [0 0];
end
segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(1).ngaus = 1;
segmentation.matlabbatch{1}.spm.spatial.preproc.tissue(6).ngaus = 2;
% Warp
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.mrf = 1;
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.cleanup = 1;
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.reg = [0 0.001 0.5 0.05 0.2];
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.affreg = 'mni';
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.fwhm = 0;
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.samp = 3;
segmentation.matlabbatch{1}.spm.spatial.preproc.warp.write=[1 1];
% Run
spm_jobman('run',segmentation.matlabbatch);
% Save filenames
[d, f, e] = fileparts(structural_fn);
output.forward_transformation = [d filesep 'y_' f e];
output.inverse_transformation = [d filesep 'iy_' f e];
output.gm_fn = [d filesep 'c1' f e];
output.wm_fn = [d filesep 'c2' f e];
output.csf_fn = [d filesep 'c3' f e];
output.bone_fn = [d filesep 'c4' f e];
output.soft_fn = [d filesep 'c5' f e];
output.air_fn = [d filesep 'c6' f e];
disp('Step 3 - done!');
% STEP 4 -- Reslice all to functional-resolution image grid
disp('Step 4...');
spm('defaults','fmri');
spm_jobman('initcfg');
reslice = struct;
% Ref
reslice.matlabbatch{1}.spm.spatial.coreg.write.ref = {[functional4D_fn ',1']};
% Source
source_fns = {};
for i = 1:6
source_fns{i} = [d filesep 'c' num2str(i) f e];
end
source_fns{7} = structural_fn;
reslice.matlabbatch{1}.spm.spatial.coreg.write.source = source_fns';
% Roptions
reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.interp = 4;
reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.wrap = [0 0 0];
reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.mask = 0;
reslice.matlabbatch{1}.spm.spatial.coreg.write.roptions.prefix = 'r';
% Run
spm_jobman('run',reslice.matlabbatch);
% Save filenames
[d, f, e] = fileparts(structural_fn);
output.rstructural_fn = [d filesep 'r' f e];
output.rgm_fn = [d filesep 'rc1' f e];
output.rwm_fn = [d filesep 'rc2' f e];
output.rcsf_fn = [d filesep 'rc3' f e];
output.rbone_fn = [d filesep 'rc4' f e];
output.rsoft_fn = [d filesep 'rc5' f e];
output.rair_fn = [d filesep 'rc6' f e];
disp('Step 4 - done!');
% STEP 5 -- Gaussian kernel smoothing of realigned data
disp('Step 5...');
spm('defaults','fmri');
spm_jobman('initcfg');
smooth = struct;
% Data
fns={};
for i = 1:Nt
fns{i} = [output.rfunctional_fn ',' num2str(i) ];
end
smooth.matlabbatch{1}.spm.spatial.smooth.data = fns';
% Other
smooth.matlabbatch{1}.spm.spatial.smooth.fwhm = [fwhm fwhm fwhm];
smooth.matlabbatch{1}.spm.spatial.smooth.dtype = 0;
smooth.matlabbatch{1}.spm.spatial.smooth.im = 0;
smooth.matlabbatch{1}.spm.spatial.smooth.prefix = 's';
% Run
spm_jobman('run',smooth.matlabbatch);
[d, f, e] = fileparts(output.rfunctional_fn);
output.srfunctional_fn = [d filesep 's' f e];
disp('Step 5 - done!');
% STEP 6 -- Gaussian kernel smoothing of unprocessed data
disp('Step 6...');
spm('defaults','fmri');
spm_jobman('initcfg');
smooth = struct;
% Data
fns={};
for i = 1:Nt
fns{i} = [functional4D_fn ',' num2str(i) ];
end
smooth.matlabbatch{1}.spm.spatial.smooth.data = fns';
% Other
smooth.matlabbatch{1}.spm.spatial.smooth.fwhm = [fwhm fwhm fwhm];
smooth.matlabbatch{1}.spm.spatial.smooth.dtype = 0;
smooth.matlabbatch{1}.spm.spatial.smooth.im = 0;
smooth.matlabbatch{1}.spm.spatial.smooth.prefix = 's';
% Run
spm_jobman('run',smooth.matlabbatch);
[d, f, e] = fileparts(functional4D_fn);
output.sfunctional_fn = [d filesep 's' f e];
disp('Step 6 - done!');