-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepSim_simulation.m
More file actions
283 lines (218 loc) · 9.27 KB
/
Copy pathrepSim_simulation.m
File metadata and controls
283 lines (218 loc) · 9.27 KB
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
%This code is largely copied from the AphaSim tool as implemeted in the REST
%toolbox
%Author: dr. Peter Van Schuerbeek (UZ Brussel - VUB)
function repSim_simulation(maskfile,fwhm,ind_type,pthr,thr_type,wwidth,mean_falff,sd_falff,pthr_group,nsub,iter,outdir,outname)
[maskpath, maskname, masketc]=fileparts(maskfile);
[mask,voxdim,header]=rp_readfile(maskfile);
[nx,ny,nz] = size(mask);
mask = logical(mask);
nxyz = numel(find(mask));
dvoxel=max(voxdim);
V=spm_vol(maskfile);
outfile=fullfile(outdir,[outname,'.txt']);
if fwhm(1) == 4
fwhm(1) = 4.55; %afni 4 : spm 4.55
end
if fwhm(2) == 4
fwhm(2) = 4.55; %afni 4 : spm 4.55
end
if fwhm(3) == 4
fwhm(3) = 4.55; %afni 4 : spm 4.55
end
count=nx*ny*nz;
counttabel = zeros(1,nsub+1);
count_sim = zeros(1,nsub+1);
max_count = zeros(1,nsub+1);
iter_grcount = zeros(iter,nsub+1);
iter_count = zeros(iter,nsub+1);
nclus_sim = [];
gr_count = zeros(1,nsub+1);
for nt = 1:iter
fprintf(['Performing iteration ' num2str(nt) ' of ' num2str(iter) '\n'])
countim = zeros(nx,ny,nz);
fim4d = zeros(nx,ny,nz,nsub);
for ns = 1:nsub
fim=randn(nx,ny,nz);
if fwhm(1)*fwhm(2)*fwhm(3) ~= 0
fim = gauss_filter(fwhm,fim,voxdim);
end
fim4d(:,:,:,ns)=fim;
if contains(thr_type,'overlap')
switch ind_type
case 'spmT'
fim = 1-normcdf(fim,mean(fim,'all'),std(fim,0,'all'));
fim2=fim;
if wwidth==0
fim(fim2<=pthr)=1;
fim(fim2>pthr)=0;
else
fim(fim2<=pthr)=1;
fim(fim2>pthr)=exp(-(1/2)*((fim2(fim2>pthr)-pthr)/wwidth).^2);
end
fim = fim.*mask;
clear fim2
case 'FALFF'
fim=sd_falff*rand(nx,ny,nz)+mean_falff;
fim(fim>=pthr)=1;
fim(fim<pthr)=0;
fim = fim.*mask;
end
countim = countim+fim;
end
end
meangr = mean(fim4d,4);
stdgr = std(fim4d,0,4);
ttestgr = meangr./(stdgr/sqrt(nsub));
ttestgr(~isfinite(ttestgr))=0;
ttestgr = 1-tcdf(ttestgr,nsub-1,'upper');
ttestgr2=ttestgr;
ttestgr(ttestgr2<=pthr_group)=1;
ttestgr(ttestgr2>pthr_group)=0;
ttestgr = ttestgr.*mask;
switch thr_type
case 'overlap'
for ci = 0:nsub
indmask = find(and(countim(mask)>=ci-0.5,countim(mask)<ci+0.5));
counttabel(ci+1) = counttabel(ci+1)+numel(indmask);
iter_count(nt,ci+1) = numel(indmask);
grmask=find(and(and(countim>=ci-0.5,countim<ci+0.5),ttestgr>0));
gr_count(ci+1) = gr_count(ci+1)+numel(grmask);
max_count(ci+1) = max(max_count(ci+1),numel(grmask));
iter_grcount(nt,ci+1) = numel(grmask);
if numel(grmask)>0; count_sim(ci+1)=count_sim(ci+1)+1; end
end
case 'clustersize'
[theCluster, theCount] = spm_bwlabel(ttestgr, 26);
isim_list = zeros(1,theCount);
for iclus=1:theCount
isim_list(iclus)=numel(find(theCluster==iclus));
end
for iclus=1:theCount
if numel(nclus_sim)<isim_list(iclus)
nclus_sim(isim_list(iclus)) = 1;
else
nclus_sim(isim_list(iclus)) = nclus_sim(isim_list(iclus)) + 1;
end
end
end
end
if exist(outfile,"file"), delete(outfile); end
switch thr_type
case 'overlap'
mean_count = round(mean(iter_grcount,1))';
sd_count = round(std(iter_grcount,0,1))';
prob_table = counttabel/(nxyz*iter);
gr_prob = gr_count/(nxyz*iter);
repsimulation.counttabel = counttabel;
repsimulation.prob_table = prob_table;
repsimulation.gr_count = gr_count;
repsimulation.gr_prob = gr_prob;
repsimulation.count_sim = count_sim;
repsimulation.mean_count = mean_count;
repsimulation.sd_count = sd_count;
repsimulation.max_count = max_count;
repsimulation.iter_count = iter_count;
repsim
repsimulation.resultfile = outfile;
repsimulation.maskfile = maskfile;
repsimulation.ind_type = ind_type;
repsimulation.fwhm = fwhm;
repsimulation.pthr_ind = pthr;
repsimulation.wwidth = wwidth;
repsimulation.pthr_group = pthr_group;
repsimulation.nsub = nsub;
repsimulation.iter = iter;
repsimulation.nxyz = nxyz;
ulation.iter_grcount = iter_grcount;
matfile = fullfile(outdir,[outname,'.mat']);
save(matfile,'repsimulation');
fid=fopen(sprintf('%s',outfile),'w');
fprintf(fid,'RepSim: Monte Carlo simulations to determine the probability of repeated false positive results');
fprintf(fid,'\nThis tool is bassed on AlphaSim as implemented in REST');
fprintf(fid,'\nAuthor: dr. Peter Van Schuerbeek (UZ Brussel - VUB)\n');
fprintf(fid,'\nType individual maps = %s',ind_type);
fprintf(fid,'\nMask filename = %s\n',maskfile);
fprintf(fid,'Voxels in mask = %d\n',nxyz);
fprintf(fid,'Gaussian filter width (FWHMx, in mm) = %.3f\n',fwhm(1));
fprintf(fid,'Gaussian filter width (FWHMy, in mm) = %.3f\n',fwhm(2));
fprintf(fid,'Gaussian filter width (FWHMz, in mm) = %.3f\n',fwhm(3));
fprintf(fid,'Individual voxel threshold probability = %.3f\n',pthr);
fprintf(fid,'Group voxel threshold probability = %.3f\n',pthr_group);
if wwidth>0
fprintf(fid,'Weidthed filter width = %.3f\n',wwidth);
fprintf(fid,'Applied weighting function:\n');
fprintf(fid,'1 if p<=pthres\n');
fprintf(fid,'exp(-(1/2)*((p-pthres)/width)^2) if p>pthres\n');
else
fprintf(fid,'No wighting filter applied\n');
end
fprintf(fid,'Number of subjects = %d\n',nsub);
fprintf(fid,'Number of Monte Carlo simulations = %d\n',iter);
fprintf(fid,['\nNumber of subjects (n)', ...
'\tPercentage of subjects',...
'\tFrequency =n',...
'\tProbability of =n', ...
'\tFreq significant group',...
'\tProbability significant group', ...
'\tFound in x simulations',...
'\tMean freq per simulation',...
'\tSD freq per simulation',...
'\tMax freq per simulation',...
'\tp corrected']);
for i=0:nsub
p_corrected = 1-(1-gr_prob(i+1))^nxyz;
fprintf(fid,'\n\t%d\t%d\t%d\t%.3e\t%d\t%.3e\t%d\t%d\t%d\t%d\t%.3e',i,(i/nsub)*100,counttabel(i+1),prob_table(i+1),gr_count(i+1),gr_prob(i+1),count_sim(i+1),mean_count(i+1),sd_count(i+1),max_count(i+1),p_corrected);
end
fclose(fid);
case 'clustersize'
clus_vox = nclus_sim;
tmp = find(nclus_sim>0);
clus_vox(tmp) = nclus_sim(tmp) .* tmp;
nvox_sim = [];
for iclussize=1:numel(nclus_sim)
nvox_sim(iclussize) = sum(clus_vox(iclussize:end));
end
fid=fopen(sprintf('%s',outfile),'w');
fprintf(fid,'RepSim: Monte Carlo simulations to determine the probability of false positive results');
fprintf(fid,'\nThis tool is bassed on AlphaSim as implemented in REST');
fprintf(fid,'\nAuthor: dr. Peter Van Schuerbeek (UZ Brussel - VUB)\n');
fprintf(fid,'\nType individual maps = %s',ind_type);
fprintf(fid,'\nMask filename = %s\n',maskfile);
fprintf(fid,'Voxels in mask = %d\n',nxyz);
fprintf(fid,'Gaussian filter width (FWHMx, in mm) = %.3f\n',fwhm(1));
fprintf(fid,'Gaussian filter width (FWHMy, in mm) = %.3f\n',fwhm(2));
fprintf(fid,'Gaussian filter width (FWHMz, in mm) = %.3f\n',fwhm(3));
fprintf(fid,'Group voxel threshold probability = %.3f\n',pthr_group);
fprintf(fid,'Number of subjects = %d\n',nsub);
fprintf(fid,'Number of Monte Carlo simulations = %d\n',iter);
fprintf(fid,['\nCluster size (n)', ...
'\tFrequency =n',...
'\tNumber of voxels if Ke>=n',...
'\tProbability']);
for i=1:numel(nclus_sim)
fprintf(fid,'\n\t%d\t%d\t%d\t%.3e',i,nclus_sim(i),nvox_sim(i),nvox_sim(i)/(nxyz*iter));
end
fclose(fid);
end
fprintf('Done\n')
end
function Q=gauss_filter(s,P,VOX)
if length(s) == 1; s = [s s s];end
s = s./VOX; % voxel anisotropy
s = max(s,ones(size(s))); % lower bound on FWHM
s = s/sqrt(8*log(2)); % FWHM -> Gaussian parameter
x = round(6*s(1)); x = [-x:x];
y = round(6*s(2)); y = [-y:y];
z = round(6*s(3)); z = [-z:z];
x = exp(-(x).^2/(2*(s(1)).^2));
y = exp(-(y).^2/(2*(s(2)).^2));
z = exp(-(z).^2/(2*(s(3)).^2));
x = x/sum(x);
y = y/sum(y);
z = z/sum(z);
i = (length(x) - 1)/2;
j = (length(y) - 1)/2;
k = (length(z) - 1)/2;
Q=P;
spm_conv_vol(P,Q,x,y,z,-[i,j,k]);
end