-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTSleepScorer.m
1064 lines (889 loc) · 42.1 KB
/
MTSleepScorer.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
classdef MTSleepScorer < handle
% MTSleepScorer Interactive GUI for scoring and viewing sleep EEG data
%
% Usage:
% obj = MTSleepScorer()
%
% Input:
% varargin: Additional input arguments
%
% Description:
% The MTSleepScorer class provides functionality for scoring sleep stages in EEG data.
%
% Properties (Access = public):
% - event_marker: EventMarker object for marking events on the spectrogram
% - numstages: Number of sleep stages (3 or 5)
% - data_path: Path to data file (default: loading default)
% - save_path: Path for saving scored data (default: saving default)
% - file_name: Current data file
% - save_fname: File name for saving scored data
% - initials: Scorer's initials
% - mainfig_h: Main figure handle
% - axes_main: Main figure axes array
%
% Public Methods (Access = public):
% - MTSleepScorer: Constructor method. Calls the initialization script
%
% Keyboard Shortcuts:
% - left/right arrow (scroll wheel): Pan screen-width
% - up/down arrow (shift + scroll wheel): Zoom
% - z: Set zoom window size
% - ,/.: Cycle through electrodes
% - w/5: Add Wake stage
% - r/4: Add REM stage
% - n(1/2/3): Add NREM stage
% - x: Automatically detect artifacts
% - a: Add Artifact event
% - u: Toggle slice power spectrum
% - d: Create 3D popout of region
% - h: Toggle this help window
% - q: Quit the program
%
% Example:
% %Initialize the MTSleepScorer class and load EEG data:
% obj = MTSleepScorer();
%
% See also EventMarker
%
% Copyright 2023 Michael J. Prerau Laboratory. - http://www.sleepEEG.org
%********************************************************************
%%%%%%%%%%%%%%%% public properties %%%%%%%%%%%%%%%%%%
properties (Access = public)
%Replace handling with Event Marker
event_marker;
%Number of stages (3 or 5)
numstages;
%File info
data_path; %Loading default
save_path; %Saving Default
file_name; %Current file
save_fname; %Scoring save file
%Scorer initials
initials;
%Main figure
mainfig_h;
%Main figure axes array
axes_main;
end
%%%%%%%%%%%%%%%% private properties %%%%%%%%%%%%%%%%%%
properties (Access = private)
%Loaded data
channel_labels;
curr_channel;
num_channels;
Fs;
data;
%Spectrogram properties
stimes;
sfreqs;
scube;
spect_title_h;
curr_resolution = 1;
%Spectrogram parameters
mt_params;
mt_param_scales;
%Message textboxes
msg_textbox_h;
zoom_textbox_h;
title_textbox_h;
%Popup handles
popfig_h;
popfigax_h;
%Clim handles
clim_sliders_h;
clim_editboxes_h;
autoscale_h;
%Help window handles
helpfig_h;
%Hypnogram graphics objects
zoomrect;
hypnoline;
%Spectrogram graphics object
spect_h;
%Channel title
h_spect_title;
%Slider objects
zoom_slider;
pan_slider;
%Callbacks
WindowButtonDownFcn
WindowButtonMotionFcn
WindowButtonUpFcn
WindowKeyPressFcn
WindowKeyReleaseFcn
WindowScrollWheelFcn
end
%%%%%%%%%%%%%%% public methods %%%%%%%%%%%%%%%%%%%%%%
methods (Access = public)
%***********************************************
% CONSTRUCTOR METHOD
%***********************************************
function obj = MTSleepScorer(varargin)
%Run the initialization script
MT_scoring_init_script;
%Set default paths
obj.data_path = data_path;
obj.save_path = save_path;
%Set staging MT parameters
obj.mt_params = mt_params;
obj.mt_param_scales = mt_param_scales;
%Load data
obj.load_data;
%Set up initial figure
obj.init_fig;
end
end
methods (Access = private)
%************************************************************
% LOAD DATA
%************************************************************
function load_data(obj)
%---------------------------------------------------------
% SELECT DATA AND LOAD
%---------------------------------------------------------
%Close all other figures
c=get(0,'children');
if ~isempty(c)
for i=1:length(c)
delete(c(i));
end
end
clc;
%Get the initials of the scorer to be used for saving
prompt={'Enter scorer initials:';};
name='Enter Scorer Initials';
numlines=1;
defaultanswer={''};
answer=inputdlg(prompt,name,numlines,defaultanswer);
%Return if no answer
if isempty(answer)
return;
end
obj.initials=answer{1};
if ~exist(obj.data_path,'dir')
error('Invalid data path');
end
if ~exist(obj.save_path,'dir')
error('Invalid save path');
end
%Let the user select the data file to load
d=dir(fullfile(obj.data_path,'*.edf'));
str = sort({d.name});
s=listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str);
obj.file_name = str{s};
%Load the file
[~, shdr] = blockEdfLoad(fullfile(obj.data_path,obj.file_name));
labels = {shdr.signal_labels};
s=listdlg('PromptString','Select channels to load:',...
'SelectionMode','multiple',...
'ListString',labels);
h=msgbox(['Loading ' obj.file_name '...']);
%Load EDF
[hdr, shdr, obj.data] = blockEdfLoad(fullfile(obj.data_path,obj.file_name),labels(s));
obj.Fs = hdr.samplingfrequency;
obj.channel_labels = {shdr.signal_labels};
obj.num_channels = length(obj.Fs);
close(h);
res_name = {'full night', 'stage', 'microevent'};
%Make sure to start parallel pool if available
if license('test','distrib_computing_toolbox')
gcp;
end
%Loop over resolutions
for res = 1:length(obj.mt_params)
params = obj.mt_params{res};
f = waitbar(0,['Computing ' res_name{res} ' multitaper spectrograms...']);
%Find channels that need to be rejected because of a different
%duration
skipped_chans = false(1, obj.num_channels);
%Compute MTS for each channel
for ii = 1:obj.num_channels
%Preallocate first time around
if ii == 1
[spect,obj.stimes{res},obj.sfreqs{res}] = multitaper_spectrogram_mex(obj.data{ii}, obj.Fs(ii), [params.frequency_range(1) min(params.frequency_range(2), obj.Fs(ii)/2)], ...
params.taper_params, params.window_params,params.min_nfft, params.detrend_opt, params.weighting, false, false);
spect_size = size(spect);
obj.scube{res} = zeros([obj.num_channels, spect_size]);
else
spect = multitaper_spectrogram_mex(obj.data{ii}, obj.Fs(ii), [params.frequency_range(1) min(params.frequency_range(2), obj.Fs(ii)/2)], ...
params.taper_params, params.window_params,params.min_nfft, params.detrend_opt, params.weighting, false, false);
end
%Check for channels of different duration
if ~any(size(spect) - spect_size)
obj.scube{res}(ii,:,:) = pow2db(spect);
else
skipped_chans(ii) = true;
end
waitbar(ii/obj.num_channels,f,['Computing ' res_name{res} ' multitaper spectrograms...']);
end
close(f);
%Remove skipped channels
if any(skipped_chans)
h = msgbox(['Skipped channels with different duration: ' sprintf('"%s" ', obj.channel_labels{skipped_chans})]);
pause(2);
if ishandle(h)
close(h);
end
obj.scube{res} = obj.scube{res}(~skipped_chans,:,:);
obj.num_channels = sum(~skipped_chans);
obj.channel_labels = obj.channel_labels(~skipped_chans);
end
end
obj.curr_channel = 1;
%UPDATE TO FIX DIRECTORY
obj.save_fname=fullfile(obj.save_path,['/scored_' obj.initials '_' obj.file_name(1:end-4) '.mat']);
obj.numstages=3;
end
%---------------------------------------------------------
% SET UP MAIN FIGURE
%---------------------------------------------------------
function init_fig(obj)
%Set up the figure (hide until complete)
obj.mainfig_h=figure('units','normalized','position',[0 0 .6 .6],'units','normalized',...
'color','w','KeyPressFcn',@obj.handle_keys,'visible','off','windowbuttonupfcn',@obj.save_scoring);
%Make axes
obj.axes_main(1) = axes('Parent',obj.mainfig_h,'Position',[0.05 0.82 0.9 0.1]); %Hypnogram
obj.axes_main(2) = axes('Parent',obj.mainfig_h,'Position',[0.05 0.1 0.9 0.64]); %Spectrogram
%Create text boxes
obj.msg_textbox_h=annotation(obj.mainfig_h,'textbox',...
[0.7 0.006 0.7 0.02],...
'String',{' '},...
'FontSize',14,'HorizontalAlignment','right',...
'FitBoxToText','off',...
'LineStyle','none');
obj.zoom_textbox_h=annotation(obj.mainfig_h,'textbox',...
[0.02 0.87 0.19 0.1],...
'String',{' '},...
'FontSize',14,...
'FitBoxToText','off',...
'LineStyle','none');
obj.title_textbox_h=annotation(obj.mainfig_h,'textbox',...
[0.43 0.957 0.12 0.026],...
'String',strrep(obj.file_name(1:end-4),'_','-'),...
'FontWeight','bold',...
'FontSize',24,...
'FitBoxToText','off',...
'LineStyle','none');
% ---------------------------------------------------------
% PLOT INITIAL DATA
% ---------------------------------------------------------
%Plot the hypnogram
axes(obj.axes_main(1));
if obj.numstages==3
set(obj.axes_main(1),'ylim',[2.5 6.5],'ytick',3:6,'yticklabel',{'NREM','REM','Wake','Art.'},'xtick',[]);
else
set(obj.axes_main(1),'ylim',[.5 5.5],'ytick',1:5,'yticklabel',{'N3','N2','N1','REM','Wake','Art.'},'xtick',[]);
end
obj.h_spect_title=title('');
xlim(obj.stimes{obj.curr_resolution}([1 end]));
hold on
%Create a blank hypnogram
yl = ylim(obj.axes_main(1));
obj.zoomrect=fill([0 obj.stimes{obj.curr_resolution}(end) obj.stimes{obj.curr_resolution}(end) 0],[yl(1) yl(1) yl(2) yl(2)],[.9 .9 1],'edgecolor','none');
obj.hypnoline=plot(obj.stimes{obj.curr_resolution}([1 end]),[0 0],'color','k','linewidth',2);
%Plot the initial spectrogram
axes(obj.axes_main(2));
obj.spect_h=imagesc(obj.stimes{obj.curr_resolution}, obj.sfreqs{obj.curr_resolution}, squeeze(obj.scube{obj.curr_resolution}(obj.curr_channel,:,:)));
axis xy;
climscale;
topcolorbar(.1,.01,.03);
colormap(jet(2^10));
ylabel('Frequency (Hz)');
obj.spect_title_h = title(['Sleep Spectrogram: ' obj.channel_labels(obj.curr_channel)],'FontWeight','bold','FontSize',18,'units','normalized');
obj.spect_title_h.Position(2) = obj.spect_title_h.Position(2) + .05;
%Plot xticks in HH:MM:SS every 30 min
min_step = 30*60;
set(obj.axes_main(2),'xtick',0:min_step:obj.stimes{obj.curr_resolution}(end),'xticklabel',datestr((0:min_step:obj.stimes{obj.curr_resolution}(end))*datefact,'HH:MM:SS')); %#ok<*DATST>
%Link to main figure
obj.mainfig_h.UserData=obj.mainfig_h;
%---------------------------------------------------------
% ADD EVENT MARKER
%---------------------------------------------------------
%
%Call event marker class to mark on the image
%obj = EventMarker(event_axis, xbounds, ybounds, event_types, event_list, line_colors, font_size, motioncallback)
axes(obj.axes_main(2));
em=EventMarker(obj.axes_main(2),xlim(obj.axes_main(2)), ylim(obj.axes_main(2)), [], [], [], 15, @obj.EM_callback);
%obj.add_event_type(EventObject(<event type name>, <event ID>, <region? vs. point>, <bounded to yaxis?>)
if obj.numstages == 3
em.add_event_type(EventObject('W',5,false,false));
em.add_event_type(EventObject('R',4,false,false));
em.add_event_type(EventObject('N',3,false,false));
em.add_event_type(EventObject('A',6,true,true));
em.add_event_type(EventObject([],7,true,false));
elseif obj.numstages == 5
em.add_event_type(EventObject('W',5,false,false));
em.add_event_type(EventObject('R',4,false,false));
em.add_event_type(EventObject('N1',3,false,false));
em.add_event_type(EventObject('N2',2,false,false));
em.add_event_type(EventObject('N3',1,false,false));
em.add_event_type(EventObject('A',6,true,true));
em.add_event_type(EventObject([],7,true,false));
else
error('Must have either 3 or 5 stages');
end
%Add to main object
obj.event_marker = em;
%Load scoring if save file exists
if exist(obj.save_fname,'file')
obj.event_marker.load(obj.save_fname);
else
%Add a default event at time 0
obj.event_marker.mark_event(5, 0);
end
%---------------------------------------------------------
% CLIM SLIDERS
%---------------------------------------------------------
%Create the sliders
cx = caxis(obj.axes_main(2));
clim_bound=(cx(2)-cx(1))/2-.0001;
minslider_h = uicontrol(obj.mainfig_h,'units','normalized','Style','slider',...
'Max',cx(1)+clim_bound,'Min',cx(1)-clim_bound,'Value',cx(1),...
'SliderStep',[0.05 0.2],...
'Position',[0.8979 0.9653 0.0552 0.0217]);
maxslider_h = uicontrol(obj.mainfig_h,'units','normalized','Style','slider',...
'Max',cx(2)+clim_bound,'Min',cx(2)-clim_bound,'Value',cx(2),...
'SliderStep',[0.05 0.2],...
'Position',[0.8979 0.94 0.0552 0.0217]);
obj.clim_sliders_h=[maxslider_h minslider_h];
%Create the edit boxes for manual entry of parameter values
minedit_h=uicontrol(obj.mainfig_h,'units','normalized','Style','edit','string',get(maxslider_h,'value'),'Position',[0.9542 0.9425 0.0352 0.0217],'backgroundcolor',get(obj.mainfig_h,'color'),'horizontalalign','right');
maxedit_h=uicontrol(obj.mainfig_h,'units','normalized','Style','edit','string',get(minslider_h,'value'),'Position',[0.9542 0.9679 0.0352 0.0217],'backgroundcolor',get(obj.mainfig_h,'color'),'horizontalalign','right');
obj.autoscale_h = uicontrol(obj.mainfig_h,'units','normalized','string','Autoscale','Position',[0.9542 0.9225 0.0352 0.0217],'backgroundcolor',...
get(obj.mainfig_h,'color'),'horizontalalign','center','callback',@obj.clim_autoscale);
%Array of all edit box handles
obj.clim_editboxes_h=[maxedit_h minedit_h];
%Set continuous callbaxs for the sliders
addlistener(maxslider_h,'ContinuousValueChange',@obj.clim_slider_update);
addlistener(minslider_h,'ContinuousValueChange',@obj.clim_slider_update);
%Make Labels
uicontrol(obj.mainfig_h,'units','normalized','Style','text','string','Clim Min','Position',[0.8637 0.9718 0.0352 0.0158],'backgroundcolor',get(obj.mainfig_h,'color'),'horizontalalign','right');
uicontrol(obj.mainfig_h,'units','normalized','Style','text','string','Clim Max','Position', [0.8637 0.9479 0.0352 0.0158],'backgroundcolor',get(obj.mainfig_h,'color'),'horizontalalign','right');
%Set the edit box callbacks
set(maxedit_h,'callback',@obj.clim_edit_update);
set(minedit_h,'callback',@obj.clim_edit_update);
%---------------------------------------------------------
% SET UP THE ZOOM/PAN SLIDERS
%---------------------------------------------------------
[obj.zoom_slider, ~]=scrollzoompan(obj.axes_main(2),'x', @obj.update_zoom, @obj.update_pan);%, bounds);
set(obj.zoom_slider,'min',10);
%---------------------------------------------------------
% SLICE POPUP WINDOW
%---------------------------------------------------------
%Activate slice popups
[obj.popfig_h, obj.popfigax_h]=slicepopup(obj.mainfig_h, obj.axes_main(2), obj.stimes{obj.curr_resolution}, obj.sfreqs{obj.curr_resolution}, [], 'Time (s)','Frequency (Hz)','Power (dB)', 'y', 0);
%Adjust size and axes
set(obj.popfig_h,'units','normalized','position',[ 0 0.7 .8 .2],'color','w','CloseRequestFcn',@obj.toggle_visible,'WindowKeyPressFcn',{@obj.toggle_with_key,'u'});
set(obj.popfigax_h,'xlim',[.5 20],'ylim',caxis(obj.axes_main(2)));
obj.update_hypno([]);
obj.update_timetext;
set(obj.mainfig_h,'CloseRequestFcn',@obj.close_all);
obj.mainfig_h.Visible = 'on';
%---------------------------------------------------------
% HELP WINDOW
%---------------------------------------------------------
obj.display_help;
end
%************************************************************
% HELP FUNCTION
%************************************************************
function display_help(obj)
obj.helpfig_h=figure('units','normalized','position',[0 0.7 0.13 0.25],'color','w','name','EEG Viewer','menubar','none');
axes('units','normalized','position',[0 0 1 .8]);
text('position',[.05 .7],'string',...
{'Keyboard Shortcuts:',...
'',...
' left/right arrow (scroll wheel): Pan screen-width'...
' up/down arrow (shift + scroll wheel): Zoom'...
' z: Set zoom window size'...
' ,/.: Cycle through electrodes'...
'',...
' w/5: Add Wake stage'...
' r/4: Add REM stage'...
' n(1/2/3): Add NREM stage'...
''...
' x: Automatically detect artifacts'...
' a: Add Artifact event'...
'',...
' u: Toggle slice power spectrum'...
' d: Create 3D popout of region'...
'',...
' h: Toggle this help window'...
' q: Quit the program'...
},'fontsize',12)
axis off;
set(obj.helpfig_h,'CloseRequestFcn',@obj.toggle_visible,'WindowKeyPressFcn',{@obj.toggle_with_key,'h'});
obj.helpfig_h.UserData=obj.mainfig_h;
end
%************************************************************
% UPDATE DURING ZOOM
%************************************************************
function update_zoom(obj)
obj.update_timetext;
obj.update_zoomrect;
obj.update_resolution;
end
%************************************************************
% UPDATE THE DURING PAN
%************************************************************
function update_pan(obj)
obj.update_timetext;
obj.update_zoomrect;
end
%************************************************************
% FUNCTION DURING EVENT MARKER MOVEMENT
%************************************************************
function EM_callback(obj, emarker_h, varargin)
%Grab the current position of the sliding event marker
if strcmpi(class(emarker_h),'images.roi.Line') || strcmpi(class(emarker_h),'images.roi.Rectangle')
emarker_pos = emarker_h.Position;
else
emarker_pos = getPosition(emarker_h);
end
if obj.event_marker.event_list(obj.event_marker.selected_ind).region
event_time = [emarker_pos(1,1), emarker_pos(1,1)+emarker_pos(1,3)];
else
event_time = emarker_pos(1,1);
end
%Update the hypnogram
obj.update_hypno(event_time);
end
%************************************************************
% UPDATE THE TIME WINDOW TEXT WHEN THE LINES ARE MOVED
%************************************************************
function update_timetext(obj)
xl = xlim(obj.axes_main(2));
set(obj.zoom_textbox_h,'string',...
sprintf(['Time Range:\t\t\t' datestr(xl(1)*datefact,'HH:MM:SS') ' - ' datestr(xl(2)*datefact,'HH:MM:SS')...
'\nWindow Size:\t\t' datestr(diff(xl)*datefact,'HH:MM:SS')]));
%Update the xticks depending on the time range
x_range = diff(xl);
if x_range<5
min_step = .5;
elseif x_range<30
min_step = 1;
elseif x_range<120
min_step = 5;
elseif x_range<5*60
min_step = 30;
elseif x_range<10*60
min_step=60;
elseif x_range<30*60
min_step = 5*60;
elseif x_range<2*3600
min_step = 10*60;
elseif x_range<5*3600
min_step = 15*60;
else
min_step = 3600;
end
if x_range>10
set(obj.axes_main(2),'xtick',0:min_step:obj.stimes{obj.curr_resolution}(end),'xticklabel',datestr((0:min_step:obj.stimes{obj.curr_resolution}(end))*datefact,'HH:MM:SS')); %#ok<*DATST>
else
set(obj.axes_main(2),'xtick',0:min_step:obj.stimes{obj.curr_resolution}(end),'xticklabel',datestr((0:min_step:obj.stimes{obj.curr_resolution}(end))*datefact,'HH:MM:SS:FFF')); %#ok<*DATST>
end
end
%************************************************************
% UPDATE THE ZOOM RECTANGLE WHEN THE LINES ARE MOVED
%************************************************************
function update_zoomrect(obj)
xl = xlim(obj.axes_main(2));
set(obj.zoomrect,'xdata',[xl fliplr(xl)]);
end
%************************************************************
% UPDATE THE RESOLUTION OF THE SPECTROGRAM
%************************************************************
function update_resolution(obj)
xl = xlim(obj.axes_main(2));
dt = diff(xl);
for ii = 1:length(obj.mt_param_scales)
if obj.curr_resolution ~= ii && dt <= obj.mt_param_scales(ii) && dt>obj.mt_param_scales(ii+1)
obj.spect_h.CData=squeeze(obj.scube{ii}(obj.curr_channel,:,:));
obj.spect_h.XData = obj.stimes{ii};
obj.spect_h.YData = obj.sfreqs{ii};
obj.curr_resolution = ii;
end
end
set(obj.zoomrect,'xdata',[xl fliplr(xl)]);
end
%************************************************************
% UPDATE THE HYPNOGRAM WHEN THE LINES ARE MOVED
%************************************************************
function update_hypno(obj, event_time)
if isempty(event_time)
selected_stage = [];
selected_ind = [];
else
%Get selected stage
selected_ind = obj.event_marker.selected_ind;
selected_stage = obj.event_marker.event_list(selected_ind).type_ID;
end
%Get all the stage events and times
event_types = [obj.event_marker.event_list.type_ID];
stage_inds = event_types<6; %Pick only stages
stage_vals = event_types(stage_inds);
stage_times = arrayfun(@(x)x.time_bounds,obj.event_marker.event_list(stage_inds));
%Add an end point
stage_times = [stage_times length(obj.data{obj.curr_channel})/obj.Fs(obj.curr_channel)];
stage_vals = [stage_vals stage_vals(end)];
art_inds = event_types==6; %Pick only stages
if any(art_inds)
if selected_stage == 6
art_inds(selected_ind) = 0;
end
art_times = arrayfun(@(x)x.time_bounds',obj.event_marker.event_list(art_inds),'UniformOutput',false);
if selected_stage == 6
art_times = [art_times{:} event_time'];
else
art_times = [art_times{:}];
end
else
art_times = [];
end
if ~isempty(event_time)
%Return if not a stage time
if selected_stage~=6
%Find the original stage time
selected_time_old = obj.event_marker.event_list(selected_ind).obj_handle.XData(1);
%Find and update the time corresponding to the selected stage
selected_ind_new = stage_times == selected_time_old;
if any(selected_ind_new)
stage_times(selected_ind_new) = event_time;
end
end
end
if ~isempty(art_times)
%Find the proper end stage
stage_ends = interp1(stage_times,stage_vals,art_times(2,:),'previous');
%Delete the stages in the middle
for ii = 1:size(art_times,2)
delete_inds = stage_times>=art_times(1,ii) & stage_times<art_times(2,ii) ;
stage_times = stage_times(~delete_inds);
stage_vals = stage_vals(~delete_inds);
end
%Add to the hypnogram
stage_times = [stage_times art_times(1,:) art_times(2,:)];
stage_vals = [stage_vals 6*ones(1,size(art_times,2)) stage_ends];
end
%Sort the times and plot the hypnogram
[stage_times,sind]=sort(stage_times);
stage_vals=stage_vals(sind);
%Stairs is SLOW
x=[obj.stimes{obj.curr_resolution}(1) stage_times obj.stimes{obj.curr_resolution}(end)];
y=[0 stage_vals(:)' 0];
X=reshape(repmat(x,2,1),1,length(x)*2);
Y=reshape(repmat(y,2,1),1,length(y)*2);
set(obj.hypnoline,'ydata',Y(1:end-1),'xdata',X(2:end));
drawnow;
end
%************************************************************
% UPDATE SPECTROGRAM CHANNEL
%************************************************************
function update_channel(obj, new_chan)
if new_chan<1 || new_chan>obj.num_channels
return;
end
obj.curr_channel = new_chan;
obj.spect_h.CData=squeeze(obj.scube{obj.curr_resolution}(new_chan,:,:));
obj.spect_title_h.String = ['Sleep Spectrogram: ' obj.channel_labels(obj.curr_channel)];
end
%************************************************************
% TOGGLE POPUP VISIBILITY
%************************************************************
function toggle_visible(~, src, varargin)
%Toggles the current visibility of the popup
if strcmp(get(src,'visible'),'on')
set(src,'visible','off');
else
set(src,'visible','on');
end
%Resets focus to the main window
set(0,'currentfigure',get(get(gcbo,'parent'),'parent'));
end
%************************************************************
% HELPER FUNCTION TO CLOSE WINDOWS WITH A HOTKEY
%************************************************************
function toggle_with_key(obj, src,event,keychar)
if lower(event.Key) == keychar
toggle_visible(obj, src,event);
end
end
%************************************************************
% TOGGLE SPECTRUM RESOLUTION
%************************************************************
function toggle_spectrum(varargin)
if strcmpi(get(obj.spect_h,'visible'),'off')
set(obj.spect_h,'visible','on');
set(GUI_data.h_stagespect,'visible','off');
else
set(obj.spect_h,'visible','off');
set(obj.h_stagespect,'visible','on');
end
end
%************************************************************
% SAVE USER SCORING DATA
%************************************************************
function save_scoring(obj, varargin)
obj.msg_textbox_h.String = 'Saving...';
obj.event_marker.save(obj.save_fname);
obj.msg_textbox_h.String = '';
end
%************************************************************
% DETECT ARTIFACTS
%************************************************************
function detect_EEGartifacts(obj, varargin)
% %Get all the stage events and times
% event_types = [obj.event_marker.event_list.type_ID];
% stage_inds = event_types<=6; %Pick only stages
% stage_vals = event_types(stage_inds);
% stage_times = cellfun(@(x)x.XData(1),{obj.event_marker.event_list(stage_inds).obj_handle});
artifacts = detect_artifacts(obj.data{obj.curr_channel}, obj.Fs(obj.curr_channel),...
'zscore_method','robust','hf_crit', 5.5,'bb_crit', 5.5,'slope_test',true, 'verbose',true);
%Get consecutive artifacts longer than one time point
[~, run_inds] = consecutive_runs(artifacts,2);
start_inds = cellfun(@(x)x(1),run_inds);
end_inds = cellfun(@(x)x(end),run_inds);
start_times = start_inds/obj.Fs(obj.curr_channel);
end_times = end_inds/obj.Fs(obj.curr_channel);
ylims = get(obj.axes_main(2),'YLim');
for ii = 1:length(start_times)
newpos = [start_times(ii), ylims(1), end_times(ii)-start_times(ii), diff(ylims)];
obj.event_marker.mark_event(6,newpos)
end
obj.update_hypno([]);
end
%************************************************************
% UPDATE CLIM SLIDER
%************************************************************
function clim_slider_update(obj, varargin)
maxval=get(obj.clim_sliders_h(1),'value');
minval=get(obj.clim_sliders_h(2),'value');
if maxval<=get(obj.clim_sliders_h(2),'value')
maxval=get(obj.clim_sliders_h(2),'value')+1e-10;
end
if minval>=get(obj.clim_sliders_h(1),'value')
minval=get(obj.clim_sliders_h(1),'value')-1e-10;
end
cx=[minval maxval];
% disp(clims);
set(obj.axes_main(2),'clim',cx);
set(obj.clim_editboxes_h(1),'string',num2str(cx(1)));
set(obj.clim_editboxes_h(2),'string',num2str(cx(2)));
end
%************************************************************
% UPDATE CLIM EDIT BOX
%************************************************************
function clim_edit_update(obj, varargin)
%Get the new value from the edited text box
minval=str2double(get(obj.clim_editboxes_h(1),'string'));
maxval=str2double(get(obj.clim_editboxes_h(2),'string'));
if maxval<=get(obj.clim_sliders_h(2),'value')
maxval=get(obj.clim_sliders_h(2),'value')+.001;
end
if minval>=get(obj.clim_sliders_h(1),'value')
minval=get(obj.clim_sliders_h(1),'value')-.001;
end
cx=[minval maxval];
if maxval>get(obj.clim_sliders_h(1),'max')
set(obj.clim_sliders_h(1),'max',maxval,'value',maxval);
elseif maxval<get(obj.clim_sliders_h(1),'min')
set(obj.clim_sliders_h(1),'min',maxval,'value',maxval);
end
if minval>get(obj.clim_sliders_h(2),'max')
set(obj.clim_sliders_h(2),'max',minval,'value',minval);
elseif minval<get(obj.clim_sliders_h(2),'min')
set(obj.clim_sliders_h(2),'min',minval,'value',minval);
end
set(obj.clim_sliders_h(1),'value',maxval);
set(obj.clim_sliders_h(2),'value',minval);
set(obj.clim_editboxes_h(1),'string',minval);
set(obj.clim_editboxes_h(2),'string',maxval);
set(obj.axes_main(2),'clim',cx);
end
%************************************************************
% UPDATE AUTOSCALE
%************************************************************
function clim_autoscale(obj, varargin)
%Autoscale colormap
climscale(obj.axes_main(2),[],true);
cx = caxis(obj.axes_main(2));
set(obj.clim_sliders_h(1),'value', cx(2));
set(obj.clim_sliders_h(2),'value', cx(1));
set(obj.clim_editboxes_h(1),'string',num2str(cx(1)));
set(obj.clim_editboxes_h(2),'string',num2str(cx(2)));
end
%************************************************************
% UPDATE AUTOSCALE
%************************************************************
function pop_3d(obj, varargin)
%Check for image processing toolbox
if ~license('test','image_toolbox')
msgbox('This function is only available with the Image Processing Toolbox');
return
end
%Pause callbacks to let the rectangle draw
obj.pause_callbacks();
%Use drawrectangle if available
if which('drawrectangle.m')
h1 = drawrectangle('Label','Select Region and Hit Enter','Color',[1 0 0]);
else
h1 = imrect; %#ok<IMRECT>
end
%Save the position and kill the rectangle
wait(h1);
pos = h1.Position;
delete(h1);
%Resume all callbacks
obj.resume_callbacks;
%Get the frequency and time range selected
finds = obj.sfreqs{obj.curr_resolution} >= pos(2) & obj.sfreqs{obj.curr_resolution} <= pos(2)+pos(4);
tinds = obj.stimes{obj.curr_resolution} >= pos(1) & obj.stimes{obj.curr_resolution} <= pos(1)+pos(3);
%Extract the spectrogram in the range
pop_spect = squeeze(obj.scube{obj.curr_resolution}(obj.curr_channel,finds,tinds));
pop_sfreqs = obj.sfreqs{obj.curr_resolution}(finds);
pop_stimes = obj.stimes{obj.curr_resolution}(tinds);
%Create the popup figure
figure
surface(pop_stimes,pop_sfreqs,pop_spect,'edgecolor','none');
xlabel('Times (s)');
ylabel('Frequency (Hz)');
zlabel('Power (dB)');
colormap(jet)
view(3);
caxis(caxis(obj.axes_main(2)))
camlight left
camlight right
material dull
lighting phong
shading interp
end
%************************************************************
% HANDLE HOTKEYS
%************************************************************
function handle_keys(obj, ~, event)
switch event.Key
case {'backspace','delete'}
obj.event_marker.delete_selected;
end
%Check for hotkeys pressed
switch lower(event.Character)
case {'w', '5'}
obj.event_marker.mark_event(5);
obj.save_scoring;
case {'r', '4'}
obj.event_marker.mark_event(4);
obj.save_scoring;
case 'n'
obj.event_marker.mark_event(3);
obj.save_scoring;
case '1'
if obj.numstages == 5
obj.event_marker.mark_event(3);
obj.save_scoring;
end
case '2'
if obj.numstages == 5
obj.event_marker.mark_event(2);
obj.save_scoring;
end
case '3'
if obj.numstages == 5
obj.event_marker.mark_event(1);
obj.save_scoring;
end
% case 's'
% obj.event_marker.mark_event(7);
% obj.save_scoring;
case 'a'
%Add artifact event
obj.event_marker.mark_event(6);
obj.save_scoring;
case 't'
art_inds = [obj.event_marker.event_list.type_ID] == 6;
art_handles = [obj.event_marker.event_list(art_inds).obj_handle];
art_label_handles = [obj.event_marker.event_list(art_inds).label_handle];
isvis = strcmp(get(art_label_handles(1),'visible'),'on');
if isvis
set(art_handles,'visible','off');
set(art_label_handles,'visible','off');
else
set(art_handles,'visible','on');
set(art_label_handles,'visible','on');
end
case 'h'
obj.toggle_visible(obj.helpfig_h);
case 'u'
obj.toggle_visible(obj.popfig_h);
case 'q'
obj.close_all;
case 'd'
obj.pop_3d;
case 'x'
obj.detect_EEGartifacts;
case ','
new_chan = obj.curr_channel + 1;
if new_chan > obj.num_channels
new_chan = 1;
end
obj.update_channel(new_chan);
case '.'
new_chan = obj.curr_channel - 1;
if new_chan < 1