-
Notifications
You must be signed in to change notification settings - Fork 3
/
langouEEG.py
1125 lines (1071 loc) · 46.5 KB
/
langouEEG.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
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
import os
import numpy as np
import mne
from matplotlib import pyplot as plt
from matplotlib.pyplot import MultipleLocator
import pandas as pd
from mne.time_frequency import tfr_morlet, psd_multitaper, psd_welch
from copy import deepcopy
from mne.preprocessing import create_ecg_epochs, create_eog_epochs, read_ica
import sys
from tensorpac import Pac, EventRelatedPac, PreferredPhase
from tensorpac.utils import PeakLockedTF, PSD, ITC, BinAmplitude
from scipy.integrate import simps
import os.path as op
import matplotlib.pyplot as plt
from scipy.stats import *
from eeg_microstates3 import *
import time
import pickle
from mne.datasets import sample
from mne.datasets import fetch_fsaverage
from mne.viz import plot_topomap
from mpl_toolkits.mplot3d import Axes3D # noqa
import logging
from lempel_ziv_complexity import lempel_ziv_complexity
#mne.utils.set_config('MNE_USE_CUDA', 'true')
dataRoot = "/data/home/viscent/Light"
# dataRoot = sys.path[0]
def init_prog():
global ratio_TD_all_r, ratio_TU_all_r, ratio_DU_all_r
ratio_TD_all_r, ratio_TU_all_r, ratio_DU_all_r = [],[],[]
global ratio_TD_all_f, ratio_TU_all_f, ratio_DU_all_f
ratio_TD_all_f, ratio_TU_all_f, ratio_DU_all_f = [],[],[]
global ratioMA_TD_all_r, ratioMA_TU_all_r, ratioMA_DU_all_r
ratioMA_TD_all_r, ratioMA_TU_all_r, ratioMA_DU_all_r = [],[],[]
global ratioMA_TD_all_f, ratioMA_TU_all_f, ratioMA_DU_all_f
ratioMA_TD_all_f, ratioMA_TU_all_f, ratioMA_DU_all_f = [],[],[]
global rel_power_R, rel_power_F, subj_number_R, subj_number_F, rel_power_R_ran, rel_power_F_ran
rel_power_R, rel_power_F, subj_number_R, subj_number_F, rel_power_R_ran, rel_power_F_ran = [],[],[],[],[],[]
return
def save_ratios(filefolder = dataRoot + '/Light'):
global ratio_TD_all_r, ratio_TU_all_r, ratio_DU_all_r
global ratio_TD_all_f, ratio_TU_all_f, ratio_DU_all_f
global ratioMA_TD_all_r, ratioMA_TU_all_r, ratioMA_DU_all_r
global ratioMA_TD_all_f, ratioMA_TU_all_f, ratioMA_DU_all_f
df_r = pd.DataFrame()
df_r['ratio_TD'] = ratio_TD_all_r
df_r['ratio_TU'] = ratio_TU_all_r
df_r['ratio_DU'] = ratio_DU_all_r
df_r.to_csv(filefolder + '/ratios_rest.csv')
df_f = pd.DataFrame()
df_f['ratio_TD'] = ratio_TD_all_f
df_f['ratio_TU'] = ratio_TU_all_f
df_f['ratio_DU'] = ratio_DU_all_f
df_f.to_csv(filefolder + '/ratios_flicker.csv')
dfMA_r = pd.DataFrame()
dfMA_r['ratio_TD'] = ratioMA_TD_all_r
dfMA_r['ratio_TU'] = ratioMA_TU_all_r
dfMA_r['ratio_DU'] = ratioMA_DU_all_r
dfMA_r.to_csv(filefolder + '/ratiosMA_rest.csv')
dfMA_f = pd.DataFrame()
dfMA_f['ratio_TD'] = ratioMA_TD_all_f
dfMA_f['ratio_TU'] = ratioMA_TU_all_f
dfMA_f['ratio_DU'] = ratioMA_DU_all_f
dfMA_f.to_csv(filefolder + '/ratiosMA_flicker.csv')
return
def csv_transformat(filefolder = dataRoot + '/Light', type='flicker'):
filefolder = dataRoot + '/Light'
df=pd.read_csv(filefolder + '/ratios_{0}.csv'.format(type))
shape = df.shape
print(shape)
rows = shape[0]
column = shape[1]
col_names = df.columns.values.tolist()
ratios = []
labels = []
print(col_names)
for i in range(1, column):
ratio = df.iloc[0:rows,i].tolist()
type_label = [col_names[i]] * len(ratio)
ratios += ratio
labels += type_label
save = pd.DataFrame()
save['ratios'] = ratios
save['labels'] = labels
save.to_csv(filefolder + '/ratios_{0}_all.csv'.format(type))
print('{0} CSV transformation complete'.format(type))
return
def csv_transformat_MA(filefolder = dataRoot + '/Light', type='flicker'):
filefolder = dataRoot + '/Light'
df=pd.read_csv(filefolder + '/ratiosMA_{0}.csv'.format(type))
shape = df.shape
print(shape)
rows = shape[0]
column = shape[1]
col_names = df.columns.values.tolist()
ratios = []
labels = []
print(col_names)
for i in range(1, column):
ratio = df.iloc[0:rows,i].tolist()
type_label = [col_names[i]] * len(ratio)
ratios += ratio
labels += type_label
save = pd.DataFrame()
save['ratios'] = ratios
save['labels'] = labels
save.to_csv(filefolder + '/ratiosMA_{0}_all.csv'.format(type))
print('{0} CSV transformation complete'.format(type))
return
def initData(subject_name,picks_str=['O1','O2','OZ']):
file_path = dataRoot+'/Light/'+subject_name + " Data.cnt"
print(dataRoot)
print(file_path)
raw = mne.io.read_raw_cnt(file_path, preload=True)
# picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=True, exclude='bads')
picks = mne.pick_channels(raw.info["ch_names"], picks_str)
raw = raw.set_channel_types({'Trigger':'stim','VEO':'eog'})
if not os.path.exists(dataRoot+('/Light')):
os.mkdir('Light')
return raw,picks,picks_str
def initData_clean(subject_name,picks_str=['O1','O2','OZ']):
file_path = dataRoot+'/Light/'+subject_name + " Data.cnt"
print(dataRoot)
print(file_path)
raw = mne.io.read_raw_cnt(file_path, preload=True)
# picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=True, exclude='bads')
picks = picks_str
if not os.path.exists(dataRoot+('/Light')):
os.mkdir('Light')
return raw,picks,picks_str
def initLayout(raw):
# layout = pd.read_csv(dataRoot + '/channel_dict.txt', sep = '\t')
# layout.columns = layout.columns.str.strip()
# layout["labels"] = layout["labels"].str.strip()
# layout = layout.set_index('labels')
# layout = layout.to_dict(orient = "index")
# for channel in layout.keys():
# yxz = np.array([layout[channel]["Y"], layout[channel]["X"], layout[channel]["Z"]])
# layout[channel] = yxz
# layout = mne.channels.make_dig_montage(layout, coord_frame='head')
# mne.viz.plot_montage(layout)
# raw.set_montage(layout)
quikMontage = mne.channels.read_custom_montage(os.path.join(dataRoot,'LangouEEG','quikCap.elc'))
raw = raw.copy().set_montage(quikMontage)
return raw
def extractEvents(raw):
# cnt file describe
# print("file info:")
# print(raw.info)
# print("channel names:")
# print(raw.info["ch_names"])
# print("time period:")
# print(raw.n_times)
# #print("time points:")
# #print(raw.times)
# print("events:")
events, event_dict = mne.events_from_annotations(raw)
# event_dict = {'random_flicker-60s':1, 'random_rest-300s':2, '40Hz_rest-300s':3, '40Hz_flicker-60s':4}
print(event_dict)
return events, event_dict
def filterRaw(raw,picks, ref_set_average=False, ref_channels=['M1', 'M2']):
raw = raw.filter(0.1, None, fir_design='firwin')
if ref_set_average:
# 可以考虑使用所有通道的平均值作为参考
raw = raw.copy().set_eeg_reference(ref_channels='average', projection=True)
else:
# 使用特定的参考电极
raw = raw.copy().set_eeg_reference(ref_channels=ref_channels, projection=True)
raw = raw.apply_proj()
raw = raw.notch_filter(freqs=50,method='spectrum_fit')
# raw.plot_psd(area_mode='range', tmax=10.0, picks=picks, average=False)
return raw
def dbgPlot(raw):
# Plot raw data
img_raw_psd = raw.plot_psd()
# Print
scale = dict(mag=1e-12, grad=4e-11, eeg=128e-6, eog=150e-6, ecg=5e-4,
emg=1e-3, ref_meg=1e-12, misc=1e-3, stim=1,
resp=1, chpi=1e-4, whitened=1e2)
# Set fig size
img_raw_plot = raw.plot(duration = 40, n_channels=65, scalings=scale,start=288)
img_raw_plot.set_size_inches([20,20])
# img_raw_plot.savefig(dataRoot + '/img_raw_plot3.png', dpi=300)
def runICA(raw):
# set up and fit the ICA
ica = mne.preprocessing.ICA(n_components=20, random_state=0)
ica.fit(raw)
# ica.plot_components()
bad_ica = ica.detect_artifacts(raw).exclude
raw = ica.apply(raw.copy(), exclude=bad_ica)
return raw
def extractEpochsBlind(raw,events,picks,tmin_rest = -20,tmax_rest = -10,tmin_flick = 3,tmax_flick = 30):
# Get epoch for each event
custom_event_ids = {'LB':5, 'RB':7,'4F':9,'RF':12,'4R':8,'RR':11}
events, event_dict = mne.events_from_annotations(raw)
print(event_dict)
tmin_rest = tmin_rest
tmax_rest = tmax_rest
tmin_flick = tmin_flick
tmax_flick = tmax_flick
reject=dict()
## Epoch: Random flicker
event_id = custom_event_ids['RB']
event_id = event_dict[str(event_id)]
tmin = tmin_flick
tmax = tmax_flick
epoch_RF = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject, baseline=(tmin_flick, tmin_flick), preload=True)
evoked_RF = epoch_RF.average()
#evoked_RF.plot(time_unit='s')
## Epoch: Random rest
event_id = custom_event_ids['RB']
event_id = event_dict[str(event_id)]
tmin = tmin_rest
tmax = tmax_rest
epoch_RR = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject, baseline=(tmin_rest, tmin_rest), preload=True)
evoked_RR = epoch_RR.average()
#evoked_RR.plot(time_unit='s')
## Epoch: 40 Hz rest
event_id = custom_event_ids['LB']
event_id = event_dict[str(event_id)]
tmin = tmin_rest
tmax = tmax_rest
epoch_4R = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject, baseline=(tmin_rest, tmin_rest), preload=True)
#epoch_4R.drop([0,1])
evoked_4R = epoch_4R.average()
#evoked_4R.plot(time_unit='s')
## Epoch: 40 Hz flicker
event_id = custom_event_ids['LB']
event_id = event_dict[str(event_id)]
tmin = tmin_flick
tmax = tmax_flick
epoch_4F = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject,baseline=(tmin_flick, tmin_flick), preload=True)
epoch_4F_all = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
baseline=(tmin_flick, tmin_flick), preload=True,
reject=dict())
#epoch_4F.drop([0,1])
#epoch_4F_all.drop([0,1])
evoked_4F = epoch_4F.average()
#evoked_4F.plot(time_unit='s')
return epoch_RR,epoch_RF,epoch_4R,epoch_4F
def extractEpochs(raw,events,picks,tmin_rest = -30,tmax_rest = -20,tmin_flick = 10,tmax_flick = 20):
# Get epoch for each event
custom_event_ids = {'LB':5, 'RB':7,'4F':9,'RF':12,'4R':8,'RR':11}
tmin_rest = tmin_rest
tmax_rest = tmax_rest
tmin_flick = tmin_flick
tmax_flick = tmax_flick
reject=dict()
## Epoch: Random flicker
events, event_dict = mne.events_from_annotations(raw)
event_id = event_dict[str(custom_event_ids['RF'])]
tmin = tmin_flick
tmax = tmax_flick
epoch_RF = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject, baseline=(tmin, tmax), preload=True)
evoked_RF = epoch_RF.average()
#evoked_RF.plot(time_unit='s')
## Epoch: Random rest
event_id = event_dict[str(custom_event_ids['RF'])]
tmin = tmin_rest
tmax = tmax_rest
epoch_RR = mne.Epochs(raw, events, event_id, tmin_rest, tmax_rest, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
evoked_RR = epoch_RR.average()
#evoked_RR.plot(time_unit='s')
## Epoch: 40 Hz rest
event_id = event_dict[str(custom_event_ids['4F'])]
tmin = tmin_rest
tmax = tmax_rest
epoch_4R = mne.Epochs(raw, events, event_id, tmin_rest, tmax_rest, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
#epoch_4R.drop([0,1])
evoked_4R = epoch_4R.average()
#evoked_4R.plot(time_unit='s')
## Epoch: 40 Hz flicker
event_id = event_dict[str(custom_event_ids['4F'])]
tmin = tmin_flick
tmax = tmax_flick
epoch_4F = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject,baseline=(tmin_flick, tmin_flick), preload=True)
epoch_4F_all = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
baseline=(tmin_flick, tmin_flick), preload=True,
reject=dict())
#epoch_4F.drop([0,1])
#epoch_4F_all.drop([0,1])
evoked_4F = epoch_4F.average()
#evoked_4F.plot(time_unit='s')
return epoch_RR,epoch_RF,epoch_4R,epoch_4F
def extractEpochs_id(raw,events,picks,tmin_rest = 60,tmax_rest = 120,tmin_flick = 10,tmax_flick = 20):
# Get epoch for each event
custom_event_ids = {'LB':5, 'RB':7,'4F':9,'RF':12,'4R':8,'RR':11}
tmin_rest = tmin_rest
tmax_rest = tmax_rest
tmin_flick = tmin_flick
tmax_flick = tmax_flick
reject=dict()
## Epoch: Random flicker
events, event_dict = mne.events_from_annotations(raw)
event_id = event_dict[str(custom_event_ids['RF'])]
tmin = tmin_flick
tmax = tmax_flick
epoch_RF = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject, baseline=(tmin_flick, tmin_flick), preload=True)
evoked_RF = epoch_RF.average()
#evoked_RF.plot(time_unit='s')
## Epoch: Random rest
event_id = event_dict[str(custom_event_ids['RR'])]
tmin = tmin_rest
tmax = tmax_rest
epoch_RR = mne.Epochs(raw, events, event_id, tmin_rest, tmax_rest, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
evoked_RR = epoch_RR.average()
#evoked_RR.plot(time_unit='s')
## Epoch: 40 Hz rest
event_id = event_dict[str(custom_event_ids['4R'])]
tmin = tmin_rest
tmax = tmax_rest
epoch_4R = mne.Epochs(raw, events, event_id, tmin_rest, tmax_rest, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
#epoch_4R.drop([0,1])
evoked_4R = epoch_4R.average()
#evoked_4R.plot(time_unit='s')
## Epoch: 40 Hz flicker
event_id = event_dict[str(custom_event_ids['4F'])]
tmin = tmin_flick
tmax = tmax_flick
epoch_4F = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject,baseline=(tmin_flick, tmin_flick), preload=True)
epoch_4F_all = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
baseline=(tmin_flick, tmin_flick), preload=True,
reject=dict())
#epoch_4F.drop([0,1])
#epoch_4F_all.drop([0,1])
evoked_4F = epoch_4F.average()
#evoked_4F.plot(time_unit='s')
return epoch_RR,epoch_RF,epoch_4R,epoch_4F
def extractEpochs_forall(raw,events,picks,tmin_rest = 60,tmax_rest = 120,tmin_flick = 10,tmax_flick = 20):
# Get epoch for each event
custom_event_ids = {'LB':5, 'RB':7,'4F':9,'RF':12,'4R':8,'RR':11}
tmin_rest = tmin_rest
tmax_rest = tmax_rest
tmin_flick = tmin_flick
tmax_flick = tmax_flick
reject=dict()
## Epoch: Random flicker
events, event_dict = mne.events_from_annotations(raw)
event_id = event_dict[str(custom_event_ids['RF'])]
tmin = tmin_flick
tmax = tmax_flick
epoch_RF = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
evoked_RF = epoch_RF.average()
#evoked_RF.plot(time_unit='s')
## Epoch: Random rest
event_id = event_dict[str(custom_event_ids['RR'])]
tmin = tmin_rest
tmax = tmax_rest
epoch_RR = mne.Epochs(raw, events, event_id, tmin_rest, tmax_rest, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
evoked_RR = epoch_RR.average()
#evoked_RR.plot(time_unit='s')
## Epoch: 40 Hz rest
event_id = event_dict[str(custom_event_ids['4R'])]
tmin = tmin_rest
tmax = tmax_rest
epoch_4R = mne.Epochs(raw, events, event_id, tmin_rest, tmax_rest, proj=True,
picks=picks,reject=reject, baseline=None, preload=True)
#epoch_4R.drop([0,1])
evoked_4R = epoch_4R.average()
#evoked_4R.plot(time_unit='s')
## Epoch: 40 Hz flicker
event_id = event_dict[str(custom_event_ids['4F'])]
tmin = tmin_flick
tmax = tmax_flick
epoch_4F = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
picks=picks,reject=reject,baseline=None, preload=True)
epoch_4F_all = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
baseline=None, preload=True,
reject=dict())
#epoch_4F.drop([0,1])
#epoch_4F_all.drop([0,1])
evoked_4F = epoch_4F.average()
#evoked_4F.plot(time_unit='s')
return epoch_RR,epoch_RF,epoch_4R,epoch_4F
def doMA(psds,n=20):
for i in range(psds.shape[0]):
tempSum=0
for j in range(int(n)):
if j+i < psds.shape[0]:
tempSum+=psds[j+i]
else:
n=j
break
psds[i]=tempSum/n
return psds
def doMA3D(psds):
for i in range(psds.shape[0]):
for j in range(psds.shape[1]):
psds[i][j] = doMA(psds[i][j])
return psds
def snr_spectrum(psd, noise_n_neighbor_freqs=1, noise_skip_neighbor_freqs=1):
"""Compute SNR spectrum from PSD spectrum using convolution.
Parameters
----------
psd : ndarray, shape ([n_trials, n_channels,] n_frequency_bins)
Data object containing PSD values. Works with arrays as produced by
MNE's PSD functions or channel/trial subsets.
noise_n_neighbor_freqs : int
Number of neighboring frequencies used to compute noise level.
increment by one to add one frequency bin ON BOTH SIDES
noise_skip_neighbor_freqs : int
set this >=1 if you want to exclude the immediately neighboring
frequency bins in noise level calculation
Returns
-------
snr : ndarray, shape ([n_trials, n_channels,] n_frequency_bins)
Array containing SNR for all epochs, channels, frequency bins.
NaN for frequencies on the edges, that do not have enough neighbors on
one side to calculate SNR.
"""
# Construct a kernel that calculates the mean of the neighboring
# frequencies
averaging_kernel = np.concatenate((
np.ones(noise_n_neighbor_freqs),
np.zeros(2 * noise_skip_neighbor_freqs + 1),
np.ones(noise_n_neighbor_freqs)))
averaging_kernel /= averaging_kernel.sum()
# Calculate the mean of the neighboring frequencies by convolving with the
# averaging kernel.
mean_noise = np.apply_along_axis(
lambda psd_: np.convolve(psd_, averaging_kernel, mode='valid'),
axis=-1, arr=psd
)
# The mean is not defined on the edges so we will pad it with nas. The
# padding needs to be done for the last dimension only so we set it to
# (0, 0) for the other ones.
edge_width = noise_n_neighbor_freqs + noise_skip_neighbor_freqs
pad_width = [(0, 0)] * (mean_noise.ndim - 1) + [(edge_width, edge_width)]
mean_noise = np.pad(
mean_noise, pad_width=pad_width, constant_values=np.nan
)
return psd / mean_noise
def add_arrows(axes):
# add some arrows at 60 Hz and its harmonics
for ax in axes:
freqs = ax.lines[-1].get_xdata()
psds = ax.lines[-1].get_ydata()
freq = 40
idx = np.searchsorted(freqs, freq)
# get ymax of a small region around the freq. of interest
y = psds[(idx - 4):(idx + 5)].max()
ax.arrow(x=freqs[idx], y=y + 18, dx=0, dy=-12, color='red',
width=0.1, head_width=3, length_includes_head=True)
def specPlot(epochs):
# MA: 是否做滑动平均
for epoch in epochs:
epoch.plot_psd(fmin=0.1, fmax=100., average=True, spatial_colors=False)
def plot_psd_sub(epoch,ax, fmin=.1, fmax=100, n_jobs=8, color='k', alpha=.5, label='Default', isma=False):
psds, freqs = psd_multitaper(epoch, fmin=fmin, fmax=fmax, n_jobs=n_jobs)
# MA: 是否做滑动平均
if isma:
psds = doMA3D(psds)
psds = 10. * np.log10(psds)
psds_mean = psds.mean(0).mean(0)
psds_std = psds.mean(0).std(0)
ax.plot(freqs, psds_mean, color=color, label = label)
ax.fill_between(freqs, psds_mean - psds_std, psds_mean + psds_std,
color=color, alpha=alpha)
ax.legend()
def superposGamma(epoch_4R,epoch_4F,epoch_RF,subject_name,MA=False):
fmin = 35
fmax = 45
alpha = .2
f, ax = plt.subplots(figsize=(15,7))
if MA:
plot_psd_sub(ax=ax,epoch = epoch_4R, color='y', fmin=fmin, fmax=fmax, alpha=alpha, label='Rest State', isma=True)
plot_psd_sub(ax=ax,epoch = epoch_4F, color='k', fmin=fmin, fmax=fmax, alpha=alpha, label='40 Hz Light Stimulation', isma=True)
plot_psd_sub(ax=ax,epoch = epoch_RF, color='r', fmin=fmin, fmax=fmax, alpha=alpha, label='Random Hz Light Stimulation', isma=True)
else:
plot_psd_sub(ax=ax,epoch = epoch_4R, color='y', fmin=fmin, fmax=fmax, alpha=alpha, label='Rest State')
plot_psd_sub(ax=ax,epoch = epoch_4F, color='k', fmin=fmin, fmax=fmax, alpha=alpha, label='40 Hz Light Stimulation')
plot_psd_sub(ax=ax,epoch = epoch_RF, color='r', fmin=fmin, fmax=fmax, alpha=alpha, label='Random Hz Light Stimulation')
plt.xlabel("Frequency")
plt.ylabel("Power spectral density (PSD) in log")
plt.savefig(dataRoot + '/Light/Light_figures_non/' + subject_name + '_35_45.png')
def superposFull(epoch_4R,epoch_4F,epoch_RF,subject_name,MA=False):
fmin = 0
fmax = 120
alpha = .2
f, ax = plt.subplots(figsize=(15,7))
if MA:
plot_psd_sub(ax=ax,epoch = epoch_4R, color='y', fmin=fmin, fmax=fmax, alpha=alpha, label='Rest State', isma=True)
plot_psd_sub(ax=ax,epoch = epoch_4F, color='k', fmin=fmin, fmax=fmax, alpha=alpha, label='40 Hz Light Stimulation', isma=True)
plot_psd_sub(ax=ax,epoch = epoch_RF, color='r', fmin=fmin, fmax=fmax, alpha=alpha, label='Random Hz Light Stimulation', isma=True)
else:
plot_psd_sub(ax=ax,epoch = epoch_4R, color='y', fmin=fmin, fmax=fmax, alpha=alpha, label='Rest State')
plot_psd_sub(ax=ax,epoch = epoch_4F, color='k', fmin=fmin, fmax=fmax, alpha=alpha, label='40 Hz Light Stimulation')
plot_psd_sub(ax=ax,epoch = epoch_RF, color='r', fmin=fmin, fmax=fmax, alpha=alpha, label='Random Hz Light Stimulation')
plt.xlabel("Frequency")
plt.ylabel("Power spectral density (PSD) in log")
plt.savefig(dataRoot + '/Light/Light_figures_non/' + subject_name + '_0_120.png')
def superpos85(epoch_4R,epoch_4F,epoch_RF,subject_name,MA=False):
fmin = 40
fmax = 85
alpha = .2
f, ax = plt.subplots(figsize=(15,7))
if MA:
plot_psd_sub(ax=ax,epoch = epoch_4R, color='y', fmin=fmin, fmax=fmax, alpha=alpha, label='Rest State', isma=True)
plot_psd_sub(ax=ax,epoch = epoch_4F, color='k', fmin=fmin, fmax=fmax, alpha=alpha, label='40 Hz Light Stimulation', isma=True)
plot_psd_sub(ax=ax,epoch = epoch_RF, color='r', fmin=fmin, fmax=fmax, alpha=alpha, label='Random Hz Light Stimulation', isma=True)
else:
plot_psd_sub(ax=ax,epoch = epoch_4R, color='y', fmin=fmin, fmax=fmax, alpha=alpha, label='Rest State')
plot_psd_sub(ax=ax,epoch = epoch_4F, color='k', fmin=fmin, fmax=fmax, alpha=alpha, label='40 Hz Light Stimulation')
plot_psd_sub(ax=ax,epoch = epoch_RF, color='r', fmin=fmin, fmax=fmax, alpha=alpha, label='Random Hz Light Stimulation')
plt.xlabel("Frequency")
plt.ylabel("Power spectral density (PSD) in log")
plt.savefig(dataRoot + '/Light/Light_figures_non/' + subject_name + '_40_85.png')
def getRatio_rest(epoch,fmin=35.0,fmax=45.0,picks=['O1', 'OZ', 'O2']):
psds, freqs = psd_multitaper(epoch,fmin=fmin, fmax=fmax, n_jobs=8,picks=picks)
# psd.shape: (number of epoch, number of channel, frequency)
f_down = 35.0
f_low = 39.0
f_high = 41.0
f_upstream = 45.0
print(psds.shape)
num_of_epoch = psds.shape[0]
num_of_channel = psds[0].shape[0]
print("{0} epochs in total".format(num_of_epoch))
print("{0} channels in total".format(num_of_channel))
print(psds.shape)
# average all channels
psds = np.mean(psds, axis=1)
# extract power in selected frequency bands
downstream_mean_power, target_mean_power, upstream_mean_power = [],[],[]
for i in range(0, num_of_epoch):
a,b,c = [],[],[]
for j in range(0, len(freqs)):
freq = freqs[j]
power = psds[i][j]
if freq < f_low and freq > f_down:
a.append(power)
if freq < f_high and freq > f_low:
b.append(power)
if freq < f_upstream and freq > f_high:
c.append(power)
downstream_mean_power.append(np.max(a))
target_mean_power.append(np.max(b))
upstream_mean_power.append(np.max(c))
print(r'The downstream mean power is:')
print(downstream_mean_power)
print(r'The taget band mean power is:')
print(target_mean_power)
print(r'The upstream mean power is:')
print(upstream_mean_power)
ratio_TD = []
ratio_TU = []
ratio_DU = []
for i in range(0, len(target_mean_power)):
TD = target_mean_power[i]/downstream_mean_power[i]
TU = target_mean_power[i]/upstream_mean_power[i]
DU = downstream_mean_power[i]/upstream_mean_power[i]
ratio_TD.append(TD)
global ratio_TD_all_r
ratio_TD_all_r.append(TD)
ratio_TU.append(TU)
global ratio_TU_all_r
ratio_TU_all_r.append(TU)
ratio_DU.append(DU)
global ratio_DU_all_r
ratio_DU_all_r.append(DU)
print(r'The target/downstream is:')
print(ratio_TD)
print(r'The target/upstream is:')
print(ratio_TU)
print(r'The downstream/upstream is:')
print(ratio_DU)
return downstream_mean_power,target_mean_power,upstream_mean_power
def getRatio_flicker(epoch,fmin=35.0,fmax=45.0,picks=['O1', 'OZ', 'O2']):
psds, freqs = psd_multitaper(epoch,fmin=fmin, fmax=fmax, n_jobs=8,picks=picks)
# psd.shape: (number of epoch, number of channel, frequency)
f_down = 35.0
f_low = 39.0
f_high = 41.0
f_upstream = 45.0
print(psds.shape)
num_of_epoch = psds.shape[0]
num_of_channel = psds[0].shape[0]
print("{0} epochs in total".format(num_of_epoch))
print("{0} channels in total".format(num_of_channel))
print(psds.shape)
# average all channels
psds = np.mean(psds, axis=1)
# extract power in selected frequency bands
downstream_mean_power, target_mean_power, upstream_mean_power = [],[],[]
for i in range(0, num_of_epoch):
a,b,c = [],[],[]
for j in range(0, len(freqs)):
freq = freqs[j]
power = psds[i][j]
if freq < f_low and freq > f_down:
a.append(power)
if freq < f_high and freq > f_low:
b.append(power)
if freq < f_upstream and freq > f_high:
c.append(power)
downstream_mean_power.append(np.max(a))
target_mean_power.append(np.max(b))
upstream_mean_power.append(np.max(c))
print(r'The downstream mean power is:')
print(downstream_mean_power)
print(r'The taget band mean power is:')
print(target_mean_power)
print(r'The upstream mean power is:')
print(upstream_mean_power)
ratio_TD = []
ratio_TU = []
ratio_DU = []
for i in range(0, len(target_mean_power)):
TD = target_mean_power[i]/downstream_mean_power[i]
TU = target_mean_power[i]/upstream_mean_power[i]
DU = downstream_mean_power[i]/upstream_mean_power[i]
ratio_TD.append(TD)
global ratio_TD_all_f
ratio_TD_all_f.append(TD)
ratio_TU.append(TU)
global ratio_TU_all_f
ratio_TU_all_f.append(TU)
ratio_DU.append(DU)
global ratio_DU_all_f
ratio_DU_all_f.append(DU)
print(r'The target/downstream is:')
print(ratio_TD)
print(r'The target/upstream is:')
print(ratio_TU)
print(r'The downstream/upstream is:')
print(ratio_DU)
return downstream_mean_power,target_mean_power,upstream_mean_power
def get_minima(data):
center = np.argmax(data)
print(center)
min_right = data[center]
min_left = data[center]
for i in range(center, data.shape[0]-1):
if data[i+1] <= min_right:
min_right = data[i+1]
else:
break
for i in range(center, 0, -1):
if data[i-1] <= min_left:
min_left = data[i-1]
else:
break
return min_left, min_right
def getRatio_flicker_MA(epoch,fmin=38.0,fmax=42.0,picks=['O1', 'OZ', 'O2']):
psds, freqs = psd_multitaper(epoch,fmin=fmin, fmax=fmax, n_jobs=8,picks=picks)
# MA: 做滑动平均
psds = doMA3D(psds)
# psd.shape: (number of epoch, number of channel, frequency)
f_down = 35.0
f_low = 39.0
f_high = 41.0
f_upstream = 45.0
print(psds.shape)
num_of_epoch = psds.shape[0]
num_of_channel = psds[0].shape[0]
print("{0} epochs in total".format(num_of_epoch))
print("{0} channels in total".format(num_of_channel))
print(psds.shape)
# average all channels
psds = np.mean(psds, axis=1)
# extract power in selected frequency bands
downstream_mean_power, target_mean_power, upstream_mean_power = [],[],[]
for i in range(0, num_of_epoch):
a,c = get_minima(psds[i])
b = np.max(psds[i])
downstream_mean_power.append(a)
target_mean_power.append(b)
upstream_mean_power.append(c)
print(r'The downstream min power is:')
print(downstream_mean_power)
print(r'The taget band max power is:')
print(target_mean_power)
print(r'The upstream min power is:')
print(upstream_mean_power)
ratio_TD = []
ratio_TU = []
ratio_DU = []
for i in range(0, len(target_mean_power)):
TD = target_mean_power[i]/downstream_mean_power[i]
TU = target_mean_power[i]/upstream_mean_power[i]
DU = downstream_mean_power[i]/upstream_mean_power[i]
ratio_TD.append(TD)
global ratioMA_TD_all_f
ratioMA_TD_all_f.append(TD)
ratio_TU.append(TU)
global ratioMA_TU_all_f
ratioMA_TU_all_f.append(TU)
ratio_DU.append(DU)
global ratioMA_DU_all_f
ratioMA_DU_all_f.append(DU)
print(r'The target/downstream is:')
print(ratio_TD)
print(r'The target/upstream is:')
print(ratio_TU)
print(r'The downstream/upstream is:')
print(ratio_DU)
return downstream_mean_power,target_mean_power,upstream_mean_power
def getRatio_rest_MA(epoch,fmin=38.0,fmax=42.0,picks=['O1', 'OZ', 'O2']):
psds, freqs = psd_multitaper(epoch,fmin=fmin, fmax=fmax, n_jobs=8,picks=picks)
# MA: 做滑动平均
psds = doMA3D(psds)
# psd.shape: (number of epoch, number of channel, frequency)
f_down = 35.0
f_low = 39.0
f_high = 41.0
f_upstream = 45.0
print(psds.shape)
num_of_epoch = psds.shape[0]
num_of_channel = psds[0].shape[0]
print("{0} epochs in total".format(num_of_epoch))
print("{0} channels in total".format(num_of_channel))
print(psds.shape)
# average all channels
psds = np.mean(psds, axis=1)
# extract power in selected frequency bands
downstream_mean_power, target_mean_power, upstream_mean_power = [],[],[]
for i in range(0, num_of_epoch):
a,c = get_minima(psds[i])
b = np.max(psds[i])
downstream_mean_power.append(a)
target_mean_power.append(b)
upstream_mean_power.append(c)
print(r'The downstream min power is:')
print(downstream_mean_power)
print(r'The taget band max power is:')
print(target_mean_power)
print(r'The upstream min power is:')
print(upstream_mean_power)
ratio_TD = []
ratio_TU = []
ratio_DU = []
for i in range(0, len(target_mean_power)):
TD = target_mean_power[i]/downstream_mean_power[i]
TU = target_mean_power[i]/upstream_mean_power[i]
DU = downstream_mean_power[i]/upstream_mean_power[i]
ratio_TD.append(TD)
global ratioMA_TD_all_r
ratioMA_TD_all_r.append(TD)
ratio_TU.append(TU)
global ratioMA_TU_all_r
ratioMA_TU_all_r.append(TU)
ratio_DU.append(DU)
global ratioMA_DU_all_r
ratioMA_DU_all_r.append(DU)
print(r'The target/downstream is:')
print(ratio_TD)
print(r'The target/upstream is:')
print(ratio_TU)
print(r'The downstream/upstream is:')
print(ratio_DU)
return downstream_mean_power,target_mean_power,upstream_mean_power
def calc_abs_power_simp(epoch, fmin, fmax, fbottom, ftop, sfreq):
epoch = np.asarray(epoch)
band_power_all = []
# psds, freqs = mne.time_frequency.psd_array_multitaper(epoch, n_jobs=8, sfreq=500.0)
psds, freqs = mne.time_frequency.psd_array_multitaper(epoch, fmin=fbottom, fmax=ftop, n_jobs=8, sfreq=500.0)
# Average all channels
psds = np.mean(psds, axis=1)
# Resolution of calculation
freq_res = freqs[1] - freqs[0]
# Select frequency band
band = [fmin, fmax]
idx_band = ((freqs >= band[0]) & (freqs <= band[1]))
# print(freqs.shape)
# print(psds.shape)
# integrate the PSD
num = (fmax-fmin)/freq_res
for i in range(psds.shape[0]):
band_power = simps(psds[i][idx_band], dx=freq_res)
band_power_all.append(band_power)
# band_power_all.append(band_power/(num))
# print(band)
# print(band_power_all)
return band_power_all
def cal_abs_power_allch(epoch, fmin, fmax, fbottom, ftop, sfreq):
epoch = np.asarray(epoch)
band_power_all = []
# psds, freqs = mne.time_frequency.psd_array_multitaper(epoch, n_jobs=8, sfreq=500.0)
psds, freqs = mne.time_frequency.psd_array_multitaper(epoch, fmin=fbottom, fmax=ftop, n_jobs=8, sfreq=500.0)
# Average all channels
psds = np.mean(psds, axis=0)
# Resolution of calculation
freq_res = freqs[1] - freqs[0]
# Select frequency band
band = [fmin, fmax]
idx_band = ((freqs >= band[0]) & (freqs <= band[1]))
# print(freqs.shape)
# print(psds.shape)
# integrate the PSD
num = (fmax-fmin)/freq_res
for i in range(psds.shape[0]):
band_power = simps(psds[i][idx_band], dx=freq_res)
band_power_all.append(band_power)
# band_power_all.append(band_power/(num))
# print(band)
# print(band_power_all)
return band_power_all
def get_all_abs_power(epoch_R, epoch_F, f_bottom = 35.0, f_low = 39.0, f_high = 41.0, f_top = 50.0, sfreq=500.0):
power_all_R = calc_abs_power_simp(epoch=epoch_R, fmin=f_bottom, fmax=f_top, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
power_R = calc_abs_power_simp(epoch=epoch_R, fmin=f_low, fmax=f_high, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
power_all_F = calc_abs_power_simp(epoch=epoch_F, fmin=f_bottom, fmax=f_top, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
power_F = calc_abs_power_simp(epoch=epoch_F, fmin=f_low, fmax=f_high, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
return power_all_R, power_R, power_all_F, power_F
def get_allch_abs_power(epoch_R, epoch_F, f_bottom = 35.0, f_low = 39.0, f_high = 41.0, f_top = 50.0, sfreq=500.0):
power_all_R = cal_abs_power_allch(epoch=epoch_R, fmin=f_bottom, fmax=f_top, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
power_R = cal_abs_power_allch(epoch=epoch_R, fmin=f_low, fmax=f_high, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
power_all_F = cal_abs_power_allch(epoch=epoch_F, fmin=f_bottom, fmax=f_top, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
power_F = cal_abs_power_allch(epoch=epoch_F, fmin=f_low, fmax=f_high, fbottom=f_bottom, ftop=f_top, sfreq=500.0)
return power_all_R, power_R, power_all_F, power_F
def cal_rel_power_R(power_target, power_all):
global rel_power_R
for i in range(min(len(power_target), len(power_all))):
rel_power_R.append(power_target[i]/power_all[i])
# print(rel_power_R)
return rel_power_R, min(len(power_target), len(power_all))
def cal_rel_power_F(power_target, power_all):
global rel_power_F
for i in range(min(len(power_target), len(power_all))):
rel_power_F.append(power_target[i]/power_all[i])
# print(rel_power_F)
return rel_power_F, min(len(power_target), len(power_all))
def cal_rel_power_R_ran(power_target, power_all):
global rel_power_R_ran
for i in range(min(len(power_target), len(power_all))):
rel_power_R_ran.append(power_target[i]/power_all[i])
# print(rel_power_R_ran)
return rel_power_R_ran, min(len(power_target), len(power_all))
def cal_rel_power_F_ran(power_target, power_all):
global rel_power_F_ran
for i in range(min(len(power_target), len(power_all))):
rel_power_F_ran.append(power_target[i]/power_all[i])
# print(rel_power_F_ran)
return rel_power_F_ran, min(len(power_target), len(power_all))
def cal_rel_power(power_target, power_all):
rel_powers = []
for i in range(min(len(power_target), len(power_all))):
rel_powers.append(power_target[i]/power_all[i])
return rel_powers
def save_rel_powers_subj(length_R, length_F, filefolder = dataRoot + '/Light/csvs', subject_name="Undefined"):
# 添加样本序号索引
global rel_power_R, rel_power_F, subj_number_R, subj_number_F
subj_number_R = subj_number_R + list([subject_name])*length_R
subj_number_F = subj_number_F + list([subject_name])*length_F
df_R, df_F,df_subj_R, df_subj_F = pd.DataFrame(), pd.DataFrame(), pd.DataFrame(), pd.DataFrame()
df_R['rel_power_rest'] = rel_power_R
df_F['rel_power_flicker'] = rel_power_F
df_subj_R["Subject Number Rest"] = subj_number_R
df_subj_F["Subject Number Flicker"] = subj_number_F
df_csv = pd.concat([df_R, df_subj_R, df_F, df_subj_F], axis=1)
df_csv.to_csv(filefolder + '/rel_powers.csv')
def save_rel_powers(filefolder = dataRoot + '/Light/csvs'):
df_R, df_F= pd.DataFrame(), pd.DataFrame()
df_R['rel_power_rest'] = rel_power_R
df_F['rel_power_flicker'] = rel_power_F
df_csv = pd.concat([df_R, df_F], axis=1)
df_csv.to_csv(filefolder + '/rel_powers.csv')
def calc_psds(epoch, fmin, fmax, n_jobs=1, type='multitaper'):
if type=='multitaper':
psds, freqs = psd_multitaper(epoch, fmin=fmin, fmax=fmax, n_jobs=n_jobs)
return psds, freqs
return
def save_psd(psd, freqs, filepath='default.csv', pad=5):
# psd = np.mean(psd, axis=1)
psd = psd[:, ::pad]
freqs = freqs[::pad]
psd = np.insert(psd, 0, values=freqs, axis=0)
np.savetxt(filepath, psd, delimiter=',')
return
'''
For microstate analysis
'''
def plot_substate(epoch, maps, n_maps, dpi=300, save=False, filename='Default', fmt='.png', result_dir=''):
fig, axis = plt.subplots(1, n_maps, dpi=dpi)
for i in range(0,n_maps):
axis[i].set_title('state{0}'.format(i))
# axis[i].set_title('state{0}'.format(i+1))
plot_topomap(maps[i], epoch.info, axes=axis[i], show=False)
fig.show()
if save:
fig.savefig(result_dir + '/' + filename + fmt)
return fig
def save_sub_stateplots(epoch, maps, n_maps, dpi=300, save=False, filename='Default', fmt='.png', result_dir=''):
result_dir = result_dir + '/' + fmt
if not os.path.exists(result_dir): #判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(result_dir)
for i in range(0,n_maps):
fig = plt.figure(dpi = dpi)
plot_topomap(maps[i], epoch.info, show=False)
fig.savefig(result_dir + '/' + filename + "_{0}".format(i) + fmt)
fig.clf()
return
def display_gfp_peaks(gfp_peaks, x, fs):
pps = len(gfp_peaks) / (len(x)/fs) # peaks per second
print(f"\nGFP peaks per sec.: {pps:.2f}")
return
def display_gev(gev):
print("\nGlobal explained variance (GEV) per map:")
for i, g in enumerate(gev): print(f"GEV(ms-{i:d}) = {gev[i]:.2f}")
print(f"\ntotal GEV: {gev.sum():.3f}")
return
def display_info(x, n_maps, gfp_peaks, gev, fs, savelog=False, result_dir='', state='Default', tm=''):
p_hat = p_empirical(x, n_maps)
T_hat = T_empirical(x, n_maps)
print("\nEmpirical symbol distribution (RTT):\n")
for i in range(n_maps):
print(f"p_{i:d} = {p_hat[i]:.3f}")
print("\nEmpirical transition matrix:\n")
print_matrix(T_hat)
display_gfp_peaks(gfp_peaks=gfp_peaks,x=x, fs=fs)
display_gev(gev)
h_hat = H_1(x, n_maps)
h_max = max_entropy(n_maps)
print(f"\nEmpirical entropy H = {h_hat:.2f} (max. entropy: {h_max:.2f})")
h_rate, _ = excess_entropy_rate(x, n_maps, kmax=8, doplot=True)
h_mc = mc_entropy_rate(p_hat, T_hat)
print(f"\nEmpirical entropy rate h = {h_rate:.2f}")
print(f"Theoretical MC entropy rate h = {h_mc:.2f}")
if savelog:
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG,
filename=result_dir + '/result_k{0}.log'.format(n_maps),
filemode='a',
# format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
format = ''
)
logger.info('\n\n' + tm)
logger.info('\nCondition: {0}'.format(state))
logger.info(f"\ntotal GEV: {gev.sum():.6f}")
logger.info('\nEmpirical symbol distribution (RTT):\n')
for i in range(n_maps):
logger.info(f"p_{i:d} = {p_hat[i]:.8f}")
logger.info("\nEmpirical transition matrix:")
logger.info(np.array2string(T_hat, separator=', '))
return
def display_states(x, pca1):
fig, ax = plt.subplots(2, 1, figsize=(15,4), sharex=True)
ax[0].plot(x[0:3000])
ax[1].plot(pca1[0:3000])
return
def save_to_file(array, filepath):
np.asarray(array)
df = pd.DataFrame (array)
df.to_csv(filepath, index=False)