-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathswa_SleepScoring.m
1312 lines (1069 loc) · 43.2 KB
/
swa_SleepScoring.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
% -- Manual Sleep Scoring GUI -- %
function swa_SleepScoring(varargin)
% GUI to score sleep stages and save the results for further
% processing (e.g. travelling waves analysis)
% Author: Armand Mensen
DefineInterface
function DefineInterface
% define the colorscheme to use
handles.colorscheme = struct(...
'fg_col_1', [0.9, 0.9, 0.9] , ...
'fg_col_2', [0.8, 0.8, 0.8] , ...
'fg_col_3', [0.5, 0.5, 0.5] , ...
'bg_col_1', [0.1, 0.1, 0.1] , ...
'bg_col_2', [0.2, 0.2, 0.2] , ...
'bg_col_3', [0.15, 0.15, 0.15] );
handles.fig = figure(...
'Name', 'Sleep Scoring',...
'NumberTitle', 'off',...
'Color', handles.colorscheme.bg_col_1,...
'MenuBar', 'none',...
'Units', 'normalized',...
'Outerposition',[0 0.04 .5 0.96]);
set(handles.fig, 'CloseRequestFcn', {@fcn_close_request});
%% Menus
handles.menu.File = uimenu(handles.fig, 'Label', 'File');
handles.menu.LoadEEG = uimenu(handles.menu.File,...
'Label', 'Load EEG',...
'Accelerator', 'L');
handles.menu.SaveEEG = uimenu(handles.menu.File,...
'Label', 'Save EEG',...
'Accelerator', 'S');
handles.menu.Options = uimenu(handles.fig, 'Label', 'Options');
handles.menu.EpochLength = uimenu(handles.menu.Options,...
'Label', 'Epoch Length');
handles.menu.StartTime = uimenu(handles.menu.Options,...
'Label', 'Start Time');
handles.menu.ColorScheme = uimenu(handles.menu.Options,...
'Label', 'Color Scheme',...
'Enable', 'off');
handles.menu.Montage = uimenu(handles.fig, 'Label', 'Montage', 'Enable', 'off');
handles.menu.Export = uimenu(handles.fig, 'Label', 'Export', 'Enable', 'off');
handles.menu.N2 = uimenu(handles.menu.Export,'Label', 'N2');
handles.menu.N3 = uimenu(handles.menu.Export,'Label', 'N3');
handles.menu.REM = uimenu(handles.menu.Export,'Label', 'REM');
handles.menu.Statistics = uimenu(handles.fig, 'Label', 'Statistics', 'Enable', 'off');
% can use html labels here to alter fonts
% menu callbacks
set(handles.menu.LoadEEG,...
'Callback', {@menu_LoadEEG});
set(handles.menu.SaveEEG,...
'Callback', {@menu_SaveEEG});
% option callbacks
set(handles.menu.EpochLength,...
'Callback', {@fcn_options, 'EpochLength'});
set(handles.menu.StartTime,...
'Callback', {@fcn_options, 'StartTime'});
set(handles.menu.Statistics,...
'Callback', {@fcn_statistics});
% montage
set(handles.menu.Montage,...
'Callback', {@updateMontage, handles.fig});
% set export callbacks
set(handles.menu.N2,...
'Callback', {@menu_Export, 2});
set(handles.menu.N3,...
'Callback', {@menu_Export, 3});
set(handles.menu.REM,...
'Callback', {@menu_Export, 5});
%% Status Bar
handles.StatusBar = uicontrol(...
'Parent', handles.fig,...
'Style', 'text',...
'backgroundColor', handles.colorscheme.bg_col_3 ,...
'foregroundColor', handles.colorscheme.fg_col_1 ,...
'String', 'Status Updates',...
'Units', 'normalized',...
'Position', [0 0 1 0.03],...
'FontName', 'Century Gothic',...
'FontSize', 10);
% get the java handle of the status bar
jStatusBar = findjobj(handles.StatusBar);
% set the status bar alignments
jStatusBar.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
jStatusBar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
% Hidden epoch tracker
% ````````````````````
handles.current_epoch = uicontrol(...
'Parent', handles.fig,...
'Style', 'text',...
'Visible', 'off',...
'Value', 1);
% Create axes
% ``````````````````````````
handles.channel_axes = axes(...
'parent', handles.fig ,...
'position', [0.05 0.275, 0.85, 0.675],...
'nextPlot', 'add' ,...
'color', handles.colorscheme.bg_col_2 ,...
'xtick', [] ,...
'ytick', [] );
% click axes to mark events
set(handles.channel_axes, 'buttondownfcn', {@fcn_select_events, handles.channel_axes, 'buttondown'})
% invisible name axis
handles.label_axes = axes(...
'parent', handles.fig ,...
'position', [0 0.275, 0.1, 0.675] ,...
'visible', 'off');
% Scoring Buttons
% ```````````````
handles.button_group = uibuttongroup(...
'Title', 'Stage',...
'FontName', 'Century Gothic',...
'FontSize', 11,...
'FontWeight', 'bold',...
'BackgroundColor', handles.colorscheme.bg_col_2,...
'borderType', 'none' ,...
'Position', [0.92 0.375 0.05 0.5]);
% Create radio buttons in the button group.
handles.rb(1) = uicontrol('Style','radiobutton',...
'String','wake','UserData', 0,...
'Units', 'normalized', 'Position',[0.1 11/13 0.9 0.1],...
'Parent',handles.button_group,'HandleVisibility','off');
handles.rb(2) = uicontrol('Style','radiobutton',...
'String','nrem1','UserData', 1,...
'Units', 'normalized', 'Position',[0.1 9/13 0.9 0.1],...
'Parent',handles.button_group,'HandleVisibility','off');
handles.rb(3) = uicontrol('Style','radiobutton',...
'String','nrem2','UserData', 2,...
'Units', 'normalized', 'Position',[0.1 7/13 0.9 0.1],...
'Parent',handles.button_group,'HandleVisibility','off');
handles.rb(4) = uicontrol('Style','radiobutton',...
'String','nrem3','UserData', 3,...
'Units', 'normalized', 'Position',[0.1 5/13 0.9 0.1],...
'Parent',handles.button_group,'HandleVisibility','off');
handles.rb(5) = uicontrol('Style','radiobutton',...
'String','rem', 'UserData', 5,...
'Units', 'normalized', 'Position',[0.1 3/13 0.9 0.1],...
'Parent',handles.button_group,'HandleVisibility','off');
handles.rb(6) = uicontrol('Style','radiobutton',...
'String','artifact','UserData', 6,...
'Units', 'normalized', 'Position',[0.1 1/13 0.9 0.1],...
'Parent',handles.button_group,'HandleVisibility','off');
set(handles.button_group,'SelectedObject',[]); % No selection
set(handles.rb,...
'backgroundColor', handles.colorscheme.bg_col_2,...
'foregroundColor', handles.colorscheme.fg_col_1,...
'FontName', 'Century Gothic',...
'FontSize', 10);
% create stage bar
handles.StageBar = uicontrol(...
'Parent', handles.fig,...
'Style', 'text',...
'String', '1: Unscored',...
'backgroundColor', handles.colorscheme.bg_col_3 ,...
'foregroundColor', handles.colorscheme.fg_col_1 ,...
'Units', 'normalized',...
'Position', [0.05 0.935 0.85 0.02],...
'FontName', 'Century Gothic',...
'FontWeight', 'bold',...
'FontSize', 10);
% create hyponogram
handles.axes_hypnogram = axes(...
'Parent', handles.fig,...
'Position', [0.05 0.050 0.85 0.20],...
'color', handles.colorscheme.bg_col_2 ,...
'xColor', handles.colorscheme.fg_col_1 ,...
'yColor', handles.colorscheme.fg_col_1 ,...
'YLim', [0 6.5],...
'YDir', 'reverse',...
'YTickLabel', {'wake', 'nrem1', 'nrem2', 'nrem3', '', 'rem', 'artifact'},...
'NextPlot', 'add',...
'FontName', 'Century Gothic',...
'FontSize', 8);
% hidden scale for amplitude
handles.et_Scale = uicontrol(...
'Parent', handles.fig,...
'Style', 'edit',...
'backgroundColor', handles.colorscheme.bg_col_2,...
'foregroundColor', handles.colorscheme.fg_col_1 ,...
'String', '200',...
'value', 200,...
'Units', 'normalized',...
'Position', [0.825, 0.275, 0.075, 0.035],...
'FontName', 'Century Gothic',...
'FontSize', 10,...
'visible', 'off');
% horizontal grid line
handles.et_Threshold = uicontrol(...
'Parent', handles.fig,...
'Style', 'edit',...
'backgroundColor', handles.colorscheme.bg_col_2,...
'foregroundColor', handles.colorscheme.fg_col_1 ,...
'String', '75',...
'value', 75,...
'Units', 'normalized',...
'Position', [0.825, 0.275, 0.075, 0.035],...
'FontName', 'Century Gothic',...
'FontSize', 10);
% Set Callbacks
% `````````````
% set hypnogram click
set(handles.axes_hypnogram,...
'ButtonDownFcn', {@bd_hypnoEpochSelect});
set(handles.et_Scale,...
'Callback', {@updateScale});
set(handles.et_Threshold,...
'Callback', {@updateThreshold});
set(handles.button_group,...
'SelectionChangeFcn', {@updateStage});
set(handles.fig,...
'KeyPressFcn', {@cb_KeyPressed,});
% Make Figure Visible and Maximise
% ````````````````````````````````
set(handles.fig, 'Visible', 'on');
drawnow; pause(0.001)
jFrame = get(handle(handles.fig),'JavaFrame');
jFrame.setMaximized(true); % to maximize the figure
guidata(handles.fig, handles)
% Menu Functions
% ``````````````
function menu_LoadEEG(object, ~)
handles = guidata(object);
% load dialog box with file type
[dataFile, dataPath] = uigetfile('*.set', 'Please Select Sleep Data');
% just return if no datafile was actually selected
if isequal(dataFile, 0)
set(handles.StatusBar, 'String', 'Information: No file selected'); drawnow;
return;
end
% Load the Files
% ``````````````
set(handles.StatusBar, 'String', 'Busy: Loading EEG (May take some time)...'); drawnow;
% load the struct to the workspace
load([dataPath, dataFile], '-mat');
if ~exist('EEG', 'var')
set(handles.StatusBar, 'String', 'Warning: No EEG structure found in file'); drawnow;
return;
end
% TODO: make loading compatible with old .dat files from EEGLAB
if strcmp(EEG.data(end-3: end), '.dat')
fprintf(1, 'Warning: currently not compatible with .dat files, use EEGLAB to convert to .fdt files');
return;
end
% memory map the actual data...
tmp = memmapfile(fullfile(dataPath, EEG.data),...
'Format', {'single', [EEG.nbchan EEG.pnts EEG.trials], 'eegData'});
eegData = tmp.Data.eegData;
% eegData = mmo(EEG.data, [EEG.nbchan EEG.pnts EEG.trials], false);
% EEG = pop_loadset([dataPath, dataFile]);
set(handles.fig, 'Name', ['Sleep Scoring: ', dataFile]);
% Check for Previous Scoring
% ``````````````````````````
if isfield(EEG, 'swa_scoring')
EEG.swa_scoring.display_channels = 8;
% if there is a previously scoring file
% set the epochlength
% set(handles.et_EpochLength, 'String', num2str(EEG.swa_scoring.epochLength));
samples_in_epoch = EEG.swa_scoring.epochLength*EEG.srate;
number_of_epochs = floor(size(eegData,2)/samples_in_epoch);
% check whether the scoring file matches the length
if length(EEG.swa_scoring.stages) ~= EEG.pnts
fprintf(1, 'previous scoring file a different size as the data, start new scoring session \n');
% pre-allocate the variables
% each sample is assigned a stage (255 is default unscored)
EEG.swa_scoring.stages = uint8(ones(1,EEG.pnts) * 255);
EEG.swa_scoring.arousals = logical(zeros(1,EEG.pnts) * 255);
% only every epoch is assigned a name
EEG.swa_scoring.stageNames = cell(1, number_of_epochs);
EEG.swa_scoring.stageNames(:) = {'Unscored'};
end
% check for startTime
if ~isfield(EEG.swa_scoring, 'startTime')
EEG.swa_scoring.startTime = 0;
end
else
% check for the number of channels in the dataset to plot
if EEG.nbchan > 8
EEG.swa_scoring.display_channels = 8;
else
EEG.swa_scoring.display_channels = EEG.nbchan;
end
% get the default setting for the epoch length from the figure
EEG.swa_scoring.epochLength = 30;
% calculate samples per epoch
samples_in_epoch = EEG.swa_scoring.epochLength*EEG.srate; % samples per epoch
% calculate number of epochs in the entire series
number_of_epochs = floor(size(eegData,2)/samples_in_epoch);
% pre-allocate the variables
% each sample is assigned a stage (255 is default unscored)
EEG.swa_scoring.stages = uint8(ones(1,EEG.pnts)*255);
EEG.swa_scoring.arousals = logical(zeros(1,EEG.pnts)*255);
% only every epoch is assigned a name
EEG.swa_scoring.stageNames = cell(1,number_of_epochs);
EEG.swa_scoring.stageNames(:) = {'Unscored'};
EEG.swa_scoring.startTime = 0;
% check for minimum number of channels
num_channels = min(EEG.nbchan, 8);
% assign montage defaults
EEG.swa_scoring.montage.labels = cell(num_channels, 1);
EEG.swa_scoring.montage.labels(:) = {'undefined'};
EEG.swa_scoring.montage.channels = ....
[1:num_channels; ones(1, num_channels) * size(eegData,1)]';
EEG.swa_scoring.montage.filterSettings = ...
[ones(1, num_channels) * 0.5; ones(1, num_channels)*30]';
end
% ``````````````
% save the samples_in_epoch to the EEG structure
EEG.swa_scoring.samples_in_epoch = samples_in_epoch;
% enable the menu items
set(handles.menu.Export, 'Enable', 'on');
set(handles.menu.Statistics, 'Enable', 'on');
set(handles.menu.Montage, 'Enable', 'on');
% reset the status bar
set(handles.StatusBar, 'String', 'Idle')
% update the handles structure
guidata(handles.fig, handles)
% use setappdata for data storage to avoid passing it around in handles when not necessary
setappdata(handles.fig, 'EEG', EEG);
setappdata(handles.fig, 'eegData', eegData);
% set the current epoch
set(handles.current_epoch, 'Value', 1);
% draw the initial eeg
fcn_initial_plot(handles.fig)
set(handles.StatusBar, 'String', 'EEG Loaded'); drawnow;
function menu_SaveEEG(object, ~)
handles = guidata(object); % Get handles
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
% Ask where to put file...
[saveFile, savePath] = uiputfile('*.set');
% check for cancel press
if isempty(saveFile)
return;
end
% since the data has not changed we can just save the EEG part, not the data
save(fullfile(savePath, saveFile), 'EEG', '-mat');
set(handles.StatusBar, 'String', 'Data Saved')
function menu_Export(object, ~, stage)
handles = guidata(object); % Get handles
% get the eegData structure out of the figure
EEG = getappdata(handles.fig, 'EEG');
eegData = getappdata(handles.fig, 'eegData');
% Find the matching stage and remove arousal samples
keepSamples = false(1,size(EEG.swa_scoring.stages,2));
keepSamples(EEG.swa_scoring.stages == stage) = true;
keepSamples(EEG.swa_scoring.arousals) = false;
if sum(keepSamples) == 0
set(handles.StatusBar, 'String', ['Cannot Export: No epochs found for stage ', num2str(stage)])
return;
end
% copy the data to a new structure
Data.N2 = double(eegData(:, keepSamples));
% get the necessary info from the EEG
if isempty(EEG.chanlocs)
Info.Electrodes = EEG.urchanlocs;
else
Info.Electrodes = EEG.chanlocs;
end
Info.sRate = EEG.srate;
% calculate the time of each sample
time = (1:size(EEG.swa_scoring.stages,2))/EEG.srate;
Info.time = time(keepSamples);
[saveName,savePath] = uiputfile('*.mat');
if ~isempty(saveName)
set(handles.StatusBar, 'String', 'Busy: Exporting Data')
save([savePath, saveName], 'Data', 'Info', '-mat', '-v7.3')
set(handles.StatusBar, 'String', 'Idle')
end
function fcn_initial_plot(object)
% initial plot of the eeg data and hypnogram
% get the handles
handles = guidata(object);
% get the eegData structure out of the figure
EEG = getappdata(handles.fig, 'EEG');
eegData = getappdata(handles.fig, 'eegData');
% select the plotting data
samples_in_epoch = EEG.swa_scoring.samples_in_epoch;
current_epoch = get(handles.current_epoch, 'value');
range = (current_epoch - 1) * samples_in_epoch + 1 : ...
(current_epoch * samples_in_epoch);
channels = 1:EEG.swa_scoring.display_channels;
% re-reference the data
data = eegData(EEG.swa_scoring.montage.channels(channels,1), range)...
- eegData(EEG.swa_scoring.montage.channels(channels,2), range);
% filter the data
% ~~~~~~~~~~~~~~~
% loop each channel for their individual settings
for i = 1:EEG.swa_scoring.display_channels;
[EEG.filter.b(i,:), EEG.filter.a(i,:)] = ...
butter(2,[EEG.swa_scoring.montage.filterSettings(i,1)/(EEG.srate/2),...
EEG.swa_scoring.montage.filterSettings(i,2)/(EEG.srate/2)]);
% transpose data twice
data(i,:) = single(filtfilt(EEG.filter.b(i,:), EEG.filter.a(i,:),...
double(data(i,:)'))');
end
% plot the data
% ~~~~~~~~~~~~~
% define accurate spacing
scale = get(handles.et_Scale, 'value') * -1;
toAdd = [1:EEG.swa_scoring.display_channels]'*scale;
toAdd = repmat(toAdd, [1, length(range)]);
% space out the data for the single plot
data = data+toAdd;
set([handles.channel_axes, handles.label_axes],...
'yLim', [scale 0]*(EEG.swa_scoring.display_channels+1));
% in the case of replotting delete the old handles
if isfield(handles, 'channel_plots')
delete(handles.channel_plots);
delete(handles.labels);
delete(handles.scale_lines);
delete(handles.line_hypno);
delete(handles.plot_arousal);
delete(handles.plot_hypno);
end
% calculate the time in seconds
time = range/EEG.srate;
set(handles.channel_axes, 'xlim', [time(1), time(end)]);
% plot grid line
integer_times = time(~mod(time, 2)); % find all integer times
grid_times = repmat(integer_times, 2, 1);
y_limits = get(handles.channel_axes, 'ylim');
grid_limits = repmat(y_limits, length(grid_times), 1)';
handles.gridlines = line(grid_times, grid_limits,...
'LineStyle', ':',...
'Color', [0.4 0.4 0.4],...
'hitTest', 'off' ,...
'Parent', handles.channel_axes);
% plot the data
handles.channel_plots = line(time, data,...
'color', handles.colorscheme.fg_col_2,...
'parent', handles.channel_axes);
% draw scale lines
threshold = (get(handles.et_Threshold, 'value') * -1 ) / 2;
handles.threshold_lines(1) = line([time(1), time(end)],...
[toAdd(3, 1) - threshold, toAdd(3, 1) - threshold]);
handles.threshold_lines(2) = line([time(1), time(end)],...
[toAdd(3, 1) + threshold, toAdd(3, 1) + threshold]);
set(handles.threshold_lines,...
'color', handles.colorscheme.fg_col_3,...
'linestyle', '--',...
'parent', handles.channel_axes);
% plot the arousals
data(:, ~EEG.swa_scoring.arousals(range)) = nan;
handles.plot_arousal = line(time, data,...
'color', [0.9, 0, 0],...
'parent', handles.channel_axes);
% plot the labels in their own boxes
handles.labels = zeros(EEG.swa_scoring.display_channels, 1);
for chn = 1:length(EEG.swa_scoring.montage.labels)
handles.labels(chn) = text(...
0.5, toAdd(chn,1) + scale/5, EEG.swa_scoring.montage.labels{chn},...
'parent', handles.label_axes,...
'fontsize', 10,...
'fontweight', 'bold',...
'color', handles.colorscheme.fg_col_1,...
'backgroundcolor', handles.colorscheme.bg_col_2,...
'horizontalAlignment', 'center');
end
% Set Hypnogram
% `````````````
time = [1:EEG.pnts] / EEG.srate / 60 / 60;
% set the x-limit to the number of stages
set(handles.axes_hypnogram,...
'XLim', [0 time(end)]);
% plot the epoch indicator line
handles.line_hypno = line([0, 0], [0, 6.5],...
'color', [0.5, 0.5, 0.5],...
'parent', handles.axes_hypnogram);
% plot the stages
handles.plot_hypno = plot(time, EEG.swa_scoring.stages,...
'LineWidth', 2,...
'Color', handles.colorscheme.fg_col_2);
% set the new parameters
guidata(handles.fig, handles);
setappdata(handles.fig, 'EEG', EEG);
% Update Functions
% ````````````````
function updateAxes(handles)
% get the eegData structure out of the figure
EEG = getappdata(handles.fig, 'EEG');
eegData = getappdata(handles.fig, 'eegData');
% data section
samples_in_epoch = EEG.swa_scoring.samples_in_epoch;
current_epoch = get(handles.current_epoch, 'value');
range = (current_epoch*samples_in_epoch-(samples_in_epoch-1)):(current_epoch*samples_in_epoch);
% rereference the data
negative_up = false;
if negative_up
data = eegData(EEG.swa_scoring.montage.channels(:,2),range)...
- eegData(EEG.swa_scoring.montage.channels(:,1),range);
else
data = eegData(EEG.swa_scoring.montage.channels(:,1),range)...
- eegData(EEG.swa_scoring.montage.channels(:,2),range);
end
% define accurate spacing
scale = get(handles.et_Scale, 'value') * -1;
toAdd = [1:8]' * scale;
toAdd = repmat(toAdd, [1, length(range)]);
% adjust the threshold lines
threshold = (get(handles.et_Threshold, 'value') * -1) / 2;
set(handles.threshold_lines(1), 'ydata',...
[toAdd(3, 1) - threshold, toAdd(3, 1) - threshold]);
set(handles.threshold_lines(2), 'ydata',...
[toAdd(3, 1) + threshold, toAdd(3, 1) + threshold]);
% loop for each individual channels settings
% plot the new data
for n = 1 : EEG.swa_scoring.display_channels
data(n,:) = single(filtfilt(EEG.filter.b(n,:), EEG.filter.a(n,:),...
double(data(n,:)'))'); %transpose data twice
set(handles.channel_plots(n), 'yData', data(n,:) + toAdd(n, :));
end
% plot the events
data(:, ~EEG.swa_scoring.arousals(range)) = nan;
for n = 1 : EEG.swa_scoring.display_channels
set(handles.plot_arousal(n), 'yData', data(n, :) + toAdd(n, :))
end
function fcn_epochChange(object, ~, figurehandle)
% get the handles from the guidata
handles = guidata(figurehandle);
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
current_epoch = get(handles.current_epoch, 'value');
if current_epoch < 1
set(handles.StatusBar, 'String', 'This is the first epoch')
set(handles.current_epoch, 'value', 1);
elseif current_epoch > length(EEG.swa_scoring.stageNames)
set(handles.StatusBar, 'String', 'No further epochs')
set(handles.current_epoch, 'value', length(EEG.swa_scoring.stageNames));
end
current_epoch = get(handles.current_epoch, 'value');
% update the hypnogram indicator line
x = current_epoch * EEG.swa_scoring.epochLength/60/60;
set(handles.line_hypno, 'Xdata', [x, x]);
% set the stage name to the current stage
% calculate the current time
current_time = datestr(...
(EEG.swa_scoring.startTime+current_epoch*EEG.swa_scoring.epochLength-30)/(60*60*24),...
'HH:MM:SS');
set(handles.StageBar, 'String',...
[num2str(current_epoch), ' | ', current_time, ' | ', EEG.swa_scoring.stageNames{current_epoch}]);
% update the GUI handles (*updates just fine)
guidata(handles.fig, handles)
setappdata(handles.fig, 'EEG', EEG);
% update all the axes
updateAxes(handles);
function updateScale(object, ~)
handles = guidata(object); % Get handles
% Get the new scale value
ylimits = str2double(get(handles.et_Scale, 'String'));
% Update all the axis to the new scale limits
set(handles.channel_axes,...
'YLim', [-ylimits, ylimits]);
function updateThreshold(object, ~)
% callback when threshold is changed
handles = guidata(object); % Get handles
% get the string in the edit box
threshold = get(handles.et_Threshold, 'string') ;
% set the value from the string
set(handles.et_Threshold, 'Value', str2double(threshold));
% update the axes
updateAxes(handles)
function updateStage(object, ~)
% get the updated handles from the GUI
handles = guidata(object);
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
% current epoch range
samples_in_epoch = EEG.swa_scoring.epochLength*EEG.srate; % samples per epoch
current_epoch = get(handles.current_epoch, 'value');
range = (current_epoch*samples_in_epoch-(samples_in_epoch-1)):(current_epoch*samples_in_epoch);
% set the current sleep stage value and name
EEG.swa_scoring.stages(range) = get(get(handles.button_group, 'SelectedObject'), 'UserData');
EEG.swa_scoring.stageNames{current_epoch} = get(get(handles.button_group, 'SelectedObject'), 'String');
% reset the scoring box
set(handles.button_group,'SelectedObject',[]); % No selection
% change the scores value
set(handles.plot_hypno, 'Ydata', EEG.swa_scoring.stages);
% Update the handles in the GUI
guidata(handles.fig, handles);
setappdata(handles.fig, 'EEG', EEG);
% go to the next epoch
set(handles.current_epoch, 'value', current_epoch+1);
fcn_epochChange(object, [], handles.fig);
function fcn_update_epoch_length(object, ~)
% get handles
handles = guidata(object);
% get the eegData structure out of the figure
EEG = getappdata(handles.fig, 'EEG');
eegData = getappdata(handles.fig, 'eegData');
% check for minimum (5s) and maximum (120s) and give warning...
if EEG.swa_scoring.epochLength > 240
set(handles.StatusBar, 'String', 'No more than 240s Epochs')
EEG.swa_scoring.epochLength = 240;
return;
elseif EEG.swa_scoring.epochLength < 5
set(handles.StatusBar, 'String', 'No less than 5s Epochs')
EEG.swa_scoring.epochLength = 5;
return;
end
% calculate the total number of epochs in dataset
samples_in_epoch = EEG.swa_scoring.epochLength * EEG.srate;
number_of_epochs = floor(size(eegData,2)/samples_in_epoch);
% re-calculate the stage names from the value (e.g. 0 = wake)
EEG.swa_scoring.stageNames = cell(1, number_of_epochs);
count = 0;
for i = 1:samples_in_epoch:number_of_epochs*samples_in_epoch
count = count+1;
switch EEG.swa_scoring.stages(i)
case 0
EEG.swa_scoring.stageNames(count) = {'wake'};
case 1
EEG.swa_scoring.stageNames(count) = {'nrem1'};
case 2
EEG.swa_scoring.stageNames(count) = {'nrem2'};
case 3
EEG.swa_scoring.stageNames(count) = {'nrem3'};
case 5
EEG.swa_scoring.stageNames(count) = {'rem'};
case 6
EEG.swa_scoring.stageNames(count) = {'artifact'};
otherwise
EEG.swa_scoring.stageNames(count) = {'unscored'};
end
end
% update the hypnogram
set(handles.plot_hypno, 'yData', EEG.swa_scoring.stages);
% save the samples_in_epoch to the EEG structure
EEG.swa_scoring.samples_in_epoch = samples_in_epoch;
% update the GUI handles
guidata(handles.fig, handles)
setappdata(handles.fig, 'EEG', EEG);
% redraw the axes
fcn_initial_plot(object)
function cb_KeyPressed(object, eventdata)
% get the updated handles structure (*not updated properly)
handles = guidata(object);
% get the EG structure out of the figure
EEG = getappdata(handles.fig, 'EEG');
% movement keys
switch eventdata.Key
case 'rightarrow'
% move to the next epoch
set(handles.current_epoch, 'Value', get(handles.current_epoch, 'Value') + 1);
fcn_epochChange(object, [], handles.fig)
case 'leftarrow'
% move to the previous epoch
set(handles.current_epoch, 'Value', get(handles.current_epoch, 'Value') - 1);
fcn_epochChange(object, [], handles.fig)
case 'uparrow'
scale = get(handles.et_Scale, 'value');
if scale <= 20
value = scale / 2;
else
value = scale - 20;
end
set(handles.et_Scale, 'string', num2str(value));
set(handles.et_Scale, 'value', value);
set(handles.channel_axes, 'yLim',...
[value * -1, 0] * (EEG.swa_scoring.display_channels + 1))
updateAxes(handles)
case 'downarrow'
scale = get(handles.et_Scale, 'value');
if scale <= 20
value = scale * 2;
else
value = scale + 20;
end
set(handles.et_Scale, 'string', num2str(value));
set(handles.et_Scale, 'value', value);
set(handles.channel_axes, 'yLim',...
[value * -1, 0] * (EEG.swa_scoring.display_channels + 1))
updateAxes(handles)
case 'g'
% get the current status
current_status = get(handles.gridlines(1), 'visible');
switch current_status
case 'on'
set(handles.gridlines, 'visible', 'off');
case 'off'
set(handles.gridlines, 'visible', 'on');
end
end
% sleep staging
switch eventdata.Character
case '0'
set(handles.button_group,'SelectedObject',handles.rb(1));
updateStage(object, eventdata);
case '1'
set(handles.button_group,'SelectedObject',handles.rb(2));
updateStage(object, eventdata);
case '2'
set(handles.button_group,'SelectedObject',handles.rb(3));
updateStage(object, eventdata);
case '3'
set(handles.button_group,'SelectedObject',handles.rb(4));
updateStage(object, eventdata);
case '5'
set(handles.button_group,'SelectedObject',handles.rb(5));
updateStage(object, eventdata);
case '6'
set(handles.button_group,'SelectedObject',handles.rb(6));
updateStage(object, eventdata);
end
guidata(handles.fig, handles)
function checkFilter(figureHandle, ~)
% get the updated handles structure
handles = guidata(figureHandle);
% User feedback since this often takes some time
set(handles.StatusBar, 'string',...
'Checking filter parameters'); drawnow;
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
% loop each channel for their individual settings
for i = 1:8
[EEG.filter.b(i,:), EEG.filter.a(i,:)] = ...
butter(2,[EEG.swa_scoring.montage.filterSettings(i,1)/(EEG.srate/2),...
EEG.swa_scoring.montage.filterSettings(i,2)/(EEG.srate/2)]);
end
% save EEG struct back into the figure
setappdata(handles.fig, 'EEG', EEG);
% update the axes with the new filters
updateAxes(handles)
function bd_hypnoEpochSelect(object, ~)
% function when the user clicks in the hypnogram
% get the handles
handles = guidata(object);
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
current_point = get(handles.axes_hypnogram, 'CurrentPoint');
current_epoch = floor(current_point(1)*60*60*EEG.srate/EEG.swa_scoring.samples_in_epoch);
% set the current epoch
set(handles.current_epoch, 'value', current_epoch);
% Update the handles in the GUI
guidata(handles.fig, handles)
% update the figure
fcn_epochChange(object, [], handles.fig);
function fcn_options(object, ~, type)
% get the handles
handles = guidata(object);
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
switch type
case 'EpochLength'
answer = inputdlg('epoch length (s)',...
'', 1, {num2str(EEG.swa_scoring.epochLength)});
% if different from previous
if ~isempty(answer)
newLength = str2double(answer{1});
if newLength ~= EEG.swa_scoring.epochLength
EEG.swa_scoring.epochLength = newLength;
% update the eeg structure before call
setappdata(handles.fig, 'EEG', EEG);
fcn_update_epoch_length(object, []);
end
end
case 'StartTime'
answer = inputdlg('epoch length (s)',...
'', 1, {'hh:mm:ss'});
if ~isempty(answer)
% calculate time in seconds from input
EEG.swa_scoring.startTime = round(86400*mod(datenum(answer{1}),1 ));
% update the eeg structure before call
setappdata(handles.fig, 'EEG', EEG);
fcn_epochChange(object, [], handles.fig);
end
end
function fcn_statistics(object, ~)
% get the handles
handles = guidata(object);
% Get the EEG from the figure's appdata
EEG = getappdata(handles.fig, 'EEG');
% open the new statistics figure and export the table data to workspace
[table_data] = swa_sleep_statistics(EEG, 1);
assignin('base', 'sleep_table', table_data);
% Code for selecting and marking events
% ````````````````````````````````````
function fcn_select_events(~, ~, object, event)
handles = guidata(object); % Get handles
% get the userData if there was some already (otherwise returns empty)
userData = getappdata(handles.channel_axes, 'x_range');
% if there was no userData, then pre-allocate the userData
if isempty(userData)
userData.range = []; % this is a Nx4 matrix with the selection range
userData.box = []; % this is a Nx1 vector with the line handle
end
% determine whether the user is currently making a selection
selecting = numel(userData.range)>0 && any(isnan(userData.range(end,:)));
% get the current point
p = get(handles.channel_axes, 'CurrentPoint');
p = p(1,1:2);
xLim = get(handles.channel_axes, 'xlim');
yLim = get(handles.channel_axes, 'ylim');
% limit cursor coordinates to axes...
if p(1)<xLim(1), p(1)=xLim(1); end;
if p(1)>xLim(2), p(1)=xLim(2); end;
if p(2)<yLim(1), p(2)=yLim(1); end;
if p(2)>yLim(2), p(2)=yLim(2); end;
switch lower(event)
case lower('ButtonDown')
if ~isempty(userData.range)
% the user has clicked in one of the existing selections
if any(p(1)>=userData.range(:,1) & p(1)<=userData.range(:,2))
% call the function to mark events
fcn_mark_event(handles.fig, userData, get(gcf,'selectiontype'));
% refresh the axes
updateAxes(handles);
end
end
% set the figure's windowbuttonmotionfunction
set(handles.fig, 'WindowButtonMotionFcn', {@fcn_select_events, object, 'Motion'});
% set the figure's windowbuttonupfunction
set(handles.fig, 'WindowButtonUpFcn', {@fcn_select_events, object, 'ButtonUp'});