Episode 2: Unicorn Hybrid Black
Stream a Unicorn Hybrid Black, the wireless 8-channel EEG headset with a motion sensor, into one pipeline, and give each kind of data its own filter chain and scope.
The Unicorn delivers more than EEG. This episode takes all of it and routes each stream to where it belongs.
Install the
devicesextra:pip install "gpype[devices]". It bringsgtec_unicorn, the Unicorn’s driver, on Windows x64, Linux x86_64 and macOS on Apple silicon. The Unicorn Suite is not needed.On Windows, pair the Unicorn with the computer first, in the Bluetooth settings; macOS and Linux open a discoverable Unicorn paired or not.
Activate a Unicorn Python Hybrid Black licence. Without one the Unicorn cannot be opened, and
p.start()raisesgtec_unicorn.LicenseError, naming the licence it needs.
HybridBlack always runs at 250 Hz and delivers the eight EEG channels in µV. Three switches add the rest, all off unless you ask for them: include_accel adds the accelerometer’s X, Y, Z in g; include_gyro the gyroscope’s in degrees per second; include_aux the battery in percent, a counter and a validation flag. With all three on, that is 17 channels.
10 # 8 EEG channels at 250 Hz, plus the optional streams: 3 accelerometer,
11 # 3 gyroscope, and battery, counter, validation. 17 channels in all
12 source = gp.HybridBlack(include_accel=True, include_gyro=True,
13 include_aux=True)
A Router separates the streams. Only the EEG goes through the bandpass and the two notch filters; each stream gets a scope scaled for its own unit, and each scope is named, because five scopes sharing the default name could not be saved.
15 # Split them by stream. The counter (channel 15) is left out: a ramp
16 # no fixed axis shows, and a lost sample does not show in it
17 splitter = gp.Router(input_channels=gp.Router.ALL,
18 output_channels={"EEG": list(range(8)),
19 "ACC": [8, 9, 10],
20 "GYRO": [11, 12, 13],
21 "BATTERY": [14],
22 "VALIDATION": [16]})
23
24 # Only the EEG is filtered
25 bandpass = gp.Bandpass(f_lo=1, f_hi=30)
26 notch50 = gp.Bandstop(f_lo=48, f_hi=52)
27 notch60 = gp.Bandstop(f_lo=58, f_hi=62)
28
29 # One scope per stream, each scaled for its own unit: uV, g, deg/s,
30 # percent, and 1 or 0. Named: five scopes sharing the default name
31 # could not be saved
32 eeg_scope = gp.TimeSeriesScope(name="eeg", amplitude_limit=50,
33 time_window=10)
34 acc_scope = gp.TimeSeriesScope(name="acc", amplitude_limit=2,
35 time_window=10)
36 gyro_scope = gp.TimeSeriesScope(name="gyro", amplitude_limit=250,
37 time_window=10)
38 battery_scope = gp.TimeSeriesScope(name="battery", amplitude_limit=100,
39 time_window=10)
40 valid_scope = gp.TimeSeriesScope(name="validation", amplitude_limit=1,
41 time_window=10)
Run the script. At rest, gravity alone reads 1 g on one accelerometer axis, which makes that scope a quick check that the data is real. A lost sample does not show up where you would look for it: the counter keeps counting across a dropout, because the driver fills the gap with fabricated scans. Only the validation channel tells a filled-in sample from a real one, 1 for real and 0 for filled, which is why the example plots validation and leaves the counter out.
Bluetooth delivers in bursts. The Unicorn uses classic Bluetooth, which hands samples over in bursts rather than one at a time. If three reads in a row fail, acquisition ends with an error in the log telling you to check the device and start the pipeline again; it does not resume on its own.
On macOS, keep the window. macOS hands every Bluetooth callback of the Unicorn’s driver to the main thread’s run loop, and only the main thread can run it. The example’s app.run() does. A script without a window, whose main thread waits in time.sleep, receives nothing: its first read fails with “… the main run loop is not being serviced, and IOBluetooth delivers RFCOMM data there”.
More than one Unicorn paired? Without a serial, the node opens the first Unicorn the operating system lists, and that order is not defined. Name the one you mean: gp.HybridBlack(serial="UN-XXXX.XX.XX"). No electrodes at hand? test_signal=True makes the Unicorn send its built-in test signal instead of the inputs.
In this episode, you learned how to:
switch on the Unicorn’s motion and status streams
split one stream into several with a Router and filter only the EEG
read the validation channel for lost samples
Proceed to Episode 3, where a GNautilus arrives through a base station and the GDS service.
Nodes in this episode: HybridBlack, Router, Bandpass, Bandstop, TimeSeriesScope
File s3e2_hybrid_black.py – View file on GitHub
1"""Season 3, Episode 2: a Unicorn's EEG, motion and status in one pipeline."""
2
3import gpype as gp
4
5if __name__ == "__main__":
6
7 app = gp.MainApp()
8 p = gp.Pipeline()
9
10 # 8 EEG channels at 250 Hz, plus the optional streams: 3 accelerometer,
11 # 3 gyroscope, and battery, counter, validation. 17 channels in all
12 source = gp.HybridBlack(include_accel=True, include_gyro=True,
13 include_aux=True)
14
15 # Split them by stream. The counter (channel 15) is left out: a ramp
16 # no fixed axis shows, and a lost sample does not show in it
17 splitter = gp.Router(input_channels=gp.Router.ALL,
18 output_channels={"EEG": list(range(8)),
19 "ACC": [8, 9, 10],
20 "GYRO": [11, 12, 13],
21 "BATTERY": [14],
22 "VALIDATION": [16]})
23
24 # Only the EEG is filtered
25 bandpass = gp.Bandpass(f_lo=1, f_hi=30)
26 notch50 = gp.Bandstop(f_lo=48, f_hi=52)
27 notch60 = gp.Bandstop(f_lo=58, f_hi=62)
28
29 # One scope per stream, each scaled for its own unit: uV, g, deg/s,
30 # percent, and 1 or 0. Named: five scopes sharing the default name
31 # could not be saved
32 eeg_scope = gp.TimeSeriesScope(name="eeg", amplitude_limit=50,
33 time_window=10)
34 acc_scope = gp.TimeSeriesScope(name="acc", amplitude_limit=2,
35 time_window=10)
36 gyro_scope = gp.TimeSeriesScope(name="gyro", amplitude_limit=250,
37 time_window=10)
38 battery_scope = gp.TimeSeriesScope(name="battery", amplitude_limit=100,
39 time_window=10)
40 valid_scope = gp.TimeSeriesScope(name="validation", amplitude_limit=1,
41 time_window=10)
42
43 p.connect(source, splitter)
44 p.connect(splitter["EEG"], bandpass)
45 p.connect(bandpass, notch50)
46 p.connect(notch50, notch60)
47 p.connect(notch60, eeg_scope)
48 p.connect(splitter["ACC"], acc_scope)
49 p.connect(splitter["GYRO"], gyro_scope)
50 p.connect(splitter["BATTERY"], battery_scope)
51 p.connect(splitter["VALIDATION"], valid_scope)
52
53 for scope in (eeg_scope, acc_scope, gyro_scope, battery_scope,
54 valid_scope):
55 app.add_widget(scope)
56
57 p.start()
58 app.run()
59 p.stop()
60 p.close()