This repository has been archived by the owner on May 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswreport.m
176 lines (146 loc) · 5.51 KB
/
swreport.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
function [minall] = swreport(cfg)
%SWREPORT read output of swrand and make it in text
% this function has an output, which can be used by sw2csv to get the
% lowest p-value
mversion = 13;
%13 12/02/27 report R value as well
%12 12/02/23 report both
%11 12/01/16 report mean and sd instead of single points
%10 11/11/13 report single points for each design/function
%09 11/11/09 use ft_read_mri instead of external toolbox
%08 11/11/03 use atlasquery for interesting areas
%07 11/10/26 only load sp, sw or freq if used
%06 11/10/25 use cluster to report significant clusters
%05 11/10/20 correlation between rall and detd might become useful if I understand how FSL calculates correlations
%04 11/10/17 added mnival, it'll be used in sw2csv
%03 11/10/17 write down n of detected slow waves and spindles
%02 11/10/10 it works with multiple contrasts
%01 11/08/23 created
%---------------------------%
%-start log
output = sprintf('%s (v%02.f) started at %s on %s\n', ...
mfilename, mversion, datestr(now, 'HH:MM:SS'), datestr(now, 'dd-mmm-yy'));
tic_t = tic;
%---------------------------%
%-------------------------------------%
%-loop over designs
for d = 1:numel(cfg.swdes.des)
output = sprintf('%s\n#---------------------------#\n%s\n', output, cfg.swdes.des(d).name);
%-------------------------------%
%-loop over contrasts
for f = 1:numel(cfg.swdes.des(d).fun)
output = sprintf('%s#----------------------#\n%s\n', output, cfg.swdes.des(d).fun{f});
%-----------------%
%-save single points
load([cfg.desd cfg.swdes.des(d).name '_' num2str(f)], 'despnt')
sparam = sprintf('\t% 5.2f', despnt);
outval = sprintf(' mean % 5.2f s.d. % 5.2f\n%s\n\n', mean(despnt), std(despnt), sparam);
output = sprintf('%s%s\n', output, outval');
%-----------------%
%---------------------------%
%-loop over types
for i = 1:numel(cfg.preprdti.type)
if ~strcmp(cfg.preprdti.type{i}(1), 'V')
output = sprintf('%s#-----------------#\n%s\n', output, cfg.preprdti.type{i});
%-------------------%
%-loop over tests
for t = 1:numel(cfg.swreport.test)
output = sprintf('%s#-------#\nIMG:%s FUN:%s\n', output, cfg.swreport.test(t).img, func2str(cfg.swreport.test(t).fun));
%---------%
%-read image
rimg = [cfg.rand 'swdti_' cfg.swdes.des(d).name '_' cfg.preprdti.type{i} cfg.swreport.test(t).img num2str(f) '.nii.gz'];
rall = [cfg.tbss 'stats/all_' cfg.preprdti.type{i} '.nii.gz'];
%---------%
%---------%
%-find in img
[found minval] = find_in_img(cfg.swreport.test(t).fun, rimg); %, rall, alldet{f+bf});
output = [output found];
%---------%
%---------%
%-keep the min p-value
if t == cfg.test2write
minall{d}{f}{i} = minval;
end
%---------%
output = sprintf('%s#-------#\n', output);
end
%-------------------%
output = sprintf('%s#-----------------#\n', output);
end
end
%---------------------------%
output = sprintf('%s#----------------------#\n', output);
end
%-------------------------------%
%-------------------%
%-write output
output = sprintf('%s#---------------------------#\n', output);
%-------------------%
end
%-------------------------------------%
%---------------------------%
%-end log
toc_t = toc(tic_t);
outtmp = sprintf('%s (v%02.f) ended at %s on %s after %s\n\n', ...
mfilename, mversion, datestr(now, 'HH:MM:SS'), datestr(now, 'dd-mmm-yy'), ...
datestr( datenum(0, 0, 0, 0, 0, toc_t), 'HH:MM:SS'));
output = [output outtmp];
%-----------------%
fprintf(output)
fid = fopen([cfg.log '.txt'], 'a');
fwrite(fid, output);
fclose(fid);
%-----------------%
%---------------------------%
function [found minval] = find_in_img(fun, rimg) %, rall, detd)
%where fun is based on max with two outputs:
% fun = @max
% fun = @(x)deal(1-x, find(x > .95)) -> get significant voxels and transform their value in normal p-values (from FSL p-values)
%-------%
%-from tstat to R
t2r = @(t, n) sqrt((t^2)/(t^2 + (n-2)));
n = 14; % number of subjects...
%-------%
%-------%
%-read image
img = ft_read_mri(rimg);
tstat = NaN;
if strfind(rimg, '_tfce_corrp_')
timg = ft_read_mri(strrep(rimg, '_tfce_corrp_', '_'));
tstat = 1;
end
%-------%
%-------%
%-calculate
[v i] = feval(fun, img.anatomy(:));
[x, y, z] = ind2sub(size(img.anatomy), i);
[xyz] = [x, y, z] - 1; % -1 is the convention used by FSL, not sure if it's standard
mni = [xyz ones(size(xyz,1),1)] * img.transform';
%-------%
%-----------------%
%-print
found = sprintf('%s\n\n', func2str(fun));
if size(mni,1) == 0
found = [found sprintf('no voxel below the threshold\n')];
minval = NaN;
else
[minval imin] = min(v(i));
if ~isnan(tstat)
tstat = t2r(timg.anatomy(imin), n)^2;
end
found = [found sprintf('%1.f voxels below the threshold, with min at %1.3f (r^2:%1.3f)\n', size(mni,1), min(v(i)), tstat)];
[~, outtmp] = system(['cluster -i ' rimg ' -t .95 --mm']);
if size(outtmp,2) > 112
found = sprintf('%s%s\n', found, outtmp);
%-------%
%-write which brain areas are involved
imgpath = fileparts(rimg);
imgthr = [imgpath '/img_thr.nii.gz'];
unix(['fslmaths ' rimg ' -thr .95 ' imgthr ]);
[~, areas] = unix(['atlasquery -a "JHU White-Matter Tractography Atlas" -m ' imgthr]);
delete(imgthr)
found = sprintf('%s%s\n', found, areas);
%-------%
end
end
%-----------------%