Episode 2: Triggering Data
Cut an epoch around every key press with a Trigger, and watch the average of the epochs build in a TriggerScope.
A brain’s response to a single event is far too small to see in the raw EEG. It appears only when the signal is cut into pieces around each event and the pieces are lined up. This episode cuts one such piece, an epoch, around every key press, and averages them as they arrive.
A Trigger has two inputs: the continuous signal, and the events on its trigger port. For every event whose code is in target, it cuts from time_pre before the event to time_post after it, 0.2 s + 0.7 s, so 225 samples at 250 Hz. Any other code is ignored. An epoch leaves the node only once its last sample exists, so each one appears at least 0.7 s after its key press.
15 # One epoch per Up (38) or Right (39) press, from 0.2 s before it to
16 # 0.7 s after; any other key is ignored
17 trigger = gp.Trigger(time_pre=0.2, time_post=0.7, target=[38, 39])
A TriggerScope shows the average of every epoch so far, recomputed as each new one arrives, while the TimeSeriesScope beside it keeps showing the continuous signal. The events go to the trigger’s port by name.
19 # The average of every epoch so far, beside the continuous signal
20 epochs = gp.TriggerScope(amplitude_limit=30)
21 scope = gp.TimeSeriesScope(amplitude_limit=30, time_window=10)
22
23 p.connect(source, trigger)
24 p.connect(keyboard, trigger[gp.Trigger.PORT_TRIGGER])
25 p.connect(trigger, epochs)
26 p.connect(source, scope)
27 app.add_widget(epochs)
28 app.add_widget(scope)
Run the script and press Up or Right. The first press shows one noisy epoch; the more presses, the smoother the average. Left and Down do nothing: their codes are not in target.
Figure 36: The average over a handful of presses, and the signal they were cut from.
Connected to a Collector rather than a scope, the epochs stack into one array shaped (time, channel, trial), and result.times counts from the start of each epoch, so the event itself sits at 0.2 s, not at 0. Only epochs can go into a TriggerScope: its input takes epochs, not a stream that arrives sample after sample, so the continuous signal has to pass through a Trigger, which cuts it into epochs around the events. And the events need not come from a keyboard: a Marker sends them from your own code, marker.emit(code), when your program is what knows the moment something happened.
In this episode, you learned how to:
cut an epoch around every event of a chosen code with a Trigger
watch the epochs’ running average in a TriggerScope
know what else an epoch stream can feed, and what it cannot
Proceed to Episode 3, where a paradigm supplies the events and the response is a real one.
Nodes in this episode: Generator, Keyboard, Trigger, TriggerScope, TimeSeriesScope
File s5e2_triggering_data.py – View file on GitHub
1"""Season 5, Episode 2: an epoch cut around every key press, and averaged."""
2
3import gpype as gp
4
5if __name__ == "__main__":
6
7 app = gp.MainApp()
8 p = gp.Pipeline()
9
10 source = gp.Generator(sampling_rate=250, channel_count=8,
11 signal_frequency=10, signal_amplitude=10,
12 noise_amplitude=10)
13 keyboard = gp.Keyboard()
14
15 # One epoch per Up (38) or Right (39) press, from 0.2 s before it to
16 # 0.7 s after; any other key is ignored
17 trigger = gp.Trigger(time_pre=0.2, time_post=0.7, target=[38, 39])
18
19 # The average of every epoch so far, beside the continuous signal
20 epochs = gp.TriggerScope(amplitude_limit=30)
21 scope = gp.TimeSeriesScope(amplitude_limit=30, time_window=10)
22
23 p.connect(source, trigger)
24 p.connect(keyboard, trigger[gp.Trigger.PORT_TRIGGER])
25 p.connect(trigger, epochs)
26 p.connect(source, scope)
27 app.add_widget(epochs)
28 app.add_widget(scope)
29
30 p.start()
31 app.run()
32 p.stop()
33 p.close()