Episode 11: Record a Run, Replay It Anywhere
Record every source of a pipeline with one launch flag, and replay the run with another, on a machine with nothing plugged in.
Episodes 1 and 5 put a writer in the pipeline. There is a second way to record that needs no writer at all: --save-as records what every source produces, and --load-from replays it in their place. The pipeline does not know which of the three it is running in.
The recorder of Episode 1, without the writer. What is new is only the names: a recording is filed under its source’s name, so the generator is eeg and the keyboard keys.
25 app = gp.MainApp()
26 p = gp.Pipeline()
27
28 # The names are what the recordings are filed under: eeg.h5, keys.h5
29 source = gp.Generator(name="eeg", sampling_rate=250, channel_count=8,
30 signal_frequency=10, signal_amplitude=10,
31 noise_amplitude=10)
32 keyboard = gp.Keyboard(name="keys")
33 router = gp.Router(input_channels=[gp.Router.ALL, gp.Router.ALL])
LaunchConfig holds what the command line said. The script reads it only to say so; the pipeline below does not consult it.
14 # What was asked for on the command line, read only to say so; the
15 # pipeline below does not consult it
16 launch = gp.LaunchConfig.get()
17 if launch.load_from:
18 print(f"Replaying '{launch.load_from}'. Hands off the keyboard.")
19 elif launch.save_as:
20 print(f"Recording every source under '{launch.save_as}_*'.")
21 else:
22 print("Live run, nothing recorded. To record, pass --save-as "
23 "my_run; to replay, pass --load-from my_run_<timestamp>")
Run it with --save-as:
python s4e11_record_and_replay.py --save-as my_run
Press a few arrow keys and close the window. The run went into a directory my_run_<timestamp>, one HDF5 file per source, named after the node. The directory carries a timestamp so a second recording never overwrites the first, which is why the script looks it up rather than guesses it, and prints the command that replays it.
54 # The run directory carries a timestamp, so it is looked up rather
55 # than guessed
56 if launch.save_as:
57 stem = Path(launch.save_as)
58 runs = sorted(stem.parent.glob(stem.name + "_*"))
59 if runs:
60 newest = runs[-1]
61 print(f"\nRecorded {len(list(newest.glob('*.h5')))} stream(s):")
62 for entry in sorted(newest.iterdir()):
63 print(f" {entry.name}")
64 print(f"\nReplay it with:\n"
65 f" python {Path(__file__).name} --load-from {newest}")
Run it again with the flag the script printed:
python s4e11_record_and_replay.py --load-from my_run_<timestamp>
Every source now reads its file instead of its device: the generator does not generate and no keyboard listener runs, so hands off the keyboard proves nothing is live. The presses you recorded appear as the same markers, on the same samples. The two flags together are refused.
After the recording run:
Recording every source under 'my_run_*'.
Recorded 2 stream(s):
eeg.h5
keys.h5
manifest.json
Replay it with:
python s4e11_record_and_replay.py --load-from my_run_<timestamp>
The manifest is what pairs each file with its source on replay; streams are matched by it, never by position. During the replay, the scope shows the recorded signal and the recorded presses, and goes quiet when the recording runs out; close the window as usual.
Any source records this way, an amplifier included, so a session recorded in the lab can be replayed on a machine with no device, and a pipeline can be developed and tested against the same run as often as needed. A writer in the pipeline, as in Episode 1, records what a node produced; this records what the sources saw, before anything was done to it.
In this episode, you learned how to:
record every source of a run with
--save-as, and find the run directoryreplay it with
--load-from, with nothing plugged inname your sources, since a recording is filed under the name
That closes Season 4. Proceed to Season 5, where a paradigm presents stimuli and the pipeline averages the responses.
Nodes in this episode: Generator, Keyboard, Router, TimeSeriesScope
File s4e11_record_and_replay.py – View file on GitHub
1"""Season 4, Episode 11: every source recorded at launch, and replayed.
2
3Nothing in the pipeline changes: --save-as writes a run directory with
4one HDF5 file per source, named after the node, and --load-from replays
5it with no keyboard present. The two flags together are refused.
6"""
7
8from pathlib import Path
9
10import gpype as gp
11
12if __name__ == "__main__":
13
14 # What was asked for on the command line, read only to say so; the
15 # pipeline below does not consult it
16 launch = gp.LaunchConfig.get()
17 if launch.load_from:
18 print(f"Replaying '{launch.load_from}'. Hands off the keyboard.")
19 elif launch.save_as:
20 print(f"Recording every source under '{launch.save_as}_*'.")
21 else:
22 print("Live run, nothing recorded. To record, pass --save-as "
23 "my_run; to replay, pass --load-from my_run_<timestamp>")
24
25 app = gp.MainApp()
26 p = gp.Pipeline()
27
28 # The names are what the recordings are filed under: eeg.h5, keys.h5
29 source = gp.Generator(name="eeg", sampling_rate=250, channel_count=8,
30 signal_frequency=10, signal_amplitude=10,
31 noise_amplitude=10)
32 keyboard = gp.Keyboard(name="keys")
33 router = gp.Router(input_channels=[gp.Router.ALL, gp.Router.ALL])
34
35 # A replayed press shows as the same marker, on the same sample
36 mk = gp.TimeSeriesScope.Markers
37 markers = [mk(color="r", label="up", channel=8, value=38),
38 mk(color="g", label="right", channel=8, value=39),
39 mk(color="c", label="down", channel=8, value=40),
40 mk(color="y", label="left", channel=8, value=37)]
41 scope = gp.TimeSeriesScope(amplitude_limit=30, time_window=10,
42 markers=markers, hidden_channels=[8])
43
44 p.connect(source, router["in1"])
45 p.connect(keyboard, router["in2"])
46 p.connect(router, scope)
47 app.add_widget(scope)
48
49 p.start()
50 app.run()
51 p.stop()
52 p.close()
53
54 # The run directory carries a timestamp, so it is looked up rather
55 # than guessed
56 if launch.save_as:
57 stem = Path(launch.save_as)
58 runs = sorted(stem.parent.glob(stem.name + "_*"))
59 if runs:
60 newest = runs[-1]
61 print(f"\nRecorded {len(list(newest.glob('*.h5')))} stream(s):")
62 for entry in sorted(newest.iterdir()):
63 print(f" {entry.name}")
64 print(f"\nReplay it with:\n"
65 f" python {Path(__file__).name} --load-from {newest}")