-
Notifications
You must be signed in to change notification settings - Fork 159
/
figure_spectrum.m
1843 lines (1742 loc) · 76 KB
/
figure_spectrum.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = figure_spectrum( varargin )
% FIGURE_SPECTRUM: Creation and callbacks for power density spectrums figures (x-axis = frequency).
%
% USAGE: hFig = figure_spectrum('CreateFigure', FigureId)
% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2012-2019
% Martin Cousineau, 2017
% Marc Lalancette, 2020
eval(macro_method);
end
%% ===== CREATE FIGURE =====
function hFig = CreateFigure(FigureId) %#ok<DEFNU>
import org.brainstorm.icon.*;
MatlabVersion = bst_get('MatlabVersion');
% Get renderer name
if (MatlabVersion <= 803) % zbuffer was removed in Matlab 2014b
rendererName = 'zbuffer';
elseif (bst_get('DisableOpenGL') == 1)
rendererName = 'painters';
else
rendererName = 'opengl';
end
% Create new figure
hFig = figure('Visible', 'off', ...
'NumberTitle', 'off', ...
'IntegerHandle', 'off', ...
'MenuBar', 'none', ...
'Toolbar', 'none', ...
'DockControls', 'on', ...
'Units', 'pixels', ...
'Interruptible', 'off', ...
'BusyAction', 'queue', ...
'Tag', FigureId.Type, ...
'Renderer', rendererName, ...
'Color', [.8 .8 .8], ...
'Pointer', 'arrow', ...
'CloseRequestFcn', @(h,ev)bst_figures('DeleteFigure',h,ev), ...
'KeyPressFcn', @FigureKeyPressedCallback, ...
'WindowButtonDownFcn', @FigureMouseDownCallback, ...
'WindowButtonUpFcn', @FigureMouseUpCallback, ...
bst_get('ResizeFunction'), @(h,ev)figure_timeseries('ResizeCallback',h,ev));
% Define Mouse wheel callback separately (not supported by old versions of Matlab)
if isprop(hFig, 'WindowScrollWheelFcn')
set(hFig, 'WindowScrollWheelFcn', @FigureMouseWheelCallback);
end
% Disable automatic legends (after 2017a)
if (MatlabVersion >= 902)
set(hFig, 'defaultLegendAutoUpdate', 'off');
end
% Prepare figure appdata
setappdata(hFig, 'FigureId', FigureId);
setappdata(hFig, 'hasMoved', 0);
setappdata(hFig, 'isPlotEditToolbar', 0);
setappdata(hFig, 'isSensorsOnly', 0);
setappdata(hFig, 'GraphSelection', []);
setappdata(hFig, 'isStatic', 0);
setappdata(hFig, 'isStaticFreq', 1);
setappdata(hFig, 'Colormap', db_template('ColormapInfo'));
% Time-freq specific appdata
TfInfo = db_template('TfInfo');
setappdata(hFig, 'Timefreq', TfInfo);
end
%% ===========================================================================
% ===== FIGURE CALLBACKS ====================================================
% ===========================================================================
%% ===== CURRENT FREQ CHANGED =====
function CurrentTimeChangedCallback(hFig) %#ok<DEFNU>
% If no time in this figure
if getappdata(hFig, 'isStatic')
return;
end
TfInfo = getappdata(hFig, 'Timefreq');
switch (TfInfo.DisplayMode)
% Spectrum: redraw everything
case 'Spectrum'
UpdateFigurePlot(hFig, 1);
% Time series: Move cursor
case 'TimeSeries'
hAxes = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph');
if ~isempty(hAxes)
PlotCursor(hFig, hAxes);
end
end
end
%% ===== CURRENT FREQ CHANGED =====
function CurrentFreqChangedCallback(hFig) %#ok<DEFNU>
global GlobalData;
% If no frequencies for this figure
if getappdata(hFig, 'isStaticFreq')
return;
end
TfInfo = getappdata(hFig, 'Timefreq');
switch (TfInfo.DisplayMode)
% Spectrum: Move cursor
case 'Spectrum'
hAxes = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph');
if ~isempty(hAxes)
PlotCursor(hFig, hAxes);
end
% Time series: redraw everything
case 'TimeSeries'
TfInfo.iFreqs = GlobalData.UserFrequencies.iCurrentFreq;
setappdata(hFig, 'Timefreq', TfInfo);
UpdateFigurePlot(hFig, 1);
end
end
%% ===== DISPLAY OPTIONS CHANGED =====
function DisplayOptionsChangedCallback(hFig) %#ok<DEFNU>
% Restore intial view
%ResetView(hFig);
% Update display
UpdateFigurePlot(hFig, 1);
end
%% ===== SELECTED ROW CHANGED =====
function SelectedRowChangedCallback(iDS, iFig)
global GlobalData;
% Get figure appdata
hFig = GlobalData.DataSet(iDS).Figure(iFig).hFigure;
% Get current selection for the figure
curSelRows = figure_timeseries('GetFigSelectedRows', hFig);
% Get new selection that the figure should show (keep only the ones available for this figure)
allFigRows = GlobalData.DataSet(iDS).Figure(iFig).Handles.LinesLabels;
% Remove spaces
allFigRows = cellfun(@(c)strrep(c,' ',''), allFigRows, 'UniformOutput', 0);
% Get new selection that the figure should show (keep only the ones available for this figure)
newSelRows = intersect(GlobalData.DataViewer.SelectedRows, allFigRows);
% Sensors to select
rowsToSel = setdiff(newSelRows, curSelRows);
if ~isempty(rowsToSel)
figure_timeseries('SetFigSelectedRows', hFig, rowsToSel, 1);
end
% Sensors to unselect
rowsToUnsel = setdiff(curSelRows, newSelRows);
if ~isempty(rowsToUnsel)
figure_timeseries('SetFigSelectedRows', hFig, rowsToUnsel, 0);
end
end
%% ===========================================================================
% ===== KEYBOARD AND MOUSE CALLBACKS ========================================
% ===========================================================================
%% ===== FIGURE MOUSE DOWN =====
function FigureMouseDownCallback(hFig, ev)
% Get selected object in this figure
hObj = get(hFig,'CurrentObject');
if isempty(hObj)
return;
end
% Get object tag
objTag = get(hObj, 'Tag');
% Re-select main axes
drawnow;
hAxes = findobj(hFig, '-depth', 1, 'tag', 'AxesGraph');
set(hFig,'CurrentObject', hAxes(1), 'CurrentAxes', hAxes(1));
% Get figure properties
MouseStatus = get(hFig, 'SelectionType');
% Switch between available graphic objects
switch (objTag)
case 'Spectrum'
% Figure: Keep the main axes as clicked object
hAxes = hAxes(1);
case 'AxesGraph'
% Axes: selectec axes = the one that was clicked
hAxes = hObj;
case 'DataLine'
% Time series lines: select
if (~strcmpi(MouseStatus, 'alt') || (get(hObj, 'LineWidth') > 1))
LineClickedCallback(hObj);
return;
end
case 'SelectionPatch'
% Shift+click: zoom into selection (otherwise, regular click)
if strcmpi(MouseStatus, 'extend')
ZoomSelection(hFig);
return;
else
hAxes = get(hObj, 'Parent');
end
case {'Cursor', 'TextCursor'}
hAxes = get(hObj, 'Parent');
case 'legend'
legendButtonDownFcn = get(hObj, 'ButtonDownFcn');
if ~isempty(legendButtonDownFcn)
if iscell(legendButtonDownFcn)
legendButtonDownFcn{1}(hObj, ev, legendButtonDownFcn{2});
else
% After Matlab 2014b....
end
end
return
otherwise
% Any other object: consider as a click on the main axes
end
% ===== PROCESS CLICKS ON MAIN TS AXES =====
% Start an action (Move time cursor, pan)
switch(MouseStatus)
% Left click
case 'normal'
clickAction = 'selection';
% Initialize time selection
[Xcur, iXcur, Xvector] = GetMouseX(hFig, hAxes);
if (length(Xvector) > 1)
setappdata(hFig, 'GraphSelection', [Xcur, Inf]);
else
setappdata(hFig, 'GraphSelection', []);
end
% CTRL+Mouse, or Mouse right
case 'alt'
clickAction = 'gzoom';
set(hFig, 'Pointer', 'top');
% SHIFT+Mouse
case 'extend'
clickAction = 'pan';
set(hFig, 'Pointer', 'fleur');
% DOUBLE CLICK
case 'open'
ResetView(hFig);
return;
% OTHER : nothing to do
otherwise
return
end
% Reset the motion flag
setappdata(hFig, 'hasMoved', 0);
% Record mouse location in the figure coordinates system
setappdata(hFig, 'clickPositionFigure', get(hFig, 'CurrentPoint'));
% Record action to perform when the mouse is moved
setappdata(hFig, 'clickAction', clickAction);
% Record axes ibject that was clicked (usefull when more than one axes object in figure)
setappdata(hFig, 'clickSource', hAxes);
% Register MouseMoved callbacks for current figure
set(hFig, 'WindowButtonMotionFcn', @FigureMouseMoveCallback);
end
%% ===== FIGURE MOUSE MOVE =====
function FigureMouseMoveCallback(hFig, ev)
% Get current mouse action
clickAction = getappdata(hFig, 'clickAction');
hAxes = getappdata(hFig, 'clickSource');
if isempty(clickAction) || isempty(hAxes)
return
end
% Set the motion flag
setappdata(hFig, 'hasMoved', 1);
% Get current mouse location
curptFigure = get(hFig, 'CurrentPoint');
motionFigure = (curptFigure - getappdata(hFig, 'clickPositionFigure')) / 100;
% Update click point location
setappdata(hFig, 'clickPositionFigure', curptFigure);
% Switch between different actions (Pan, Rotate, Contrast)
switch(clickAction)
case 'pan' % maybe could use figure_timeseries.FigurePan
% Get initial XLim and YLim
XLimInit = getappdata(hAxes, 'XLimInit');
YLimInit = getappdata(hAxes, 'YLimInit');
% Move view along X axis
XLim = get(hAxes, 'XLim');
isXLog = strcmpi(get(hAxes, 'XScale'), 'log');
if isXLog
XLim = log10(XLim);
XLimInit = log10(XLimInit);
end
XLim = XLim - (XLim(2) - XLim(1)) * motionFigure(1);
XLim = bst_saturate(XLim, XLimInit, 1);
if isXLog
XLim = 10.^XLim;
end
set(hAxes, 'XLim', XLim);
% Move view along Y axis
YLim = get(hAxes, 'YLim');
isYLog = strcmpi(get(hAxes, 'YScale'), 'log');
if isYLog
YLim = log10(YLim);
YLimInit = log10(YLimInit);
end
YLim = YLim - (YLim(2) - YLim(1)) * motionFigure(2);
YLim = bst_saturate(YLim, YLimInit, 1);
if isYLog
YLim = 10.^YLim;
end
set(hAxes, 'YLim', YLim);
% Set the cursor height
hCursor = findobj(hAxes, '-depth', 1, 'Tag', 'Cursor');
set(hCursor, 'YData', YLim);
% Set the selection rectangle dimensions
hSelectionPatch = findobj(hAxes, '-depth', 1, 'Tag', 'SelectionPatch');
if ~isempty(hSelectionPatch)
set(hSelectionPatch, 'YData', [YLim(1), YLim(1), YLim(2), YLim(2)]);
end
case 'selection'
% Get time selection
GraphSelection = getappdata(hFig, 'GraphSelection');
% Time selection
if ~isempty(GraphSelection)
% Update time selection
Xcur = GetMouseX(hFig, hAxes);
GraphSelection(2) = Xcur;
setappdata(hFig, 'GraphSelection', GraphSelection);
% Redraw time selection
DrawSelection(hFig);
end
case 'gzoom'
% Gain zoom
ScrollCount = -motionFigure(2) * 10;
figure_timeseries('FigureScroll', hFig, ScrollCount, 'gzoom');
% Apply same limits as when panning
YLimInit = getappdata(hAxes, 'YLimInit');
YLim = get(hAxes, 'YLim');
isYLog = strcmpi(get(hAxes, 'YScale'), 'log');
if isYLog
YLim = log10(YLim);
YLimInit = log10(YLimInit);
end
YLim = bst_saturate(YLim, YLimInit, 1);
if isYLog
YLim = 10.^YLim;
end
set(hAxes, 'YLim', YLim);
end
end
%% ===== FIGURE MOUSE UP =====
function FigureMouseUpCallback(hFig, event)
% Get mouse state
hasMoved = getappdata(hFig, 'hasMoved');
MouseStatus = get(hFig, 'SelectionType');
% Reset figure mouse fields
setappdata(hFig, 'clickAction', '');
setappdata(hFig, 'hasMoved', 0);
% Restore mouse pointer
set(hFig, 'Pointer', 'arrow');
drawnow;
% Get axes handles
hAxes = getappdata(hFig, 'clickSource');
if isempty(hAxes) || ~ishandle(hAxes)
return
end
% If mouse has not moved: popup or time change
Xmode = 'unknown';
if ~hasMoved && ~isempty(MouseStatus)
if strcmpi(MouseStatus, 'normal')
% Get current frequency
[Xcur, iXcur, Xvector, Xmode] = GetMouseX(hFig, hAxes);
% Update plot
if ~isempty(Xcur)
% Move time cursor to new time
hCursor = findobj(hAxes, '-depth', 1, 'Tag', 'Cursor');
set(hCursor, 'XData', Xcur.*[1 1]);
drawnow;
% Update the current time in the whole application
switch(Xmode)
case 'Spectrum'
panel_freq('SetCurrentFreq', iXcur);
case 'TimeSeries'
panel_time('SetCurrentTime', Xcur);
end
% Remove previous time selection patch
setappdata(hFig, 'GraphSelection', []);
DrawSelection(hFig);
end
else
% Popup
DisplayFigurePopup(hFig);
end
end
% Reset MouseMove callbacks for current figure
set(hFig, 'WindowButtonMotionFcn', []);
% Remove mouse callbacks appdata
setappdata(hFig, 'clickSource', []);
setappdata(hFig, 'clickAction', []);
% Update figure selection
bst_figures('SetCurrentFigure', hFig, 'TF');
bst_figures('SetCurrentFigure', hFig, '2D');
end
%% ===== GET MOUSE X =====
function [Xcur, iXcur, Xvector, Xmode] = GetMouseX(hFig, hAxes)
% Get current point in axes
Xcur = get(hAxes, 'CurrentPoint');
XLim = get(hAxes, 'XLim');
TfInfo = getappdata(hFig, 'Timefreq');
% Check whether cursor is out of display time bounds
Xcur= bst_saturate(Xcur(1,1), XLim);
% Get the X vector
[Time, Freqs] = figure_timefreq('GetFigureData', hFig);
switch (TfInfo.DisplayMode)
case 'Spectrum', Xvector = Freqs;
case 'TimeSeries', Xvector = Time;
end
Xmode = TfInfo.DisplayMode;
% Bands (time or freq)
if iscell(Xvector)
CenterBand = mean(process_tf_bands('GetBounds', Freqs), 2);
iXcur = bst_closest(Xcur, CenterBand);
Xcur = CenterBand(iXcur);
else
iXcur = bst_closest(Xcur, Xvector);
Xcur = Xvector(iXcur);
end
end
%% ===== DRAW SELECTION =====
function DrawSelection(hFig)
% Get axes (can have more than one)
hAxesList = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph');
% Get time selection
GraphSelection = getappdata(hFig, 'GraphSelection');
% Get display mode
TfInfo = getappdata(hFig, 'Timefreq');
% Process all the axes
for i = 1:length(hAxesList)
hAxes = hAxesList(i);
% Draw new time selection patch
if ~isempty(GraphSelection) && ~isinf(GraphSelection(2))
% Get axes limits
YLim = get(hAxes, 'YLim');
% Get previous patch
hSelPatch = findobj(hAxes, '-depth', 1, 'Tag', 'SelectionPatch');
% Position of the square patch
XData = [GraphSelection(1), GraphSelection(2), GraphSelection(2), GraphSelection(1)];
YData = [YLim(1), YLim(1), YLim(2), YLim(2)];
ZData = [0.01 0.01 0.01 0.01];
% If patch do not exist yet: create it
if isempty(hSelPatch)
% EraseMode: Only for Matlab <= 2014a
if (bst_get('MatlabVersion') <= 803)
optErase = {'EraseMode', 'xor'}; % INCOMPATIBLE WITH OPENGL RENDERER (BUG), REMOVED IN MATLAB 2014b
patchColor = [.3 .3 1];
else
optErase = {};
patchColor = [.7 .7 1];
end
% Draw patch
hSelPatch = patch('XData', XData, ...
'YData', YData, ...
'ZData', ZData, ...
'LineWidth', 1, ...
optErase{:}, ...
'FaceColor', patchColor, ...
'FaceAlpha', 1, ...
'EdgeColor', patchColor, ...
'EdgeAlpha', 1, ...
'Tag', 'SelectionPatch', ...
'Parent', hAxes);
% Else, patch already exist: update it
else
% Change patch limits
set(hSelPatch, ...
'XData', XData, ...
'YData', YData, ...
'ZData', ZData, ...
'Visible', 'on');
end
% === UPDATE X-LABEL ===
switch (TfInfo.DisplayMode)
case 'Spectrum'
strSelection = sprintf('Selection: [%1.2f Hz - %1.2f Hz]', min(GraphSelection), max(GraphSelection));
case 'TimeSeries'
% Get current time units
timeUnit = panel_time('GetTimeUnit');
% Update label according to the time units
switch (timeUnit)
case 'ms'
strSelection = sprintf('Selection: [%1.2f ms - %1.2f ms]', min(GraphSelection)*1000, max(GraphSelection)*1000);
case 's'
strSelection = sprintf('Selection: [%1.4f s - %1.4f s]', min(GraphSelection), max(GraphSelection));
end
strLength = sprintf(' Duration: [%d ms]', round(abs(GraphSelection(2) - GraphSelection(1)) * 1000));
strSelection = [strSelection, strLength];
end
% Get selection label
hTextTimeSel = findobj(hFig, '-depth', 1, 'Tag', 'TextTimeSel');
if ~isempty(hTextTimeSel)
% Update label
set(hTextTimeSel, 'Visible', 'on', 'String', strSelection);
end
else
% Remove previous selection patch
set(findobj(hAxes, '-depth', 1, 'Tag', 'SelectionPatch'), 'Visible', 'off');
set(findobj(hFig, '-depth', 1, 'Tag', 'TextTimeSel'), 'Visible', 'off');
end
end
end
%% ===== SET FREQ SELECTION =====
% Define manually the freq selection for a given Spectrum figure
%
% USAGE: SetFreqSelection(hFig, Xsel)
% SetFreqSelection(hFig)
function SetFreqSelection(hFig, Xsel)
% Get figure display mode
TfInfo = getappdata(hFig, 'Timefreq');
% Get the X vector for this figure
[Time, Freqs] = figure_timefreq('GetFigureData', hFig);
switch (TfInfo.DisplayMode)
case 'Spectrum'
Xvector = Freqs;
strUnits = 'Hz';
case 'TimeSeries'
Xvector = Time;
strUnits = 's';
end
if (length(Xvector) <= 1) || iscell(Xvector)
return;
end
% Ask for a frequency range
if (nargin < 2) || isempty(Xsel)
Xsel = panel_freq('InputSelectionWindow', Xvector([1,end]), 'Set frequency selection', strUnits);
if isempty(Xsel)
return
end
end
% Select the closest point in time vector
Xsel = Xvector(bst_closest(Xsel, Xvector));
% Draw new time selection
setappdata(hFig, 'GraphSelection', Xsel);
DrawSelection(hFig);
end
%% ===== ZOOM INTO SELECTION =====
function ZoomSelection(hFig)
% Get time selection
GraphSelection = getappdata(hFig, 'GraphSelection');
if isempty(GraphSelection) || isinf(GraphSelection(2))
return;
end
% Set axes bounds to selection
hAxesList = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph');
set(hAxesList, 'XLim', [min(GraphSelection), max(GraphSelection)]);
% Draw new time selection
setappdata(hFig, 'GraphSelection', []);
DrawSelection(hFig);
end
%% ===== FIGURE MOUSE WHEEL =====
function FigureMouseWheelCallback(hFig, event)
if isempty(event)
return;
end
% SHIFT + Scroll
if ismember('shift', get(hFig,'CurrentModifier'))
figure_timeseries('FigureScroll', hFig, event.VerticalScrollCount, 'gzoom');
% CTRL + Scroll
elseif ismember('control', get(hFig,'CurrentModifier'))
figure_timeseries('FigureScroll', hFig, event.VerticalScrollCount, 'vertical');
% Regular scroll
else
figure_timeseries('FigureScroll', hFig, event.VerticalScrollCount, 'horizontal');
end
end
%% ===== KEYBOARD CALLBACK =====
function FigureKeyPressedCallback(hFig, ev)
global GlobalData;
% Convert event to Matlab (in case it's coming from a java callback)
[keyEvent, isControl, isShift] = gui_brainstorm('ConvertKeyEvent', ev);
if isempty(keyEvent.Key)
return
end
% Prevent multiple executions
hAxes = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph')';
set([hFig hAxes], 'BusyAction', 'cancel');
% Get time freq information
TfInfo = getappdata(hFig, 'Timefreq');
TfFile = TfInfo.FileName;
if isempty(TfFile)
return;
end
% Process event
switch (keyEvent.Key)
% Arrows
case {'leftarrow', 'rightarrow', 'pageup', 'pagedown', 'home', 'end'}
switch (TfInfo.DisplayMode)
case 'Spectrum', panel_freq('FreqKeyCallback', keyEvent);
case 'TimeSeries', panel_time('TimeKeyCallback', keyEvent);
end
case {'uparrow', 'downarrow'}
% UP/DOWN: Change data row
if ~isempty(TfInfo.RowName) && (ischar(TfInfo.RowName) || (length(TfInfo.RowName) == 1))
panel_display('SetSelectedRowName', hFig, keyEvent.Key);
else
switch (TfInfo.DisplayMode)
case 'Spectrum', panel_freq('FreqKeyCallback', keyEvent);
case 'TimeSeries', panel_time('TimeKeyCallback', keyEvent);
end
end
% CTRL+D : Dock figure
case 'd'
if isControl
isDocked = strcmpi(get(hFig, 'WindowStyle'), 'docked');
bst_figures('DockFigure', hFig, ~isDocked);
end
% CTRL+I : Save as image
case 'i'
if isControl
out_figure_image(hFig);
end
% CTRL+J : Open as image
case 'j'
if isControl
out_figure_image(hFig, 'Viewer');
end
% CTRL+F : Open as figure
case 'f'
if isControl
out_figure_image(hFig, 'Figure');
end
% CTRL+R : Recordings time series
case 'r'
if isControl
% Get figure description
[hFig, iFig, iDS] = bst_figures('GetFigure', hFig);
% If there is an associated an available DataFile
if ~isempty(GlobalData.DataSet(iDS).DataFile)
view_timeseries(GlobalData.DataSet(iDS).DataFile, GlobalData.DataSet(iDS).Figure(iFig).Id.Modality);
end
end
% CTRL+T : Default topography
case 't'
if isControl
view_topography(TfFile, [], '2DSensorCap', [], 0);
end
% Y : Scale to fit Y axis
case 'y'
TsInfo = getappdata(hFig, 'TsInfo');
if strcmpi(TsInfo.DisplayMode, 'butterfly')
figure_timeseries('ScaleToFitY', hFig, ev);
end
% RETURN: VIEW SELECTED CHANNELS
case 'return'
DisplaySelectedRows(hFig);
% DELETE: SET CHANNELS AS BAD
case {'delete', 'backspace'}
% Get figure description
[hFig, iFig, iDS] = bst_figures('GetFigure', hFig);
% Get selected rows
SelChan = figure_timeseries('GetFigSelectedRows', hFig);
% Only for PSD attached directly to a data file
if ~isempty(SelChan) && ~isempty(GlobalData.DataSet(iDS).DataFile) && ...
(length(GlobalData.DataSet(iDS).Figure(iFig).SelectedChannels) ~= length(SelChan)) && ...
~isempty(strfind(TfFile, '_psd')) && ...
strcmpi(file_gettype(GlobalData.DataSet(iDS).DataFile), 'data')
AddParentBadChannels(hFig, SelChan);
end
% ESCAPE: CLEAR SELECTION
case 'escape'
bst_figures('SetSelectedRows', []);
% OTHER
otherwise
% Not found: test based on the character that was generated
if isfield(keyEvent, 'Character') && ~isempty(keyEvent.Character)
switch(keyEvent.Character)
% PLUS/MINUS: GAIN CONTROL
case '+'
figure_timeseries('UpdateTimeSeriesFactor', hFig, 1.1);
case '-'
figure_timeseries('UpdateTimeSeriesFactor', hFig, .9091);
end
end
end
% Restore events
if ~isempty(hFig) && ishandle(hFig)
hAxes = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph')';
set([hFig hAxes], 'BusyAction', 'queue');
end
end
%% ===== ADD BAD CHANNELS =====
function AddParentBadChannels(hFig, BadChan)
global GlobalData;
% Get figure description
[hFig, iFig, iDS] = bst_figures('GetFigure', hFig);
if isempty(hFig)
return;
end
% Get indices in the channel file
iBad = [];
for i = 1:length(BadChan)
iBad = [iBad, find(strcmpi(BadChan{i}, {GlobalData.DataSet(iDS).Channel.Name}))];
end
% Get selected rows
if ~isempty(iBad) && strcmpi(file_gettype(GlobalData.DataSet(iDS).DataFile), 'data') && ~isempty(GlobalData.DataSet(iDS).DataFile)
% Add new bad channels
newChannelFlag = GlobalData.DataSet(iDS).Measures.ChannelFlag;
newChannelFlag(iBad) = -1;
% Update channel flag
panel_channel_editor('UpdateChannelFlag', GlobalData.DataSet(iDS).DataFile, newChannelFlag);
% Reset selection (done in UpdateChannelFlag)
%bst_figures('SetSelectedRows', []);
end
end
%% ===== GET DEFAULT FACTOR =====
function defaultFactor = GetDefaultFactor(Modality)
global GlobalData
if isempty(GlobalData.DataViewer.DefaultFactor)
defaultFactor = 1;
else
iMod = find(cellfun(@(c)isequal(c,Modality), GlobalData.DataViewer.DefaultFactor(:,1)));
if isempty(iMod)
defaultFactor = 1;
else
defaultFactor = GlobalData.DataViewer.DefaultFactor{iMod,2};
end
end
end
%% ===== LINE CLICKED =====
function LineClickedCallback(hLine, ev)
global GlobalData;
% Get figure handle
hFig = get(hLine, 'Parent');
while ~strcmpi(get(hFig, 'Type'), 'figure') || isempty(hFig)
hFig = get(hFig, 'Parent');
end
if isempty(hFig)
return;
end
hAxes = get(hLine, 'Parent');
setappdata(hFig, 'clickSource', []);
% Get figure description
[hFig, iFig, iDS] = bst_figures('GetFigure', hFig);
sFig = GlobalData.DataSet(iDS).Figure(iFig);
% Get row indice
iRow = find(sFig.Handles.hLines == hLine);
if isempty(iRow)
return
end
RowName = sFig.Handles.LinesLabels{iRow};
% Get click type
isRightClick = strcmpi(get(hFig, 'SelectionType'), 'alt');
% Right click : display popup menu
if isRightClick
setappdata(hFig, 'clickSource', hAxes);
DisplayFigurePopup(hFig, RowName);
setappdata(hFig, 'clickSource', []);
% Left click: Select/unselect line
else
bst_figures('ToggleSelectedRow', RowName);
end
% Update figure selection
bst_figures('SetCurrentFigure', hFig, '2D');
bst_figures('SetCurrentFigure', hFig, 'TF');
end
%% ===== DISPLAY SELECTED CHANNELS =====
% USAGE: DisplaySelectedRows(hFig)
function DisplaySelectedRows(hFig)
% Get selected rows
RowNames = figure_timeseries('GetFigSelectedRows', hFig);
if isempty(RowNames)
return;
end
% Reset selection
bst_figures('SetSelectedRows', []);
% Get figure info
TfInfo = getappdata(hFig, 'Timefreq');
% Plot figure
view_spectrum(TfInfo.FileName, TfInfo.DisplayMode, RowNames, 1);
end
%% ===== RESET VIEW =====
function ResetView(hFig)
% Get list of axes in this figure
hAxes = findobj(hFig, '-depth', 1, 'Tag', 'AxesGraph');
% Restore initial X and Y zooms
XLim = getappdata(hAxes, 'XLimInit');
YLim = getappdata(hAxes, 'YLimInit');
set(hAxes, 'XLim', XLim);
set(hAxes, 'YLim', YLim);
% Set the time cursor height to the maximum of the display
hCursor = findobj(hAxes, '-depth', 1, 'Tag', 'Cursor');
set(hCursor, 'YData', YLim);
% Set the selection rectangle dimensions
hSelectionPatch = findobj(hAxes, '-depth', 1, 'Tag', 'SelectionPatch');
if ~isempty(hSelectionPatch)
set(hSelectionPatch, 'YData', [YLim(1), YLim(1), YLim(2), YLim(2)]);
end
end
%% ===== HIDE/SHOW LEGENDS =====
function newPropVal = ToggleAxesProperty(hAxes, propName)
switch get(hAxes(1), propName)
case 'on'
set(hAxes, propName, 'off');
newPropVal = 0;
case 'off'
set(hAxes, propName, 'on');
newPropVal = 1;
end
end
function SetShowLegend(iDS, iFig, ShowLegend)
global GlobalData;
% Update TsInfo field
hFig = GlobalData.DataSet(iDS).Figure(iFig).hFigure;
TsInfo = getappdata(hFig, 'TsInfo');
TsInfo.ShowLegend = ShowLegend;
setappdata(hFig, 'TsInfo', TsInfo);
% Redraw figure
UpdateFigurePlot(hFig, 1);
end
function ToggleGrid(hAxes, hFig, xy)
isSel = ToggleAxesProperty(hAxes, [xy 'Grid']);
ToggleAxesProperty(hAxes, [xy 'MinorGrid']);
TsInfo = getappdata(hFig, 'TsInfo');
TsInfo = setfield(TsInfo, ['Show' xy 'Grid'], isSel);
setappdata(hFig, 'TsInfo', TsInfo);
RefreshGridBtnDisplay(hFig, TsInfo);
end
function ToggleLogScaleX(hAxes, hFig, loglin)
set(hAxes, 'XScale', loglin);
TsInfo = getappdata(hFig, 'TsInfo');
TsInfo.XScale = loglin;
setappdata(hFig, 'TsInfo', TsInfo);
RefreshLogScaleBtnDisplay(hFig, TsInfo);
bst_set('XScale', loglin);
end
function RefreshLogScaleBtnDisplay(hFig, TsInfo)
% Toggle selection of associated button if possible
buttonContainer = findobj(hFig, '-depth', 1, 'Tag', 'ButtonSetScaleLog');
if ~isempty(buttonContainer)
button = get(buttonContainer, 'UserData');
button.setSelected(strcmp(TsInfo.XScale, 'log'));
end
end
function RefreshGridBtnDisplay(hFig, TsInfo)
% Toggle selection of associated button if possible
buttonContainer = findobj(hFig, '-depth', 1, 'Tag', 'ButtonShowGrids');
if ~isempty(buttonContainer)
button = get(buttonContainer, 'UserData');
button.setSelected((TsInfo.ShowXGrid & TsInfo.ShowYGrid) || ...
(strcmpi(TsInfo.DisplayMode, 'column') & TsInfo.ShowXGrid));
end
end
%% ===== POPUP MENU =====
function DisplayFigurePopup(hFig, menuTitle)
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import org.brainstorm.icon.*;
global GlobalData;
% If menuTitle not specified
if (nargin < 2)
menuTitle = '';
end
% Get figure description
[hFig, iFig, iDS] = bst_figures('GetFigure', hFig);
% Get axes handles
hAxes = getappdata(hFig, 'clickSource');
if isempty(hAxes)
return
end
% Get time freq information
TfInfo = getappdata(hFig, 'Timefreq');
TfFile = TfInfo.FileName;
if isempty(TfFile)
return;
end
TsInfo = getappdata(hFig, 'TsInfo');
% Get loaded information
iTimefreq = bst_memory('GetTimefreqInDataSet', iDS, TfFile);
% Create popup menu
jPopup = java_create('javax.swing.JPopupMenu');
% Menu title
if ~isempty(menuTitle)
jTitle = gui_component('Label', jPopup, [], ['<HTML><B>' menuTitle '</B>']);
jTitle.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,35,0,0));
jPopup.addSeparator();
end
% ==== DISPLAY OTHER FIGURES ====
% Only for MEG and EEG time series
if strcmpi(GlobalData.DataSet(iDS).Timefreq(iTimefreq).DataType, 'data')
% === View RECORDINGS ===
if ~isempty(GlobalData.DataSet(iDS).DataFile)
jItem = gui_component('MenuItem', jPopup, [], 'Recordings', IconLoader.ICON_TS_DISPLAY, [], @(h,ev)view_timeseries(GlobalData.DataSet(iDS).DataFile, GlobalData.DataSet(iDS).Figure(iFig).Id.Modality));
jItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_MASK));
end
% === View TOPOGRAPHY ===
if ~isempty(GlobalData.DataSet(iDS).Figure(iFig).Id.Modality) && ismember(GlobalData.DataSet(iDS).Figure(iFig).Id.Modality, {'MEG MAG','MEG GRAD','MEG','EEG'})
jItem = gui_component('MenuItem', jPopup, [], '2D Sensor cap', IconLoader.ICON_TOPOGRAPHY, [], @(h,ev)bst_call(@view_topography, TfFile, [], '2DSensorCap', [], 0));
jItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.CTRL_MASK));
jPopup.addSeparator();
end
end
% === VIEW SELECTED ===
jItem = gui_component('MenuItem', jPopup, [], 'View selected', IconLoader.ICON_SPECTRUM, [], @(h,ev)DisplaySelectedRows(hFig));
jItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); % ENTER
% === SET SELECTED AS BAD CHANNELS ===
% Get selected rows
SelChan = figure_timeseries('GetFigSelectedRows', hFig);
% Only for PSD attached directly to a data file
if ~isempty(SelChan) && ~isempty(GlobalData.DataSet(iDS).DataFile) && ...
(length(GlobalData.DataSet(iDS).Figure(iFig).SelectedChannels) ~= length(SelChan)) && ...
~isempty(strfind(TfFile, '_psd')) && ...
strcmpi(file_gettype(GlobalData.DataSet(iDS).DataFile), 'data')
jItem = gui_component('MenuItem', jPopup, [], 'Mark selected as bad', IconLoader.ICON_BAD, [], @(h,ev)AddParentBadChannels(hFig, SelChan));
jItem.setAccelerator(KeyStroke.getKeyStroke(int32(KeyEvent.VK_DELETE), 0)); % DEL
end
% === RESET SELECTION ===
jItem = gui_component('MenuItem', jPopup, [], 'Reset selection', IconLoader.ICON_SURFACE, [], @(h,ev)bst_figures('SetSelectedRows',[]));
jItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)); % ESCAPE
jPopup.addSeparator();
% ==== MENU: SELECTION ====
% No time/freq bands
if ~iscell(GlobalData.DataSet(iDS).Timefreq(iTimefreq).Freqs) && isempty(GlobalData.DataSet(iDS).Timefreq(iTimefreq).TimeBands)
% Menu "Selection"
switch(TfInfo.DisplayMode)
case 'Spectrum', strMenu = 'Frequency selection';
case 'TimeSeries', strMenu = 'Time selection';
end
jMenuSelection = gui_component('Menu', jPopup, [], strMenu, IconLoader.ICON_TS_SELECTION);
% Set selection
gui_component('MenuItem', jMenuSelection, [], 'Set selection manually...', IconLoader.ICON_TS_SELECTION, [], @(h,ev)SetFreqSelection(hFig));
% Get current time selection
GraphSelection = getappdata(hFig, 'GraphSelection');
isSelection = ~isempty(GraphSelection) && ~any(isinf(GraphSelection(:)));
if isSelection
gui_component('MenuItem', jMenuSelection, [], 'Zoom into selection (Shift+click)', IconLoader.ICON_ZOOM_PLUS, [], @(h,ev)ZoomSelection(hFig));
jMenuSelection.addSeparator();
% === EXPORT TO DATABASE ===
if ~strcmpi(TfInfo.DisplayMode, 'TimeSeries')
gui_component('MenuItem', jMenuSelection, [], 'Export to database', IconLoader.ICON_SPECTRUM, [], @(h,ev)bst_call(@out_figure_timefreq, hFig, 'Database', 'Selection'));
end
% === EXPORT TO FILE ===
gui_component('MenuItem', jMenuSelection, [], 'Export to file', IconLoader.ICON_TS_EXPORT, [], @(h,ev)bst_call(@out_figure_timefreq, hFig, [], 'Selection'));
% === EXPORT TO MATLAB ===
gui_component('MenuItem', jMenuSelection, [], 'Export to Matlab', IconLoader.ICON_MATLAB_EXPORT, [], @(h,ev)bst_call(@out_figure_timefreq, hFig, 'Variable', 'Selection'));
end
jPopup.addSeparator();
end
% ==== MENU: SNAPSHOT ====
jMenuSave = gui_component('Menu', jPopup, [], 'Snapshots', IconLoader.ICON_SNAPSHOT);
% === SAVE AS IMAGE ===
jItem = gui_component('MenuItem', jMenuSave, [], 'Save as image', IconLoader.ICON_SAVE, [], @(h,ev)bst_call(@out_figure_image, hFig));
jItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK));
% === OPEN AS IMAGE ===
jItem = gui_component('MenuItem', jMenuSave, [], 'Open as image', IconLoader.ICON_IMAGE, [], @(h,ev)bst_call(@out_figure_image, hFig, 'Viewer'));
jItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J, KeyEvent.CTRL_MASK));
jItem = gui_component('MenuItem', jMenuSave, [], 'Open as figure', IconLoader.ICON_IMAGE, [], @(h,ev)bst_call(@out_figure_image, hFig, 'Figure'));