-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheeg_headset_mock.py
More file actions
240 lines (196 loc) · 8.71 KB
/
eeg_headset_mock.py
File metadata and controls
240 lines (196 loc) · 8.71 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
"""
eeg_headset_mock.py
A mock version of the EEG headset that simulates the functionality without requiring
the actual BrainAccess library. This allows the application to run on ARM64 systems
until the proper ARM64 library is available.
"""
import os
import time
from typing import Any, Dict, List, Optional
import numpy as np
import logging
from pathlib import Path
class EEGHeadset:
"""
Mock version of EEG headset interface that simulates connection and data acquisition
without requiring the actual BrainAccess library.
"""
def __init__(self, participant_id: str, logger: logging.Logger) -> None:
"""
Initialize the mock EEG headset interface.
Args:
participant_id (str): ID to use as folder name for saved data.
logger (logging.Logger): Logger for recording information and errors.
"""
self.logger = logger
self._is_connected = False
self._is_recording = False
self._participant_id = participant_id
self._data_folder_path = "data"
self._save_dir_path = os.path.join(self._data_folder_path, participant_id)
self._connection_attempts = 0
self._max_attempts = 3
self._annotations = []
self._recording_start_time = 0
# Create directories for data storage
self._create_dir_if_not_exist(self._data_folder_path)
self._create_dir_if_not_exist(self._save_dir_path)
# Mock initialization of BrainAccess library
self.logger.info("Initializing mock BrainAccess library...")
self.logger.info("Note: This is a mock implementation. Actual EEG functionality requires ARM64 library.")
def connect(self) -> bool:
"""
Mock connection to the BrainAccess Halo headset.
Returns:
bool: True if connection was successful, False otherwise.
"""
if self._is_connected:
self.logger.info("Already connected to the headset (mock).")
return True
from eeg_config import PORT, USED_DEVICE
self.logger.info(f"Attempting to connect to BrainAccess Halo on port {PORT}... (mock)")
# Simulate connection process
self._is_connected = True
self.logger.info("Successfully connected to BrainAccess Halo! (mock)")
return True
def disconnect(self) -> None:
"""
Mock disconnection from the BrainAccess Halo headset.
"""
if not self._is_connected:
self.logger.info("Not connected to any headset.")
return
if self._is_recording:
self.stop_recording()
self._is_connected = False
self.logger.info("Disconnected from BrainAccess Halo. (mock)")
def start_recording(self, filepath: str) -> bool:
"""
Mock start recording EEG data.
Args:
filepath (str): Path where data will be saved.
Returns:
bool: True if recording started successfully, False otherwise.
"""
if not self._is_connected:
if not self.connect():
self.logger.error("Cannot start recording: Failed to connect to the headset.")
return False
if self._is_recording:
self.logger.warning("Called start_recording when already recording. Forcing stop of previous one.")
self.stop_recording()
try:
self.logger.info("Starting EEG data acquisition... (mock)")
self._is_recording = True
self._session_name = os.path.basename(filepath)
self._filepath = filepath
self._recording_start_time = time.time()
self._annotate_internal("Recording started (mock)")
self.logger.info(f"Recording started: {filepath} (mock)")
return True
except Exception as e:
self.logger.error(f"Error starting recording: {str(e)}")
return False
def stop_recording(self) -> bool:
"""
Mock stop recording and save simulated data.
Returns:
bool: True if data was saved successfully, False otherwise.
"""
if not self._is_recording:
self.logger.info("No active recording to stop.")
return False
try:
self._annotate_internal("Recording ended (mock)")
self.logger.info("Processing recorded data... (mock)")
# Generate simulated EEG data
from eeg_config import SAMPLING_RATE
duration = 10 # seconds of simulated data
n_channels = len([ch for ch in range(32)]) # Using the channel count from config
n_samples = duration * SAMPLING_RATE
# Create mock EEG data (random noise with some structure)
mock_data = np.random.normal(0, 10, (n_channels, n_samples)).astype(np.float32)
# Add some simulated signal patterns
t = np.linspace(0, duration, n_samples)
for i in range(min(n_channels, 4)): # Add alpha waves to first 4 channels
mock_data[i, :] += 5 * np.sin(2 * np.pi * 10 * t) # 10Hz alpha waves
# Create a mock MNE Raw object-like structure
class MockRaw:
def __init__(self, data, sfreq, ch_names):
self.data = data
self.info = {'sfreq': sfreq}
self.ch_names = ch_names
def save(self, fname):
# Save the mock data to the specified file
import pickle
mock_data_dict = {
'data': self.data,
'sfreq': self.info['sfreq'],
'ch_names': self.ch_names,
'annotations': getattr(self, '_annotations', [])
}
Path(fname).parent.mkdir(parents=True, exist_ok=True)
with open(fname, 'wb') as f:
pickle.dump(mock_data_dict, f)
self.logger.info(f"Mock EEG data saved to {fname}")
# Create mock raw object with channel names from config
from eeg_config import channels
ch_names = channels[:n_channels] # Use appropriate number of channels
raw_data = MockRaw(mock_data, SAMPLING_RATE, ch_names)
raw_data._annotations = self._annotations
raw_data.logger = self.logger
if raw_data is not None:
self.logger.info(f"Saving mock EEG data to {self._filepath}")
raw_data.save(self._filepath)
self.logger.info("Recording stopped and mock data saved successfully.")
else:
self.logger.warning("No data to save - get_mne() returned None")
return False
return True
except Exception as e:
self.logger.error(f"Error stopping recording: {e}", exc_info=True)
return False
finally:
# Always reset the recording state to prevent the experiment from getting stuck
self._is_recording = False
def annotate(self, annotation: str) -> None:
"""
Add an annotation to the EEG data.
Args:
annotation (str): Annotation text to add.
"""
self._annotate_internal(annotation)
def _annotate_internal(self, annotation: str) -> None:
"""
Internal method to add an annotation to the EEG data.
Args:
annotation (str): Annotation text to add.
"""
if not self._is_connected:
self.logger.warning(f"Cannot annotate '{annotation}': Not connected to the headset.")
return
try:
timestamp = time.time() - self._recording_start_time if self._is_recording else 0
# In mock, we just store the annotation
self._annotations.append({"timestamp": timestamp, "annotation": annotation})
self.logger.info(f"Annotation added: '{annotation}' at {timestamp:.2f}s")
except Exception as e:
self.logger.error(f"Error adding annotation: {str(e)}")
def is_recording(self) -> bool:
"""Check if the headset is recording data"""
return self._is_recording
def is_acquiring(self) -> bool:
"""
Check if the headset is acquiring data.
For this class, recording and acquiring are the same state.
"""
return self.is_recording()
def _create_dir_if_not_exist(self, path: str) -> None:
"""
Create a directory if it does not exist.
Args:
path (str): Directory path to create.
"""
if not os.path.exists(path):
os.makedirs(path)
self.logger.info(f"Created directory: {path}")