Episode 7: Showing a Decision
Show a decision as a number with a ResultScope: a BandPower measured twice a second, a Threshold that turns it into 0 or 1, and the labels that name them.
A BCI ends in a decision, and a decision is not a signal. It is one value, a few times a second, that says yes or no. A time-series scope needs a sampling rate and draws a trace against time; a decision needs neither.
Three Generator nodes and an Equation, as in Season 2, Episode 3: 10 Hz alpha, switched on for 2.5 s and off for 2.5 s by a 0.2 Hz rectangle, in noise.
10 # 10 Hz alpha switched on for 2.5 s and off for 2.5 s, in noise
11 alpha = gp.Generator(name="alpha", sampling_rate=250, channel_count=1,
12 signal_frequency=10, signal_amplitude=10)
13 gate = gp.Generator(name="gate", sampling_rate=250, channel_count=1,
14 signal_frequency=0.2, signal_shape="rect",
15 signal_amplitude=1)
16 noise = gp.Generator(name="noise", sampling_rate=250, channel_count=1,
17 noise_amplitude=3)
18 eeg = gp.Equation("a * (1 + g) / 2 + n")
19 p.connect(alpha, eeg["a"])
20 p.connect(gate, eeg["g"])
21 p.connect(noise, eeg["n"])
An FFT over a one-second window answers once per half window, and a BandPower reduces each spectrum to the alpha band’s power. A Threshold turns that into 0 or 1.
23 # One spectrum per half second (window 1 s, overlap 0.5), reduced to
24 # the alpha band's power
25 fft = gp.FFT(window_size=250)
26 power = gp.BandPower(bands=["alpha"])
27 p.connect(eeg, fft)
28 p.connect(fft, power)
29
30 # The decision: 1 while alpha is present
31 decision = gp.Threshold(level=20)
32 p.connect(power, decision)
A Router puts measurement and decision side by side, and a ChannelLabeler names them, since the readout shows whatever the labels say.
34 # Measurement and decision side by side, named for the readout
35 both = gp.Router(input_channels=[gp.Router.ALL, gp.Router.ALL])
36 names = gp.ChannelLabeler(labels=["alpha power", "alpha present"])
37 p.connect(power, both["in1"])
38 p.connect(decision, both["in2"])
39 p.connect(both, names)
A ResultScope shows the current value of each channel as a number, named by the channel’s label. It reads no sampling rate, so it takes a classifier output, a band power or a sparse label alike, and it draws in the same window as any other widget. history=4 shows the last four values beside the current one, and decimals= sets how many digits they keep. The signal the decision is made from goes to a TimeSeriesScope for comparison.
41 # The last four values beside the current one, one decimal
42 readout = gp.ResultScope(name="decision", history=4, decimals=1)
43 p.connect(names, readout)
44
45 # The signal the decision is made from, for comparison
46 scope = gp.TimeSeriesScope(name="eeg", amplitude_limit=30,
47 time_window=10)
48 p.connect(eeg, scope)
49
50 app.add_widget(scope)
51 app.add_widget(readout)
Run the script. The readout’s two rows, alpha power and alpha present, update twice a second; the decision lags the burst by up to a second, because the FFT answers once per half window, over a window one second long. Season 2, Episode 11 explains that trade.
Figure 40: A measurement and a decision as numbers, beside the signal they come from.
In this episode, you learned how to:
turn a measurement into a decision with a Threshold
name rows for a readout with a ChannelLabeler
show values that have no sampling rate in a ResultScope
That closes Season 5. Proceed to Season 6, where a pipeline that does not do what you expect is read for what it says.
Nodes in this episode: Generator, Equation, FFT, BandPower, Threshold, Router, ChannelLabeler, ResultScope, TimeSeriesScope
File s5e7_showing_a_decision.py – View file on GitHub
1"""Season 5, Episode 7: a decision shown as a number, not as a trace."""
2
3import gpype as gp
4
5if __name__ == "__main__":
6
7 app = gp.MainApp()
8 p = gp.Pipeline()
9
10 # 10 Hz alpha switched on for 2.5 s and off for 2.5 s, in noise
11 alpha = gp.Generator(name="alpha", sampling_rate=250, channel_count=1,
12 signal_frequency=10, signal_amplitude=10)
13 gate = gp.Generator(name="gate", sampling_rate=250, channel_count=1,
14 signal_frequency=0.2, signal_shape="rect",
15 signal_amplitude=1)
16 noise = gp.Generator(name="noise", sampling_rate=250, channel_count=1,
17 noise_amplitude=3)
18 eeg = gp.Equation("a * (1 + g) / 2 + n")
19 p.connect(alpha, eeg["a"])
20 p.connect(gate, eeg["g"])
21 p.connect(noise, eeg["n"])
22
23 # One spectrum per half second (window 1 s, overlap 0.5), reduced to
24 # the alpha band's power
25 fft = gp.FFT(window_size=250)
26 power = gp.BandPower(bands=["alpha"])
27 p.connect(eeg, fft)
28 p.connect(fft, power)
29
30 # The decision: 1 while alpha is present
31 decision = gp.Threshold(level=20)
32 p.connect(power, decision)
33
34 # Measurement and decision side by side, named for the readout
35 both = gp.Router(input_channels=[gp.Router.ALL, gp.Router.ALL])
36 names = gp.ChannelLabeler(labels=["alpha power", "alpha present"])
37 p.connect(power, both["in1"])
38 p.connect(decision, both["in2"])
39 p.connect(both, names)
40
41 # The last four values beside the current one, one decimal
42 readout = gp.ResultScope(name="decision", history=4, decimals=1)
43 p.connect(names, readout)
44
45 # The signal the decision is made from, for comparison
46 scope = gp.TimeSeriesScope(name="eeg", amplitude_limit=30,
47 time_window=10)
48 p.connect(eeg, scope)
49
50 app.add_widget(scope)
51 app.add_widget(readout)
52
53 p.start()
54 app.run()
55 p.stop()
56 p.close()