-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewPSD.m
468 lines (442 loc) · 16 KB
/
viewPSD.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
function [funTmpName,funPsdNorm,hF] = viewPSD(funPsd,f0,mask,fpass,id,slc,brain)
normMethod = 'psdNoise'; % 'psdNoise', 'smoothPsdAv', 'psdAv' or 'none'
globalNormMethod = 'to1'; % 'toImageAverage' or 'to1';
cLimMethod = 'fullRange';% 'fullRange', '99prctile' or 'highFloor'
fwhm = 5;
if iscell(funPsd)
psdStruct = funPsd{2};
funPsd = funPsd{1};
end
if ischar(f0)
switch f0
case 'all'
freeviewFlag = 1;
otherwise
error('f0 can only be numeric of ''all''')
end
f0 = [];
else
if isempty(f0)
freeviewFlag = 0;
else
freeviewFlag = 1;
end
end
if ~exist('id','var') || isempty(id)
id = '';
elseif isnumeric(id)
id = num2str(id);
end
if ~exist('mask','var') || isempty(mask)
mask = funPsd.vol2vec;
switch funPsd.vol2vecFlag
case 'allInclusiveMask'
doMaskOutline = false;
case 'customMask'
doMaskOutline = true;
otherwise
error('X')
end
else
doMaskOutline = true;
end
if ~exist('slc','var') || isempty(slc)
slc = round(funPsd.depth*0.5);
end
f = funPsd.psd.f;
w = funPsd.psd.w;
% %% Perform spatial normalization
% switch normMethod
% case 'psdNoise'
% if isempty(funPsd.vec)
% funPsd = vol2vec(funPsd);
% end
% noiseInd = funPsd.psd.f>4 & funPsd.psd.f<funPsd.psd.f(round(0.95*end));
% normFact = exp( mean(log(funPsd.vec(noiseInd,:)),1) );
% normFact0 = exp(mean(log(normFact),2));
% case 'psdAv'
% if isempty(funPsd.vec)
% funPsd = vol2vec(funPsd);
% end
% normFact = exp( mean(log(funPsd.vec),1) );
% normFact0 = exp(mean(log(normFact),2));
% case 'smoothPsdAv'
% error('this is probably broken')
% %%% Compute average power and write to disk
% funNorm = funPsd;
% if isempty(funNorm.vol)
% funNorm.vec = mean(log(funNorm.vec),1);
% funNorm.nframes = 1;
% normFact0 = exp(funNorm.vec);
% funNorm = vec2vol(funNorm);
% else
% funNorm.vol = mean(log(funNorm.vol),4);
% funNorm.nframes = 1;
% tmp = vol2vec(funNorm);
% normFact0 = exp(tmp.vec); clear tmp
% end
% funNormTmpName = [tempname '.nii.gz'];
% MRIwrite(funNorm,funNormTmpName);
%
%
% %%% Define mask for smoothing and write to disk
% funMask = funNorm;
% funMask.vol = ~isnan(funMask.vol);
% funMaskTmpName = [tempname '.nii.gz'];
% MRIwrite(funMask,funMaskTmpName);
%
% %%% Apply smoothing (mri_fwhm) to normalization map
% cmd = {'source /usr/local/freesurfer/fs-dev-env-autoselect'};
% cmd{end+1} = ['mri_fwhm'...
% ' --i ' funNormTmpName...
% ' --o ' funNormTmpName...
% ' --smooth-only'...
% ' --mask ' funMaskTmpName...
% ' --fwhm ' num2str(fwhm)];
% [status,cmdout] = system(strjoin(cmd,'; '));
%
% % funNormSm = MRIread(funNormTmpName);
% % figure('WindowStyle','docked');
% % imagesc(exp(funNorm.vol(:,:,46)));
% % ax = gca; ax.ColorScale = 'log';
% % cLim = clim;
% % figure('WindowStyle','docked');
% % imagesc(exp(funNormSm.vol(:,:,46)));
% % ax = gca; ax.ColorScale = 'log';
% % clim(cLim);
%
% funNorm = MRIread(funNormTmpName);
% funNorm = vol2vec(funNorm,funPsd.vol2vec);
% normFact = exp(funNorm.vec); clear funNorm
% case 'none'
% sz = size(funPsd.vec);
% normFact = ones([1 sz(2)]);
% normFact0 = ones([1 sz(2)]);
% otherwise
% error('')
% end
%
% %%% Apply normalization
% if isempty(funPsd.vec)
% funPsd = vol2vec(funPsd);
% end
% funPsdNorm = funPsd;
% switch globalNormMethod
% case 'toImageAverage'
% funPsdNorm.vec = funPsd.vec./normFact .* normFact0;
% case 'to1'
% funPsdNorm.vec = funPsd.vec./normFact;
% end
% funPsdNorm.normFact = normFact;
%% Compute summary statistics for later
tmpMask = logical(mask(funPsdNorm.vol2vec));
if any(all(funPsdNorm.vec==0,1))
warning('some voxels are all zeros. fixing it but expect wierd stuff')
end
tmpMask(all(funPsdNorm.vec==0,1)) = false;
psdSpecNormAv = exp(mean(log(funPsdNorm.vec(:,tmpMask)),2));
% psdSpecNormEr = exp(prctile(log(funPsdNorm.vec(:,mask(funPsdNorm.vol2vec))),[2.7 97.5],2));
% psdSpecNormEr(:,1) = psdSpecNormEr(:,1) - psdSpecNormAv; psdSpecNormEr(:,2) = psdSpecNormAv - psdSpecNormEr(:,2);
%% Prepare for ploting
hF = figure('WindowStyle','docked');
% Title
if exist('id','var') && ischar(id) && ~isempty(id)
titleStr = {['subj' id]};
elseif iscell(id)
titleStr = {strjoin(id,'; ')};
else
titleStr = {''};
end
titleStr{end+1} = ['tw=' num2str(funPsd.psd.tw) ', ' 'w=' num2str(w) 'Hz' ', ' 'k=' num2str(funPsd.psd.param.tapers(2))];
% titleStr{end+1} = ['w=' num2str(w) 'Hz'];
% switch normMethod
% case 'smoothPsdAv'
% titleStr{end+1} = ['fwhm=' num2str(fwhm) 'mm'];
% end
[~,b,c] = fileparts(funPsd.fspec);
titleStr{end+1} = [b c];
% titleStr = [strjoin(titleStr(1:end-1),'; ') titleStr(end)];
if doMaskOutline
maskC = getMaskOutline(mask(:,:,slc),5);
end
if ~isempty(f0)
funPsd = vec2vol(funPsd);
funPsdNorm = vec2vol(funPsdNorm);
[~,f0Ind] = min(abs(f-f0'),[],2);
if length(f0)==1
tl = tiledlayout(2,4); tl.TileSpacing = "tight"; tl.Padding = "tight";
tl.TileIndexing = 'rowmajor';
%% Plot spatial pattern before and after normalization
xStr = {['f0=' num2str(f0) 'Hz']};
xStr{end+1} = ['slc' num2str(slc)];
% Before spatial normalization
nexttile
imagesc(squeeze(funPsd.vol(:,:,slc,f0Ind)));
ylabel(colorbar,['psd @ f0=' num2str(f0) 'Hz'])
ax = gca; ax.ColorScale = 'log'; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = []; ax.YDir = 'normal';
ax.Colormap = jet;
tmp = funPsd.vol(:,:,slc,f0Ind); tmp = tmp(logical(mask));
switch cLimMethod
case 'highFloor'
cLim = exp(prctile(log(tmp),[0.5 100]));
case '99prctile'
cLim = exp(prctile(log(tmp),[0.5 99.5]));
case 'fullRange'
cLim = [min(tmp) max(tmp)];
otherwise
error('X')
end
ax.CLim = cLim;
xlabel({strjoin(xStr,'; ') 'before spatial norm'})
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','m'); end
% Average power
nexttile
tmp = funPsdNorm; tmp.vec = normFact; tmp.nframes = 1; tmp = vec2vol(tmp);
imagesc(squeeze(tmp.vol(:,:,slc))); clear tmp;
ylabel(colorbar,'full spectrum psd')
ax = gca; ax.ColorScale = 'log'; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = []; ax.YDir = 'normal';
ax.Colormap = jet;
ax.CLim = cLim;
if strcmp(normMethod,'psdNoise')
xlabel('high frequency noise psd')
else
xlabel('full spectrum psd')
end
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','m'); end
% Normalization factor
nexttile
tmp = funPsdNorm; tmp.vec = normFact; tmp.nframes = 1; tmp = vec2vol(tmp);
imagesc(squeeze(tmp.vol(:,:,slc))); clear tmp;
ylabel(colorbar,'full spectrum psd')
ax = gca; ax.ColorScale = 'log'; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = []; ax.YDir = 'normal';
ax.Colormap = jet;
ax.CLim = cLim;
xlabel('normalization factor')
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','m'); end
% % After spatial normalization (same scale)
% nexttile
% imagesc(squeeze(funPsdNorm.vol(:,:,slc,f0Ind)));
% ylabel(colorbar,['psd @ f0=' num2str(f0) 'Hz'])
% ax = gca; ax.ColorScale = 'log'; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = []; ax.YDir = 'normal';
% ax.CLim = cLim;
% xlabel({strjoin(xStr,'; ') 'after spatial norm' '(same scale)'})
% hold on; plot(maskC,'FaceColor','none','EdgeColor','m')
% After spatial normalization (auto scale)
nexttile
imagesc(squeeze(funPsdNorm.vol(:,:,slc,f0Ind)));
ylabel(colorbar,['psd @ f0=' num2str(f0) 'Hz'])
ax = gca; ax.ColorScale = 'log'; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = []; ax.YDir = 'normal';
ax.Colormap = jet;
xlabel({strjoin(xStr,'; ') 'after spatial norm'})
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','m'); end
tmp = funPsdNorm.vol(:,:,slc,f0Ind); tmp = tmp(logical(mask));
switch normMethod
case 'psdNoise'
cLim = [1 exp(prctile(log(tmp),95))];
otherwise
switch cLimMethod
case 'highFloor'
cLim = exp(prctile(log(tmp),[0.5 100]));
case '99prctile'
cLim = exp(prctile(log(tmp),[0.5 99.5]));
case 'fullRange'
cLim = [min(tmp) max(tmp)];
otherwise
error('X')
end
end
ax.CLim = cLim;
else
if ~isempty(brain)
if iscell(brain)
tl = tiledlayout(2,length(f0)+length(brain)); tl.TileSpacing = "tight"; tl.Padding = "tight";
for iii = 1:length(brain)
nexttile
imagesc(brain{iii})
ax = gca; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = [];
ax.CLim = prctile(brain{iii}(:),[1 99]);
ax.Colormap = gray;
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','r'); end
end
else
tl = tiledlayout(2,length(f0)+1); tl.TileSpacing = "tight"; tl.Padding = "tight";
nexttile
imagesc(brain)
ax = gca; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = [];
ax.CLim = prctile(brain(:),[1 99]);
ax.Colormap = gray;
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','r'); end
end
else
tl = tiledlayout(2,length(f0)); tl.TileSpacing = "tight"; tl.Padding = "tight";
end
tl.TileIndexing = 'rowmajor';
% After spatial normalization (auto scale)
for iii = 1:length(f0Ind)
nexttile
imagesc(squeeze(funPsdNorm.vol(:,:,slc,f0Ind(iii))));
ylabel(colorbar,['psd @ f0=' num2str(f0(iii)) 'Hz'])
ax = gca; ax.ColorScale = 'log'; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = [];
% ax.YDir = 'normal';
ax.Colormap = jet;
% xlabel(strjoin(xStr,'; '),'interpreter','none')
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','m'); end
cLim = ax.CLim;
end
end
else
%% Plot brain and mask
xStr = {['slc' num2str(slc)]};
nexttile
imagesc(brain.vol(:,:,slc))
ax = gca; ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1]; ax.XTick = []; ax.YTick = []; ax.YDir = 'normal';
ax.Colormap = gray;
xlabel({strjoin(xStr,'; ') 'after spatial norm' 'auto scale'})
hold on;
if doMaskOutline; plot(maskC,'FaceColor','none','EdgeColor','w','LineWidth',0.2); end
xlabel({'mean brain' strjoin(xStr,'; ')});
nexttile
% eventually put std here
ax = gca; ax.Visible = 'off';
nexttile
% eventually put std normalization factor here
ax = gca; ax.Visible = 'off';
end
tlTitle = title(tl,titleStr,'interpreter','none');
% %% Plot average spectra on loglog plot
% nexttile([1 tl.GridSize(2)])
% plot(f,psdSpecNormAv,'k'); hold on
% ax = gca; ax.YScale = 'log';
% ax.XScale = 'log';
% ax.YAxisLocation = 'right';
% xlabel('Hz'); ylabel('psd averaged within mask');
% grid on
% if exist('fpass','var') && ~isempty(fpass)
% xlim(fpass)
% else
% xlim tight
% end
% ylim tight
%
% %%% Add w references
% f0w = exp(linspace(log(f(2)),log(f(end)),6)); f0w([1 end]) = [];
% yLim = ylim;
% y = yLim(1).*[1 1];
% for ii = 1:length(f0w)
% x = f0w(ii)+[-1 1].*w;
% if x(1)>f(1) && x(2)<f(end)
% plot(x,y,'g','LineWidth',3)
% end
% end
%
% %%% Add f0 reference
% if ~isempty(f0)
% for iii = 1:length(f0)
% plot([1 1].*f(f0Ind(iii)),ylim,':r');
% x = f(f0Ind(iii))+[-1 1].*w;
% yLim = ylim;
% y = yLim(2).*[1 1];
% plot(x,y,'g','LineWidth',3)
% end
% end
%% Plot average spectra on semilog plot
nexttile([1 tl.GridSize(2)])
if exist('psdStruct','var')
plot(psdStruct.f,psdStruct.psd,'k'); hold on
plot(psdStruct.f,squeeze(psdStruct.psdErr),'r');
else
plot(f,psdSpecNormAv,'k'); hold on
end
ax = gca; ax.YScale = 'log';
ax.YAxisLocation = 'right';
xlabel('Hz'); ylabel('psd averaged within mask');
grid on
grid minor
if exist('fpass','var') && ~isempty(fpass)
xlim(fpass)
else
xlim tight
end
noiseBase = mean(psdSpecNormAv(f>2.5));
yLim = ylim; yLim(1) = noiseBase; yLim(1) = yLim(1) - 0.1*range(yLim);
ylim(yLim)
% ylim tight
%%% Add noise floor
if strcmp(normMethod,'psdNoise')
tmpInd = [find(noiseInd,1,'first') find(noiseInd,1,'last')];
plot(funPsdNorm.psd.f(tmpInd),exp(mean(log(normFact))).*ones(size(tmpInd)),'r')
end
%%% Add f0 (if provided) and w references
if ~isempty(f0)
for iii = 1:length(f0)
plot([1 1].*f(f0Ind(iii)),ylim,':r');
x = f(f0Ind(iii))+[-1 1].*w;
yLim = ylim;
y = yLim(2).*[1 1];
plot(x,y,'-g','LineWidth',3)
end
if strcmp(normMethod,'psdNoise')
legend({'psd' 'noiseFloor' 'f0' '2*w'},'box','off','Location','southeast')
else
legend({'psd' 'f0' '2*w'},'box','off','Location','southeast')
end
else
% x = f(round(end/2))+[-1 1].*w;
x = mean(fpass)+[-1 1].*w;
yLim = ylim;
y = yLim(1).*[1 1];
plot(x,y,'-g','LineWidth',3)
if strcmp(normMethod,'psdNoise')
error('code that')
else
legend({'psd' '2*w'},'box','off','Location','northeast')
end
end
drawnow
%% Launch 3D viewer
if freeviewFlag
funPsdNormFS = funPsdNorm;
if isempty(funPsdNormFS.vol)
funPsdNormFS = vec2vol(funPsdNormFS);
end
if ~isempty(f0)
f0IndExc = true(size(f));
f0IndExc(f0Ind) = false;
funPsdNormFS.vol(:,:,:,f0IndExc) = [];
funPsdNormFS.nframes = nnz(~f0IndExc);
end
funPsdNormFS.vol = log(funPsdNormFS.vol);
funPsdNormFS.vol(isnan(funPsdNormFS.vol)) = 0;
funTmpName = [tempname '.nii.gz'];
display('******************')
display('Writing log(psd) volumes to disk...')
MRIwrite(funPsdNormFS,funTmpName);
%%% Print command to view
anatTmpName = strsplit(funPsdNormFS.fspec,'/'); anatTmpName = strjoin(anatTmpName(1:11),'/');
cmd = {'freeview'};
% cmd{end+1} = fullfile(anatTmpName,'T1w_restore.nii.gz');
% cmd{end+1} = fullfile(anatTmpName,'T1w_restore.2.nii.gz');
% cmd{end+1} = fullfile(anatTmpName,'T2w_restore.nii.gz');
% cmd{end+1} = fullfile(anatTmpName,'T2w_restore.2.nii.gz');
% cmd{end+1} = fullfile(anatTmpName,'Results/rfMRI_REST__brainMean.nii.gz');
% cmd{end+1} = fullfile(anatTmpName,'Results/rfMRI_REST__brainStd.nii.gz');
cmd{end+1} = [funTmpName ':colormap=turbo'];
if isempty(f0)
display(['To view 3D volume of log(psd) :'])
else
display(['To view 3D volume of log(psd) @' num2str(f(f0Ind),'%0.4f') 'Hz :'])
end
display(strjoin(cmd,' '))
% display(['freeview ' funTmpName ':colormap=turbo'])
if ~isempty(f0)
display(['recommended window: ' num2str(log(cLim(1))) ' to ' num2str(log(cLim(2)))])
end
display('******************')
end