-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtryL2svd.m
215 lines (183 loc) · 6.76 KB
/
tryL2svd.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
function tryL2svd(volPsd)
method = 1;
adjustPhase = 1;
saveFlag = 1;
N = volPsd.svd.dim(1);
W = volPsd.svd.dim(2);
R = volPsd.svd.dim(3);
K = volPsd.svd.dim(4);
F = volPsd.svd.dim(5);
V = volPsd.svd.dim(6);
W = volPsd.svd.dim(7);
M = volPsd.svd.dim(8);
% subStr = strsplit(replace(volPsd.fspec,'.nii.gz',''),filesep); subStr = strsplit(subStr{end},'_');
subStr = strsplit(replace(volPsd.fspec,'.nii.gz',''),filesep); subStr = strsplit(subStr{end-1},'_');
spSV = permute(volPsd.svd.spSV,[5 6 8 1 2 3 4 7]); % [F V M]
switch method
case 1
spSV = reshape(spSV(:,:,1),[F V*1]); % [F V*M]
case 2
spSV = reshape(spSV,[F V*M]); % [F V*M]
end
spSV = permute(spSV,[2 1]); % [V*M F]
Ml2 = min(size(spSV));
[u,s,v] = svd(spSV,'econ','vector');
spSVl2 = permute(u,[2 1]); %[Ml2 V*M]
switch method
case 1
spSVl2 = reshape(spSVl2,[Ml2 V 1]); % [Ml2 V M];
case 2
spSVl2 = reshape(spSVl2,[Ml2 V M]); % [Ml2 V M];
end
frSVl2 = permute(v,[2 1]); %[Ml2 F*M]
frSVl2 = reshape(frSVl2,[Ml2 F]); %[Ml2 F]
GridSize = [2 3];
extraMode = [];
%% Spatial singular vector -- MAG
fMag = figure('WindowStyle','docked');
ht = tiledlayout(GridSize(1),GridSize(2)); ax = {};
ht.TileSpacing = 'tight'; ht.Padding = 'tight';
ax{end+1} = nexttile;
plot(s,'.-');
xline(prod(ht.GridSize)-1+0.5,'r')
xlabel('Ml2'); ylabel('singular value');
hold on
plot(extraMode,s(extraMode),'.r');
for ml2 = 1:(prod(ht.GridSize)-1)
if ml2>(prod(ht.GridSize)-1-length(extraMode)); ml2 = extraMode(ml2-(prod(ht.GridSize)-1)+length(extraMode)); end
ax{end+1} = nexttile;
im = zeros(size(volPsd.vol2vec));
im(volPsd.vol2vec) = mean(abs(spSVl2(ml2,:,:)),3);
imagesc(im.*s(ml2))
ax{end}.DataAspectRatio = [1 1 1];
ax{end}.Colormap = turbo;
ax{end}.XAxis.Visible = 'off'; ax{end}.YAxis.Visible = 'off';
ylabel(colorbar,'singular vector mag * singular value')
title(['Level-2 mode ' num2str(ml2) '; method' num2str(method)])
end
axIm = [ax{2:end}];
cLim = get(axIm,'CLim');
cLim = [min([cLim{:}]) max([cLim{:}])];
set(axIm,'CLim',cLim);
title(ht,strjoin(subStr(1:3)))
% add underlay
drawnow
set(axIm,'Color','none');
ax = {};
for i = 1:length(axIm)
ax{end+1} = axes('InnerPosition',axIm(i).Position,'DataAspectRatio',axIm(i).DataAspectRatio);
imagesc(volPsd.imMean);
ax{end}.Colormap = gray;
ax{end}.XAxis.Visible = 'off'; ax{end}.YAxis.Visible = 'off';
uistack(ax{end},'bottom')
end
axImMag = axIm;
axImMag_under = [ax{:}];
%% Spatial singular vector -- PHASE
% f = figure('SizeChangedFcn',@(src,evn) disp('Window resized'))
fPhase = figure('WindowStyle','docked');
% figure('WindowStyle','docked','KeyPressFcn',@Key_Down);
% figure('WindowStyle','docked');
ht = tiledlayout(GridSize(1),GridSize(2)); ax = {};
ht.TileSpacing = 'tight'; ht.Padding = 'tight';
ax{end+1} = nexttile;
plot(s,'.-');
xline(prod(ht.GridSize)-1+0.5,'r')
xlabel('Ml2'); ylabel('singular value');
hold on
plot(extraMode,s(extraMode),'.r');
for ml2 = 1:(prod(ht.GridSize)-1)
if ml2>(prod(ht.GridSize)-1-length(extraMode)); ml2 = extraMode(ml2-(prod(ht.GridSize)-1)+length(extraMode)); end
ax{end+1} = nexttile;
im = zeros(size(volPsd.vol2vec));
im(volPsd.vol2vec) = angle(spSVl2(ml2,:,1));
alphaVal = mean(abs(spSVl2(ml2,:,:)),3);
alphaVal = alphaVal - min(alphaVal);
alphaVal = alphaVal ./ max(alphaVal);
alphaVal = alphaVal .* (s(ml2)/s(1));
alphaIm = zeros(size(volPsd.vol2vec));
alphaIm(volPsd.vol2vec) = alphaVal;
if adjustPhase
[~,b] = max(alphaIm(:));
im = wrapToPi(im - im(b));
end
hIm = imagesc(im);
if adjustPhase
[x,y] = ind2sub(size(alphaIm),b);
xline(y,':w'); yline(x,':w')
end
hIm.AlphaData = alphaIm;
% hIm.AlphaData = ones(size(alphaIm)).*0.5;
ax{end}.DataAspectRatio = [1 1 1];
ax{end}.Colormap = hsv;
ax{end}.XAxis.Visible = 'off'; ax{end}.YAxis.Visible = 'off';
ax{end}.Color = [0.5 0.5 0.5];
ylabel(colorbar,'singular vector phase')
title(['Level-2 mode ' num2str(ml2) '; method' num2str(method)])
end
axIm = [ax{2:end}];
cLim = [-pi pi];
set(axIm,'CLim',cLim);
title(ht,strjoin(subStr(1:3)))
% add underlay
drawnow
set(axIm,'Color','none');
ax = {};
for i = 1:length(axIm)
ax{end+1} = axes('InnerPosition',axIm(i).Position,'DataAspectRatio',axIm(i).DataAspectRatio);
imagesc(volPsd.imMean);
ax{end}.Colormap = gray;
ax{end}.XAxis.Visible = 'off'; ax{end}.YAxis.Visible = 'off';
uistack(ax{end},'bottom')
end
axImPhase = axIm;
axImPhase_under = [ax{:}];
linkaxes([axImMag axImMag_under axImPhase axImPhase_under])
addlistener(axImMag ,'XLim','PostSet',@(src,evn) set( axImMag_under' , {'Position'} , get(axImMag ,'Position') ));
addlistener(axImMag ,'YLim','PostSet',@(src,evn) set( axImMag_under' , {'Position'} , get(axImMag ,'Position') ));
addlistener(axImPhase,'XLim','PostSet',@(src,evn) set( axImPhase_under' , {'Position'} , get(axImPhase,'Position') ));
addlistener(axImPhase,'YLim','PostSet',@(src,evn) set( axImPhase_under' , {'Position'} , get(axImPhase,'Position') ));
fMag.KeyPressFcn = @toggleOverlay;
fPhase.KeyPressFcn = @toggleOverlay;
fMag.SizeChangedFcn = @(src,evn) set( axImMag_under' , {'Position'} , get(axImMag ,'Position') );
fPhase.SizeChangedFcn = @(src,evn) set( axImPhase_under' , {'Position'} , get(axImPhase ,'Position') );
% switch event.Key; case 'leftarrow', disp( 'Izquierda' ); case 'rightarrow', disp( 'Derecha' ); end
% addlistener(fMag,'YLim','PostSet',@(src,evn) set( axImPhase_under' , {'Position'} , get(axImPhase,'Position') ));
% % 'KeyPressFcn',@(src,evn) disp('key')
%% Frequency singular vector
fSV = figure('WindowStyle','docked');
ht = tiledlayout(GridSize(1),GridSize(2)); ax = {};
ht.TileSpacing = 'tight'; ht.Padding = 'tight';
ax{end+1} = nexttile;
plot(s,'.-');
xline(prod(ht.GridSize)-1+0.5,'r')
xlabel('Ml2'); ylabel('singular value');
hold on
plot(extraMode,s(extraMode),'.r');
for ml2 = 1:(prod(ht.GridSize)-1)
if ml2>(prod(ht.GridSize)-1-length(extraMode)); ml2 = extraMode(ml2-(prod(ht.GridSize)-1)+length(extraMode)); end
ax{end+1} = nexttile;
x = squeeze(volPsd.svd.f);
y = abs(frSVl2(ml2,:));
plot(x,y.*s(ml2));
xlabel('Hz')
ylabel('singular vector mag * singular value')
title(['Level-2 mode ' num2str(ml2) '; method' num2str(method)])
grid on
if isfield(volPsd.dsgn,'onsets')
xline(1/mean(diff(volPsd.dsgn.onsets)),'--r')
else
xline(1/mean(diff(volPsd.dsgn.onsetList)),'--r')
end
end
axIm = [ax{2:end}];
yLim = get(axIm,'YLim');
yLim = [min([yLim{:}]) max([yLim{:}])];
set(axIm,'YLim',yLim);
title(ht,strjoin(subStr(1:3)))
if saveFlag
[a,b,~] = fileparts(fileparts(volPsd.fspec));
saveas(fMag, fullfile(a,[b '_L2SVD_spSvMag.fig']))
saveas(fPhase,fullfile(a,[b '_L2SVD_spSvPhase.fig']))
saveas(fSV, fullfile(a,[b '_L2SVD_tSvMag.fig']))
end