Episode 4: Oddball Paradigms

Play a rare tone among frequent ones, and compare the average response to each with two Trigger nodes and one TriggerScope that draws their difference.

A rare sound among frequent ones draws attention, and attention leaves a trace of its own in the EEG: the P300, largest over the parietal midline some 300 to 500 ms after a rare tone the listener is counting. This episode plays a rare and a frequent tone, and compares the average response to each. Count the rare tones silently while it runs: heard passively, a rare tone leaves a smaller and earlier response instead. The pipeline is Episode 3’s; what changes is the paradigm and the triggers. It needs Paradigm Presenter, and so runs on Windows only.

The paradigm

AEPOddball.xml plays 480 tones of 0.25 s each, in random order: one in eight is the target (code 1), the rest are non-targets (code 2). A pause of 0.2, 0.3 or 0.4 s follows each. With 20 s of rest before and 10 s after, a run takes about five minutes and gives about 60 targets and 420 non-targets. One tone every 0.45 to 0.65 s against an epoch of 0.9 s means the epochs overlap: each holds the next tone’s onset, and each baseline the tail of the tone before. The difference wave is what makes that tolerable, since both averages carry the same overlap and it cancels between them; the baseline stays contaminated either way.

12PARADIGM = os.path.join(os.path.dirname(os.path.abspath(__file__)),
13                        "paradigms", "AEPOddball.xml")
14CHANNELS = 8
15TARGET = 1  # the rare tone's code
16NONTARGET = 2  # the frequent one's
Two triggers, one scope, three curves

One Trigger cuts epochs around code 1, another around code 2. The TriggerScope is given plots=["target", "nontarget", "target-nontarget"]: every name in those expressions becomes one of its inputs, and each entry becomes a curve. The third is computed by the scope from the other two averages. It is the difference wave, which is where the rare tone’s effect shows.

65    # One trigger per code, and a scope with a curve per average and a
66    # third for their difference
67    rare = gp.Trigger(time_pre=0.2, time_post=0.7, target=TARGET)
68    frequent = gp.Trigger(time_pre=0.2, time_post=0.7, target=NONTARGET)
69    average = gp.TriggerScope(amplitude_limit=5,
70                              plots=["target", "nontarget",
71                                     "target-nontarget"])
72    p.connect(notch60, rare)
73    p.connect(presenter, rare[gp.Trigger.PORT_TRIGGER])
74    p.connect(notch60, frequent)
75    p.connect(presenter, frequent[gp.Trigger.PORT_TRIGGER])
76    p.connect(rare, average["target"])
77    p.connect(frequent, average["nontarget"])
The rest is Episode 3

The scope gains a second marker colour for the non-targets, and the file is AEPOddball.csv. The filters, the raw recording and the M key are unchanged.

43    markers = [mk(color="r", label="target", channel=CHANNELS,
44                  value=TARGET),
45               mk(color="g", label="nontarget", channel=CHANNELS,
46                  value=NONTARGET),
47               mk(color="c", label="M key", channel=CHANNELS + 1, value=77)]
Result

Run the script and press Start. The rare average is the noisy one: averaging reduces noise by the square root of the number of epochs, as Episode 6 measures, so 60 targets keep about 2.6 times the noise of 420 non-targets, the square root of 420 / 60. Judge the difference wave with that in mind, and let the run finish.

S5E4 example screenshot

Figure 38: Rare, frequent and their difference, part way through a run on a BCI Core-8.

Summary

In this episode, you learned how to:

  • average two kinds of event separately with one Trigger each

  • let a TriggerScope draw a curve per average, and one for their difference

  • expect the rarer average to be the noisier one, by a known factor

Proceed to Episode 5, where two codes are grouped into one category and compared with a third.

Nodes in this episode: Generator, Bandpass, Bandstop, Keyboard, Router, TimeSeriesScope, CsvWriter, Trigger, TriggerScope, ParadigmPresenter

