-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWS_utils.py
executable file
·1138 lines (986 loc) · 46.3 KB
/
SWS_utils.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 sys
from scipy.integrate import simpson as simps
import scipy.signal as signal
import matplotlib.pyplot as plt
plt.ion()
import matplotlib.image as mpimg
import matplotlib.patches as patch
import copy as cp
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn import preprocessing
import joblib
import pandas as pd
pd.options.mode.chained_assignment = None
import cv2
import math
from pylab import *
from matplotlib import *
import time
import glob
from dateutil.parser import parse
from datetime import datetime, timedelta
import json
import seaborn as sns
import shutil
import matplotlib.colors as mcolors
import PKA_Sleep as PKA
def generate_signal(downsamp_signal, epochlen, fs): # fs = fsd here
# mean of 4 seconds
normmean = np.mean(downsamp_signal)
normstd = np.std(downsamp_signal)
binl = epochlen * fs # bin size in array slots | number of points in an epoch; a bin == an epoch
sig_var = np.zeros(int(np.size(downsamp_signal) / (epochlen * fs)))
sig_mean = np.zeros(int(np.size(downsamp_signal) / (epochlen * fs)))
sig_max = np.zeros(int(np.size(downsamp_signal) / (epochlen * fs)))
for i in np.arange(np.size(sig_var)):
sig_var[i] = np.var(downsamp_signal[epochlen * fs * (i):(epochlen * fs * (i + 1))])
sig_mean[i] = np.mean(np.abs(downsamp_signal[epochlen * fs * (i):(epochlen * fs * (i + 1))]))
sig_max[i] = np.max(downsamp_signal[epochlen * fs * (i):(epochlen * fs * (i + 1))])
sig_var = (sig_var - normmean) / normstd # normalization
sig_max = (sig_max - np.average(sig_max)) / np.std(sig_max)
# we do not normalize mean (for some reason)
return sig_var, sig_max, sig_mean
def bandPower(this_eeg, fsd, freq_dict = None, minfreq = 0.5, maxfreq = 16, window_length = 10,
noverlap = 9.9, window_type = None):
power_dict = {}
Pxx, freqs, bins = plot_spectrogram(None, this_eeg, fsd, minfreq = minfreq, maxfreq = maxfreq,
window_length = window_length, noverlap = noverlap, window_type = window_type)
freq_res = freqs[1]-freqs[0]
if freq_dict:
for k in list(freq_dict.keys()):
print('Calculating ' + k + ' Band Power...')
idx_low = freq_dict[k][0]
idx_high = freq_dict[k][1]
power_dict[k] = simps(Pxx[np.where(np.logical_and(freqs>=idx_low,freqs<=idx_high))],
axis = 0, dx=freq_res)
power_dict['Total_Power'] = simps(Pxx, dx=freq_res, axis = 0)
power_dict['Bins'] = bins
return power_dict
def peak_freq(this_eeg, fsd, freq_dict = None, minfreq = 0.5, maxfreq = 16, window_length = 10,
noverlap = 9.9, window_type = None):
Pxx, freqs, bins = plot_spectrogram(None, this_eeg, fsd, minfreq = minfreq, maxfreq = maxfreq,
window_length = window_length, noverlap = noverlap, window_type = window_type)
peak_freqs_overall = np.zeros(np.shape(Pxx)[1])
# peak_theta = np.zeros(np.shape(Pxx)[1])
for f in np.arange(0, np.shape(Pxx)[1]):
# theta_band = Pxx[np.where(np.logical_and(freqs>=5,freqs<=8))]
# theta_freqs = freqs[np.where(np.logical_and(freqs>=5,freqs<=8))]
peak_idx_overall = np.argmax(Pxx[:,f])
# peak_idx_theta = np.argmax(theta_band[:,f])
peak_freqs_overall[f] = freqs[peak_idx_overall]
# peak_theta[f] = theta_freqs[peak_idx_theta]
return peak_idx_overall
def post_pre(post, pre):
post = np.append(post, 0)
post = np.delete(post, 0)
pre = np.append(0, pre)
pre = pre[0:-1]
return post, pre
### functions used in the Hengen code
def fix_states(states, alter_nums = False):
if alter_nums == True:
states[states == 1] = 0
states[states == 3] = 5
for ss in np.arange(np.size(states)-1):
#check if it is a flicker state
if (ss != 0 and ss < np.size(states)-1):
if states[ss+1] == states[ss-1]:
states[ss] = states[ss+1]
if (states[ss] == 0 and states[ss+1] == 5):
states[ss] = 2
if alter_nums == True:
states[states == 0] = 1
states[states == 5] = 3
return states
def random_forest_classifier(features, target):
clf = RandomForestClassifier(n_estimators=300)
# clf.fit(preprocessing.LabelEncoder().fit_transform(features), target)
clf.fit(features, target)
return clf
def plot_spectrogram(ax, eegdat, fsd, minfreq = 1, maxfreq = 16, additional_ax = None, window_length = 10,
noverlap = 9.9, vmin = -50, vmax = -10, window_type = None):
dt = 1/fsd
t_elapsed = eegdat.shape[0]/fsd
t = np.arange(0.0, t_elapsed, dt)
noverlap = noverlap * fsd
NFFT = window_length * fsd
if window_type:
window_array = window_type(int(NFFT))
else:
window_array = None
if additional_ax:
additional_ax.set_xlabel('Time (seconds)')
additional_ax.set_ylabel('Frequency (Hz)')
if ax:
ax.set_xlabel('Time (seconds)')
ax.set_ylabel('Frequency (Hz)')
# the minfreq and maxfreq args will limit the frequencies
Pxx, freqs, bins, im = my_specgram(eegdat, ax = ax, NFFT=int(NFFT), Fs=fsd, noverlap=int(noverlap),
cmap=cm.get_cmap('plasma'), minfreq = minfreq, maxfreq = maxfreq,
xextent = (0,int(t_elapsed)), additional_ax = additional_ax, vmin = vmin,
vmax = vmax, window = window_array)
return Pxx, freqs, bins, im
else:
Pxx, freqs, bins = my_specgram(eegdat, ax = ax, NFFT=int(NFFT), Fs=fsd, noverlap=int(noverlap),
cmap=cm.get_cmap('plasma'), minfreq = minfreq, maxfreq = maxfreq,
xextent = (0,int(t_elapsed)), additional_ax = additional_ax, vmin = vmin,
vmax = vmax, window = window_array)
return Pxx, freqs, bins
def my_specgram(x, ax = None, NFFT=400, Fs=200, Fc=0, detrend=mlab.detrend_none,
window=None, noverlap=200,
cmap=None, xextent=None, pad_to=None, sides='default',
scale_by_freq=None, minfreq = None, maxfreq = None, additional_ax = None, vmin = -50, vmax = -10,
**kwargs):
"""
call signature::
specgram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
window=mlab.window_hanning, noverlap=128,
cmap=None, xextent=None, pad_to=None, sides='default',
scale_by_freq=None, minfreq = None, maxfreq = None, **kwargs)
Compute a spectrogram of data in *x*. Data are split into
*NFFT* length segments and the PSD of each section is
computed. The windowing function *window* is applied to each
segment, and the amount of overlap of each segment is
specified with *noverlap*.
%(PSD)s
*Fc*: integer
The center frequency of *x* (defaults to 0), which offsets
the y extents of the plot to reflect the frequency range used
when a signal is acquired and then filtered and downsampled to
baseband.
*cmap*:
A :class:`matplotlib.cm.Colormap` instance; if *None* use
default determined by rc
*xextent*:
The image extent along the x-axis. xextent = (xmin,xmax)
The default is (0,max(bins)), where bins is the return
value from :func:`mlab.specgram`
*minfreq, maxfreq*
Limits y-axis. Both required
*kwargs*:
Additional kwargs are passed on to imshow which makes the
specgram image
Return value is (*Pxx*, *freqs*, *bins*, *im*):
- *bins* are the time points the spectrogram is calculated over
- *freqs* is an array of frequencies
- *Pxx* is a len(times) x len(freqs) array of power
- *im* is a :class:`matplotlib.image.AxesImage` instance
Note: If *x* is real (i.e. non-complex), only the positive
spectrum is shown. If *x* is complex, both positive and
negative parts of the spectrum are shown. This can be
overridden using the *sides* keyword argument.
**Example:**
.. plot:: mpl_examples/pylab_examples/specgram_demo.py
"""
#####################################
# modified axes.specgram() to limit
# the frequencies plotted
#####################################
# this will fail if there isn't a current axis in the global scope
#ax = gca()
Pxx, freqs, bins = mlab.specgram(x, NFFT, Fs, detrend,
window, noverlap, pad_to, sides, scale_by_freq)
# modified here
#####################################
if minfreq is not None and maxfreq is not None:
Pxx = Pxx[(freqs >= minfreq) & (freqs <= maxfreq)]
freqs = freqs[(freqs >= minfreq) & (freqs <= maxfreq)]
#####################################
Z = 10. * np.log10(Pxx)
Z = np.flipud(Z)
if xextent is None: xextent = 0, np.amax(bins)
xmin, xmax = xextent
freqs += Fc
extent = xmin, xmax, freqs[0], freqs[-1]
if not vmin and not vmax:
vmin = np.percentile(np.concatenate(Z), 2)
vmax = np.percentile(np.concatenate(Z), 98)
if ax:
im = ax.imshow(Z, cmap, extent=extent, **kwargs, vmin = vmin, vmax = vmax)
print('vmin: '+str(vmin)+'; vmax: '+str(vmax))
ax.axis('auto')
xticks = np.arange(100,900,100)*4
ax.set_xticks(xticks)
if additional_ax:
im = additional_ax.imshow(Z, cmap, extent=extent, **kwargs, vmin = vmin, vmax = vmax)
additional_ax.axis('auto')
additional_ax.set_xticks(xticks)
return Pxx, freqs, bins, im
else:
return Pxx, freqs, bins
def plot_predicted(ax, Predict_y, is_predicted, clf, Features):
ax.set_title('Predicted States', x=0.5, y=0.7)
for state in np.arange(np.size(Predict_y)):
if Predict_y[state] == 0:
rect7 = patch.Rectangle((state, 0), 3.8, height = 1, color = 'grey')
ax.add_patch(rect7)
if Predict_y[state] == 1:
rect7 = patch.Rectangle((state, 0), 3.8, height = 1, color = 'green')
ax.add_patch(rect7)
elif Predict_y[state] == 2:
rect7 = patch.Rectangle((state, 0), 3.8, height = 1, color = 'blue')
ax.add_patch(rect7)
elif Predict_y[state] == 3:
rect7 = patch.Rectangle((state, 0), 3.8, height = 1, color = 'red')
ax.add_patch(rect7)
elif Predict_y[state] == 4:
rect7 = patch.Rectangle((state, 0), 3.8, height=1, color='purple')
ax.add_patch(rect7)
else:
print("Model predicted an unknown state.")
ax.set_ylim(0.3, 1)
ax.set_yticklabels([])
ax.set_xlim(0, np.size(Predict_y))
ax.set_xticks(np.arange(100, np.size(Predict_y), 100))
ax.tick_params(axis="x",direction="in", pad=-15)
if is_predicted:
predictions = clf.predict_proba(Features)
confidence = np.max(predictions, 1)
else:
confidence = np.ones(np.size(Predict_y))
ax.plot(confidence, color = 'k')
# This is the plotting collection function for the coarse prediction figure
def create_prediction_figure(d, Predict_y, is_predicted, clf, Features, fs, eeg_AD0, eeg_AD2,
this_emg, EEG_t, epochlen, start, end, maxfreq, minfreq, additional_axes, v = None):
plt.ion()
vmin = d['vmin']
vmax = d['vmax']
if vmin == 'None':
vmin = None
if vmax =='None':
vmax = None
fig1, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(nrows = 5, ncols = 1, figsize = (11, 6))
if v is not None:
ax4.plot(v[1], v[0], color = 'k', linestyle = '--')
ax4.set_ylabel('Velocity')
ax4.set_ylim([0,40])
ax4.set_xlim([0,v[1][-1]])
else:
ax4.text(0.5, 0.5, 'No Movement Available',
horizontalalignment='center', verticalalignment='center')
ax4.set_yticklabels([])
ax4.set_xticklabels([])
Pxx, freqs, bins, im = plot_spectrogram(ax1, eeg_AD0, fs, maxfreq = maxfreq, minfreq = minfreq,
additional_ax = additional_axes[0], vmin = vmin, vmax = vmax)
Pxx, freqs, bins, im = plot_spectrogram(ax3, eeg_AD2, fs, maxfreq = maxfreq, minfreq = minfreq,
additional_ax = additional_axes[1], vmin = vmin, vmax = vmax)
ax1.xaxis.set_ticks_position('top')
ax3.set_xticklabels([])
plot_predicted(ax2, Predict_y, is_predicted, clf, Features)
ax5.plot(EEG_t, this_emg, color= 'r')
ax5.set_xlim([EEG_t[0],EEG_t[-1]])
ax5.set_ylabel('EMG Amplitude')
fig1.tight_layout()
fig1.subplots_adjust(wspace=0, hspace=0)
return fig1, ax1, ax2, ax3, ax4, ax5
def update_sleep_df(model_dir, mod_name, df_additions):
try:
Sleep_Model = np.load(file = model_dir + mod_name + '_model.pkl', allow_pickle = True)
Sleep_Model = Sleep_Model.append(df_additions, ignore_index = True)
except FileNotFoundError:
print('no model created...I will save this one')
df_additions.to_pickle(model_dir + mod_name + '_model.pkl')
Sleep_Model = df_additions
Sleep_Model.to_pickle(model_dir + mod_name + '_model.pkl')
return Sleep_Model
def load_joblib(FeatureDict, emg_flag, movement_flag, mod_name):
try:
del FeatureDict['animal_name']
except KeyError:
pass
try:
del FeatureDict['State']
except KeyError:
pass
if emg_flag:
jobname = mod_name + '_EMG'
print("EMG flag on")
else:
x_features.remove('EMG')
jobname = mod_name + '_no_EMG'
print('Just so you know...this model has no EMG')
if movement_flag:
jobname = jobname + '_movement'
else:
jobname = jobname + '_no_movement'
jobname = jobname + '.joblib'
return jobname, list(FeatureDict.keys())
def retrain_model(Sleep_Model, x_features, model_dir, jobname):
print("Retrain model")
prop = 1 / 2
model_inputs = Sleep_Model[x_features][0:int((max(Sleep_Model.index) + 1) * prop)].apply(pd.to_numeric)
train_x = model_inputs.values
model_input_states = Sleep_Model['State'][0:int((max(Sleep_Model.index) + 1) * prop)].apply(pd.to_numeric)
train_y = model_input_states.values
model_test = Sleep_Model[x_features][int((max(Sleep_Model.index) + 1) * prop):].apply(pd.to_numeric)
test_x = model_test.values
model_test_states = Sleep_Model['State'][int((max(Sleep_Model.index) + 1) * prop):].apply(pd.to_numeric)
test_y = model_test_states.values
print('Calculating tree...')
clf = random_forest_classifier(train_x, train_y)
print("Train Accuracy :: ", accuracy_score(train_y, clf.predict(train_x)))
print("Test Accuracy :: ", accuracy_score(test_y, clf.predict(test_x)))
clf = random_forest_classifier(Sleep_Model[x_features].apply(pd.to_numeric).values,
Sleep_Model['State'].apply(pd.to_numeric).values)
print("Train Accuracy :: ", accuracy_score(Sleep_Model['State'].apply(pd.to_numeric).values,
clf.predict(Sleep_Model[x_features].apply(pd.to_numeric).values)))
joblib.dump(clf, model_dir + jobname)
def pull_up_movie(d, cap, start, end, vid_file, epochlen, this_timestamp):
v = get_videofn_from_csv(d, this_timestamp['Filename'][start])
print('Pulling up video: '+v)
print('starting on frame '+str(start))
start_sec = this_timestamp['Offset_Time'][start]
print('starting at second '+str(start_sec))
print('starting '+ v + ' at ' + str(start_sec) + ' seconds')
if not cap[v].isOpened():
print("Error opening video stream or file")
score_win_sec = [start_sec + epochlen, start_sec + epochlen*2]
try:
score_win_idx1 = int(this_timestamp.index[this_timestamp['Offset_Time']>(score_win_sec[0])][0])
score_win_idx2 = int(this_timestamp.index[this_timestamp['Offset_Time']>(score_win_sec[1])][0])
except IndexError:
print('No video availabile during this time.')
return
score_win = np.arange(score_win_idx1, int(score_win_idx2))
for f in np.arange(start, end+200):
cap[v].set(1, f)
ret, frame = cap[v].read()
if ret:
if f in score_win:
cv2.putText(frame, "SCORE WINDOW", (50, 105), cv2.FONT_HERSHEY_PLAIN, 4, (225, 0, 0), 2)
cv2.imshow('Frame', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('v'):
break
cv2.destroyAllWindows()
#Creates line objects for the fine graph that plots data over 12s intervals
def create_zoomed_fig(ax8, ax9, ax10, long_emg, long_emg_t, long_ThD, long_ThD_t, long_v, long_v_t,
start_trace, end_trace, epochlen, ThD_ylims = None, emg_ylims = None, v_ylims = None):
#Delta-Theta
line1 = plot_zoomed_data(ax8, long_ThD, long_ThD_t, start_trace, end_trace, color = '#5170d7',
epochlen = epochlen, ylabel = 'Theta/Delta\nRatio', ylims = ThD_ylims)
if long_v is None:
line3 = ax9.plot([0,0], [1,1], linewidth = 0, color = 'w')
ax9.text(0.5, 0.5, 'No Movement Available', horizontalalignment='center',
verticalalignment='center')
else:
line3 = plot_zoomed_data(ax9, long_v, long_v_t, start_trace, end_trace, color = '#a87dc2',
epochlen = epochlen, ylabel = 'Velocity', ylims = v_ylims)
if long_emg is None:
ax10.text(0.5, 0.5, 'There is no EMG', horizontalalignment='center',
verticalalignment='center')
line2 = ax10.plot([0,0], [1,1], linewidth = 0, color = 'w')
else:
line2 = plot_zoomed_data(ax10, long_emg, long_emg_t, start_trace, end_trace, color = '#fd411e',
epochlen = epochlen, ylabel = 'EMG Amplitude', linewidth = 2, ylims = emg_ylims)
plt.show()
return line1, line2, line3
def plot_zoomed_data(ax, data, t, start_trace, end_trace, color, epochlen, ylabel = None,
ylims = None, linewidth = 3):
start_idx = np.where(t>=start_trace)[0][0]
end_idx = np.where(t<=end_trace)[0][-1]
line, = ax.plot(t[start_idx:end_idx+1], data[start_idx:end_idx+1],
color = color, linewidth = linewidth)
ax.set_xlim(t[start_idx], t[end_idx])
if ylabel:
ax.set_ylabel(ylabel)
if ylims:
ax.set_ylim(ylims)
else:
ylims = list(ax.get_ylim())
h = ylims[-1]-ylims[0]
rect = patch.Rectangle((0, ylims[0]),epochlen, h, color='#fac205', alpha = 0.3)
ax.add_patch(rect)
return line
def add_buffer(data_array, t_array, buffer_seconds, fs):
if data_array is None:
buffered_data = None
buffered_t = None
else:
buffered_data = np.concatenate((np.full(int(buffer_seconds*fs), 0),data_array, np.full(int(buffer_seconds*fs), 0)))
pre_buffer = np.arange(-buffer_seconds, 0, 1/fs)
post_buffer = np.arange(t_array[-1]+1/fs, t_array[-1]+buffer_seconds+1/fs, 1/fs)
buffered_t = np.concatenate([pre_buffer, t_array, post_buffer])
assert np.size(buffered_data) == np.size(buffered_t)
return buffered_data, buffered_t
def clear_bins(bins, ax2):
start_bin = bins[0]
end_bin = bins[1]
if end_bin-start_bin == 1:
end_bin = end_bin+1
for b in np.arange(start_bin, end_bin-1):
b = math.floor(b)
location = b
rectangle = patch.Rectangle((location, 0), 1.5, height = 2, color = 'white')
ax2.add_patch(rectangle)
def correct_bins(start_bin, end_bin, ax2, new_state):
if end_bin-start_bin == 1:
end_bin = end_bin+1
for b in np.arange(start_bin, end_bin-1):
b = math.floor(b)
location = b
color = 'white'
if new_state == 1:
color = 'green'
if new_state == 2:
color = 'blue'
if new_state == 3:
color = 'red'
if new_state == 4:
color = 'purple'
print("color: " + str(color))
rectangle = patch.Rectangle((location, 0), 1.5, height = 2, color = color)
print('loc: ', location)
ax2.add_patch(rectangle)
def create_scoring_figure(extracted_dir, a, this_eeg, this_emg, EEG_t, fsd,
maxfreq, minfreq, epochlen, v = None, additional_ax = None):
plt.ion()
if v is not None:
fig, (ax1, ax2, ax3, axx, button) = plt.subplots(nrows = 5, ncols = 1, figsize = (16, 8))
ax3.plot(v[1], v[0], color = 'k', linestyle = '--')
ax3.set_ylabel('Velocity')
ax3.set_ylim([0,40])
ax3.set_xlim([0,int(np.size(EEG_t)/fsd)])
ax3.set_yticklabels([])
ax3.set_xticklabels([])
else:
fig, (ax1, ax2, axx, button) = plt.subplots(nrows = 4, ncols = 1, figsize = (11, 6))
Pxx, freqs, bins, im = plot_spectrogram(ax1, this_eeg, fsd, maxfreq = maxfreq, minfreq = minfreq,
additional_ax = additional_ax)
ax1.xaxis.set_ticks_position('top')
ax2.set_xlim([0,math.ceil(EEG_t[-1]/epochlen)])
fig.tight_layout()
fig.subplots_adjust(wspace=0, hspace=0)
axx.plot(EEG_t, this_emg, color= 'r')
axx.set_xlim([EEG_t[0],EEG_t[-1]])
axx.set_ylabel('EMG Amplitude')
button.set_xlim([0,1])
button.set_ylim([0,1])
rect = patch.Rectangle((0,0),1,1, color = 'k')
button.add_patch(rect)
button.text(0.5, 0.5, 'Click here for video', horizontalalignment='center',
verticalalignment='center', transform=button.transAxes, color = 'w', fontsize = 16)
button.tick_params(axis='both',which='both',bottom=False,top=False,left = False,
labelbottom=False, labelleft = False)
fig.show()
if v is not None:
return fig, ax1, ax2, ax3, axx, button
else:
return fig, ax1, ax2, axx, button
def update_raw_trace(fig1, fig2, line1, line2, line3, line4, line5, long_emg, long_emg_t, long_ThD,
long_ThD_t, long_v, long_v_t, markers, this_epoch_t, start_trace, end_trace, epochlen):
start_idx_ThD = np.where(long_ThD_t>=start_trace)[0][0]
end_idx_ThD = np.where(long_ThD_t<=end_trace)[0][-1]
try:
assert np.size(line1.get_ydata()) == (end_idx_ThD-start_idx_ThD)+1
except AssertionError:
diff = ((end_idx_ThD-start_idx_ThD)+1)-np.size(line1.get_ydata())
end_idx_ThD = end_idx_ThD-diff
line1.set_ydata(long_ThD[start_idx_ThD:end_idx_ThD+1])
if long_emg is not None:
start_idx_emg = np.where(long_emg_t>=start_trace)[0][0]
end_idx_emg = np.where(long_emg_t<=end_trace)[0][-1]
try:
assert np.size(line2.get_ydata()) == (end_idx_emg-start_idx_emg)+1
except AssertionError:
diff = ((end_idx_emg-start_idx_emg)+1)-np.size(line2.get_ydata())
end_idx_emg = end_idx_emg-diff
line2.set_ydata(long_emg[start_idx_emg:end_idx_emg+1])
if long_v is not None:
v_idx, = np.where(np.logical_and(long_v_t>=start_trace, long_v_t<=end_trace))
# start_idx_v = np.where(v_t>=start_trace)[0][0]
# end_idx_v = np.where(v_t<=end_trace)[0][-1]
try:
assert np.size(line3.get_xdata()) == np.size(v_idx)
y_update = long_v[v_idx]
# assert np.size(line3.get_ydata()) == (end_idx_v-start_idx_v)+1
except AssertionError:
y_update = np.empty(np.size(line3.get_xdata()))
y_update[:] = np.nan
if len(v_idx) != 0:
y_update[:len(v_idx)] = long_v[v_idx]
line3.set_ydata(y_update)
for i,m in enumerate(markers):
if i == 1:
m.set_xdata([int(this_epoch_t/epochlen),int(this_epoch_t/epochlen)])
else:
m.set_xdata([this_epoch_t,this_epoch_t])
fig2.axes[0].set_xlim([this_epoch_t-600, this_epoch_t+600])
fig2.axes[1].set_xlim([this_epoch_t-600, this_epoch_t+600])
line4.set_xdata([this_epoch_t,this_epoch_t])
line5.set_xdata([this_epoch_t,this_epoch_t])
fig1.canvas.draw()
fig2.canvas.draw()
def make_marker(fig, x, epochlen):
markers = []
for i,a in enumerate(fig.axes):
ylims = list(a.get_ylim())
xlims = list(a.get_xlim())
if i == 1:
marker, = a.plot([x/epochlen, x/epochlen], ylims, color = 'k')
else:
marker, = a.plot([x, x], ylims, color = 'k')
a.set_ylim(ylims)
markers.append(marker)
return markers
def load_video(d, this_timestamp):
print('Loading video now, this might take a second....')
cap = {}
fps = {}
these_ts_files = np.unique(this_timestamp['Filename'])
for ts in these_ts_files:
v = get_videofn_from_csv(d, ts)
print('Loading '+v+'...')
cap[v] = cv2.VideoCapture(v)
fps[v] = cap[v].get(cv2.CAP_PROP_FPS)
return cap, fps
def timestamp_extracting(d, a):
if d['Bonsai Version'] < 6:
timestamps = glob.glob(d['csv_dir']+'*.txt')
timestamps_csv = glob.glob(d['csv_dir']+'*.csv')
for t in timestamps_csv:
timestamps.append(t)
for tf in timestamps:
print('I think I found a timestamp file: ' + tf)
with open(tf, "r") as file:
first_line = file.readline()
try:
parse(first_line, fuzzy=False)
print('This is a timestamp file. Moving on...')
timestamp_file = tf
except Exception:
print('This is not a timestamp file. Help.')
if d['Bonsai Version'] >= 6:
timestamp_files = glob.glob(os.path.join(d['csv_dir'], '*imestamp*.csv'))
timestamp_files = sort_files(timestamp_files, d['basename'])
if len(timestamp_files) == 2*len(d['Acquisition']):
timestamp_files = glob.glob(os.path.join(d['csv_dir'], '*sidetimestamp*.csv'))
timestamp_files = sort_files(timestamp_files, d['basename'])
file_idx = d['Acquisition'].index(int(a))
timestamp_file = timestamp_files[int(file_idx)]
print('Timestamp file: ' + timestamp_file)
timestamp_df = pd.read_csv(timestamp_file, header=None)
timestamp_df.columns = ['Timestamps']
timestamp_df['Filename'] = timestamp_file
ts_format = '%Y-%m-%dT%H:%M:%S.%f'
short_ts = [x[:-6] for x in list(timestamp_df['Timestamps'].loc[~timestamp_df['Timestamps'].isnull()])]
if len(short_ts) > 0:
timestamp_df['Timestamps'] = [datetime.strptime(short_ts[i][:-1], ts_format) for i in np.arange(len(short_ts))]
return timestamp_df
def pulling_timestamp(timestamp_df, EEG_datetime, this_eeg, fsd):
acq_len = int(np.size(this_eeg)/fsd)
end_ts = EEG_datetime
start_ts = end_ts-timedelta(seconds=acq_len)
ts_idx, = np.where(np.logical_and(timestamp_df['Timestamps'] < end_ts, timestamp_df['Timestamps'] > start_ts))
this_timestamp = timestamp_df.iloc[ts_idx]
offset_times = this_timestamp['Timestamps']-this_timestamp['Timestamps'].iloc[0]
this_timestamp['Offset_Time'] = [offset_times.iloc[i].total_seconds() for i in range(len(offset_times))]
return this_timestamp
def initialize_vid_and_move(d, a, EEG_datetime, acq_len, this_eeg):
if d['vid']:
video_list = glob.glob(os.path.join(d['video_dir'], '*.mp4'))
if len(video_list) == 0:
video_list = glob.glob(os.path.join(d['video_dir'], '*.avi'))
if len(video_list) == 0:
print('No videos found! Please check directory')
sys.exit()
video_list = sort_files(video_list, d['basename'])
try:
assert len(video_list) == len(d['Acquisition'])
except AssertionError:
if len(video_list) > len(d['Acquisition']):
print('There are more videos than aquisitions. Please move any videos that do not have a corresponding acquisition out of this directory: ' + str(d['video_dir']))
if len(video_list) < len(d['Acquisition']):
print('There are more acquisitions than videos. Only list acquisitions with videos in the Score_Settings.json file')
vid_idx = d['Acquisition'].index(int(a))
if d['video_dir'] == "F:/FLiP_Videos/jaLC_FLiPAKAREEGEMG004/":
this_video = glob.glob(os.path.join(d['video_dir'], '*_' + str(int(a)-1) +'.mp4'))[0]
else:
this_video = video_list[int(vid_idx)]
else:
this_video = None
print('no video available')
if d['movement']:
movement_df = pd.read_pickle(os.path.join(d['savedir'], 'All_movement.pkl'))
acq_len = int(np.size(this_eeg)/d['fsd'])
end_ts = EEG_datetime
start_ts = end_ts-timedelta(seconds=acq_len)
move_idx, = np.where(np.logical_and(movement_df['Timestamps'] < end_ts, movement_df['Timestamps'] > start_ts))
this_motion = movement_df.iloc[move_idx]
v = movement_processing(this_motion)
else:
v = None
this_motion = None
return this_video, v, this_motion
def get_ThD(this_eeg, fsd):
Pxx, freqs, bins = my_specgram(this_eeg, Fs = fsd)
delta_band = np.sum(Pxx[np.where(np.logical_and(freqs>=1,freqs<=4))],axis = 0)
theta_band = np.sum(Pxx[np.where(np.logical_and(freqs>=5,freqs<=8))],axis = 0)
return theta_band/delta_band
def movement_extracting(d, a):
if d['Bonsai Version'] >= 6:
movement_files = glob.glob(os.path.join(d['csv_dir'], '*motion*.csv'))
if len(movement_files) == 0:
movement_files = glob.glob(os.path.join(movement_filedir, '*movement*.csv'))
movement_files = sort_files(movement_files, d['basename'])
file_idx = d['Acquisition'].index(int(a))
movement_file = movement_files[file_idx]
print('This is your movement file: ' + movement_file)
else:
print('This scoring engine no longer supports Bonsai version < 6. Please run DLC.')
if d['DLC']:
movement_df_full = pd.read_csv(movement_file)
movement_df = movement_df_full[[d['DLC Label']+'_x', d['DLC Label']+'_y', d['DLC Label']+'_likelihood']]
movement_df.columns = ['X', 'Y', 'Likelihood']
else:
movement_df = pd.read_csv(movement_file, header = None)
if len(movement_df.columns) == 2:
movement_df.columns = ['X','Y']
if movement_df['X'].iloc[0] == 'X':
movement_df = movement_df.drop(0)
movement_df = movement_df.reset_index(drop = True)
else:
movement_df.columns = ['Timestamps', 'X','Y']
ts_format = '%Y-%m-%dT%H:%M:%S.%f'
short_ts = [x[:-6] for x in list(movement_df['Timestamps'])]
movement_df['Timestamps'] = [datetime.strptime(short_ts[i][:-1], ts_format) for i in np.arange(len(short_ts))]
movement_df['Filename'] = movement_file
return movement_df
def movement_processing(this_motion, binsize = 4):
this_motion['X'] = this_motion['X'].fillna(0)
this_motion['Y'] = this_motion['Y'].fillna(0)
try:
t_vect = this_motion['Timestamps']-this_motion['Timestamps'].iloc[0]
except IndexError:
t = np.arange(0, 3600, binsize)
v = [np.nan for i in range(len(t))]
v = np.vstack([v,t])
return v
t_vect = t_vect.dt.total_seconds().values
# t_vect = [t_vect.iloc[i].total_seconds() for i in range(len(t_vect))]
bins = np.arange(0, t_vect[-1]+binsize, binsize)
# dx = []
# dy = []
# t = []
# ts = []
idxs = [np.where(np.logical_and(t_vect>=bins[i], t_vect<bins[i+1]))[0]
for i in np.arange(0, np.size(bins)-1)]
dx = [this_motion['X'].iloc[ii[-1]] - this_motion['X'].iloc[ii[0]]
for ii in idxs if len(ii) > 0]
dy = [this_motion['Y'].iloc[ii[-1]] - this_motion['Y'].iloc[ii[0]]
for ii in idxs if len(ii) > 0]
t = [t_vect[ii[-1]] for ii in idxs if len(ii) > 0]
v = np.sqrt((np.square(dx) + np.square(dy)))
v = np.vstack([v,t])
# for i in np.arange(0, np.size(bins)-1)]
# for i in np.arange(0, np.size(bins)-1):
# idxs, = np.where(np.logical_and(t_vect>=bins[i], t_vect<bins[i+1]))
# temp_x = list(this_motion['X'].iloc[idxs])
# dx.append(int(float(temp_x[-1]))-int(float(temp_x[0])))
# temp_y = list(this_motion['Y'].iloc[idxs])
# dy.append(int(float(temp_y[-1]))-int(float(temp_y[0])))
# t.append(t_vect[idxs[-1]])
# ts.append(this_motion['Timestamps'].iloc[idxs[-1]])
return v
def get_movement_segs(movement_df, time_window):
t_vect = (movement_df['Timestamps'] - movement_df['Timestamps'].iloc[0]).dt.total_seconds()
vs = []
if time_window.ndim == 2:
for w in time_window:
this_motion = movement_df.loc[(t_vect >= w[0]) & (t_vect < w[1])]
vs.append(movement_processing(this_motion))
return vs
else:
this_motion = movement_df.loc[(t_vect >= time_window[0]) & (t_vect < time_window[1])]
v = movement_processing(this_motion)
return v
def prepare_feature_data(FeatureDict, movement_flag, smooth = False):
del FeatureDict['animal_name']
FeatureDict = adjust_movement(FeatureDict, movement_flag)
if smooth:
FeatureList = []
for f in FeatureDict.keys():
FeatureList_smoothed.append(signal.medfilt(FeatureDict[f], 5))
else:
FeatureList = list(FeatureDict.values())
Features = np.column_stack((FeatureList))
Features = np.nan_to_num(Features)
return Features
def build_feature_dict(this_eeg, fsd, epochlen, this_emg = None, normVal = None):
FeatureDict = {}
print('Generating EMG vectors...')
if this_emg is not None:
FeatureDict['EMGvar'], EMGmax, EMGmean = generate_signal(this_emg, epochlen, fsd)
print('Generating EEG vectors...')
FeatureDict['EEGvar'], EEGmax, EEGmean = generate_signal(this_eeg, epochlen, fsd)
acq_len = np.size(this_eeg)/fsd
num_epochs = acq_len/epochlen
freq_dict = freq_dict = {'Delta': [0.5, 4],
'Theta':[5, 8],
'Alpha': [8, 12],
'BroadTheta':[2, 16],
'Fire': [4, 20]}
power_dict = bandPower(this_eeg, fsd, freq_dict = freq_dict, minfreq = 0.5, maxfreq = 16)
epoch_bins = np.arange(0, acq_len, epochlen)
epoch_idx = [np.where(np.logical_and(power_dict['Bins']>=i, power_dict['Bins']<i+4))[0] for i in epoch_bins]
for band in list(freq_dict.keys()):
FeatureDict['EEG'+band] = [np.median(power_dict[band][i]) for i in epoch_idx]/normVal
for n in np.where(np.isnan(FeatureDict['EEG'+band]))[0]:
if n < np.size(FeatureDict['EEG'+band])-1:
FeatureDict['EEG'+band][n] = FeatureDict['EEG'+band][n+1]
else:
FeatureDict['EEG'+band][n] = FeatureDict['EEG'+band][n-1]
# FeatureDict['EEG'+band] = FeatureDict['EEG'+band]/normVal
assert np.size(FeatureDict['EEG'+band]) == num_epochs
FeatureDict['EEGnb'] = FeatureDict['EEGTheta'] / FeatureDict['EEGBroadTheta'] # narrow-band theta
# # delt_thet = EEGdelta / EEGtheta # ratio; esp. important
FeatureDict['thet_delt'] = FeatureDict['EEGTheta'] / FeatureDict['EEGDelta']
# frame shifting
FeatureDict['delta_post'], FeatureDict['delta_pre'] = post_pre(FeatureDict['EEGDelta'],
FeatureDict['EEGDelta'])
FeatureDict['theta_post'], FeatureDict['theta_pre'] = post_pre(FeatureDict['EEGTheta'],
FeatureDict['EEGTheta'])
FeatureDict['delta_post2'], FeatureDict['delta_pre2'] = post_pre(FeatureDict['delta_post'],
FeatureDict['delta_pre'])
FeatureDict['theta_post2'], FeatureDict['theta_pre2'] = post_pre(FeatureDict['theta_post'],
FeatureDict['theta_pre'])
FeatureDict['delta_post3'], FeatureDict['delta_pre3'] = post_pre(FeatureDict['delta_post2'],
FeatureDict['delta_pre2'])
FeatureDict['theta_post3'], FeatureDict['theta_pre3'] = post_pre(FeatureDict['theta_post2'],
FeatureDict['theta_pre2'])
FeatureDict['nb_post'], FeatureDict['nb_pre'] = post_pre(FeatureDict['EEGnb'],
FeatureDict['EEGnb'])
return FeatureDict
def adjust_movement(FeatureDict, movement_flag, epochlen = 4):
if movement_flag:
# this_video, v, this_motion = SWS_utils.initialize_vid_and_move(bonsai_v, vid_flag, movement_flag, video_dir, a,
# acq, this_eeg, fsd, EEG_datetime, extracted_dir)
v = FeatureDict['Velocity']
if np.size(v) > 900:
v_reshape = np.reshape(v, (-1,epochlen))
mean_v = np.mean(v_reshape, axis = 1)
mean_v[np.isnan(mean_v)] = 0
elif np.size(v) < 900:
diff = 900 - np.size(v)
nans = np.empty(diff)
nans[:] = 0
mean_v = np.concatenate((v, nans))
else:
mean_v = v
else:
mean_v = np.zeros(900)
mean_v[np.isnan(mean_v)] = 0
FeatureDict['Velocity'] = mean_v
return FeatureDict
def model_feature_importance(filename_sw):
with open(filename_sw, 'r') as f:
d = json.load(f)
emg_flag = int(d['emg'])
movement_flag = int(d['movement'])
model_dir = str(d['model_dir'])
mod_name = str(d['mod_name'])
if emg_flag:
jobname = mod_name + '_EMG'
print("EMG flag on")
else:
x_features.remove('EMG')
jobname = mod_name + '_no_EMG'
print('Just so you know...this model has no EMG')
if movement_flag:
jobname = jobname + '_movement'
else:
jobname = jobname + '_no_movement'
jobname = jobname + '.joblib'
clf = joblib.load(os.path.join(model_dir, jobname))
Sleep_Model = np.load(file = model_dir + mod_name + '_model.pkl', allow_pickle = True)
del Sleep_Model['State']
del Sleep_Model['animal_name']
fig,ax = plt.subplots(figsize = (14,8))
y = clf.feature_importances_
x = np.arange(len(y))
ax.bar(x, y, color = 'k')
ax.set_xticks(x)
ax.set_xticklabels(list(Sleep_Model.columns), rotation = 45)
ax.set_xlabel('Features')
ax.set_ylabel('Feature Importance')
sns.despine()
return fig
def rename_DLC_csvs(csv_dir, basename):
DLC_coords_files = glob.glob(os.path.join(csv_dir, 'Coord*.csv'))
for f in DLC_coords_files:
fname = os.path.split(f)[1]
substring_1 = 'Coord'
idx = fname.find('DLC')
fname_new = fname[:idx]
fname_new = fname_new.replace(substring_1, "")
insert_idx = fname_new.find(basename) + len(basename)
fname_change = fname_new[:insert_idx]+'_motion'+fname_new[insert_idx:]+'.csv'
os.rename(f,os.path.join(csv_dir, fname_change))
def DLC_check_fig(csv_file):
coords_df = pd.read_csv(csv_file)
color_dict = {}
labels = ['center', 'ear1', 'ear2', 'nose', 'baseoftail']
color_dict['center'] = '#fffd01'
color_dict['ear1'] = '#7e1e9c'
color_dict['ear2'] = '#cb416b'
color_dict['nose'] = '#2000b1'
color_dict['baseoftail'] = '#f0944d'
fig, ax = plt.subplots(nrows = len(labels), figsize = (15, 8))
vel_dict = {}
x = np.linspace(0, 3600, len(coords_df[labels[0]+'_x']))
bins = np.arange(0, 3601)
for i,l in enumerate(labels):
dx = []
dy = []
for ii in np.arange(0, np.size(bins)-1):
idxs, = np.where(np.logical_and(x>=bins[ii], x<bins[ii+1]))
temp_x = list(coords_df[l+'_x'].iloc[idxs])
dx.append(temp_x[-1]-temp_x[0])
temp_y = list(coords_df[l+'_y'].iloc[idxs])
dy.append(temp_y[-1]-temp_y[0])
vel_dict[l] = np.sqrt((np.square(dx) + np.square(dy)))
ax[i].plot(bins[1:]/60, vel_dict[l], color = color_dict[l], label = l)
ax[i].set_title(l)
ax[i].set_xlim([0,60])
ax[i].set_xticks(np.arange(0, 60))
ax[i].set_yticklabels([])
sns.despine()
fig.tight_layout()
def transfer_DLC_files(transfer_directory, basenames, DLC_model_dir):
for b in basenames:
try:
os.rename(os.path.join(transfer_directory, b,b+'_csv'), os.path.join(transfer_directory, b,b+'_csv_old2'))
except OSError:
pass
try:
os.mkdir(os.path.join(transfer_directory, b, 'DLC_Outputs'))
except FileExistsError:
pass
try:
os.mkdir(os.path.join(transfer_directory, b, b+'_csv'))
except FileExistsError:
pass
files_to_move = []
coord_files = []
for i in ['up_day_t/Test_day_updated-Samarth-2023-05-17', 'up_night_t/Test_night_updated-Samarth-2023-05-17']:
files_to_move.append(glob.glob(os.path.join(DLC_model_dir, i, 'Testing', b+'*labeled.mp4')))
files_to_move.append(glob.glob(os.path.join(DLC_model_dir, i, 'Testing', b+'*.pickle')))
files_to_move.append(glob.glob(os.path.join(DLC_model_dir, i, 'Testing', b+'*.h5')))
files_to_move.append(glob.glob(os.path.join(DLC_model_dir, i, 'Testing', b+'*.csv')))
coord_files.append(glob.glob(os.path.join(DLC_model_dir, i, 'Testing/coords_csv', '*'+b+'*.csv')))