-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcvncheckfreesurferalignment.m
454 lines (414 loc) · 15.1 KB
/
cvncheckfreesurferalignment.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
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
function cvncheckfreesurferalignment(subjectid,outputdir,meanfunctional,skip,dims,figuredir)
% function cvncheckfreesurferalignment(subjectid,outputdir,meanfunctional,skip,dims,figuredir)
%
% <subjectid> is like 'C0001'
% <outputdir> is like '/home/stone-ext1/fmridata/20151008-ST001-kk,test/freesurferalignment'
% <meanfunctional> is like '/home/stone-ext1/fmridata/20151008-ST001-kk,test/preprocess/mean.nii'
% <skip> (optional) is number of slices to increment by. Default: 1.
% <dims> (optional) is a vector of slice dimensions to process. Default: [1 2 3].
% <figuredir> (optional) is where to write figures to. Default: <outputdir>.
%
% Write out diagnostic images of the FreeSurfer output that reflect alignment to the
% EPI functional data. The images show the mean functional volume with contours of
% the white and pial surfaces overlaid on these slices. We also create images that
% show slices through the T1 and T2 that reflect the alignment of the T1 and T2 to
% the EPI. The slices through the T1/T2 are matched exactly (field of view, resolution)
% to the mean functional volume.
%
% This function parallels cvncheckfreesurfer.m.
%
% history:
% - 2018/07/27 - substantial revamp of figure output; also add input <figuredir>
%
%
% THIS FUNCTION IS DEPRECATED due to the fstoint.m and vertex interpretation stuff.
% We keep it just for historical purposes.
fprintf('WARNING: THIS FUNCTION IS OBSOLETE!\n');
% internal constants
colors = {[0 .4 0] [0 1 0];
[0 .4 .4] [0 1 1]}; % LH is green, RH is cyan; white is darker, pial is lighter
% input
if ~exist('skip','var') || isempty(skip)
skip = 1;
end
if ~exist('dims','var') || isempty(dims)
dims = [1 2 3];
end
if ~exist('figuredir','var') || isempty(figuredir)
figuredir = outputdir;
end
% calc
fsdir = sprintf('%s/%s',cvnpath('freesurfer'),subjectid);
% load T1 anatomy (can either be standard 1-mm isotropic 256 x 256 x 256, or something else!)
file0 = sprintf('%s/mri/T1.nii.gz',fsdir);
t1vol = load_untouch_nii(file0);
t1volsize = t1vol.hdr.dime.pixdim(2:4);
t1vol = double(t1vol.img);
if t1vol.hdr.dime.scl_slope ~= 0
t1vol = t1vol * t1vol.hdr.dime.scl_slope + t1vol.hdr.dime.scl_inter;
end
t1vol = fstoint(t1vol); % NOTICE the fstoint!
t1vol(isnan(t1vol)) = 0;
% load T2 anatomy
file0 = sprintf('%s/mri/T2alignedtoT1.nii.gz',fsdir);
t2vol = load_untouch_nii(file0);
t2volsize = t2vol.hdr.dime.pixdim(2:4);
t2vol = double(t2vol.img);
if t2vol.hdr.dime.scl_slope ~= 0
t2vol = t2vol * t2vol.hdr.dime.scl_slope + t2vol.hdr.dime.scl_inter;
end
t2vol = fstoint(t2vol); % NOTICE the fstoint!
t2vol(isnan(t2vol)) = 0;
% calc
xyzsize = size(t1vol);
assert(isequal(size(t1vol),size(t2vol)));
assert(xyzsize(1)==xyzsize(2) & xyzsize(2)==xyzsize(3)); % assume isotropic, equal matrix size
% load transformation
load(sprintf('%s/alignment.mat',outputdir),'tr');
% load surfaces
prefixes = {'lh' 'rh'};
surfs = {'white' 'pial'};
vertices = {}; faces = {};
for p=1:length(prefixes)
for q=1:length(surfs)
[vertices{p,q},faces{p,q}] = freesurfer_read_surf_kj(sprintf('%s/surf/%s.%s',fsdir,prefixes{p},surfs{q}));
end
end
% post-process surfaces for internal MATLAB use
for p=1:length(prefixes)
for q=1:length(surfs)
vertices{p,q} = bsxfun(@plus,vertices{p,q}',[128; 129; 128]); % NOTICE THIS!!!
%%%% this is not needed. i think it's because our internal space is true millimeters.
%%%% vertices{p,q} = (vertices{p,q} - .5)/256 * xyzsize(1) + .5;
vertices{p,q}(4,:) = 1; % now: 4 x V
faces{p,q} = faces{p,q}(:,[1 3 2]); % now: F x 3
end
end
% calc neighbors
neighbors = {};
for p=1:length(prefixes)
for q=1:length(surfs)
neighbors{p,q} = facestoneighbors(faces{p,q},size(vertices{p,q},2));
end
end
% take vertices to EPI space
for p=1:length(prefixes)
for q=1:length(surfs)
vertices{p,q} = volumetoslices(vertices{p,q},tr);
end
end
% load the mean functional
epi = load_untouch_nii(meanfunctional);
episize = epi.hdr.dime.pixdim(2:4);
epi = double(epi.img);
if epi.hdr.dime.scl_slope ~= 0
epi = epi * epi.hdr.dime.scl_slope + epi.hdr.dime.scl_inter;
end
epi(isnan(epi)) = 0;
% figure out a reasonable contrast range for the T1, T2, and EPI
rngt1 = prctile(t1vol(:),[1 99]);
rngt2 = prctile(t2vol(:),[1 99]);
rngepi = prctile(epi(:),[1 99]);
% get slices from the anatomy to match the EPI
t1match = extractslices(t1vol,t1volsize,epi,episize,tr);
t2match = extractslices(t2vol,t2volsize,epi,episize,tr);
% process each slice orientation
for dim=dims
fprintf('processing dim %d',dim);
% calc
slicestodo = 1:skip:size(epi,dim);
% pre-compute stuff
t1matches = {};
t2matches = {};
epimatches = {};
allvals = {};
for sl=1:length(slicestodo)
% get slices
t1matches{sl} = squeeze(subscript(t1match,indexall(3,dim,slicestodo(sl))));
t2matches{sl} = squeeze(subscript(t2match,indexall(3,dim,slicestodo(sl))));
epimatches{sl} = squeeze(subscript(epi, indexall(3,dim,slicestodo(sl))));
% visualize the surface contours
for p=1:length(prefixes)
for q=1:length(surfs)
isects = findfaceintersections(vertices{p,q},faces{p,q},dim,slicestodo(sl),neighbors{p,q});
if isempty(isects)
allvals{sl,p,q} = [];
continue;
end
[faces0,vertices0,fvad0] = joinfaceintersections(vertices{p,q},isects);
iix = find(all(abs(vertices0-slicestodo(sl))<1e-3,1)); % weird precision issue
allvals{sl,p,q} = {faces0 vertices0 fvad0 iix};
end
end
end
% make one big figure
figureprep([100 100 900*length(slicestodo) 900]);
subplotresize(1,length(slicestodo));
% each slice gets a different subplot
hh = [];
for sl=1:length(slicestodo)
subplot(1,length(slicestodo),sl); hold on;
% visualize the slice
hh(sl) = imagesc(t1matches{sl},rngt1);
colormap(gray);
% unnecessary:
% set(hh,'XData',resamplingindices(1,xyzsize(2),size(anatmatch0,2)));
% set(hh,'YData',resamplingindices(1,xyzsize(1),size(anatmatch0,1)));
% put the contours in
h = [];
for p=1:length(prefixes)
for q=1:length(surfs)
if isempty(allvals{sl,p,q})
continue;
end
h = [h patch('Faces',allvals{sl,p,q}{1}, ...
'Vertices',fliplr(allvals{sl,p,q}{2}(:,setdiff(1:3,allvals{sl,p,q}{4}))), ...
'FaceVertexAlphaData',allvals{sl,p,q}{3}, ...
'FaceColor','none','LineWidth',1,'EdgeColor',colors{p,q},'EdgeAlpha',.5)];
end
end
% deal with axis
axis equal;
axis([.5 size(t1matches{sl},2)+.5 .5 size(t1matches{sl},1)+.5]);
set(gca,'YDir','reverse');
end
%%% T1:
for sl=1:length(slicestodo)
subplot(1,length(slicestodo),sl); hold on;
set(hh(sl),'CData',t1matches{sl});
caxis(rngt1);
end
figurewrite(sprintf('view%d_T1',dim),[],-1,sprintf('%s/',figuredir),1);
%%% T2:
for sl=1:length(slicestodo)
subplot(1,length(slicestodo),sl); hold on;
set(hh(sl),'CData',t2matches{sl});
caxis(rngt2);
end
figurewrite(sprintf('view%d_T2',dim),[],-1,sprintf('%s/',figuredir),1);
%%% EPI:
for sl=1:length(slicestodo)
subplot(1,length(slicestodo),sl); hold on;
set(hh(sl),'CData',epimatches{sl});
caxis(rngepi);
end
figurewrite(sprintf('view%d_EPI',dim),[],-1,sprintf('%s/',figuredir),1);
%%% continue...
% finally, close the figure!
close;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OLD WAY:
%
% function cvncheckfreesurferalignment(subjectid,outputdir,meanfunctional,skip,dims,figuredir)
%
% % function cvncheckfreesurferalignment(subjectid,outputdir,meanfunctional,skip,dims,figuredir)
% %
% % <subjectid> is like 'C0001'
% % <outputdir> is like '/home/stone-ext1/fmridata/20151008-ST001-kk,test/freesurferalignment'
% % <meanfunctional> is like '/home/stone-ext1/fmridata/20151008-ST001-kk,test/preprocess/mean.nii'
% % <skip> (optional) is number of slices to increment by. Default: 1.
% % <dims> (optional) is a vector of slice dimensions to process. Default: [1 2 3].
% % <figuredir> (optional) is where to write figures to. Default: <outputdir>.
% %
% % Write out diagnostic images of the FreeSurfer output that reflect alignment to the
% % EPI functional data. The images show the mean functional volume with contours of
% % the white and pial surfaces overlaid on these slices. We also create images that
% % show slices through the T1 and T2 that reflect the alignment of the T1 and T2 to
% % the EPI. The slices through the T1/T2 are matched exactly (field of view, resolution)
% % to the mean functional volume.
% %
% % This function parallels cvncheckfreesurfer.m.
%
% % internal constants
% colors = {[0 .4 0] [0 1 0];
% [0 .4 .4] [0 1 1]}; % LH is green, RH is cyan; white is darker, pial is lighter
% postfun = {@(x) rotatematrix(x,1,2,-1) ... % post-process the image files
% @(x) x ...
% @(x) x};
%
% % input
% if ~exist('skip','var') || isempty(skip)
% skip = 1;
% end
% if ~exist('dims','var') || isempty(dims)
% dims = [1 2 3];
% end
% if ~exist('figuredir','var') || isempty(figuredir)
% figuredir = outputdir;
% end
%
% % calc
% fsdir = sprintf('%s/%s',cvnpath('freesurfer'),subjectid);
%
% % load T1 anatomy (can either be standard 1-mm isotropic 256 x 256 x 256, or something else!)
% file0 = sprintf('%s/mri/T1.nii.gz',fsdir);
% t1vol = load_untouch_nii(gunziptemp(file0));
% t1volsize = t1vol.hdr.dime.pixdim(2:4);
% t1vol = fstoint(double(t1vol.img)); % NOTICE the fstoint!
% t1vol(isnan(t1vol)) = 0;
%
% % load T2 anatomy
% file0 = sprintf('%s/mri/T2alignedtoT1.nii.gz',fsdir);
% t2vol = load_untouch_nii(gunziptemp(file0));
% t2volsize = t2vol.hdr.dime.pixdim(2:4);
% t2vol = fstoint(double(t2vol.img)); % NOTICE the fstoint!
% t2vol(isnan(t2vol)) = 0;
%
% % calc
% xyzsize = size(t1vol);
% assert(isequal(size(t1vol),size(t2vol)));
% assert(xyzsize(1)==xyzsize(2) & xyzsize(2)==xyzsize(3)); % assume isotropic, equal matrix size
%
% % load transformation
% load(sprintf('%s/alignment.mat',outputdir),'tr');
%
% % load surfaces
% prefixes = {'lh' 'rh'};
% surfs = {'white' 'pial'};
% vertices = {}; faces = {};
% for p=1:length(prefixes)
% for q=1:length(surfs)
% [vertices{p,q},faces{p,q}] = freesurfer_read_surf_kj(sprintf('%s/surf/%s.%s',fsdir,prefixes{p},surfs{q}));
% end
% end
%
% % post-process surfaces for internal MATLAB use
% for p=1:length(prefixes)
% for q=1:length(surfs)
% vertices{p,q} = bsxfun(@plus,vertices{p,q}',[128; 129; 128]); % NOTICE THIS!!!
% %%%% this is not needed. i think it's because our internal space is true millimeters.
% %%%% vertices{p,q} = (vertices{p,q} - .5)/256 * xyzsize(1) + .5;
% vertices{p,q}(4,:) = 1; % now: 4 x V
% faces{p,q} = faces{p,q}(:,[1 3 2]); % now: F x 3
% end
% end
%
% % calc neighbors
% neighbors = {};
% for p=1:length(prefixes)
% for q=1:length(surfs)
% neighbors{p,q} = facestoneighbors(faces{p,q},size(vertices{p,q},2));
% end
% end
%
% % take vertices to EPI space
% for p=1:length(prefixes)
% for q=1:length(surfs)
% vertices{p,q} = volumetoslices(vertices{p,q},tr);
% end
% end
%
% % load the mean functional
% epi = load_untouch_nii(gunziptemp(meanfunctional));
% episize = epi.hdr.dime.pixdim(2:4);
% epi = double(epi.img);
% epi(isnan(epi)) = 0;
%
% % figure out a reasonable contrast range for the T1, T2, and EPI
% rngt1 = prctile(t1vol(:),[1 99]);
% rngt2 = prctile(t2vol(:),[1 99]);
% rngepi = prctile(epi(:),[1 99]);
%
% % get slices from the anatomy to match the EPI
% t1match = extractslices(t1vol,t1volsize,epi,episize,tr);
% t2match = extractslices(t2vol,t2volsize,epi,episize,tr);
%
% % process each slice orientation
% for dim=dims
% fprintf('processing dim %d',dim);
%
% for sl=1:skip:size(epi,dim)
% statusdots(sl,size(epi,dim));
%
% % prepare
% figureprep([100 100 900 900]);
% subplotresize(1,1); hold on;
%
% % get slices
% t1match0 = squeeze(subscript(t1match,indexall(3,dim,sl)));
% t2match0 = squeeze(subscript(t2match,indexall(3,dim,sl)));
% epi0 = squeeze(subscript(epi, indexall(3,dim,sl)));
%
% % visualize the slice
% hh = imagesc(t1match0,rngt1);
% colormap(gray);
% % unnecessary:
% % set(hh,'XData',resamplingindices(1,xyzsize(2),size(anatmatch0,2)));
% % set(hh,'YData',resamplingindices(1,xyzsize(1),size(anatmatch0,1)));
%
% % deal with axis
% axis equal;
% axis([.5 size(t1match0,2)+.5 .5 size(t1match0,1)+.5]);
% set(gca,'YDir','reverse');
%
% % visualize the surface contours
% h = [];
% for p=1:length(prefixes)
% for q=1:length(surfs)
% isects = findfaceintersections(vertices{p,q},faces{p,q},dim,sl,neighbors{p,q});
% if isempty(isects)
% continue;
% end
% [faces0,vertices0,fvad0] = joinfaceintersections(vertices{p,q},isects);
% iix = find(all(abs(vertices0-sl)<1e-3,1)); % weird precision issue
% h = [h patch('Faces',faces0,'Vertices',fliplr(vertices0(:,setdiff(1:3,iix))), ...
% 'FaceVertexAlphaData',fvad0,'FaceColor','none','LineWidth',1,'EdgeColor',colors{p,q},'EdgeAlpha',.5)];
% end
% end
%
% % init
% files = {}; files2 = {};
%
% %%% T1:
%
% % write out volume with contours on top
% set(h,'EdgeAlpha',.5);
% set(hh,'CData',t1match0);
% caxis(rngt1);
% files = [files figurewrite(sprintf('slice%03d',sl),[],[1 72],sprintf('%s/png/view%d_T1_surf',figuredir,dim),1)];
% files2 = [files2 figurewrite(sprintf('slice%03d',sl),[],0,sprintf('%s/eps/view%d_T1_surf',figuredir,dim),1)];
%
% % write out raw volume
% set(h,'EdgeAlpha',0);
% files = [files figurewrite(sprintf('slice%03d',sl),[],[1 72],sprintf('%s/png/view%d_T1_vol',figuredir,dim),1)];
% files2 = [files2 figurewrite(sprintf('slice%03d',sl),[],0,sprintf('%s/eps/view%d_T1_vol',figuredir,dim),1)];
%
% %%% T2:
%
% % write out volume with contours on top
% set(h,'EdgeAlpha',.5);
% set(hh,'CData',t2match0);
% caxis(rngt2);
% files = [files figurewrite(sprintf('slice%03d',sl),[],[1 72],sprintf('%s/png/view%d_T2_surf',figuredir,dim),1)];
% files2 = [files2 figurewrite(sprintf('slice%03d',sl),[],0,sprintf('%s/eps/view%d_T2_surf',figuredir,dim),1)];
%
% % write out raw volume
% set(h,'EdgeAlpha',0);
% files = [files figurewrite(sprintf('slice%03d',sl),[],[1 72],sprintf('%s/png/view%d_T2_vol',figuredir,dim),1)];
% files2 = [files2 figurewrite(sprintf('slice%03d',sl),[],0,sprintf('%s/eps/view%d_T2_vol',figuredir,dim),1)];
%
% %%% EPI:
%
% % write out volume with contours on top
% set(h,'EdgeAlpha',.5);
% set(hh,'CData',epi0);
% caxis(rngepi);
% files = [files figurewrite(sprintf('slice%03d',sl),[],[1 72],sprintf('%s/png/view%d_EPI_surf',figuredir,dim),1)];
% files2 = [files2 figurewrite(sprintf('slice%03d',sl),[],0,sprintf('%s/eps/view%d_EPI_surf',figuredir,dim),1)];
%
% % write out raw volume
% set(h,'EdgeAlpha',0);
% files = [files figurewrite(sprintf('slice%03d',sl),[],[1 72],sprintf('%s/png/view%d_EPI_vol',figuredir,dim),1)];
% files2 = [files2 figurewrite(sprintf('slice%03d',sl),[],0,sprintf('%s/eps/view%d_EPI_vol',figuredir,dim),1)];
%
% %%% continue...
%
% % finally, close the figure!
% close;
%
% % perform post-processing
% processimages(files,postfun{dim});
%
% end
% end