File s5e4_oddball_paradigms.py – View file on GitHub

 1"""Season 5, Episode 4: a rare tone among frequent ones, two averages.
 2
 3Windows only: it needs Paradigm Presenter, see Episode 1. The generator
 4stands in for an amplifier; to record a person, uncomment one amplifier
 5line and comment out the generator.
 6"""
 7
 8import os
 9
10import gpype as gp
11
12PARADIGM = os.path.join(os.path.dirname(os.path.abspath(__file__)),
13                        "paradigms", "AEPOddball.xml")
14CHANNELS = 8
15TARGET = 1  # the rare tone's code
16NONTARGET = 2  # the frequent one's
17
18if __name__ == "__main__":
19
20    app = gp.MainApp()
21    p = gp.Pipeline()
22
23    # amp = gp.BCICore(channel_count=CHANNELS)
24    # amp = gp.GNautilus(sampling_rate=250, channel_count=CHANNELS)
25    amp = gp.Generator(sampling_rate=250, channel_count=CHANNELS,
26                       signal_frequency=10, signal_amplitude=15,
27                       noise_amplitude=10)
28
29    # Filtered for display and epoching: 1 to 30 Hz, both mains notches
30    bandpass = gp.Bandpass(f_lo=1, f_hi=30)
31    notch50 = gp.Bandstop(f_lo=48, f_hi=52)
32    notch60 = gp.Bandstop(f_lo=58, f_hi=62)
33    p.connect(amp, bandpass)
34    p.connect(bandpass, notch50)
35    p.connect(notch50, notch60)
36
37    # The presenter receives the codes; the M key (77) marks by hand
38    presenter = gp.ParadigmPresenter(PARADIGM)
39    keyboard = gp.Keyboard()
40
41    # Filtered signal, codes and key presses side by side on one scope
42    mk = gp.TimeSeriesScope.Markers
43    markers = [mk(color="r", label="target", channel=CHANNELS,
44                  value=TARGET),
45               mk(color="g", label="nontarget", channel=CHANNELS,
46                  value=NONTARGET),
47               mk(color="c", label="M key", channel=CHANNELS + 1, value=77)]
48    scope = gp.TimeSeriesScope(amplitude_limit=50, time_window=10,
49                               markers=markers,
50                               hidden_channels=[CHANNELS, CHANNELS + 1])
51    shown = gp.Router(input_channels=[gp.Router.ALL] * 3)
52    p.connect(notch60, shown["in1"])
53    p.connect(presenter, shown["in2"])
54    p.connect(keyboard, shown["in3"])
55    p.connect(shown, scope)
56
57    # The recording keeps the unfiltered signal, with the same two beside
58    # it, in a file named after the paradigm
59    kept = gp.Router(input_channels=[gp.Router.ALL] * 3)
60    p.connect(amp, kept["in1"])
61    p.connect(presenter, kept["in2"])
62    p.connect(keyboard, kept["in3"])
63    p.connect(kept, gp.CsvWriter(file_name="AEPOddball.csv"))
64
65    # One trigger per code, and a scope with a curve per average and a
66    # third for their difference
67    rare = gp.Trigger(time_pre=0.2, time_post=0.7, target=TARGET)
68    frequent = gp.Trigger(time_pre=0.2, time_post=0.7, target=NONTARGET)
69    average = gp.TriggerScope(amplitude_limit=5,
70                              plots=["target", "nontarget",
71                                     "target-nontarget"])
72    p.connect(notch60, rare)
73    p.connect(presenter, rare[gp.Trigger.PORT_TRIGGER])
74    p.connect(notch60, frequent)
75    p.connect(presenter, frequent[gp.Trigger.PORT_TRIGGER])
76    p.connect(rare, average["target"])
77    p.connect(frequent, average["nontarget"])
78
79    app.add_widget(presenter)
80    app.add_widget(scope)
81    app.add_widget(average)
82
83    p.start()
84    app.run()
85    p.stop()
86    p.close()