-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK_plotLinearPhaseWave.m
367 lines (290 loc) · 10 KB
/
K_plotLinearPhaseWave.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
function [h, N, outdata] = K_plotLinearPhaseWave(varargin)
% K_plotLinearPhaseWave create a linear histogram out of phase values in
% radians. It works on single sample and group data as well.
%
% [h, N, outdata] = K_plotLinearPhaseWave(S)
% [h, N, outdata] = K_plotLinearPhaseWave(axh, _____)
% [h, N, outdata] = K_plotLinearPhaseWave(_____, 'Param', Value, ...)
%
%
% INPUT ARGUMENTS
%
% S Scalar (single data) or non-scalar (groupdata) structure
% with the fields
%
% 'binmean'
% 'binstd'
% 'binsem'
% 'axrad'
% 'meanvec'
% 'bootstrap'
%
% axh [] | an Axes object | two Axes objects
% Axes handle. This can be empty, scalar or two-element array.
% For scalar axh, this function will delete axh and draw the
% main and sub axes in place of axh, so that The outmost
% rectangle of the main and sub axes tallies with Position of
% axh. If axh has two elements, then axh(1) will be the main
% axes and axh(2) will be the sub axes.
%
% OPTIONAL PARAMETER/VALUE PAIRS
%
% 'Color' ColorSpec
%
% 'XGrid' 'on' (default) | 'off' (1 or 0)
%
% 'PlotType' 'line' | 'bar' (default) | 'surface' |'none'
%
% 'ErrorBar' 'none' (default) | 'std' | 'sem'
%
% 'AnalysisMode'
% 'auto' (default) | 'one' | 'group'
% When S is scalar you can decide which type of analysis you
% want to perform.
%
% 'Title' string for title
%
% 'YUnit' 'mv2mv' (default) | 'mv2microv'
% 'mv2microv' will 1000-fold values for Y axis and use
% microvolt(µV) as unit.
%
% OUTPUT ARGUMENTS
%
% h structure of handles
% h.main main axes
% h.sub sub axes for sign curve
% h.titlepane axes for title
%
% N Sample number
%
% outdata Structure with fields:
% outdata.x
% outdata.y
% outdata.yerr
%
% Supporting group data and shaded error bars, 21/06/2013
%
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% 24-May-2017 13:13:54
%
% See also
% K_plotCircPhaseWave_one, K_plotCircPhaseWave_group
% K_plotLinearPhaseHist, K_plotLinearPhase, K_PhaseWave
% K_PhaseHist, K_plotCircPhaseHist_one
% K_plotCircularSingle, K_plotCircularGroup % what's the difference among them?
[axrad,binmean,binerr,errorBar,axh,ColorSpec,xgrid,plottype,titlestr,...
btstrp,circsh,meanvec,analysismode,yunit] = local_parser(varargin{:});
%%
switch analysismode
case 'one'
N = 1;
x = axrad;
y = binmean;
yerr = binerr;
case 'group'
N = size(binmean,2);
assert(all(arrayfun(@(k) all(axrad(:,1) == axrad(:,k)),1:N)),...
'Assuming axrad is identical among samples (mandatory to compute group mean).')
if isempty(axrad)
h = [];
outdata = [];
disp('No data to plot.')
return
end
x = axrad(:,1);
switch plottype
case {'line','bar','none'}
y = nanmean(binmean,2);
switch errorBar
case 'std'
yerr = nanstd(binmean,0,2);
case 'sem'
binstd = nanstd(binmean,0,2);
yerr = binstd/sqrt(N);
case 'none'
yerr = [];
end
case 'surface'
y = binmean;
yerr = [];
end
end
if strcmpi(yunit,'mv2microv')
y = y.*1000;
yerr = yerr.*1000;
end
if strcmp(plottype,'none')
axh = [];
ylabelstr = '';
[h,outdata] = plotLinearPhase(axh,x,y,yerr,analysismode,ColorSpec,xgrid,plottype,...
titlestr,ylabelstr,errorBar);
else
if isempty(axh)
figure;
axh = axes;
end
switch plottype
case 'surface'
ylabelstr = '';
otherwise
if strcmpi(yunit,'mv2microv')
ylabelstr = 'Modulation (µV)';
else
ylabelstr = 'Modulation (mV)';
end
end
[h,outdata] = plotLinearPhase(axh,x,y,yerr,analysismode,ColorSpec,xgrid,plottype,...
titlestr,ylabelstr,errorBar);
if strcmpi(yunit,'mv2microv') && strcmpi(plottype,'surface')
h.colorbar.Label.String = regexprep(h.colorbar.Label.String,'\(mV\)',...
sprintf('(%sV)',char(181)));
end
%% Add text
if ~isempty(btstrp) && ~isempty(meanvec)
h.txt = local_placetext(N,btstrp,circsh,meanvec,h.main.axh,analysismode); %TODO
end
end
end
%--------------------------------------------------------------------------
function [axrad,binmean,binerr,errorBar,axh,ColorSpec,xgrid,plottype,titlestr,...
btstrp,circsh,meanvec,analysismode,yunit] = local_parser(varargin)
% initialization
axh = [];
% vfaxhrad = @(x) isvector(x) && isnumeric(x) || ...
% iscell(x) && ...
% all(cellfun(@(y) isnumeric(y) && iscolumn(y) || isempty(y), x)) ||...
% all(cellfun(@(y) isnumeric(y) && isrow(y) || isempty(y) , x));
%NOTE cell contents must be all columns or all rows
if ishandle(varargin{1})
% [h,N] = K_plotLinearPhaseWave(axh, _____)
axh = varargin{1};
assert(numel(axh) <= 2 && all(isgraphics(axh,'axes')))
varargin = varargin(2:end);
end
p = inputParser;
p.addRequired('S',@(x) isstruct(x) && isequal(sort(fieldnames(x)),sort({...
'binmean';...
'binstd';...
'binsem';...
'axrad';...
'meanvec';...
'circshift';...
'bootstrap'})));
p.addParameter('Color','b',@iscolorspec);
p.addParameter('XGrid','on',@(x) ~isempty(x) &&...
ischar(x) &&...
isrow(x) &&...
ismember(x, {'on','off'}));
p.addParameter('ErrorBar','sem',@(x) ismember(x,{'std','sem','none'}));
p.addParameter('PlotType','line',@(x) ~isempty(x) &&...
ischar(x) &&...
isrow(x) &&...
ismember(x, {'line','bar','surface','none'}));
p.addParameter('Title','',@(x) ~isempty(x) &&...
ischar(x) &&...
isrow(x));
p.addParameter('AnalysisMode','auto',@(x) ismember(x,{'one','group','auto'}));
p.addParameter('YUnit','mV2mV',@(x) ismember(lower(x),{'mv2mv','mv2microv'}));
p.parse(varargin{:});
S = p.Results.S;
ColorSpec = p.Results.Color;
errorBar = p.Results.ErrorBar;
analysismode = p.Results.AnalysisMode;
xgrid = p.Results.XGrid;
plottype = p.Results.PlotType;
titlestr = p.Results.Title;
yunit = lower(p.Results.YUnit);
axrad = [S(:).axrad]; % S(k).axrad is a column vector
binmean = [S(:).binmean];
btstrp = [S(:).bootstrap];
circsh = [S(:).circshift];
meanvec = [S(:).meanvec];
switch errorBar
case 'std'
binerr = [S(:).binstd];
case 'sem'
binerr = [S(:).binsem];
case 'none'
binerr = [];
end
if isscalar(S)
if isequal(analysismode,'auto')
analysismode = 'one';
else
% user's analysismode is chosen
end
else
analysismode = 'group';
end
end
%--------------------------------------------------------------------------
function txth = local_placetext(N,btstrp,circsh,meanvec,mainaxh,analysismode)
switch analysismode
case 'one'
if (isempty(btstrp) && isempty(circsh)) || ...
(isempty(btstrp.p_lessthan) && isempty(circsh.p_lessthan))
pstr = '';
else
if ~isempty(circsh.p_lessthan)
pstr = sprintf('Circshift test: p < %f\n', ...
circsh.p_lessthan);
elseif ~isempty(btstrp.p_lessthan)
pstr = sprintf('Bootstrap test: p < %f\n', ...
btstrp.p_lessthan);
end
end
thestr = sprintf(['%s',...
'Phase of mean vector: %.3f%s\n',...
'Length of mean vector: %.3e mV\n'], ...
pstr,...
meanvec.degree,char(176),...
meanvec.length);
case 'group'
% circmean of phase values and mean length
samplevecrad = [meanvec(:).radian]';
groupradmean = circ_mean(samplevecrad);
groupradstd = circ_std(samplevecrad);
sampleveclen = [meanvec(:).length];
groupveclenmean = mean(sampleveclen);
groupveclenstd = std(sampleveclen);
if isempty([circsh(:).p_lessthan]) && isempty([circsh(:).p_lessthan])
pstr = '';
else
if ~isempty([circsh(:).p_lessthan])
p = [circsh(:).p_lessthan]';
n1 = nnz(p < 0.001);
n2 = nnz(p < 0.01);
n3 = nnz(p < 0.05);
n4 = nnz(0>= 0.05);
sigN = n3;
pstr = sprintf('Circshift test (p < 0.05): %d of %d\n', ...
sigN,N);
elseif ~isempty([circsh(:).p_lessthan])
p = [btstrp(:).p_lessthan]';
n1 = nnz(p < 0.001);
n2 = nnz(p < 0.01);
n3 = nnz(p < 0.05);
n4 = nnz(0>= 0.05);
sigN = n3;
pstr = sprintf('Bootstrap test (p < 0.05): %d of %d\n', ...
sigN,N);
end
end
thestr = sprintf(['n = %d\n',...
'%s',...
'Group cicrular mean %s std: %.3f %s %.3f%s\n', ...
'Vector length mean %s std: %3f %s %3f mV\n'], ...
N,...
pstr,...
char(177),rad2deg(groupradmean),char(177),rad2deg(groupradstd),char(176),...
char(177),groupveclenmean,char(177),groupveclenstd);
end
axes(mainaxh);
txth = text(0.95, 0.95, thestr,...
'HorizontalAlignment', 'right', 'VerticalAlignment','top',...
'Units', 'normalized','Tag','Annotation Text');
end