-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcentrationSense.py
126 lines (97 loc) · 4.31 KB
/
ConcentrationSense.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
import UnicornPy
import neurolArthur
import time
from pylsl import StreamInlet, resolve_stream
from neurolArthur import streams
from neurolArthur.connect_device import get_lsl_EEG_inlets
from neurolArthur.BCI import generic_BCI, automl_BCI
from neurolArthur import BCI_tools
from neurolArthur.models import classification_tools
'''
Define the calibrator, transformer, and classifier.
We are recording the alpha_low and alpha_high bands for all 8 electrodes.
Our classifier returns a simple High or Low based on the calibration which is currently set to the 10th percentile
'''
clb = lambda stream: BCI_tools.band_power_calibrator(stream, ['EEG 1', 'EEG 2', 'EEG 3', 'EEG 4', 'EEG 5', 'EEG 6', 'EEG 7', 'EEG 8'], 'unicorn', bands=['alpha_low', 'alpha_high'],
percentile=10, recording_length=10, epoch_len=1, inter_window_interval=0.25)
gen_tfrm = lambda buffer, clb_info: BCI_tools.band_power_transformer(buffer, clb_info, ['EEG 1', 'EEG 2', 'EEG 3', 'EEG 4', 'EEG 5', 'EEG 6', 'EEG 7', 'EEG 8'], 'unicorn', bands=['alpha_low', 'alpha_high'],
epoch_len=1)
def clf(clf_input, clb_info):
clf_input = clf_input[:clb_info.shape[0]]
binary_label = classification_tools.threshold_clf(clf_input, clb_info, clf_consolidator='all')
label = classification_tools.decode_prediction(
binary_label, {True: 'HIGH', False: 'LOW'})
return label
'''
Create a class called concentration.
Initializes with a user specified run length in second (default 60s).
updateConcentration function records everytime tehre are 100 highs or lows in a row and updates 'sum" accordingly gfuyrt
Exits program onece a sum of 5 is reached or the time limit is reached
'''
def intro():
askedTime = int(input("How many minutes would you like to concentrate for?"))
prompt1 = askedTime*60
print("For the calibration period, keep your hands on your thighs and close your eyes.")
return prompt1
class concentration:
def __init__(self, run_length=60, verbose=False):
self.concentration_high = 0
self.concentration_low = 0
self.concentration_sum = 0
self.verbose = verbose
self.start_time = None
self.run_length = run_length
self.timer_start = False
self.highcount = 0
self.lowcount = 0
def updateConcentration(self, EEG_output):
if not self.timer_start:
self.start_time = time.time()
self.timer_start = True
current_time = time.time()
if current_time - self.start_time > self.run_length:
print("Time's up!")
print("High: ", self.highcount)
print("Low: ", self.lowcount)
exit()
if EEG_output == 'LOW':
print("Low")
self.lowcount += 1
self.concentration_low += 1
self.concentration_high = 0
if self.concentration_low == 1000:
self.concentration_sum += 1
self.concentration_low = 0
print (self.concentration_sum)
if EEG_output == 'HIGH':
print("High")
self.highcount += 1
self.concentration_high += 1
self.concentration_low = 0
if self.concentration_high >= 100 and self.concentration_sum > 0:
self.concentration_sum -= 1
self.concentration_high = 0
print (self.concentration_sum)
if self.concentration_sum == 5:
print("Concentration has fallen too much")
print("High: ", self.highcount)
print("Low: ", self.lowcount)
exit()
#no interval notications
'''
Collect LSL stream
'''
streams1 = resolve_stream("name='Unicorn'")
inlet = StreamInlet(streams1[0])
stream = streams.lsl_stream(inlet, buffer_length=1024)
runtime = intro()
'''
Initialize concentration class
'''
concentration1 = concentration(run_length = runtime, verbose = False)
'''
Initialize BCI, Calibrate BCI, Run BCI
'''
concentration_BCI = generic_BCI(clf, transformer=gen_tfrm, action=concentration1.updateConcentration, calibrator=clb)
concentration_BCI.calibrate(stream)
concentration_BCI.run(stream)