-
Notifications
You must be signed in to change notification settings - Fork 27
/
limo_display_image.m
536 lines (483 loc) · 20.3 KB
/
limo_display_image.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
function limo_display_image(LIMO,toplot,mask,mytitle,dynamic)
% This function displays images with a intensity plotted as function of
% time or frequency (x) and electrodes (y) - for ERSP it precomputes what
% needs to be plotted and call LIMO_display_image_tf
%
% FORMAT: limo_display_image(LIMO,toplot,mask,mytitle,dynamic)
%
% INPUTS:
% LIMO.mat = Name of the file to image
% toplot = 2D matrix to plot (typically t/F values)
% mask = areas for which to show data (to show all mask = ones(size(topolot))
% mytitle = title to show
% dynamic = set to 0 for no interaction (default is 1)
%
% The colour scales are from https://github.com/CPernet/brain_colours
% using linear luminance across the range with cool for negative and
% hot for positive maps and the divergent BWR scale for negative and positive
% maps. Note that masked values are always gray.
%
% Reference: Pernet & Madan (2019). Data visualization for inference in
% tomographic brain imaging.
% https://onlinelibrary.wiley.com/doi/full/10.1111/ejn.14430
%
% ----------------------------------
% Copyright (C) LIMO Team 2019
if nargin == 4
dynamic = 1;
end
%% get some informations for the plots
if sum(toplot(:)) == 0
error('the image to plot is empty')
end
% what do we plot? the data (toplot) masked (tpically of significance)
scale = toplot.*single(mask>0);
scale(scale==0) = NaN;
cc = limo_color_images(scale); % get a color map commensurate to that
v = max(scale(:)); % from the 2D data to plot, find max
[e,f]=find(scale==v); % which channel and time/frequency frame
if length(e)>1 % if we have multiple times the exact same max values
e = e(1); f = f(1); % then take the 1st (usually an artefact but allows to see it)
end
% for each cluster, get start/end/max value
% if unthresholded, uncorrected, tfce or max = mask is made up of ones
n_cluster = max(mask(:));
cluster_start = NaN(1,n_cluster); % start of each cluster
cluster_end = NaN(1,n_cluster); % end of each cluster
cluster_maxv = NaN(1,n_cluster); % max value for each cluster
cluster_maxe = NaN(1,n_cluster); % channel location of the max value of each cluster
cluster_maxf = NaN(1,n_cluster); % frame location of the max value of each cluster
for c=1:n_cluster
tmp = toplot.*(mask==c);
tmp(tmp==Inf) = NaN;
tmp(tmp==-Inf) = NaN;
sigframes = sum(tmp,1);
cluster_start(c) = find(sigframes,1,'first');
cluster_end(c) = find(sigframes,1,'last');
[V,type] = max([abs(min(tmp(:))) max(tmp(:))]);
if type == 2
cluster_maxv(c) = V(1);
else
V = -V;
cluster_maxv(c) = V(1);
end
[cluster_maxe(c),cluster_maxf(c)] = ind2sub(size(tmp),find(tmp==V(1)));
end
%% get frame information
if strcmpi(LIMO.Analysis,'Time')
if isfield(LIMO.data,'timevect')
timevect = LIMO.data.timevect;
if size(timevect,2) == 1; timevect = timevect'; end
else
timevect = [];
end
if size(timevect,2) ~= size(toplot,2)
timevect = linspace(LIMO.data.start,LIMO.data.end,size(toplot,2));
LIMO.data.timevect = timevect;
if exist(LIMO.dir,'dir')
save(fullfile(LIMO.dir,'LIMO.mat'),'LIMO','-v7.3')
end
end
ratio = abs(timevect(end)-timevect(1)) / length(timevect); % this the diff in 'size' between consecutive frames
if LIMO.data.start < 0
frame_zeros = find(timevect == 0);
if isempty(frame_zeros)
frame_zeros = round(abs(LIMO.data.start) / ratio)+1;
end
else
frame_zeros = -round(min(timevect)/ ratio);
end
elseif strcmpi(LIMO.Analysis,'Frequency')
if isfield(LIMO.data,'freqlist')
freqvect = LIMO.data.freqlist;
if size(freqvect,2) == 1; freqvect = freqvect'; end
else
freqvect = [];
end
if size(freqvect,2) ~= size(toplot,2)
freqvect = linspace(LIMO.data.start,LIMO.data.end,size(toplot,2));
LIMO.data.freqlist = freqvect;
save(fullfile(LIMO.dir,'LIMO.mat'),'LIMO')
end
ratio = abs(freqvect(end)-freqvect(1)) / length(freqvect);
if LIMO.data.start < 0
frame_zeros = find(freqvect == 0);
if isempty(frame_zeros)
frame_zeros = round(abs(LIMO.data.start) / ratio)+1;
end
else
frame_zeros = -round(min(freqvect)/ ratio);
end
elseif strcmpi(LIMO.Analysis,'Time-Frequency')
if isfield(LIMO.data,'tf_times')
timevect = LIMO.data.tf_times;
if size(timevect,2) == 1; timevect = timevect'; end
else
timevect = [];
end
if size(timevect,2) ~= size(toplot,2)
timevect = linspace(LIMO.data.start,LIMO.data.end,size(toplot,2));
LIMO.data.tf_times = timevect;
save(fullfile(LIMO.dir,'LIMO.mat'),'LIMO')
end
ratio = abs(timevect(end)-timevect(1)) / length(timevect); % this the diff in 'size' between consecutive frames
if LIMO.data.start < 0
frame_zeros = find(timevect == 0);
if isempty(frame_zeros)
frame_zeros = round(abs(LIMO.data.start) / ratio)+1;
end
else
frame_zeros = -round(min(timevect)/ ratio);
end
if isfield(LIMO.data,'tf_freqs')
freqvect = LIMO.data.tf_freqs;
if size(freqvect,2) == 1; freqvect = freqvect'; end
else
freqvect = [];
end
if size(freqvect,2) ~= size(toplot,1)
freqvect = linspace(LIMO.data.lowf,LIMO.data.highf,size(toplot,1));
LIMO.data.tf_freqs = freqvect;
save(fullfile(LIMO.dir,'LIMO.mat'),'LIMO')
end
else
error('LIMO.Analysis unspecfied')
end
if isempty(mytitle)
if isfield(LIMO.design,'name')
mytitle = LIMO.design.name;
else
mytitle = ' ';
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% make the main figure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure; set(gcf,'Color','w','InvertHardCopy','off');
% course plot at best electrode
ax(3) = subplot(3,3,9);
if ~isfield(LIMO.data, 'chanlocs') || isfield(LIMO.data,'expected_chanlocs')
LIMO.data.chanlocs = LIMO.data.expected_chanlocs;
end
if size(toplot,1) == 1
if strcmpi(LIMO.Analysis,'Time')
plot(timevect,toplot,'LineWidth',3);
elseif strcmpi(LIMO.Analysis,'Frequency')
plot(freqvect,toplot,'LineWidth',3);
end
grid on; ylabel('stat value'); axis tight
if isfield(LIMO,'Type')
if strcmpi(LIMO.Type,'Components')
mytitle2 = 'Average component';
elseif strcmpi(LIMO.Type,'Channels')
mytitle2 = 'Average channel';
end
else
mytitle2 = 'Average channel';
end
else
if strcmpi(LIMO.Analysis,'Time')
plot(timevect,toplot(e,:),'LineWidth',3);
elseif strcmpi(LIMO.Analysis,'Frequency')
plot(freqvect,toplot(e,:),'LineWidth',3);
elseif strcmpi(LIMO.Analysis,'Time-Frequency')
plot(timevect,toplot(e,:),'LineWidth',3);
mytitle2 = sprintf('stat values @ %g Hz', freqvect(e));
end
grid on; axis tight
if ~strcmpi(LIMO.Analysis,'Time-Frequency')
if isfield(LIMO,'Type')
if strcmpi(LIMO.Type,'Components')
mytitle2 = sprintf('stat values @ \n component %g', e);
elseif strcmpi(LIMO.Type,'Channels')
label = LIMO.data.chanlocs(e).labels;
mytitle2 = sprintf('stat values @ \n channel %s (%g)', label,e);
end
else
try
label = LIMO.data.chanlocs(e).labels;
mytitle2 = sprintf('stat values @ \n channel %s (%g)', label.labels,e);
catch
mytitle2 = sprintf('stat values @ y=%g', e);
end
end
end
end
title(mytitle2,'FontSize',12)
% topoplot at max time
% ---------------------
if size(toplot,1) ~= 1 && ~strcmpi(LIMO.Analysis,'Time-Frequency')
ax(2) = subplot(3,3,6);
opt = {'maplimits','maxmin','verbose','off','colormap', limo_color_images(toplot)};
if isfield(LIMO,'Type')
if strcmpi(LIMO.Type,'Components')
opt = {'maplimits','absmax','electrodes','off','verbose','off','colormap', limo_color_images(toplot)};
topoplot(toplot(:,f),LIMO.data.chanlocs,opt{:});
else
topoplot(toplot(:,f),LIMO.data.chanlocs,opt{:});
end
if size(toplot,2) == 1
title('Topoplot','FontSize',12)
else
if strcmpi(LIMO.Analysis,'Time')
title(['topoplot @ ' num2str(round(timevect(f))) 'ms'],'FontSize',12)
set(gca,'XTickLabel', timevect);
elseif strcmpi(LIMO.Analysis,'Frequency')
title(['topoplot @' num2str(round(freqvect(f))) 'Hz'],'FontSize',12);
set(gca,'XTickLabel', LIMO.data.freqlist);
end
end
elseif ~isempty(LIMO.data.chanlocs)
topoplot(toplot(:,f),LIMO.data.chanlocs,opt{:});
if size(toplot,2) == 1
title('Topoplot','FontSize',12)
else
if strcmpi(LIMO.Analysis,'Time')
title(['topoplot @ ' num2str(round(timevect(f))) 'ms'],'FontSize',12)
set(gca,'XTickLabel', timevect);
elseif strcmpi(LIMO.Analysis,'Frequency')
title(['topoplot @' num2str(round(freqvect(f))) 'Hz'],'FontSize',12);
set(gca,'XTickLabel', LIMO.data.freqlist);
end
end
end
end
% images toplot
% -------------------------------
ax(1) = subplot(3,3,[1 2 4 5 7 8]);
if strcmpi(LIMO.Analysis,'Time')
imagesc(timevect,1:size(toplot,1),scale);
colormap(gca, cc);
elseif strcmpi(LIMO.Analysis,'Frequency')
imagesc(freqvect,1:size(toplot,1),scale);
colormap(gca, cc);
elseif strcmpi(LIMO.Analysis,'Time-Frequency')
imagesc(timevect,freqvect,scale);
colormap(gca, cc);
end
set_imgaxes(LIMO,scale);
title(mytitle,'Fontsize',12)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% return cluster info
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
warning off
if contains(mytitle,'cluster')
for c=1:n_cluster
if strcmpi(LIMO.Analysis,'Time')
fprintf('cluster %g starts at %gms ends at %gms, max %g @ %gms channel %s \n', c, ...
timevect(cluster_start(c)),timevect(cluster_end(c)), cluster_maxv(c), timevect(cluster_maxf(c)), LIMO.data.chanlocs(cluster_maxe(c)).labels);
elseif strcmpi(LIMO.Analysis,'Frequency')
fprintf('cluster %g starts at %gHz ends at %gHz, max %g @ %gHz channel %s \n', c, ...
freqvect(cluster_start(c)),freqvect(cluster_end(c)), cluster_maxv(c), freqvect(cluster_maxf(c)), LIMO.data.chanlocs(cluster_maxe(c)).labels);
elseif strcmpi(LIMO.Analysis,'Time-Frequency')
f1 = scale(:,cluster_start(c)); f2 = scale(:,cluster_end(c)); f3 = scale(:,cluster_maxf(c));
fprintf('cluster %g at %gms %gHz, ends at %gms %gHz, max %g @ %gms %gHz \n', c, ...
timevect(cluster_start(c)),freqvect(find(f1==max(f1))), ...
timevect(cluster_end(c)), freqvect(find(f2==max(f2))), ...
cluster_maxv(c), timevect(cluster_maxf(c)), freqvect(find(f3==max(f3))));
end
end
else % no clusters (we have make one )
if strcmpi(LIMO.Analysis,'Time')
fprintf('1st significant frame at %gms, last signifiant frame at %gms, max %g @ %gms channel %s \n', ...
timevect(cluster_start(c)),timevect(cluster_end(c)), cluster_maxv(c), timevect(cluster_maxf(c)), LIMO.data.chanlocs(cluster_maxe(c)).labels);
elseif strcmpi(LIMO.Analysis,'Frequency')
fprintf('1st significant frame at %gHz, last signifiant frame at %gHz, max %g @ %gHz channel %s \n', ...
freqvect(cluster_start(c)),freqvect(cluster_end(c)), cluster_maxv(c), freqvect(cluster_maxf(c)), LIMO.data.chanlocs(cluster_maxe(c)).labels);
elseif strcmpi(LIMO.Analysis,'Time-Frequency')
f1 = scale(:,cluster_start(c)); f2 = scale(:,cluster_end(c)); f3 = scale(:,cluster_maxf(c));
fprintf('1st significant frame at %gms %gHz, last signifiant frame at %gms %gHz, max %g @ %gms %gHz \n', ...
timevect(cluster_start(c)),freqvect(find(f1==max(f1))), ...
timevect(cluster_end(c)), freqvect(find(f2==max(f2))), ...
cluster_maxv(c), timevect(cluster_maxf(c)), freqvect(find(f3==max(f3))));
end
end
warning on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% update with mouse clicks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if dynamic == 1
if size(toplot,1) > 1
update = 0;
while update ==0
try
[x,y,button]=ginput(1);
catch
break
end
if button > 1
update = 1; % right click to come out of the dynamic figure
end
clickedAx = gca;
if clickedAx ~=ax(1)
disp('right click to exit')
else
% topoplot at new time or freq
% because x axis is already with the correct value
frame = frame_zeros + round(x / ratio);
if frame<=0; frame = 1; end
if frame>=size(toplot,2); frame=size(toplot,2); end
if strcmpi(LIMO.Analysis,'Time-Frequency')
% course plot at selected frequency
y = round(y);
[~,yframe] = min(abs(freqvect - y));
if size(toplot,1)> 1 && yframe>size(toplot,1)
yframe = size(toplot,1);
y = freqvect(yframe);
elseif size(toplot,1)> 1 && y<1
yframe = 1;
y = freqvect(yframe);
end
else
% course plot at selected channel
y = round(y);
if size(toplot,1)> 1 && y>size(toplot,1)
y = size(toplot,1);
elseif size(toplot,1)> 1 && y<1
y = 1;
end
end
if strcmpi(LIMO.Analysis,'Time')
if ~contains(LIMO.design.name, ['one ' LIMO.Type(1:end-1)]) && ~isempty(LIMO.data.chanlocs)
subplot(3,3,6,'replace');
if size(toplot,2) == 1
topoplot(toplot(:,1),LIMO.data.chanlocs,opt{:});
title('topoplot','FontSize',12)
else
topoplot(toplot(:,frame),LIMO.data.chanlocs,opt{:});
title(['topoplot @ ' num2str(round(x)) 'ms'],'FontSize',12)
end
end
elseif strcmpi(LIMO.Analysis,'Frequency')
if ~contains(LIMO.design.name, ['one ' LIMO.Type(1:end-1)]) && ~isempty(LIMO.data.chanlocs)
subplot(3,3,6,'replace');
if size(toplot,2) == 1
topoplot(toplot(:,1),LIMO.data.chanlocs,opt{:});
title('topoplot','FontSize',12)
else
topoplot(toplot(:,frame),LIMO.data.chanlocs,opt{:});
title(['topoplot @ ' num2str(round(x)) 'Hz'],'FontSize',12)
end
end
end
subplot(3,3,9,'replace');
if size(toplot,2) == 1
bar(toplot(y,1)); grid on; axis([0 2 0 max(toplot(:))+0.2]); ylabel('stat value')
if isfield(LIMO,'Type')
if strcmpi(LIMO.Type,'Components')
mytitle2 = sprintf('component %g', y);
elseif strcmpi(LIMO.Type,'Channels')
mytitle2 = sprintf('channel %s (%g)', LIMO.data.chanlocs(y).labels,y);
end
else
mytitle2 = sprintf('channel %s (%g)', LIMO.data.chanlocs(y).labels,y);
end
else
if strcmpi(LIMO.Analysis,'Time')
plot(timevect,toplot(y,:),'LineWidth',3);
elseif strcmpi(LIMO.Analysis,'Frequency')
plot(freqvect,toplot(y,:),'LineWidth',3);
elseif strcmpi(LIMO.Analysis,'Time-Frequency')
plot(timevect,toplot(yframe,:),'LineWidth',3);
mytitle2 = sprintf('stat values @ %g Hz', y);
end
grid on; axis tight
if ~strcmpi(LIMO.Analysis,'Time-Frequency')
if isfield(LIMO,'Type')
if strcmpi(LIMO.Type,'Components')
mytitle2 = sprintf('stat values @ \n component %g', y);
elseif strcmpi(LIMO.Type,'Channels')
mytitle2 = sprintf('stat values @ \n channel %s (%g)', LIMO.data.chanlocs(y).labels,y);
end
else
try
mytitle2 = sprintf('stat values @ \n channel %s (%g)', LIMO.data.chanlocs(y).labels,y);
catch
mytitle2 = sprintf('stat values @ \n y=%g)', y);
end
end
end
end
title(mytitle2,'FontSize',12);
subplot(3,3,[1 2 4 5 7 8]); colormap(gca, cc);
try
p_values = evalin('base','p_values');
if strcmpi(LIMO.Analysis,'Time-Frequency')
if ~isnan(p_values(yframe,frame))
fprintf('Stat value: %g, p_value %g \n',toplot(yframe,frame),p_values(yframe,frame));
else
fprintf('Stat value: %g, p_value is a NaN? \n',toplot(yframe,frame));
end
else
if ~isnan(p_values(round(y),frame))
fprintf('Stat value: %g, p_value %g \n',toplot(round(y),frame),p_values(round(y),frame));
else
fprintf('Stat value: %g, p_value is a NaN? \n',toplot(round(y),frame));
end
end
catch pvalerror
fprintf('couldn''t figure the stats values?? %s \n',pvalerror.message)
end
end
end
end
end
res = true;
end
%% set axes and labels
% -------------------------------------------------------------------------
function set_imgaxes(LIMO,scale)
img_prop = get(gca);
set(gca,'LineWidth',2)
% ----- X --------
if strcmpi(LIMO.Analysis,'Time') || strcmpi(LIMO.Analysis,'Time-Frequency')
xlabel('Time in ms','FontSize',10)
elseif strcmpi(LIMO.Analysis,'Frequency')
xlabel('Frequency in Hz','FontSize',10)
end
% ----- Y --------
if strcmpi(LIMO.Analysis,'Time-Frequency')
ylabel('Frequency in Hz','FontSize',10)
else
if strcmpi(LIMO.Type,'Components')
if size(scale,1) == 1
ylabel('Optimized component','FontSize',10);
else
ylabel('Components','FontSize',10);
end
else
if size(scale,1) == 1
ylabel('Optimized channel','FontSize',10);
else
ylabel('Channels','FontSize',10);
end
end
if isfield(LIMO.data, 'chanlocs')
Ylabels = arrayfun(@(x)(x.labels), LIMO.data.chanlocs, 'UniformOutput', false);
else
Ylabels = arrayfun(@(x)(x.labels), LIMO.data.expected_chanlocs, 'UniformOutput', false);
end
newticks = round(linspace(1,length(Ylabels),length(img_prop.YTick)*2));
newticks = unique(newticks);
Ylabels = Ylabels(newticks);
if size(scale,1) == 1
set(gca,'YTick',1);
else
set(gca,'YTick',newticks);
set(gca,'YTickLabel', Ylabels);
end
end
% ----- Colormap --------
try
maxval = max(abs(max(scale(:))),abs(min(scale(:))));
if max(scale(:)) < 0
caxis([-maxval 0])
elseif min(scale(:)) > 0
caxis([0 maxval])
else
caxis([-maxval maxval])
end
catch caxiserror
fprintf('axis issue: %s\n',caxiserror.message)
end
end