brainaccess.utils.acquisition#
Classes#
High-level EEG acquisition. |
Module Contents#
- class brainaccess.utils.acquisition.EEG(mode='accumulate')[source]#
High-level EEG acquisition.
Connects to a headset through an
EEGManager, collects the stream into a buffer and returns it as an MNERawobject. Typical use: create the object, callsetup(),start_acquisition(), thenstop_acquisition()andget_mne(). Save withraw = eeg.get_mne(); raw.save(...).Call the methods from one thread at a time.
Creating an
EEGinitializes the BrainAccess Core library (seebrainaccess.core.init()), not a headset; callsetup()to connect to one. Creating severalEEGobjects is safe.- Parameters:
mode (str, default "accumulate") –
"accumulate"keeps all data from the start of the acquisition;"roll"keeps only the most recentwindow_seconds(seesetup()).- Raises:
BrainAccessException – If
modeis not"accumulate"or"roll", or the library could not be initialized.
- annotate(msg)[source]#
Add an annotation to the running stream.
- Parameters:
msg (str) – Annotation text.
- Raises:
BrainAccessException – If
setup()has not been called, no stream is running or the annotation could not be sent.- Return type:
None
- close()[source]#
Close the BrainAccess Core library.
This is a process-wide shutdown, not a per-device close. Destroy every
EEGManagerfirst (leave itswithblock). It is safe to call repeatedly; creating a newEEGinitializes the library again.- Raises:
BrainAccessException – If an
EEGManagerstill exists.
- get_annotations()[source]#
Return the annotations of the active buffer.
- Returns:
Dictionary with the keys
"annotations"(texts),"timestamps"(sample numbers, counted from the start of the stream in which each annotation was made) and"streams"(the index of that stream within the buffer; each restart withstart_acquisition()begins a new stream).- Return type:
Notes
EEGtakes over the annotations from its manager and empties the manager’s list, so do not callget_annotations()on the same manager.- Raises:
BrainAccessException – If
setup()has not been called.- Return type:
- get_battery()[source]#
Return the battery level.
- Returns:
Battery level in percent.
- Return type:
- Raises:
BrainAccessException – If
setup()has not been called or the level could not be read.
- get_mne(tim=None, samples=None, annotations=True)[source]#
Return the acquired data as an MNE
Rawobject.Pass at most one of
timandsamples; with neither, all buffered data is returned. In"roll"mode that is the whole window, padded with zeros at the start until the window has filled. The data can be read while a stream is running. Afterstop_acquisition(), call it again to include the last chunk.Channel types and units:
Electrodes:
eegchannels in V (during impedance measurement:miscchannels in kOhm; select them by the names given incap).Accelerometer axes in m/s² and gyroscope axes in deg/s:
misc."Digital"(if requested):stim."Sample", the sample number:syst.
Impedance data is kept in its own buffer;
start_acquisition()returns to the EEG buffer. An impedance buffer holds one row per impedance estimate rather than one per sample, sotimand the time axis of the result do not measure seconds there; select estimates withsamples(samples=1is the latest).- Parameters:
tim (float, optional) – Return only the last
timseconds of data.samples (int, optional) – Return only the last
samplessamples (impedance data: estimates).annotations (bool, default True) – Attach the annotations made with
annotate().
- Returns:
The data as an MNE
Rawobject.- Return type:
- Raises:
BrainAccessException – If
setup()has not been called, bothtimandsamplesare given, one of them is not positive, or no data has been acquired yet.
- setup(mgr, device_name, cap=None, zeros_at_start=0, bias=None, gain=8, sfreq=250, window_seconds=10.0, digital_input=False)[source]#
Connect to a headset and choose the channels to record.
Connecting is retried for about two seconds before giving up. If anything fails after the connection is made, the manager is disconnected again so that
setupcan be called once more.- Parameters:
mgr (EEGManager) – A new, unconnected manager.
device_name (str) – Exact advertised name of the headset, for example
"BA HALO 001".cap (dict, optional) – Maps 0-based electrode indices to channel names. Electrodes not listed are not recorded. By default an eight-electrode 10-20 layout is used (F3, F4, C3, C4, P3, P4, O1, O2); a headset with fewer electrodes (HALO) gets one channel per electrode named
Ch1toChNwithout montage positions. An empty dict records no electrodes, only the motion sensors and the sample number.zeros_at_start (int, default 0) – Number of zero samples placed in front of the data in
"accumulate"mode.bias (list of int, optional) – 0-based indices of the electrodes used for the bias (reference drive) feedback. Recommended for impedance measurement.
gain (int, default 8) – Amplifier gain of the electrodes: 4, 6, 8 or 12. Any other value triggers a warning and gain 8 is used.
sfreq (int, default 250) – Sample rate in Hz. Every headset supports 250 and 500; HALO V3 also 1000; EXG8 also 1000 and 2000.
window_seconds (float, default 10.0) – Length of the rolling window in seconds. Only used in
"roll"mode (ignored in"accumulate"mode).digital_input (bool, default False) – Debug feature: standard BrainAccess headsets do not provide a digital input; on them the channel is always 0. Requires headset firmware 3.8.1 or newer. If
True, a channel named"Digital"(typestim) is recorded.
- Raises:
BrainAccessException – If
mgris already connected, no headset is found, the connection fails, the headset is not supported orsfreqis not supported by the headset.- Return type:
None
- start_acquisition()[source]#
Start streaming EEG into the EEG buffer.
Starting again after
stop_acquisition()appends to the same buffer.- Raises:
BrainAccessException – If
setup()has not been called, a stream is already running or the stream could not be started.
- start_impedance_measurement()[source]#
Start measuring electrode impedance.
Impedance data goes into a fresh buffer, separate from the EEG buffer. The measurement uses a 31.2 Hz test wave, which a headset cannot measure at sample rates above 1000 Hz; with
sfreqof 2000 Hz the headset streams at 250 Hz, and the impedance data carries that rate. The EEG buffer keepssfreq. Passingbiastosetup()is recommended for this measurement.get_mne()raises until the first impedance values have arrived. Seeget_mne()for how impedance data is represented.- Raises:
BrainAccessException – If
setup()has not been called, a stream is already running or the stream could not be started.
- stop_acquisition()[source]#
Stop the running stream.
The data stays in the buffer. Call
get_mne()afterwards to include the last chunk.- Raises:
BrainAccessException – If
setup()has not been called or the stream could not be stopped.
- stop_impedance_measurement()[source]#
Stop the impedance measurement.
The impedance data stays available through
get_mne(). Callstart_acquisition()to record EEG again.- Raises:
BrainAccessException – If
setup()has not been called or the stream could not be stopped.