forked from markmikkelsen/Gannet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GannetSegment.m
421 lines (347 loc) · 17.7 KB
/
GannetSegment.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
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
function MRS_struct = GannetSegment(MRS_struct)
% Relies on SPM12 being installed
%
% Runs segmentation script if segmented images not present according to
% file convention of c1, c2 and c3 as prefixes on the anatomical image name
% for the GM, WM and CSF segmentations. If these files are present, they
% are loaded and used for the voxel segmentation
if nargin == 0
fprintf('\n');
error('MATLAB:minrhs', 'Not enough input arguments.');
end
MRS_struct.version.segment = '230729';
warning('off'); % temporarily suppress warning messages
% First check if SPM12 is installed and on the search path
spm_version = fileparts(which('spm'));
if isempty(spm_version)
msg = 'SPM not found! Please install SPM12 and make sure it is in your search path.';
msg = hyperlink('https://www.fil.ion.ucl.ac.uk/spm/software/spm12/', 'SPM12', msg);
fprintf('\n');
error(msg);
elseif strcmpi(spm_version(end-3:end), 'spm8')
msg = ['SPM8 detected. Gannet no longer supports SPM8. ' ...
'Please install SPM12 and make sure it is in your search path.'];
msg = hyperlink('https://www.fil.ion.ucl.ac.uk/spm/software/spm12/', 'SPM12', msg);
fprintf('\n');
error(msg);
end
if MRS_struct.p.PRIAM
vox = MRS_struct.p.vox;
else
vox = MRS_struct.p.vox(1);
end
run_count = 0;
setup_spm = 1;
% Loop over voxels if PRIAM
for kk = 1:length(vox)
for ii = 1:MRS_struct.p.numScans
% 1. Take NIfTI from GannetCoRegister and segment it in SPM
[T1dir, T1name, T1ext] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
struc = MRS_struct.mask.(vox{kk}).T1image{ii};
% Check to see if segmentation has already been done (and all
% probability tissue maps are present)
tmp = {[T1dir '/c1' T1name T1ext]
[T1dir '/c2' T1name T1ext]
[T1dir '/c3' T1name T1ext]
[T1dir '/c6' T1name T1ext]};
filesExist = zeros(1,length(tmp));
for jj = 1:length(tmp)
filesExist(jj) = exist(tmp{jj}, 'file');
end
if ~all(filesExist)
if setup_spm
% Set up SPM for batch processing (do it once per batch)
spm('defaults','fmri');
spm_jobman('initcfg');
setup_spm = 0;
end
if kk == 1 && ii == 1
fprintf('\nSegmenting %s...', [T1name T1ext]);
else
fprintf('Segmenting %s...', [T1name T1ext]);
end
CallSPM12segmentation(struc);
else
if kk == 1 && ii == 1
fprintf('\n%s has already been segmented...\n', [T1name T1ext]);
else
fprintf('%s has already been segmented...\n', [T1name T1ext]);
end
end
% 2. Calculate QC metrics and GM, WM, and CSF fractions for each voxel
if strcmp(T1dir, '')
T1dir = '.';
end
% Tissue probability maps
GM = [T1dir '/c1' T1name T1ext];
WM = [T1dir '/c2' T1name T1ext];
CSF = [T1dir '/c3' T1name T1ext];
air = [T1dir '/c6' T1name T1ext];
GMvol = spm_vol(GM);
WMvol = spm_vol(WM);
CSFvol = spm_vol(CSF);
airvol = spm_vol(air);
% Segmentation quality metrics (Chua et al. JMRI, 2009; Ganzetti et
% al. Front. Neuroinform., 2016; Esteban et al. PLOS One, 2017)
T1 = spm_vol(struc);
T1_tmp = T1.private.dat(:,:,:);
WMvol_tmp = WMvol.private.dat(:,:,:);
WMvol_tmp(WMvol_tmp < 0.9) = NaN;
WMvol_thresh = WMvol_tmp .* T1_tmp;
WMvol_thresh = WMvol_thresh(:);
GMvol_tmp = GMvol.private.dat(:,:,:);
GMvol_tmp(GMvol_tmp < 0.9) = NaN;
GMvol_thresh = GMvol_tmp .* T1_tmp;
GMvol_thresh = GMvol_thresh(:);
airvol_tmp = airvol.private.dat(:,:,:);
airvol_tmp(airvol_tmp < 0.9) = NaN;
airvol_thresh = airvol_tmp .* T1_tmp;
airvol_thresh = airvol_thresh(:);
MRS_struct.out.QA.CV_WM(ii) = std(WMvol_thresh, 'omitnan') / mean(WMvol_thresh, 'omitnan');
MRS_struct.out.QA.CV_GM(ii) = std(GMvol_thresh, 'omitnan') / mean(GMvol_thresh, 'omitnan');
MRS_struct.out.QA.CJV(ii) = (std(WMvol_thresh, 'omitnan') + std(GMvol_thresh, 'omitnan')) ...
/ abs(mean(WMvol_thresh, 'omitnan') - mean(GMvol_thresh, 'omitnan'));
MRS_struct.out.QA.CNR(ii) = abs(mean(WMvol_thresh, 'omitnan') - mean(GMvol_thresh, 'omitnan')) / ...
sqrt(var(airvol_thresh, 'omitnan') + var(WMvol_thresh, 'omitnan') + var(GMvol_thresh, 'omitnan'));
T1_tmp = T1_tmp(:);
n_vox = numel(T1_tmp);
efc_max = n_vox * (1/sqrt(n_vox)) * log(1/sqrt(n_vox));
b_max = sqrt(sum(T1_tmp.^2));
MRS_struct.out.QA.EFC(ii) = (1/efc_max) .* sum((T1_tmp ./ b_max) .* log((T1_tmp + eps) ./ b_max));
% Voxel mask
voxmaskvol = spm_vol(MRS_struct.mask.(vox{kk}).outfile{ii});
[a,b,c] = fileparts(voxmaskvol.fname);
% GM
O_GMvox.fname = fullfile(a, [b '_GM' c]);
O_GMvox.descrip = 'MRS_voxel_mask_GM';
O_GMvox.dim = voxmaskvol.dim;
O_GMvox.dt = voxmaskvol.dt;
O_GMvox.mat = voxmaskvol.mat;
GM_voxmask_vol = GMvol.private.dat(:,:,:) .* voxmaskvol.private.dat(:,:,:);
O_GMvox = spm_write_vol(O_GMvox, GM_voxmask_vol);
% WM
O_WMvox.fname = fullfile(a, [b '_WM' c]);
O_WMvox.descrip = 'MRS_voxel_mask_WM';
O_WMvox.dim = voxmaskvol.dim;
O_WMvox.dt = voxmaskvol.dt;
O_WMvox.mat = voxmaskvol.mat;
WM_voxmask_vol = WMvol.private.dat(:,:,:) .* voxmaskvol.private.dat(:,:,:);
O_WMvox = spm_write_vol(O_WMvox, WM_voxmask_vol);
% CSF
O_CSFvox.fname = fullfile(a, [b '_CSF' c]);
O_CSFvox.descrip = 'MRS_voxel_mask_CSF';
O_CSFvox.dim = voxmaskvol.dim;
O_CSFvox.dt = voxmaskvol.dt;
O_CSFvox.mat = voxmaskvol.mat;
CSF_voxmask_vol = CSFvol.private.dat(:,:,:) .* voxmaskvol.private.dat(:,:,:);
O_CSFvox = spm_write_vol(O_CSFvox, CSF_voxmask_vol);
% 3. Calculate a CSF-corrected i.u. value and output it to the structure
GMsum = sum(sum(sum(O_GMvox.private.dat(:,:,:))));
WMsum = sum(sum(sum(O_WMvox.private.dat(:,:,:))));
CSFsum = sum(sum(sum(O_CSFvox.private.dat(:,:,:))));
fGM = GMsum / (GMsum + WMsum + CSFsum);
fWM = WMsum / (GMsum + WMsum + CSFsum);
fCSF = CSFsum / (GMsum + WMsum + CSFsum);
MRS_struct.out.(vox{kk}).tissue.fGM(ii) = fGM;
MRS_struct.out.(vox{kk}).tissue.fWM(ii) = fWM;
MRS_struct.out.(vox{kk}).tissue.fCSF(ii) = fCSF;
% Correction of institutional units only feasible if water-scaling
% is performed, skip otherwise
if strcmp(MRS_struct.p.reference, 'H2O')
target = [MRS_struct.p.target, {'Cr'}, {'Cho'}, {'NAA'}]; % Add Cr, Cho, and NAA
for jj = 1:length(target)
if strcmp(target{jj}, 'GABAGlx')
MRS_struct.out.(vox{kk}).GABA.ConcIU_CSFcorr(ii) = ...
MRS_struct.out.(vox{kk}).GABA.ConcIU(ii) / (1 - fCSF);
MRS_struct.out.(vox{kk}).Glx.ConcIU_CSFcorr(ii) = ...
MRS_struct.out.(vox{kk}).Glx.ConcIU(ii) / (1 - fCSF);
else
MRS_struct.out.(vox{kk}).(target{jj}).ConcIU_CSFcorr(ii) = ...
MRS_struct.out.(vox{kk}).(target{jj}).ConcIU(ii) / (1 - fCSF);
end
end
end
% 4. Build output
if ishandle(104)
clf(104);
end
if MRS_struct.p.hide
h = figure('Visible', 'off');
else
h = figure(104);
end
% Open figure in center of screen
scr_sz = get(0,'ScreenSize');
fig_w = 1000;
fig_h = 707;
set(h,'Position',[(scr_sz(3)-fig_w)/2, (scr_sz(4)-fig_h)/2, fig_w, fig_h]);
set(h,'Color',[1 1 1]);
figTitle = 'GannetSegment Output';
set(h,'Name',figTitle,'Tag',figTitle,'NumberTitle','off');
% Output results
ha = subplot(2,3,4:6);
pos = get(ha, 'Position');
set(ha, 'Position', [0 pos(2) 1 pos(4)]);
axis off;
text_pos = 1;
if strcmp(MRS_struct.p.vendor, 'Siemens_rda')
[~,tmp2,tmp3] = fileparts(MRS_struct.metabfile{1,ii*2-1});
else
[~,tmp2,tmp3] = fileparts(MRS_struct.metabfile{1,ii});
end
fname = [tmp2 tmp3];
if length(fname) > 30
fname = [fname(1:12) '...' fname(end-11:end)];
end
text(0.5, text_pos-0.12, 'Filename: ', 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.12, [' ' fname], 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13, 'Interpreter', 'none');
[~,tmp2,tmp3] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
T1image = [tmp2 tmp3];
if length(T1image) > 30
T1image = [T1image(1:12) '...' T1image(end-11:end)];
end
text(0.5, text_pos-0.24, 'Anatomical image: ', 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.24, [' ' T1image], 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13, 'Interpreter', 'none');
% Print correction of institutional units only feasible if water-scaling is performed, skip otherwise
text_pos = text_pos-0.24;
if strcmp(MRS_struct.p.reference, 'H2O')
target = MRS_struct.p.target;
tmp = strcmp(target, 'GABAGlx');
if any(tmp)
if MRS_struct.p.HERMES
target = {'GABA','Glx',target{~tmp}};
else
target = {'GABA','Glx'};
end
end
for jj = 1:length(target)
text_pos = text_pos - 0.12;
switch target{jj}
case 'GABA'
tmp1 = 'GABA+/Water (CSF-corrected): ';
tmp2 = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).GABA.ConcIU_CSFcorr(ii));
case 'Lac'
tmp1 = 'Lac+MM/Water (CSF-corrected): ';
tmp2 = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).Lac.ConcIU_CSFcorr(ii));
case {'Glx','GSH','EtOH'}
tmp1 = [target{jj} '/Water (CSF-corrected): '];
tmp2 = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).(target{jj}).ConcIU_CSFcorr(ii));
end
text(0.5, text_pos, tmp1, 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos, tmp2, 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
end
end
tmp = sprintf(' %.2f', MRS_struct.out.(vox{kk}).tissue.fGM(ii));
text(0.5, text_pos-0.12, 'GM voxel fraction: ', 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.12, tmp, 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
tmp = sprintf(' %.2f', MRS_struct.out.(vox{kk}).tissue.fWM(ii));
text(0.5, text_pos-0.24, 'WM voxel fraction: ', 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.24, tmp, 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
tmp = sprintf(' %.2f', MRS_struct.out.(vox{kk}).tissue.fCSF(ii));
text(0.5, text_pos-0.36, 'CSF voxel fraction: ', 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.36, tmp, 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.48, 'SegmentVer: ', 'Units', 'normalized', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.48, [' ' MRS_struct.version.segment], 'Units', 'normalized', 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
% Voxel segmentation
if isfield(MRS_struct.p,'TablePosition')
voxoff = MRS_struct.p.voxoff(ii,:) + MRS_struct.p.TablePosition(ii,:);
else
voxoff = MRS_struct.p.voxoff(ii,:);
end
% Plot segmented voxel as a montage
MRS_struct.mask.(vox{kk}).img_montage{ii} = PlotSegmentedVoxels(struc, voxoff, voxmaskvol, O_GMvox, O_WMvox, O_CSFvox);
if strcmp(MRS_struct.p.vendor, 'Siemens_rda')
[~,tmp,tmp2] = fileparts(MRS_struct.metabfile{1,ii*2-1});
else
[~,tmp,tmp2] = fileparts(MRS_struct.metabfile{1,ii});
end
fname = [tmp tmp2];
if length(fname) > 30
fname = [fname(1:12) '...' fname(end-11:end)];
end
[~,tmp3,tmp4] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
T1image = [tmp3 tmp4];
if length(T1image) > 30
T1image = [T1image(1:12) '...' T1image(end-11:end)];
end
t = ['Voxel from ' fname ' on ' T1image];
title(t, 'FontName', 'Arial', 'FontSize', 15, 'Interpreter', 'none');
% Save output as PDF
run_count = SavePDF(h, MRS_struct, ii, 1, kk, vox, mfilename, run_count);
end
if MRS_struct.p.mat % save MRS_struct as mat file
mat_name = fullfile(pwd, ['MRS_struct_' vox{kk} '.mat']);
if exist(mat_name, 'file')
fprintf('\nUpdating results in %s\n', ['MRS_struct_' vox{kk} '.mat...']);
else
fprintf('\nSaving results to %s\n', ['MRS_struct_' vox{kk} '.mat...']);
end
save(mat_name, 'MRS_struct', '-v7.3');
end
if MRS_struct.p.csv % export MRS_struct fields into csv file
MRS_struct = ExportToCSV(MRS_struct, vox{kk}, 'segment');
end
end
warning('on'); % turn warnings back on
% Need to close hidden figures to show figures after Gannet is done running
if MRS_struct.p.hide && exist('figTitle','var')
close(figTitle);
end
function img_montage = PlotSegmentedVoxels(struc, voxoff, voxmaskvol, O_GMvox, O_WMvox, O_CSFvox)
img_t = flipud(voxel2world_space(spm_vol(struc), voxoff));
mask_t = flipud(voxel2world_space(voxmaskvol, voxoff));
mask_t_GM = flipud(voxel2world_space(O_GMvox, voxoff));
mask_t_WM = flipud(voxel2world_space(O_WMvox, voxoff));
mask_t_CSF = flipud(voxel2world_space(O_CSFvox, voxoff));
w_t = zeros(size(img_t));
tmp = img_t(:);
img_t = repmat(img_t / (mean(tmp(tmp > 0.01)) + 3*std(tmp(tmp > 0.01))), [1 1 3]);
img_t_GM = img_t;
img_t_WM = img_t;
img_t_CSF = img_t;
c_img_t = zeros(size(img_t));
% Voxel mask
vox_mx = 1;
vox_mn = 0;
mask_t(mask_t(:) < vox_mn) = vox_mn;
mask_t(mask_t(:) > vox_mx) = vox_mx;
mask_t = (mask_t - vox_mn) / (vox_mx - vox_mn);
mask_t_GM(mask_t_GM(:) < vox_mn) = vox_mn;
mask_t_GM(mask_t_GM(:) > vox_mx) = vox_mx;
mask_t_GM = (mask_t_GM - vox_mn) / (vox_mx - vox_mn);
mask_t_WM(mask_t_WM(:) < vox_mn) = vox_mn;
mask_t_WM(mask_t_WM(:) > vox_mx) = vox_mx;
mask_t_WM = (mask_t_WM - vox_mn) / (vox_mx - vox_mn);
mask_t_CSF(mask_t_CSF(:) < vox_mn) = vox_mn;
mask_t_CSF(mask_t_CSF(:) > vox_mx) = vox_mx;
mask_t_CSF = (mask_t_CSF - vox_mn) / (vox_mx - vox_mn);
mask_t = 0.5 * mask_t;
mask_t_GM = 0.8 * mask_t_GM;
mask_t_WM = 0.8 * mask_t_WM;
mask_t_CSF = 0.8 * mask_t_CSF;
vox_color = [1 1 0];
c_mask_t = c_img_t + cat(3, mask_t * vox_color(1,1), mask_t * vox_color(1,2), mask_t * vox_color(1,3));
c_mask_t_GM = c_img_t + cat(3, mask_t_GM * vox_color(1,1), mask_t_GM * vox_color(1,2), mask_t_GM * vox_color(1,3));
c_mask_t_WM = c_img_t + cat(3, mask_t_WM * vox_color(1,1), mask_t_WM * vox_color(1,2), mask_t_WM * vox_color(1,3));
c_mask_t_CSF = c_img_t + cat(3, mask_t_CSF * vox_color(1,1), mask_t_CSF * vox_color(1,2), mask_t_CSF * vox_color(1,3));
w_mask_t = w_t + mask_t;
w_mask_t_GM = w_t + mask_t_GM;
w_mask_t_WM = w_t + mask_t_WM;
w_mask_t_CSF = w_t + mask_t_CSF;
img_t = repmat(1 - w_mask_t, [1 1 3]) .* img_t + c_mask_t;
img_t_GM = repmat(1 - w_mask_t_GM, [1 1 3]) .* img_t_GM + c_mask_t_GM;
img_t_WM = repmat(1 - w_mask_t_WM, [1 1 3]) .* img_t_WM + c_mask_t_WM;
img_t_CSF = repmat(1 - w_mask_t_CSF, [1 1 3]) .* img_t_CSF + c_mask_t_CSF;
img_t(img_t < 0) = 0; img_t(img_t > 1) = 1;
img_t_GM(img_t_GM < 0) = 0; img_t_GM(img_t_GM > 1) = 1;
img_t_WM(img_t_WM < 0) = 0; img_t_WM(img_t_WM > 1) = 1;
img_t_CSF(img_t_CSF < 0) = 0; img_t_CSF(img_t_CSF > 1) = 1;
img_montage = cat(2, img_t, img_t_GM, img_t_WM, img_t_CSF);
hb = subplot(2,3,1:3);
imagesc(img_montage);
axis equal tight off;
text(floor(size(mask_t,2)/2), 20, 'Voxel', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
text(floor(size(mask_t,2)) + floor(size(mask_t,2)/2), 20, 'GM', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
text(2*floor(size(mask_t,2)) + floor(size(mask_t,2)/2), 20, 'WM', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
text(3*floor(size(mask_t,2)) + floor(size(mask_t,2)/2), 20, 'CSF', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
set(hb, 'Position', [0 0.17 1 1]);