forked from skelil/blinkexp.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlink_Final
130 lines (82 loc) · 3.63 KB
/
Blink_Final
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
import os
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import accuracy_score, confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
data_dictionary_1blink={}
# Path to the folder containing the files
folder_path = r'C:\Users\skelil\Documents\OpenBCI_GUI\Recordings\OpenBCISession_1_Blink'
counter=1
# Iterate over all files in the folder
for filename in os.listdir(folder_path):
if filename.endswith(".txt"): # Check if the file has a .txt extension
# Construct the full file path
file_path = os.path.join(folder_path, filename)
# Your existing code
with open(file_path, 'r') as file:
lines = file.readlines()
if lines:
lines = lines[770:2045]
my_2d_array = np.genfromtxt(lines, delimiter=',', usemask=False)
columns_to_remove = [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
result_array = np.delete(my_2d_array, columns_to_remove, axis=1)
# Calculate the mean for each row
row_means_b1 = np.mean(result_array, axis=1)
data_dictionary_1blink[counter]=row_means_b1
counter=counter+1
data_dictionary_3blinks={}
# Path to the folder containing the files
folder_path = r'C:\Users\skelil\Documents\OpenBCI_GUI\Recordings\OpenBCISession_3_Blinks'
counter=1
# Iterate over all files in the folder
for filename in os.listdir(folder_path):
if filename.endswith(".txt"): # Check if the file has a .txt extension
# Construct the full file path
file_path = os.path.join(folder_path, filename)
# Your existing code
with open(file_path, 'r') as file:
lines = file.readlines()
if lines:
lines = lines[770:2045]
my_2d_array = np.genfromtxt(lines, delimiter=',', usemask=False)
columns_to_remove = [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
result_array = np.delete(my_2d_array, columns_to_remove, axis=1)
# Calculate the mean for each row
row_means_b3 = np.mean(result_array, axis=1)
data_dictionary_3blinks[counter]=row_means_b3
counter=counter+1
feature_vector=[]
for key1,data1 in data_dictionary_1blink.items():
feature_vector.append(data1)
for key2,data2 in data_dictionary_3blinks.items():
feature_vector.append(data2)
label_y=[]
for i in range (0,30):
label_y.append(0)
for i in range (30,60):
label_y.append(1)
# Create the LinearDiscriminantAnalysis classifier
clf = LinearDiscriminantAnalysis()
# Train the classifier on the training set
clf.fit(feature_vector, label_y)
############## TESTING
file_path_testing = r'C:\Users\skelil\Documents\OpenBCI_GUI\Recordings\OpenBCISession_2023-12-26_14-45-14 - 1 Blink - S\OpenBCI-RAW-2023-12-26_14-49-03.txt'
with open(file_path_testing, 'r') as file:
lines = file.readlines()
if lines:
lines = lines[770:2045]
my_2d_array_testing = np.genfromtxt(lines, delimiter=',', usemask=False)
columns_to_remove = [0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
result_array = np.delete(my_2d_array_testing, columns_to_remove, axis=1)
# Calculate the mean for each row
row_means_testing = np.mean(result_array, axis=1)
y_pred = clf.predict(row_means_testing.reshape(1, -1) )
if y_pred==0:
num_blinks=1
else:
num_blinks=3
print("Number of blinks is:",num_blinks)