-
Notifications
You must be signed in to change notification settings - Fork 27
/
limo_add_plots.m
421 lines (394 loc) · 15.5 KB
/
limo_add_plots.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
function limo_add_plots(varargin)
% interactive ploting function for data generated by
% limo_central_tendency_and_ci, limo_plot_difference or any data in
% 4D with dim channels * frames * conditions * 3 with this last dim being
% the low end of the confidence interval, the estimator (like eg mean),
% high end of the confidence interval.
%
% FORMAT limo_add_plots % calls the GUI
% limo_add_plots({myfiles})
% limo_add_plots({myfiles},LIMOfile)
% limo_add_plots({myfiles},LIMOfile,key,value)
%
% INPUTS myfiles is a cell array of .mat files to plot
% LIMOfile is the LIMO file with the corresponding metadata (optional but recommended)
% options are defined by key value pairs
% 'channel' with a index of the channel to plot
% 'restrict' either 'Time' or 'Frequency' for Time-Frequency data
% 'dimvalue' value for reduced dimension when using Time-Frequency data
% e.g. 'Channel',49,'restrict','Time','dimvalue',5 will plot data at channel 49 in time at 5Hz
% 'variable' with the value to indicate the variable to plot for arrays of many variables
% 'figure' 'new' (default) or 'hold' to plot in existing figure
%
% ------------------------------
% Copyright (C) LIMO Team 2021
%% supported type
options = {'mean','trimmed_mean','median','Harrell_Davis','diff','data'};
%% check inputs
out = 0;
turn = 1;
infile = [];
channel = [];
restrict = [];
dimvalue = [];
fig = 'new';
warning on
if ~isempty(varargin)
for i=1:size(varargin,2)
if ischar(varargin{i})
if strcmpi(varargin{i},'channel')
channel = varargin{i+1};
elseif strcmpi(varargin{i},'restrict') % for Time-Frequency
restrict = varargin{i+1};
elseif strcmpi(varargin{i},'dimvalue') % for Time-Frequency
dimvalue = varargin{i+1};
elseif strcmpi(varargin{i},'variable') % for arrays of many variables
v = varargin{i+1};
elseif strcmpi(varargin{i},'figure') % for arrays of many variables
fig = varargin{i+1};
elseif contains(varargin{i},'LIMO.mat')
LIMO = varargin{i};
end
elseif iscell(varargin{i})
infile = varargin{i};
elseif isstruct(varargin{i})
LIMO = varargin{i}; % LIMO.mat structure passed directly
end
end
end
% ERSP hack
if length(infile) == 1 && ...
~isempty(restrict) && length(dimvalue) > 1
infile = repmat(infile,[1,length(dimvalue)]);
end
previous_file = [];
while out == 0
subjects_plot = 0;
%% Data selection
if ~isempty(infile) % allows comand line plot
if turn <= length(infile)
file = infile{turn};
index = 1;
else
out = 1; return
end
else
[file,path,index]=uigetfile('*mat',['Select Central tendency file n:' num2str(turn) '']);
file = fullfile(path,file);
if strcmp(file,previous_file) % reselect same file = want to plot a new channel
channel = [];
end
previous_file = file;
end
if index == 0
out = 1; return
else
data = load(file);
data = data.(cell2mat(fieldnames(data)));
if ~isstruct(data)
error('limo add plots input(s) must be structures from limo_central_tendency_and_ci.m and limo_plot_difference.m')
end
datatype = fieldnames(data);
datatype = datatype(cellfun(@(x) strcmp(x,'limo'), fieldnames(data))==0);
if sum(strcmpi(datatype,options)) == 0
if exist(errordlg2,'file')
errordlg2('unknown file to plot');
else
errordlg('unknown file to plot');
end
return
end
name{turn} = cell2mat(datatype);
tmp = data.(cell2mat(datatype));
% overwrite metadata if LIMO file provided
if exist('LIMO','var')
if ischar(LIMO); LIMO = load(LIMO); LIMO = LIMO.LIMO; end
F = fieldnames(LIMO);
for f=1:length(F)
data.limo.(cell2mat((F(f)))) = LIMO.(cell2mat((F(f))));
end
clear LIMO
elseif ~isfield(data,'limo')
[limofile,locpath]=uigetfile({'LIMO.mat'},'Select any LIMO with right info');
if strcmpi(limofile,'LIMO.mat')
LIMO = load(fullfile(locpath,limofile));
data.limo = LIMO.LIMO; clear LIMO;
save(fullfile(path,file),'data')
else
warning('selection aborded'); return
end
end
% reduce dimension for time frequency
if strcmpi(data.limo.Analysis,'Time-Frequency')
if ~isempty(dimvalue)
tmp = squeeze(tmp); % only 1 variable
if length(dimvalue) == 1
if turn == 1 % freq or time index always the same since dimvalue == 1
[~,~,freq,time] = limo_display_reducedim(tmp,data.limo,channel,restrict,dimvalue);
end
else
tmp_dimvalue = dimvalue(turn); % freq or time index needs to be updated
[~,~,freq,time] = limo_display_reducedim(tmp,data.limo,channel,restrict,tmp_dimvalue);
end
else
if ~isfield(data.limo,'data')
if ~exist('LIMO','var')
[Name,Path,go] = uigetfile('LIMO.mat','Data information needed, select a relevant LIMO file');
if go == 1 && strcmpi(Name,'LIMO.mat')
LIMO = load(fullfile(Path,'LIMO.mat')); LIMO = LIMO.LIMO;
data.limo.data = LIMO.data; data.limo.dir = fileparts(file);
save(file,'data');
else
disp('selection aborded'); return
end
else
data.limo.data = LIMO.data; data.limo.dir = fileparts(file);
save(file,'data');
end
end
[~,channel,freq,time] = limo_display_reducedim(tmp,data.limo,channel,restrict,dimvalue);
if length(time) == 1
restrict = 'frequency';
else
restrict = 'time';
end
end
tmp = squeeze(tmp(:,freq,time,:));
end
% the last dim of data.data can be the number of subjects or the trials
% sorted by their weights - use file name to know which estimator was used
if isfield(data,'data')
if contains(file, 'Mean','IgnoreCase',true)
name{turn} = 'Subjects'' Means';
elseif contains(file, 'Trimmed mean','IgnoreCase',true)
name{turn} = 'Subjects'' Trimmed Means';
elseif contains(file, 'HD','IgnoreCase',true)
name{turn} = 'Subjects'' Mid Deciles HD';
elseif contains(file, 'Median','IgnoreCase',true)
name{turn} = 'Subjects'' Medians';
else
if strcmpi(file,'subjects_weighted_data.mat')
name{turn} = 'Data plotted per weight';
else
underscores = strfind(file, '_');
if ~isempty(underscores)
file(underscores) = ' ';
end
ext = strfind(file, '.');
file(max(ext):end) = [];
name{turn} = file;
end
end
subjects_plot = 1;
end
end
% store each iteration into Data
if strcmpi('diff',datatype)
if size(tmp,1) == 1
if ndims(tmp) == 4
Data = nan(1,size(tmp,2),size(tmp,3),size(tmp,4));
Data(1,:,:,:) = squeeze(tmp);
else
Data = nan(1,size(tmp,2),size(tmp,3));
Data(1,:,:) = squeeze(tmp);
end
else
Data = squeeze(tmp);
end
else
if size(tmp,1) == 1 && size(tmp,3) == 1 % only 1 channel and 1 variable not squeezed yet
D = squeeze(tmp(:,:,1,:));
Data = nan(1,size(tmp,2),size(tmp,4));
Data(1,:,:) = D; clear D;
elseif size(tmp,1) > 1 && size(tmp,3) == 1 && subjects_plot==0 % only 1 variable not squeezed yet
Data = squeeze(tmp(:,:,1,:));
elseif size(tmp,1) > 1 && size(tmp,3) == 3
Data = tmp;
else % many subjects for instance
if ~exist('v','var')
if isfield(data.limo,'PlotRank')
v = cell2mat(inputdlg(['which decile to plot, 1 to ' num2str(size(tmp,4)-1)],'plotting option'));
else
if size(tmp,3) == 1
v = 1;
else
v = cell2mat(inputdlg(['which variable to plot, 1 to ' num2str(size(tmp,3))],'plotting option'));
end
end
if isempty(v)
out = 1; return
elseif ischar(v)
v = eval(v);
end
end
if subjects_plot == 0 && length(v)>1
errordlg2('only 1 parameter value expected'); return
else
if size(tmp,1) == 1 && size(tmp,3) > 1
D = squeeze(tmp(:,:,v,:));
Data = nan(1,size(tmp,2),size(tmp,4));
Data(1,:,:) = D; clear D;
else
Data = squeeze(tmp(:,:,v,:));
end
end
end
end
clear tmp
%% prep figure the 1st time rounnd
% ------------------------------
if turn == 1
if strcmpi(fig,'new')
figure('Name','Central Tendency Estimate','color','w');
end
hold on
% frame info
% ----------
if strcmpi(data.limo.Analysis,'Time')
if isfield(data.limo.data,'timevect')
vect = data.limo.data.timevect;
else
vect = data.limo.data.start:(1000/data.limo.data.sampling_rate):data.limo.data.end; % in msec
end
elseif strcmpi(data.limo.Analysis,'Frequency')
if isfield(data.limo.data,'freqlist')
vect = data.limo.data.freqlist;
else
vect = linspace(data.limo.data.start,data.limo.data.end,size(Data,2));
end
elseif strcmpi(data.limo.Analysis,'Time-Frequency')
if strcmpi(restrict,'time')
if isfield(data.limo.data,'tf_times')
vect = data.limo.data.tf_times;
else
vect = data.limo.data.start:(1000/data.limo.data.sampling_rate):data.limo.data.end; % in msec
end
elseif strcmpi(restrict,'frequency')
if isfield(data.limo.data,'tf_freqs')
vect = data.limo.data.tf_freqs;
else
vect = linspace(data.limo.data.lowf,data.limo.data.highf,size(Data,2));
end
else
warning('x axis lable info missing');
vect = 1:size(Data,2);
end
elseif ~exist('vect','var')
v = inputdlg('no axis info? enter x axis interval e.g. [0:0.5:200]');
try
vect = eval(cell2mat(v));
if length(vect) ~= size(Data,2)
warning('interval invalid - using defaults');
vect = 1:size(Data,2);
end
catch ME
warning(ME.identifier,'xaxis interval invalid:%s - using default',ME.message)
vect = 1:size(Data,2);
end
end
end
%% channel to plot
% ----------------
if size(Data,1) == 1
Data = squeeze(Data(1,:,:));
else
if isempty(channel)
channel = inputdlg(['which channel to plot 1 to' num2str(size(Data,1))],'channel choice');
if isempty(channel) % user presed cancel
disp('plot aborded');
return
end
end
if strcmp(channel,'')
if ndims(Data)==2 %#ok<ISMAT>
tmp = Data;
else
tmp = Data(:,:,2);
end
if sum(isnan(tmp(:))) == numel(tmp)
error('the data file appears empty (only NaNs)')
else
if abs(max(tmp(:))) > abs(min(tmp(:)))
[channel,~,~] = ind2sub(size(tmp),find(tmp==max(tmp(:))));
else
[channel,~,~] = ind2sub(size(tmp),find(tmp==min(tmp(:))));
end
if length(channel) ~= 1; channel = channel(1); end
Data = squeeze(Data(channel,:,:)); fprintf('ploting channel %g\n',channel)
end
else
if ndims(Data)==2 %#ok<ISMAT>
try
Data = squeeze(Data(channel,:));
catch
Data = squeeze(Data(eval(cell2mat(channel)),:));
end
else
try
Data = squeeze(Data(channel,:,:));
catch
Data = squeeze(Data(eval(cell2mat(channel)),:,:));
end
end
end
end
% finally plot
% ---------------
plotted_data.xvect = vect;
if turn==1
if subjects_plot == 1
plot(vect,Data,'LineWidth',2);
plotted_data.data = Data;
else
plot(vect,Data(:,2)','LineWidth',3);
plotted_data.data = Data';
end
assignin('base','plotted_data',plotted_data)
colorOrder = get(gca, 'ColorOrder');
colorindex = 1;
else
if subjects_plot == 1 && length(vect) ~= length(Data)
warndlg('the new data selected have a different size, plot skipped')
elseif subjects_plot == 0 && length(vect) ~= size(Data,1)
warndlg('the new data selected have a different size, plot skipped')
else
if subjects_plot == 0
plot(vect,Data(:,2)','Color',colorOrder(colorindex,:),'LineWidth',3);
plotted_data.data = Data';
else
plot(vect,Data,'LineWidth',2);
plotted_data.data = Data;
end
assignin('base','plotted_data',Data')
end
end
if subjects_plot == 0 && size(vect,2) == size(Data,1)
fillhandle = patch([vect fliplr(vect)], [Data(:,1)',fliplr(Data(:,3)')], colorOrder(colorindex,:));
set(fillhandle,'EdgeColor',colorOrder(colorindex,:),'FaceAlpha',0.2,'EdgeAlpha',0.8);%set edge color
end
grid on; axis tight; box on;
if ~isempty(restrict)
xlabel(restrict,'FontSize',14)
else
xlabel(data.limo.Analysis,'FontSize',14)
end
ylabel('Amplitude','FontSize',14)
if turn == 1
mytitle = name{1};
else
mytitle = sprintf('%s & %s',mytitle,name{turn});
end
if iscell(channel)
channel = eval(cell2mat(channel));
end
title(sprintf('channel %g \n %s',channel,mytitle),'Fontsize',16,'Interpreter','none');
% updates
turn = turn+1;
if colorindex <7
colorindex = colorindex + 1;
else
colorindex = 1;
end
clear data tmp
pause(1);
end