This repository was archived by the owner on Dec 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub_data.py
More file actions
250 lines (211 loc) · 8.77 KB
/
Copy pathsub_data.py
File metadata and controls
250 lines (211 loc) · 8.77 KB
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
from cortex import Cortex
class Subcribe:
"""
A class to subscribe data stream.
Attributes
----------
c : Cortex
Cortex communicate with Emotiv Cortex Service
Methods
-------
start():
start data subscribing process.
sub(streams):
To subscribe to one or more data streams.
on_new_data_labels(*args, **kwargs):
To handle data labels of subscribed data
on_new_eeg_data(*args, **kwargs):
To handle eeg data emitted from Cortex
on_new_mot_data(*args, **kwargs):
To handle motion data emitted from Cortex
on_new_dev_data(*args, **kwargs):
To handle device information data emitted from Cortex
on_new_met_data(*args, **kwargs):
To handle performance metrics data emitted from Cortex
on_new_pow_data(*args, **kwargs):
To handle band power data emitted from Cortex
"""
def __init__(self, app_client_id, app_client_secret, **kwargs):
"""
Constructs cortex client and bind a function to handle subscribed data streams
If you do not want to log request and response message , set debug_mode = False. The default is True
"""
print("Subscribe __init__")
self.c = Cortex(app_client_id, app_client_secret, debug_mode=True, **kwargs)
self.c.bind(create_session_done=self.on_create_session_done)
self.c.bind(new_data_labels=self.on_new_data_labels)
self.c.bind(new_eeg_data=self.on_new_eeg_data)
self.c.bind(new_mot_data=self.on_new_mot_data)
self.c.bind(new_dev_data=self.on_new_dev_data)
self.c.bind(new_met_data=self.on_new_met_data)
self.c.bind(new_pow_data=self.on_new_pow_data)
self.c.bind(inform_error=self.on_inform_error)
def start(self, streams, headsetId=""):
"""
To start data subscribing process as below workflow
(1)check access right -> authorize -> connect headset->create session
(2) subscribe streams data
'eeg': EEG
'mot' : Motion
'dev' : Device information
'met' : Performance metric
'pow' : Band power
'eq' : EEQ Quality
Parameters
----------
streams : list, required
list of streams. For example, ['eeg', 'mot']
headsetId: string , optional
id of wanted headet which you want to work with it.
If the headsetId is empty, the first headset in list will be set as wanted headset
Returns
-------
None
"""
self.streams = streams
if headsetId != "":
self.c.set_wanted_headset(headsetId)
self.c.open()
def sub(self, streams):
"""
To subscribe to one or more data streams
'eeg': EEG
'mot' : Motion
'dev' : Device information
'met' : Performance metric
'pow' : Band power
Parameters
----------
streams : list, required
list of streams. For example, ['eeg', 'mot']
Returns
-------
None
"""
self.c.sub_request(streams)
def unsub(self, streams):
"""
To unsubscribe to one or more data streams
'eeg': EEG
'mot' : Motion
'dev' : Device information
'met' : Performance metric
'pow' : Band power
Parameters
----------
streams : list, required
list of streams. For example, ['eeg', 'mot']
Returns
-------
None
"""
self.c.unsub_request(streams)
def on_new_data_labels(self, *args, **kwargs):
"""
To handle data labels of subscribed data
Returns
-------
data: list
array of data labels
name: stream name
For example:
eeg: ["COUNTER","INTERPOLATED", "AF3", "T7", "Pz", "T8", "AF4", "RAW_CQ", "MARKER_HARDWARE"]
motion: ['COUNTER_MEMS', 'INTERPOLATED_MEMS', 'Q0', 'Q1', 'Q2', 'Q3', 'ACCX', 'ACCY', 'ACCZ', 'MAGX', 'MAGY', 'MAGZ']
dev: ['AF3', 'T7', 'Pz', 'T8', 'AF4', 'OVERALL']
met : ['eng.isActive', 'eng', 'exc.isActive', 'exc', 'lex', 'str.isActive', 'str', 'rel.isActive', 'rel', 'int.isActive', 'int', 'foc.isActive', 'foc']
pow: ['AF3/theta', 'AF3/alpha', 'AF3/betaL', 'AF3/betaH', 'AF3/gamma', 'T7/theta', 'T7/alpha', 'T7/betaL', 'T7/betaH', 'T7/gamma', 'Pz/theta', 'Pz/alpha', 'Pz/betaL', 'Pz/betaH', 'Pz/gamma', 'T8/theta', 'T8/alpha', 'T8/betaL', 'T8/betaH', 'T8/gamma', 'AF4/theta', 'AF4/alpha', 'AF4/betaL', 'AF4/betaH', 'AF4/gamma']
"""
data = kwargs.get("data")
stream_name = data["streamName"]
stream_labels = data["labels"]
print("{} labels are : {}".format(stream_name, stream_labels))
def on_new_eeg_data(self, *args, **kwargs):
"""
To handle eeg data emitted from Cortex
Returns
-------
data: dictionary
The values in the array eeg match the labels in the array labels return at on_new_data_labels
For example:
{'eeg': [99, 0, 4291.795, 4371.795, 4078.461, 4036.41, 4231.795, 0.0, 0], 'time': 1627457774.5166}
"""
data = kwargs.get("data")
print("eeg data: {}".format(data))
def on_new_mot_data(self, *args, **kwargs):
"""
To handle motion data emitted from Cortex
Returns
-------
data: dictionary
The values in the array motion match the labels in the array labels return at on_new_data_labels
For example: {'mot': [33, 0, 0.493859, 0.40625, 0.46875, -0.609375, 0.968765, 0.187503, -0.250004, -76.563667, -19.584995, 38.281834], 'time': 1627457508.2588}
"""
data = kwargs.get("data")
print("motion data: {}".format(data))
def on_new_dev_data(self, *args, **kwargs):
"""
To handle dev data emitted from Cortex
Returns
-------
data: dictionary
The values in the array dev match the labels in the array labels return at on_new_data_labels
For example: {'signal': 1.0, 'dev': [4, 4, 4, 4, 4, 100], 'batteryPercent': 80, 'time': 1627459265.4463}
"""
data = kwargs.get("data")
print("dev data: {}".format(data))
def on_new_met_data(self, *args, **kwargs):
"""
To handle performance metrics data emitted from Cortex
Returns
-------
data: dictionary
The values in the array met match the labels in the array labels return at on_new_data_labels
For example: {'met': [True, 0.5, True, 0.5, 0.0, True, 0.5, True, 0.5, True, 0.5, True, 0.5], 'time': 1627459390.4229}
"""
data = kwargs.get("data")
print("pm data: {}".format(data))
def on_new_pow_data(self, *args, **kwargs):
"""
To handle band power data emitted from Cortex
Returns
-------
data: dictionary
The values in the array pow match the labels in the array labels return at on_new_data_labels
For example: {'pow': [5.251, 4.691, 3.195, 1.193, 0.282, 0.636, 0.929, 0.833, 0.347, 0.337, 7.863, 3.122, 2.243, 0.787, 0.496, 5.723, 2.87, 3.099, 0.91, 0.516, 5.783, 4.818, 2.393, 1.278, 0.213], 'time': 1627459390.1729}
"""
data = kwargs.get("data")
print("pow data: {}".format(data))
# callbacks functions
def on_create_session_done(self, *args, **kwargs):
print("on_create_session_done")
# subribe data
self.sub(self.streams)
def on_inform_error(self, *args, **kwargs):
error_data = kwargs.get("error_data")
print(error_data)
# -----------------------------------------------------------
#
# GETTING STARTED
# - Please reference to https://emotiv.gitbook.io/cortex-api/ first.
# - Connect your headset with dongle or bluetooth. You can see the headset via Emotiv Launcher
# - Please make sure the your_app_client_id and your_app_client_secret are set before starting running.
# - In the case you borrow license from others, you need to add license = "xxx-yyy-zzz" as init parameter
# RESULT
# - the data labels will be retrieved at on_new_data_labels
# - the data will be retreived at on_new_[dataStream]_data
#
# -----------------------------------------------------------
def main():
# Please fill your application clientId and clientSecret before running script
your_app_client_id = "com.sylviejose.group8,"
your_app_client_secret = "koxn2mNwOUWwlCpKl2uOczIoj7OIOt4qacTi29Ogw4q2zuWG1Mdnt1CdaSV70UWVuSvfp3I2qtJKW6GE9TnV0eS1LxseljTQLTVLRahk9eYjWVswLhrJ8TfsZZLLiOCS"
s = Subcribe(
your_app_client_id,
your_app_client_secret,
)
# list data streams
streams = ["eeg", "mot", "met", "pow"]
s.start(streams)
if __name__ == "__main__":
main()
# -----------------------------------------------------------