-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewMTsvdProulx.m
334 lines (301 loc) · 9.27 KB
/
viewMTsvdProulx.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
function F = viewMTsvdProulx(svdStruct,modePsd,modeTs,modeOrder,funPsd,funTs)
bidsDir = '/autofs/space/takoyaki_001/users/proulxs/vasomo/vasomoAna/vasomoInflow/bids';
if ~exist('modeOrder','var') || isempty(modeOrder)
modeOrder = 1;
end
if ~exist('K','var')
K = 1;
end
fileList = cell(size(funTs))';
for runInd = 1:length(funTs)
tmp = strsplit(funTs(runInd).fspec,filesep);
fileList{runInd} = tmp{end-1};
dir(replace(funTs(runInd).fspec,'.nii.gz',''))
tmp = strsplit(fileList{runInd},'_');
system(['jq ''.AcquisitionTime'' ' fullfile(bidsDir,tmp{1},tmp{2},'func',[fileList{runInd} '.json'])],'-echo');
end
fileList
%% Compute psd of the reconstructed timeseries
W = [];
K = 1;
cFlag = 1;
if ( ~exist('modePsd','var') || isempty(modePsd) ) && ( exist('modeTs','var') && ~isempty(modeTs) )
modePsd = runPSD2(modeTs,W,K,cFlag,modeOrder);
end
%% For comparison, compute psd of original timeseries
if ~exist('funPsd','var') || isempty(funPsd)
funPsd = vec2vol(runPSD2(funTs,W,K,cFlag));
else
funPsd = vec2vol(funPsd);
end
F = figure('WindowStyle','docked');
tl = tiledlayout(6,5);
tl.TileSpacing = "tight"; tl.Padding = "tight";
tl.TileIndexing = 'rowmajor';
%%% mean brain
tileInd = 1;
nexttile(tileInd,[2 1])
if isfield(funTs,'imMean')
im = funTs.imMean;
else
if length(funTs)>1
im = mean(cat(4,funTs.vol),4);
else
im = mean(funTs.vol,4);
end
end
imagesc(im)
ax = gca;
ax.Colormap = gray;
ax.YTick = []; ax.XTick = [];
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.CLim = prctile(im(:),[0 99]);
title('timeseries mean')
AX = cell(0,0);
AX{end+1} = ax;
%%% Plot psd at peak
vol = cat(6,funPsd.vol);
if ~isreal(vol); vol = conj(vol).*vol; end
for ii = 1:size(svdStruct.param.fpass,1)
if ii>3
break
end
tileInd = tileInd + 1;
ax = nexttile(tileInd,[2 1]);
fpass = svdStruct.param.fpass(ii,:) + [-1 1].*svdStruct.param.BW(ii);
fInd = funPsd(1).psd.f>fpass(1) & funPsd(1).psd.f<fpass(2);
hIm = imagesc(mean(mean(vol(:,:,:,fInd,:),5),4));
% hIm = imagesc(mean(mean(abs(vol(:,:,:,fInd,:)),4),5));
% hIm = imagesc(mean(mean(abs(vol(:,:,:,fInd,:,:)),4),6));
ax.Colormap = jet; ax.ColorScale = 'log';
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.XAxis.Visible = 'off'; ax.YAxis.Visible = 'off';
ylabel(colorbar,[num2str(mean(fpass),'psd @ %0.2fHz')])
axPsd{ii} = ax;
end
AX = [AX axPsd];
%%% Plot first singular vector
%%%% Magnitude map
tileInd = 5;
ax = nexttile(tileInd,[2 1]);
spIm = nan(size(funPsd(1).vol2vec));
spIm(funPsd(1).vol2vec) = svdStruct.sp(:,:,modeOrder);
% if ~isreal(spIm); spIm = conj(spIm).*spIm; end
if ~isreal(spIm)
hIm = imagesc(conj(spIm).*spIm);
else
hIm = imagesc(spIm);
end
ax.Colormap = jet; %ax.ColorScale = 'log';
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.ColorScale = 'log';
ax.XAxis.Visible = 'off'; ax.YAxis.Visible = 'off';
ylabel(colorbar,'first sv weigths mag (a.u.)')
AX{end+1} = ax;
%%%% Phase map
tileInd = 15;
ax = nexttile(tileInd,[2 1]);
hIm = imagesc(angle(spIm),[-pi pi]);
% scale = conj(spIm).*spIm;
scale = abs(spIm);
% scale = log(scale);
scale = scale - min(scale(:)); scale = scale ./ max(scale(:));
hIm.AlphaData = scale;
ax.Colormap = hsv; ax.ColorScale = 'linear';
ax.PlotBoxAspectRatio = [1 1 1]; ax.DataAspectRatio = [1 1 1];
ax.XAxis.Visible = 'off'; ax.YAxis.Visible = 'off';
ax.Color = [0.5 0.5 0.5];
ylabel(colorbar,'first sv weigths phase (rad)')
AX{end+1} = ax;
%%%% Phase distribution
tileInd = 14;
n = round(numel(svdStruct.sp(:,:,modeOrder))/500);
binEdges = linspace(-pi,pi,n+1);
binCent = binEdges(1:end-1)+diff(binEdges);
[~,~,bin] = histcounts(angle(svdStruct.sp(:,:,modeOrder)),binEdges);
spBin = nan(1,n);
for i = 1:n
spBin(i) = mean(svdStruct.sp(bin==i,:,modeOrder));
% spBin(i) = mean(sp(bin==i));
end
ax = nexttile(tileInd,[2 1]);
[X,Y] = pol2cart(binCent,abs(spBin));
spBin = complex(X,Y);
% polarplot(angle(spBin([1:end 1])),abs(spBin([1:end 1])));
polarplot(angle(spBin([1:end 1])),conj(spBin([1:end 1])).*spBin([1:end 1]));
ax = gca;
ax.ThetaAxisUnits = 'radians';
thetaTick = ax.ThetaTick(1:end-1);
minusPiInd = thetaTick<pi & thetaTick~=0;
plusPiInd = thetaTick>pi & thetaTick~=0;
ax.ThetaTickLabel(plusPiInd) = flip(strcat({'-'},ax.ThetaTickLabel(minusPiInd)));
ax.ThetaAxis.Label.String = 'sv weigth phase';
ax.RAxis.Label.String = 'sv weigth binned mag';
%%% Spectra
%%%% Simple spatial average
tileInd = 21;
ax = nexttile(tileInd,[1 4]);
tmpPsd = catRuns(funPsd);
tmpPsd = vol2vec(tmpPsd);
% tmpPsd = vol2vec(funPsd(1));
f = tmpPsd.psd.f;
y = tmpPsd.vec;
if ~isreal(y)
y = conj(y).*y;
end
y = mean(y(:,:),2);
% y = mean(y(:,:),2,"omitmissing");
% y = mean(mean(y,4,"omitmissing"),2,"omitmissing");
hP = plot(f,y,'k');
ax.YScale = 'log';
yLim = [min(y(2:end)) max(y(2:end))];
xLim = [min(f) max(f)];
xlim(xLim)
grid on; ax.XMinorGrid = 'on'; ax.YMinorGrid = 'on';
hold on
%%%% weighted avereage of power (no phasing)
scale = svdStruct.sp(:,:,modeOrder);
scale = abs(scale);
scale = scale./sum(scale);
tmp = vol2vec(funPsd);
vec = tmp.vec;
vec = abs(vec);
vec = mean(vec(:,:,:),3);
spec = vec*scale;
hP(end+1) = plot(f,spec,'-');
yLim(1) = min([yLim(1); spec(2:end)]);
yLim(2) = max([yLim(2); spec(2:end)]);
%%%% modePsd
% yyaxis right
hold on
tmpPsd = vol2vec(catRuns(modePsd));
f = tmpPsd.psd.f;
% y = abs(tmpPsd.vec(:,modeOrder,:,:));
y = tmpPsd.vec(:,modeOrder,:,:);
if ~isreal(y)
% y = abs(y);
y = conj(y).*y;
end
y = mean(y(:,:,:),3);
% y = abs(tmpPsd.vec(:,modeOrder,:,:));
% y = mean(mean(y,4,"omitmissing"),2,"omitmissing");
hP(end+1) = plot(f,y,'-');
yLim(1) = min([yLim(1); y(2:end)]);
yLim(2) = max([yLim(2); y(2:end)]);
ylim(yLim)
y = [yLim(1) yLim(1) yLim(2) yLim(2) yLim(1)];
if any(diff(svdStruct.param.fpass,[],2)); dbstack; error('X'); end
fRange = svdStruct.param.fpass(:,1) + svdStruct.param.BW.*[-1 1];
for i = 1:size(fRange,1)
x = [fRange(i,1) fRange(i,2) fRange(i,2) fRange(i,1) fRange(i,1)];
hPatch = patch(x,y,[1 1 1].*0.7); hPatch.EdgeColor = 'none'; uistack(hPatch,'bottom'); hPatch.FaceAlpha = 0.4;
end
% ax.YScale = 'log';
% yLim = [min(y(2:end)) max(y(2:end))];
% xLim = [min(f) max(f)];
% ylim(yLim)
% xlim(xLim)
% grid on; ax.XMinorGrid = 'on'; ax.YMinorGrid = 'on';
legend(hP,{'unweighted mean' ['mode-' num2str(modeOrder) ' weigthed mean'] ['mode-' num2str(modeOrder) ' filtered']})
% %%%% psd of modeTs
% yyaxis right
% hold on
% tmpPsd = vol2vec(catRuns(runPSD3(modeTs,W,K,modeOrder)));
% f = tmpPsd.psd.f;
% y = tmpPsd.vec;
% if ~isreal(y); y = conj(y).*y; end
% % if ~isreal(y); y = abs(y); end
% hP(2) = plot(f,mean(y(:,:),2),'-');
% ax.YScale = 'linear';
% yLim = [min(y(2:end)) max(y(2:end))];
% xLim = [min(f) max(f)];
% ylim(yLim)
% xlim(xLim)
% grid on; ax.XMinorGrid = 'on'; ax.YMinorGrid = 'on';
% legend(hP,{'unweighted mean' ['mode-' num2str(modeOrder) ' modeTs PSD']})
%%% Eigen spectrum
tileInd = 25;
ax = nexttile(tileInd,[1 1]);
plot(squeeze(svdStruct.sv/sum(svdStruct.sv).*100),'-ok'); hold on
% plot(squeeze(svdStruct.sv),'-ok'); hold on
hPtmp = plot(modeOrder,squeeze(svdStruct.sv(modeOrder)/sum(svdStruct.sv).*100),'-or');
hPtmp.MarkerFaceColor = hPtmp.Color;
% plot(svdStruct.sv,'-ok')
ax.XScale = 'log';
grid on
ylabel('% variance explained')
xlabel('mode order')
% yLim = ylim; yLim(1) = 0; ylim(yLim)
%%% Timeseries
tileInd = 26;
ax = nexttile(tileInd,[1 5]);
t = 0:modeTs(1).tr/1000:modeTs(1).tr/1000*(modeTs(1).nframes*length(modeTs)-1);
% t = 0:svdTs.tr/1000:svdTs.tr/1000.*(svdTs.nframes-1);
y = cat(4,modeTs.vol);
y = y(modeOrder,:);
% y = squeeze(svdTs.vol(modeOrder,:,:,:));
c = colororder;
plot(t,imag(y),'Color',c(1,:)); hold on
plot(t,real(y),'Color',hP(2).Color);
xLim = [min([real(t) imag(t)]) max([real(t) imag(t)])];
if length(modeTs)>1
x = [1 1] * -modeTs(1).tr/1000/2;
y = ylim;
plot(x,y,'k')
for runInd = 1:length(modeTs)
x = [1 1] * modeTs(runInd).tr/1000 * (modeTs(runInd).nframes*runInd-0.5);
plot(x,y,'k')
end
end
xlim(xLim)
%%% Spectrogram
Sx = cell(1,length(modeTs));
tx = cell(1,length(modeTs));
fx = cell(1,length(modeTs));
TW = (K+1)/2;
for runInd = 1:length(modeTs)
% y = cat(4,svdTs.vol);
% y = y(modeOrder,:);
T = funTs(runInd).tr/1000.*funTs(runInd).nframes;
W = TW/T;
param.tapers = [TW K];
param.Fs = 1/(funTs(runInd).tr/1000);
winSz = 10;
stepSz = winSz/4;
y = squeeze(modeTs(runInd).vol(modeOrder,:,:,:));
[Sx{runInd},tx{runInd},fx{runInd}]=mtspecgramc(real(y),[winSz stepSz],param);
end
% TW = (K+1)/2;
% T = funTs(1).tr/1000.*funTs(1).nframes;
% W = TW/T;
% param.tapers = [TW K];
% param.Fs = 1/(funTs(1).tr/1000);
% winSz = 10;
% stepSz = winSz/4;
% [Sx,tx,fx]=mtspecgramc(real(y),[winSz stepSz],param);
tileInd = 11;
ax = nexttile(tileInd,[2 3]);
tx2 = tx;
for runInd = 2:length(tx)
tx2{runInd} = tx2{runInd} + tx2{runInd-1}(end) + modeTs(runInd-1).tr/1000;
end
hIm = imagesc(cat(2,tx2{:}),fx{1},cat(1,Sx{:})');
% runInd = 4;
% hIm = imagesc(tx{runInd},fx{runInd},Sx{runInd}');
ax.YDir = 'normal';
ax.ColorScale = 'log';
ax.Colormap = jet;
xlabel('time (s)')
ylabel('frequency (Hz)')
ylabel(colorbar,'PSD')
hold on
if length(modeTs)>1
x = [1 1] * -modeTs(1).tr/1000/2;
y = ylim;
plot(x,y,'w')
for runInd = 1:length(modeTs)
x = [1 1] * modeTs(runInd).tr/1000 * (modeTs(runInd).nframes*runInd-0.5);
plot(x,y,'w')
end
end
linkaxes([AX{:}])