-
Notifications
You must be signed in to change notification settings - Fork 0
/
EEG Connectivity Explorer January 2023.py
815 lines (571 loc) · 29.5 KB
/
EEG Connectivity Explorer January 2023.py
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
import os
import numpy as np
import mne
mne.viz.set_browser_backend('matplotlib')
from mne.preprocessing import ICA
from mne import create_info
from mne.io import RawArray
import sys
from PIL import ImageTk, Image
import matplotlib
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.backends.backend_tkagg as tkagg
import matplotlib.ticker as ticker
matplotlib.use('Qt5Agg',force=True)
from utils.general import get_data, plot_selected,list_channel_pairs, license_window,program_info_window,start_video_tutorial
from utils.preprocessing_11_28 import remove_line_noise,find_channel_subset_annotations, repair_artifacts
from utils.animation_tools_5_3 import heatplot_barplot_animation_combined,scrolling_configuration_matrix_viewer
from utils.animation_tools_5_3 import bar_plot_connection_reinforcements,bar_plot_with_slider_rms,bar_plot_with_slider_burnos
from utils.menu_options_2_1 import conn_options, update_conn_options,conn_options_reset
from tkinter import Tk, Frame,Label, Button, Entry,Label, Checkbutton,BooleanVar,filedialog,StringVar,messagebox,ttk,DoubleVar,Frame,LEFT,TOP,RIGHT,BOTTOM,BOTH,Menu,Toplevel,PhotoImage,Canvas
from tkinter import font as tkFont
from tkmacosx import Button as Message_Button ### CHANGE TO JUST BUTTON FOR WINDOWS VERSION; DON'T IMPORT tkmacosx
### MAIN HFO FUNCTIONS
from HFO_tools_5_3 import HFOs_all_channels
from HFO_count import hfo_count
### MAIN CONNECTIVITY FUNCTIONS
from functional_connectivity_tools import coherence_at_mark,functional_connectivities,like_brain_states,clustering_coefficient_matrix
from effective_connectivity_tools import effective_connectivity,conn_count,swadtf_at_mark
################## FUNCTION TO CLOSE CURRENTLY OPEN WIDGETS
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def all_children (window) :
_list = window.winfo_children()
for item in _list :
if item.winfo_children() :
_list.extend(item.winfo_children())
return _list
############### MAIN ROOT WINDOW
root = Tk()
myfont = tkFont.Font(family='Helvetica', size=14, weight=tkFont.BOLD)
root.wm_title("EEG Connectivity Explorer")
root.geometry('1500x1000')
root.configure(bg='white')
style = ttk.Style(root)
#style.theme_use('clam')
############ FIGURE BACKGROUND AT STARTUP
########### IMAGE ON ROOT WINDOW AT STARTUP
def startup_background(root):
if getattr(sys, 'frozen', False):
# Frozen application
img_path = os.path.join(sys._MEIPASS, 'background.png')
config_path=os.path.join(sys._MEIPASS,'config.txt')
else:
# (Mac version) Original script
img_path = 'background.png'
config_path='config.txt'
# Create a Canvas
canvas = Canvas(root, width=700, height=3500)
canvas.pack(fill=BOTH, expand=True)
# Function to resize the window
def resize_image(e):
global image, resized , image2
# open image to resize it
if getattr(sys, 'frozen', False):
# Frozen application
img_path = os.path.join(sys._MEIPASS, 'background.png')
config_path=os.path.join(sys._MEIPASS,'config.txt')
else:
# (Mac version) Original script
img_path = 'background.png'
config_path='config.txt'
image = Image.open(img_path)
#print([e.width,e.height])
# resize the image with width and height of root
resized = image.resize((e.width, e.height), Image.ANTIALIAS)
image2 = ImageTk.PhotoImage(resized)
canvas.create_image(0, 0, image=image2, anchor='nw')
# Bind the function to configure the parent window
root.bind("<Configure>", resize_image)
def show_license(root):
if getattr(sys, 'frozen', False):
# Frozen application
config_path=os.path.join(sys._MEIPASS,'config.txt')
else:
# (Mac version) Original script
config_path='config.txt'
license_window(config_path)
startup_background(root)
show_license(root)
############## EEG CLASS ################################
############# A global variable with numerous attributes and methods
###################################################
class EEG:
def __init__(self,file):
raw=mne.io.read_raw(file,preload=True) # raw data as loaded from .edf file
self.raw=raw
raw_orig=raw.copy()
self.raw_orig=raw_orig # backup copy of original data so its doesn't have to be re-loaded
self.fs=int(self.raw.info['sfreq']) # retrieve sampling frequency from recording
self.channels=raw.info["ch_names"] # retrieve channel names as a list of strings, e.g. ['Fp1','Fp2',...]
# Plot duration and start in seconds; Updated in plot_selected as
# eeg.start_time = eeg.fig.mne.t_start - eeg.fig.mne.first_time
# eeg.end_time = eeg.start_time + eeg.fig.mne.duration. See "fig" below
self.start_time=0
self.end_time=30
self.plot_duration=30
self.plot_start=0
self.root=None
# fig is the current figure on display at any instant. Currently, I can extract the start time, end time, marked segment from
# fig. I was able to do this by digging deep into the MNE code.
fig=self.raw.plot(show=False,block=True,duration=self.plot_duration,bgcolor='w',color='black', bad_color='gray')
self.fig=fig
# scalings of channels; currently controlled by +/- keys
self.scalings=.00005
# current "bad", unselected channels as they appear in eeg.fig
self.bads=self.fig.mne.info['bads']
# current location of marked segment
self.segment_loc=0
self.plot_options_window=None
self.conn_options_window=None
self.canvas_frame=None
self.source_frame=None
self.ica=None
self.eog_indices=None
self.configuration_matrix=None
self.annotations=None
self.artifact_starts=[]
self.artifact_stops=[]
self.plot_options_changed=False
self.segment_options_changed=False
self.conn_options_changed=False
self.num_win=1
self.win_size=30
self.windowed=False
self.include_channel_time=False
# set a few default parameters for analysis.
self.conn_win_value=.5
self.fmin_value=5
self.fmax_value=30
self.conns_percentile=.9
############ plot method associated with EEG class ####################
def plot_canvas(self,annotations=False):
if self.source_frame in set(all_children(root)):
self.source_frame.destroy()
# annotations are marked segments in the recording where signal artifacts are repaired. LOW-PRIORITY
if annotations==False:
self.raw.set_annotations(None)
else:
raw=self.raw
channels=self.raw.info['ch_names']
fs=self.raw.info['sfreq']
all_annot=find_channel_subset_annotations(raw,fs,channels)
self.raw.set_annotations(all_annot)
self.annotations=all_annot
mne.set_config('MNE_BROWSE_RAW_SIZE','16,8')
# fig stores current figure showing in window.
self.fig=(self.raw).plot(show=False,block=True,duration=self.plot_duration,start=self.plot_start,bgcolor='lightgray',color='b', bad_color='r',scalings=dict({'eeg':.000050}),n_channels=10,precompute=True,time_format='clock')
######### THIS IS WHERE THE RAW DATA IS PLOTTED AND PLACED ON A TKINTER FIGURE CANVAS.
self.canvas_frame=Frame(root)
self.canvas_frame.pack(side=TOP,expand=True)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.canvas_frame)
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
############# PLOT ICA SOURCES ################# USED FOR SIGNAL REPAIR--LOW PRIORITY
def plot_sources(self,show=True):
if self.fig.mne.info['bads']==self.fig.mne.info['ch_names']:
messagebox.showerror("Error","No channels selected!!")
return
if self.canvas_frame in set(all_children(root)):
self.canvas_frame.destroy() # Important to close the eeg.canvas_frame before doing everything below.
bads=self.fig.mne.info['bads']
channels=self.fig.mne.info['ch_names']
to_drop=list(set(channels).intersection(set(bads)))
self.raw.drop_channels(to_drop)
channels=self.raw.info['ch_names']
raw_temp=self.raw.copy().crop(tmin=self.start_time,tmax=self.end_time)
raw_filt = raw_temp.load_data().filter(l_freq=1., h_freq=None)
self.ica = ICA(method='fastica',n_components=len(channels))
self.ica.fit(raw_filt,picks='all',decim=1)
eog_idx, eog_scores=self.ica.find_bads_eog(raw_filt,ch_name=channels,measure='correlation',threshold=.9,reject_by_annotation=False)
ecg_idx, ecg_scores=self.ica.find_bads_ecg(raw_filt,ch_name=channels[0],measure='correlation',threshold=.9,reject_by_annotation=False)
idx=set(eog_idx).union(set(ecg_idx))
excludes_idx=sorted(idx)
self.exclude_indices=excludes_idx
if show:
fig=self.ica.plot_sources(raw_filt, show=False,block=True,show_scrollbars=True,title='Latent Sources in Data')
self.source_frame=Frame(root,background="white")
self.source_frame.pack(side=TOP,expand=True)
close_button = Button(self.source_frame, text ="Repair Original", fg='black',bg='white',borderwidth=0,command = _repair)
close_button.pack(side=TOP,expand=True)
repair_button = Button(self.source_frame, text ="Close", fg='black',bg='white',borderwidth=0,command = _replot)
repair_button.pack(side=TOP,expand=True)
source_canvas = FigureCanvasTkAgg(fig, master=self.source_frame)
source_canvas.draw()
source_canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=1)
### replot using only selected channels
def _replot():
eeg.plot_canvas()
############ OPEN FILE ####################
def _open():
global eeg
widget_list = all_children(root)
for item in widget_list:
item.pack_forget()
root.filename = filedialog.askopenfilename(title = "Select file",filetypes = (("EDF files","*.edf"),("MNE files","*.fif")))#,("all files","*.*")))
if root.filename=='':
startup_background(root)
return
eeg=EEG(root.filename)
eeg.raw.info['bads']=eeg.raw.info["ch_names"]
eeg.plot_canvas()
enable_menu_pulldowns()
########### PLOT SELECTED CHANNELS CALL-BACK FUNCTION#######################
def _plot_selected():
plot_selected(root,eeg,annotations=False)
############ SELECT ALL CHANNELS CALL-BACK FUNCTION
def _select_all_channels():
eeg.start_time = eeg.fig.mne.t_start - eeg.fig.mne.first_time
eeg.end_time = eeg.start_time + eeg.fig.mne.duration
eeg.plot_start=eeg.start_time
eeg.plot_duration=eeg.end_time-eeg.start_time
eeg.canvas_frame.destroy()
eeg.raw.info['bads']=[]
eeg.plot_canvas()
####### REFRESH PLOT CALL-BACK FUNCTION
def _refresh():
eeg.canvas_frame.destroy()
raw_fresh= eeg.raw_orig.copy()
raw_fresh.info['bads']=raw_fresh.info['ch_names']
eeg.raw=raw_fresh
eeg.plot_start=0
eeg.plot_duration=30
eeg.plot_canvas()
###### QUIT CALL-BACK FUNCTION
def _quit():
widget_list = all_children(root)
for item in widget_list:
item.pack_forget()
root.quit()
root.destroy()
#root.protocol("WM_DELETE_WINDOW", enable_menu_pulldowns)
############ REMOVE LINE NOISE CALL-BACK: LOW-PRIORITY #######################
def _remove_line_noise():
remove_line_noise(eeg)
########## MARK BLINK AND MUSCLE Artifacts
'''
def _mark_artifacts():
# this calls plot_selected; eeg.raw.info['ch_names'] is updated; and only those channels are plotted by eeg.canvas()
# an error message results here if no channels are selected; artifacts are marked only for selected channels
plot_selected(root,eeg,annotations=True)
'''
########### PLOT ICA SOURCES AND REPAIR: LOW PRIORITY ##################
def _plot_sources():
eeg.plot_sources(show=True)
return
########################################################################
########################################################################
############## RECONSTRUCT REPAIRED SIGNAL USING CHOSEN ARTIFACTS
###### Create raw_repaired by using ica.apply below on
######### raw_temp=eeg.raw.copy().crop(tmin=eeg.start_time,tmax=eeg.end_time). Then append this in front and back
####### by raw_beginning=eeg.raw.copy().crop(tmin=0,tmax=eeg.start_time) and
####### raw_ending=eeg.raw.copy().crop(tmin=eeg.end_time)
# LOW PRIORITY
def _repair():
eeg.source_frame.destroy()
raw_temp=eeg.raw.copy().crop(tmin=eeg.start_time,tmax=eeg.end_time)
raw_repaired=eeg.ica.apply(raw_temp,exclude=eeg.ica.exclude)
raw_beginning=eeg.raw.copy().crop(tmin=0,tmax=eeg.start_time)
raw_ending=eeg.raw.copy().crop(tmin=eeg.end_time)
raw_beginning.append([raw_repaired,raw_ending])
eeg.raw=raw_beginning
eeg.plot_canvas()
return
######## UPDATE connectivity Options UNDER Connectivity Menu ############################
#######################################################################################
##### UPDATE CONN OPTIONS #########
def _update_conn_options():
update_conn_options(root,eeg)
enable_menu_pulldowns()
########## RESET CONN OPTIONS WINDOW
def _reset_conn_options():
conn_options_reset(root,eeg)
enable_menu_pulldowns()
########## CLOSE CONNECTIVITY OPTIONS WINDOW
def _close_conn_options():
##### ENABLE PULL DOWN MENU
eeg.conn_options_window.destroy()
enable_menu_pulldowns()
######## SET UP CONNECTIVITY MENU
def _conn_options():
disable_menu_pulldowns()
eeg.conn_options_window = Toplevel(bg="lightgray")
eeg.conn_options_window.attributes('-topmost','true')
eeg.conn_options_window.geometry('400x280')
eeg.conn_options_window.wm_title("Connectivity Options")
eeg.conn_options_window.protocol("WM_DELETE_WINDOW", _close_conn_options)
conn_options(root,eeg)
############ Update and close Connectivity options
update_conn_options_box = ttk.Button(master=eeg.conn_options_window, text="Update",command=_update_conn_options)
update_conn_options_box.pack()
reset_conn_options_box = ttk.Button(master=eeg.conn_options_window, text="Reset",command=_reset_conn_options)
reset_conn_options_box.pack()
############### CONNECTIVITY COMPUTATIONS ############################
#######################################################################
### Coherence heatmap using a window of width conn_win_value centered at selected green segment in raw plot
def _coh_mark():
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info['ch_names']) or len(eeg.fig.mne.info['ch_names'])-len(eeg.fig.mne.info['bads'])<2:
message_box(eeg.root, 'Error: At least two channels must be selected!',type='error')
return
if eeg.fig.mne.segment_loc==0:
message_box(eeg.root, 'Error: No segment marked in raw data!',type='error')
#messagebox.showerror('Error','No segment marked in raw data!')
return
coherence_at_mark(eeg)
### Sequence of coherence matrices, where each is averaged over a frequency band. PSD for each channel also computed.
def _coh():
eeg.start_time = eeg.fig.mne.t_start - eeg.fig.mne.first_time
eeg.end_time = eeg.start_time + eeg.fig.mne.duration
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info['ch_names']) or len(eeg.fig.mne.info['ch_names'])-len(eeg.fig.mne.info['bads'])<2:
message_box(eeg.root, 'Error: At least two channels must be selected!',type='error')
return
if eeg.end_time-eeg.start_time<eeg.conn_win_value:
message_box(eeg.root, 'Error: Selected interval is less than subwindow size from connectivity options! Expand interval!',type='error')
return
if eeg.end_time-eeg.start_time>120:
message_box(eeg.root,'Error: Connectivity animation limited to 120 seconds. Decrease interval length!',type='error')
return
plot_selected(root,eeg,annotations=False)
X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
data=X.T
start_time=eeg.start_time
conn_win_value=eeg.conn_win_value # max(5/(eeg.fmin_value*2),eeg.conn_win_value)
M_list,config_matrix,bar_list=functional_connectivities(root,data,channels,fs,conn_win_value,method='coh',fmin=eeg.fmin_value,fmax=eeg.fmax_value)
bar_list=bar_list*1/np.max(bar_list) #max(max(bar_list))
bar_list_max=np.max(bar_list)
heatplot_barplot_animation_combined(eeg,root,channels,M_list,bar_list,bar_list_max,start_time,conn_win_value,xlabel='channel',ylabel='channel',barlabel='Centrality',title='Coherence and Centrality Values')
eeg.configuration_matrix=config_matrix
######## cluster similar coherence matrices by time windows
def _coh_time_communities():
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info["ch_names"]):
message_box(eeg.root,'Error: No channels selected!',type='error')
#messagebox.showerror('Error','No channels selected!')
return
plot_selected(root,eeg,annotations=False)
X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
data=X.T
conn_win_value=eeg.conn_win_value
M_list,config_matrix,bar_list=functional_connectivities(root,data,channels,fs,conn_win_value,method='coh',fmin=eeg.fmin_value,fmax=eeg.fmax_value)
like_brain_states(root,eeg,config_matrix,conn_win_value,method='kmeans')
# options are 'correlation' or 'kmeans' or 'affinity_prop'
######## time sequence of mutual information and transfer entropy matrices, one for each time subwindow
##def _mutual_info_and_transfer_entropy():
## if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info["ch_names"]):
## message_box(eeg.root,'Error: No channels selected!',type='error')
## return
## plot_selected(root,eeg,annotations=False)
## X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
## conn_win_value=eeg.conn_win_value
## MI_list,TE_list=mutual_info_and_transfer_entropy(root,eeg,conn_win_value)
##
## spos,ax,fig=scrolling_stacked_matrix_sequence_viewer(root,MI_list,TE_list,channels,conn_win_value)
######### Spectrum weighted adapted direct, directed transfer function, one for each time subwindow, along with eigenvector centrality
def _swadtf():
eeg.start_time = eeg.fig.mne.t_start - eeg.fig.mne.first_time
eeg.end_time = eeg.start_time + eeg.fig.mne.duration
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info["ch_names"]) or len(eeg.fig.mne.info['ch_names'])-len(eeg.fig.mne.info['bads'])<2:
message_box(eeg.root,'Error: At least two channels must be selected!',type='error')
return
if eeg.end_time-eeg.start_time<eeg.conn_win_value:
message_box(eeg.root,'Error: Selected interval is less than subwindow size from connectivity options! Expand interval!',type='error')
return
if eeg.end_time-eeg.start_time>120:
message_box(eeg.root,'Error: Connectivity animation limited to 120 seconds. Decrease interval length!',type='error')
return
plot_selected(root,eeg,annotations=False)
X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
data=X.T
start_time=eeg.start_time
conn_win_value= eeg.conn_win_value #max(5/(eeg.fmin_value*2),eeg.conn_win_value)
M_list,bar_list=effective_connectivity(root,data,channels,fs,conn_win_value,type='swdtf',fmin=eeg.fmin_value,fmax=eeg.fmax_value)
bar_list_max=1
heatplot_barplot_animation_combined(eeg,root,channels,M_list,bar_list,bar_list_max,start_time,conn_win_value,xlabel='Sending',ylabel='Receiving',barlabel='Driving Score',title='Directed Connectivity Values and Driving Scores')
############ SWADTF at mark #########################################
def _swadtf_mark():
print(eeg.fig.mne.info['bads'])
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info['ch_names']) or len(eeg.fig.mne.info['ch_names'])-len(eeg.fig.mne.info['bads'])<2:
message_box(eeg.root,'Error: At least two channels must be selected!',type='error')
return
if eeg.fig.mne.segment_loc==0:
message_box(eeg.root,'Error: No segment marked in raw data!',type='error')
return
swadtf_at_mark(eeg)
######### connection reinforcements for above _swadtf
def _connection_reinforcements():
eeg.start_time = eeg.fig.mne.t_start - eeg.fig.mne.first_time
eeg.end_time = eeg.start_time + eeg.fig.mne.duration
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info["ch_names"]) or len(eeg.fig.mne.info['ch_names'])-len(eeg.fig.mne.info['bads'])<2:
mmessage_box(eeg.root,'Error: At least two channels must be selected!',type='error')
return
if eeg.end_time-eeg.start_time<eeg.conn_win_value:
message_box(eeg.root,'Error: Selected interval is less than subwindow size from connectivity options! Expand interval!',type='error')
return
if eeg.end_time-eeg.start_time>120:
message_box(eeg.root,'Error: Connectivity animation limited to 120 seconds. Decrease interval length!',type='error')
return
plot_selected(root,eeg,annotations=False)
X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
data=X.T
conn_win_value= eeg.conn_win_value #max(5/(eeg.fmin_value*2),eeg.conn_win_value)
M_list,bar_list=effective_connectivity(root,data,channels,fs,conn_win_value,type='swdtf',fmin=eeg.fmin_value,fmax=eeg.fmax_value)
conn_matrix,conns=conn_count(M_list,channels,cutoff=eeg.conns_percentile)
bar_plot_connection_reinforcements(conns,channels,num_bars=8,title='Connection Reinforcements',sort=True)
############### HFO COMPUTATION FUNCTIONS ############################
#######################################################################
################ RMS METHOD ######################################
##### Use eeg.plot_start and eeg.plot_duration below? Then cut out eeg.start_time,eeg.end_time
def _hfos():
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info["ch_names"]):
message_box(eeg.root,'Error: At least two channels must be selected!',type='error')
return
if eeg.fs<250:
warning_box(eeg.root,'Warning: Sampling frequency of '+str(eeg.fs)+' Hz is small. Results may not be reliable',type='warning')
########## PUT THE ENTIRE WARNING FUNCTION CODE IN HERE; IF cancel button pressed, just return from this function; if proceed_button PRESSED
############## JUST CLOSE THE WARNING WINDOW
plot_selected(root,eeg,annotations=False)
X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
hp = 80
lp = int(.9*fs/2)
counts=hfo_count(root,eeg,X,fs,channels,hp,lp, method='rms')
if len(counts)==0:
message_box(eeg.root,'Error: No HFOs found on chosen interval',type='error')
filemenu.entryconfig("Open",state="normal")
return
bar_plot_with_slider_rms(counts,channels,num_bars=35,title='HFO Counts')
filemenu.entryconfig("Open",state="normal")
################ BURNOS METHOD ####################################
def _hfos_burnos():
if set(eeg.fig.mne.info['bads'])==set(eeg.fig.mne.info["ch_names"]):
message_box(eeg.root,'Error: At least two channels must be selected!',type='error')
return
########## PUT THE ENTIRE WARNING FUNCTION CODE IN BELOW; IF cancel button pressed, just return from this function; if proceed_button PRESSED
############## JUST CLOSE THE WARNING WINDOW; make sure to filemenu.entryconfig("Open",state="normal")
if eeg.fs<250:
decision=warning_box(eeg.root,'Warning: Sampling frequency of '+str(eeg.fs)+' Hz is small. Results may not be reliable',type='warning')
print('DECISION **********************')
print(decision)
if decision=='cancel':
return
plot_selected(root,eeg,annotations=False)
X,fs,channels=get_data(eeg,eeg.raw,eeg.fig,eeg.start_time,eeg.end_time)
start_time=eeg.start_time
hp = 80
lp = int(.9*fs/2)
df_combined_results, counts=HFOs_all_channels(root,eeg,X,channels,hp,lp,fs)
if df_combined_results.empty:
message_box(eeg.root,'Error: No HFOs found on chosen interval',type='error')
filemenu.entryconfig("Open",state="normal")
return
bar_plot_with_slider_burnos(df_combined_results,counts,channels,num_bars=35)
filemenu.entryconfig("Open",state="normal")
############# MENUS ##################
######################################
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=_open)
# filemenu.add_command(label="Quit", command=quit)
menubar.add_cascade(label="File", menu=filemenu)
view_menu=Menu(menubar,tearoff=0)
view_menu.add_command(label='Plot Selected', command=_plot_selected)
view_menu.entryconfig('Plot Selected',state='disabled')
view_menu.add_command(label='Select All',command=_select_all_channels)
view_menu.entryconfig('Select All',state='disabled')
view_menu.add_command(label="Refresh", command=_refresh)
view_menu.entryconfig('Refresh',state='disabled')
menubar.add_cascade(label='Plot',menu=view_menu)
preprocess_menu=Menu(menubar,tearoff=0)
preprocess_menu.add_command(label="Remove Line Noise", command=_remove_line_noise)
preprocess_menu.entryconfig('Remove Line Noise',state='disabled')
preprocess_menu.add_command(label='Plot Independent Sources',command=_plot_sources)
preprocess_menu.entryconfig('Plot Independent Sources',state='disabled')
menubar.add_cascade(label='Data',menu=preprocess_menu)
hfo_menu=Menu(menubar,tearoff=0)
hfo_menu.add_command(label='Root Mean Square', command=_hfos)
hfo_menu.entryconfig('Root Mean Square',state='disabled')
hfo_menu.add_command(label='Hilbert-Stockwell', command=_hfos_burnos)
hfo_menu.entryconfig('Hilbert-Stockwell',state='disabled')
menubar.add_cascade(label='HFOs', menu=hfo_menu)
conn_menu=Menu(menubar,tearoff=0)
conn_menu.add_command(label="Connectivity Options", command=_conn_options)
conn_menu.entryconfig('Connectivity Options',state='disabled')
conn_menu.add_command(label='Coherence', command=_coh)
conn_menu.entryconfig('Coherence',state='disabled')
conn_menu.add_command(labe='Coherence at Mark',command=_coh_mark)
conn_menu.entryconfig('Coherence at Mark',state='disabled')
conn_menu.add_command(label='Coherence Similarity States', command=_coh_time_communities)
conn_menu.entryconfig('Coherence Similarity States',state='disabled')
conn_menu.add_command(label='Directed Connectivity', command=_swadtf)
conn_menu.entryconfig('Directed Connectivity',state='disabled')
conn_menu.add_command(label='Directed Connectivity at Mark', command=_swadtf_mark)
conn_menu.entryconfig('Directed Connectivity at Mark',state='disabled')
conn_menu.add_command(label='Reinforcement Connections', command=_connection_reinforcements)
conn_menu.entryconfig('Reinforcement Connections',state='disabled')
menubar.add_cascade(label='Connectivity', menu=conn_menu)
license_menu=Menu(menubar,tearoff=0)
license_menu.add_command(label='Video Tutorial', command=start_video_tutorial)
license_menu.add_command(label='Resources', command=program_info_window)
menubar.add_cascade(label='Help', menu=license_menu)
def disable_menu_pulldowns():
for menu in (filemenu,view_menu,preprocess_menu,conn_menu,hfo_menu): # final version add hfo_menu here
for index in range(menu.index('end')+1):
menu.entryconfigure(index, state="disable")
return
def enable_menu_pulldowns():
for menu in (filemenu,view_menu,preprocess_menu,conn_menu,hfo_menu):
for index in range(menu.index('end')+1):
menu.entryconfigure(index, state="normal")
return
root.config(menu=menubar)
def message_box(root, message,type='error'):
disable_menu_pulldowns()
decision='proceed'
message_window = Toplevel(root)
message_window.geometry(str(14*len(message))+'x90')
message_window.attributes('-topmost',1)
message_window.wm_title('')
def button_press():
message_window.destroy()
enable_menu_pulldowns()
message_label=Label(message_window,fg='red',image = '::tk::icons::'+type)
message_label.pack()
message_text=Label(message_window,fg='red',text=message,font=("Arial Bold", 14))
message_text.pack()
message_button = Message_Button(message_window, text='OK', bg='blue',fg='yellow',command=button_press)
message_button.pack()
def warning_box(root, message,type='warning'):
disable_menu_pulldowns()
decision='proceed'
warning_window = Toplevel(root)
warning_window.geometry(str(14*len(message))+'x120')
warning_window.attributes('-topmost',1)
warning_window.wm_title('')
print('Decision inside main warning_box: '+decision)
def proceed_button_press():
decision='proceed'
warning_window.destroy()
enable_menu_pulldowns()
print('Decision inside proceed: '+decision)
return decision
def cancel_button_press():
decision='cancel'
warning_window.destroy()
enable_menu_pulldowns()
print('Decision inside cancel: '+decision)
return decision
warning_label=Label(warning_window,fg='red',image = '::tk::icons::'+type)
warning_label.pack()
warning_text=Label(warning_window,fg='red',text=message,font=("Arial Bold", 14))
warning_text.pack()
proceed_button = Message_Button(warning_window, text='Continue', bg='blue',fg='yellow',command=proceed_button_press)
proceed_button.pack()
cancel_button = Message_Button(warning_window, text='Cancel', bg='blue',fg='yellow',command=cancel_button_press)
cancel_button.pack()
return
root.mainloop()