brainaccess.utils.acquisition#

Classes#

EEG

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 MNE Raw object. Typical use: create the object, call setup(), start_acquisition(), then stop_acquisition() and get_mne(). Save with raw = eeg.get_mne(); raw.save(...).

Call the methods from one thread at a time.

Creating an EEG initializes the BrainAccess Core library (see brainaccess.core.init()), not a headset; call setup() to connect to one. Creating several EEG objects is safe.

Parameters:

mode (str, default "accumulate") – "accumulate" keeps all data from the start of the acquisition; "roll" keeps only the most recent window_seconds (see setup()).

Raises:

BrainAccessException – If mode is 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 EEGManager first (leave its with block). It is safe to call repeatedly; creating a new EEG initializes the library again.

Raises:

BrainAccessException – If an EEGManager still 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 with start_acquisition() begins a new stream).

Return type:

dict

Notes

EEG takes over the annotations from its manager and empties the manager’s list, so do not call get_annotations() on the same manager.

Raises:

BrainAccessException – If setup() has not been called.

Return type:

dict

get_battery()[source]#

Return the battery level.

Returns:

Battery level in percent.

Return type:

int

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 Raw object.

Pass at most one of tim and samples; 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. After stop_acquisition(), call it again to include the last chunk.

Channel types and units:

  • Electrodes: eeg channels in V (during impedance measurement: misc channels in kOhm; select them by the names given in cap).

  • 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, so tim and the time axis of the result do not measure seconds there; select estimates with samples (samples=1 is the latest).

Parameters:
  • tim (float, optional) – Return only the last tim seconds of data.

  • samples (int, optional) – Return only the last samples samples (impedance data: estimates).

  • annotations (bool, default True) – Attach the annotations made with annotate().

Returns:

The data as an MNE Raw object.

Return type:

mne.io.BaseRaw

Raises:

BrainAccessException – If setup() has not been called, both tim and samples are 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 setup can 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 Ch1 to ChN without 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" (type stim) is recorded.

Raises:

BrainAccessException – If mgr is already connected, no headset is found, the connection fails, the headset is not supported or sfreq is 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 sfreq of 2000 Hz the headset streams at 250 Hz, and the impedance data carries that rate. The EEG buffer keeps sfreq. Passing bias to setup() is recommended for this measurement.

get_mne() raises until the first impedance values have arrived. See get_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(). Call start_acquisition() to record EEG again.

Raises:

BrainAccessException – If setup() has not been called or the stream could not be stopped.