-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewMTsvdKlein2.m
168 lines (156 loc) · 5.95 KB
/
viewMTsvdKlein2.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
function axIm = viewMTsvdKlein2(svdStruct,funPsd,peakFreq,imType,axIm)
if ~exist("imType",'var') || isempty(imType)
imType = 'psd'; % svMag, svPhase or psd
end
if exist('funPsd','var') && ~isempty(funPsd)
tmp = strsplit(funPsd.fspec,filesep); tmp = strsplit(tmp{end-1},'_');
sub = replace(tmp{contains(tmp,'sub-')},'sub-','');
ses = replace(tmp{contains(tmp,'ses-')},'ses-','');
run = replace(tmp{contains(tmp,'run-')},'run-','');
else
sub = '???';
ses = '???';
run = '???';
end
fpass = svdStruct.param.fpass;
fLim = [0 2.5];
K = svdStruct.param.tapers(2);
f = svdStruct.f;
%% Plot timeseries mean (underlay) only of not already plotted in axIm
if ~exist('axIm','var') || isempty(axIm)
axIm = {};
tmp = []; tmp.String = '';
else
tmp = [axIm{:}]; tmp = [tmp.Title];
end
if ~any(contains({tmp.String},'timeseries mean')) && exist('funPsd','var') && ~isempty(funPsd)
figure('WindowStyle','docked');
hTile = tiledlayout(6,1); hTile.Padding = 'tight'; hTile.TileSpacing = "tight";
nexttile([5 1]);
imagesc(funPsd.tMean); hold on
maskC = getMaskOutline(funPsd.roi.mask,5);
hMask = plot(maskC); hMask.FaceColor = 'none'; hMask.EdgeColor = 'r';
axIm{end+1} = gca;
%%% Crop image
cropPrc = 75;
xLim = [find(any(funPsd.tMean>prctile(funPsd.tMean(:),cropPrc),1),1,'first') find(any(funPsd.tMean>prctile(funPsd.tMean(:),cropPrc),1),1,'last')];
yLim = [find(any(funPsd.tMean>prctile(funPsd.tMean(:),cropPrc),2),1,'first') find(any(funPsd.tMean>prctile(funPsd.tMean(:),cropPrc),2),1,'last')];
w = max([diff(xLim) diff(yLim)]);
xLim = mean(xLim)+[-1 1].*w/2;
yLim = mean(yLim)+[-1 1].*w/2;
axis(axIm{end},[xLim yLim]);
axIm{end}.PlotBoxAspectRatio = [1 1 1]; axIm{end}.DataAspectRatio = [1 1 1];
axIm{end}.XTick = []; axIm{end}.YTick = [];
axIm{end}.Colormap = gray;
cb = colorbar;
ylabel(cb,'MR intensity')
title(['sub-' sub '; ses-' ses '; run-' run ' K=' num2str(K) '; timeseries mean'])
%%% Plot spectrum
nexttile([1 1]);
yyaxis left
plot(funPsd.roi.f,funPsd.roi.psd); hold on
axis tight
ylabel('psd')
yyaxis right
plot(f,svdStruct.c); hold on
ylim([1/K 1])
ylabel('coherence')
ax1 = gca;
ax1.YAxis(2).Scale = 'linear';
ax1.YAxis(1).Scale = 'log';
xlim(fLim)
grid minor
%%% Plot spectrum peaks
if ~isnan(peakFreq)
plot(([1 1].*peakFreq')',repmat(ylim,length(peakFreq),1)','-k')
yLim = ylim;
plot((peakFreq'+[-1 1].*svdStruct.w)',yLim(1).*ones(size(peakFreq,2),2)','-g','LineWidth',3)
xlabel('Hz')
end
%%% Adjust
cb.Location = 'manual';
drawnow
cb.Position = [sum(axIm{end}.Position([1 3])) axIm{end}.Position(2) 0.02 axIm{end}.Position(4)];
cb.Visible = 'off';
end
peakFreq = peakFreq(~isnan(peakFreq));
%% Plot each frequency
for fInd = 1:length(peakFreq)
[~,b] = min(abs(f - peakFreq(fInd)));
figure('WindowStyle','docked');
%%% Plot map
if exist('axIm','var') && ~isempty(axIm)
axIm{end+1} = axes(gcf,'Units',axIm{end}.Units,'Position',axIm{end}.Position);
else
axIm{end+1} = axes(gcf);
end
switch imType
case 'psd'
curIm = vec2vol(funPsd); curIm = curIm.vol(:,:,:,b);
hIm = imagesc(curIm);
axIm{end}.Colormap = jet;
cb = colorbar;
ylabel(cb,'psd')
axIm{end}.ColorScale = 'log';
cLim = curIm(logical(funPsd.roi.mask)); cLim = [1 max(cLim)];
axIm{end}.CLim = cLim;
case 'svMag'
curIm = nan(size(svdStruct.mask));
curIm(logical(svdStruct.mask)) = svdStruct.sp(:,b,1);
hIm = imagesc(abs(curIm));
axIm{end}.Colormap = jet;
cb = colorbar;
ylabel(cb,'spatial sv weigth mag')
cLim = abs(curIm(logical(funPsd.roi.mask))); cLim = [min(cLim) max(cLim)];
axIm{end}.CLim = cLim;
cbTicks = cb.Ticks; if cbTicks(1) ~= cLim(1); cbTicks = [cLim(1) cbTicks]; end; if cbTicks(end) ~= cLim(2); cbTicks = [cbTicks cLim(2)]; end; cb.Ticks = cbTicks; cb.TickLabels(2:end-1) = {''};
cb.TickLabels{1} = 'min'; cb.TickLabels{end} = 'max';
case 'svPhase'
curIm = nan(size(svdStruct.mask));
curIm(logical(svdStruct.mask)) = svdStruct.sp(:,b,1);
hIm = imagesc(wrapToPi(angle(curIm) - angle(mean(curIm(:),'omitnan'))));
hIm.AlphaData = ( abs(curIm) - min(abs(curIm(:))) ) ./ max(abs(curIm(:)));
axIm{end}.Color = [1 1 1].*0.5;
axIm{end}.Colormap = hsv;
cb = colorbar;
hYl = ylabel(cb,'spatial sv weigth phase');
cLim = [-pi pi];
axIm{end}.CLim = cLim;
cb.Ticks = [-pi -pi/2 0 pi/2 pi];
cb.TickLabels = {'-pi' '-pi/2' '0' 'pi/2' 'pi'};
end
axIm{end}.PlotBoxAspectRatio = [1 1 1]; axIm{end}.DataAspectRatio = [1 1 1];
axIm{end}.XTick = []; axIm{end}.YTick = [];
title(['sub-' sub '; ses-' ses '; run-' run ' K=' num2str(K) '; ' num2str(peakFreq(fInd),'%.3f') 'Hz'])
if length(axIm)>1
axis([axIm{end-1}.XLim axIm{end-1}.YLim])
end
%% Plot spectra
if length(axIm)>1
axes(gcf,'Units',axIm{end-1}.Parent.Children(1).Units,'Position',axIm{end-1}.Parent.Children(1).Position);
else
axes
end
yyaxis left
plot(funPsd.roi.f,funPsd.roi.psd); hold on
axis tight
ylabel('psd')
ytickformat('%2.0f')
yyaxis right
plot(f,svdStruct.c); hold on
ylim([1/K 1])
ylabel('coherence')
ax = gca; ax.YAxis(2).Scale = 'linear'; ax.YAxis(1).Scale = 'log';
xlim(fLim)
grid minor
plot(([1 1].*f(b)')',ylim,'-k')
yLim = ylim;
plot((f(b)'+[-1 1].*svdStruct.w)',yLim(1).*[1 1]','-g','LineWidth',3)
xlabel('Hz')
%% Adjust
% cb.Location = 'manual';
% drawnow
axIm{end}.Position = axIm{end-1}.Position;
% cb.Position = [sum(axIm{end}.Position([1 3])) axIm{end}.Position(2) 0.02 axIm{end}.Position(4)];
end
linkaxes([axIm{:}])