Episode 16: Processing a Recording Offline
Record a few seconds against the clock, then run the whole file through the same pipeline in one call and get a Result back.
Every episode so far ran against the clock: start(), wait, stop(). A recording can go through a pipeline the other way, all at once, and the run has an end. That is a pipeline’s batch mode, and its source decides it.
An ordinary realtime pipeline: a Generator into a CsvWriter, started, left to run for five seconds, stopped. The writer adds a timestamp to the file name, so a recording is never overwritten; the function returns the newest match.
15def record(seconds=5.0):
16 """Record the generator against the clock, and return the file."""
17 p = gp.Pipeline()
18 source = gp.Generator(sampling_rate=250, channel_count=4,
19 signal_frequency=10, signal_amplitude=10,
20 noise_amplitude=2)
21 writer = gp.CsvWriter(file_name=RECORDING)
22 p.connect(source, writer)
23
24 p.start()
25 time.sleep(seconds)
26 p.stop()
27 p.close()
28 return max(glob.glob(PATTERN), key=os.path.getmtime)
A CsvReader with mode="batch" makes the whole file one frame, so the pipeline is a batch pipeline. The Bandpass is the same node a realtime pipeline uses, unchanged. A Collector keeps the result in memory instead of a second file, and p.run() drives the recording through and returns when the last node is done.
31def reprocess(path):
32 """Bandpass the whole recording in one pass, and return the Result."""
33 with gp.Pipeline() as p:
34 # mode="batch": the whole file is one frame, and the run has an end
35 reader = gp.CsvReader(file_name=path, mode="batch")
36 # The same node a realtime pipeline uses, unchanged
37 bandpass = gp.Bandpass(f_lo=9, f_hi=11)
38 # Keeps the result in memory instead of a second file
39 collector = gp.Collector()
40 p.connect(reader, bandpass)
41 p.connect(bandpass, collector)
42 # Returns when the last node is done
43 return p.run()
Run the script. run() hands back a Result, not a bare array: its samples, and beside them the rate, the duration and the channel labels the file carried:
file : s2e16_recording_<date>_<time>.csv
shape : (..., 4)
rate : 250.0 Hz
duration : ... s
labels : ['Ch01', 'Ch02', 'Ch03', 'Ch04']
peak-to-peak of channel 1: ...
The second run of the script finds the recording and skips the five seconds. A causal filter is the same state machine offline as live, so this is also how a realtime pipeline is validated: run the recording through it and look at the whole answer at once.
In this episode, you learned how to:
make a pipeline a batch pipeline with
mode="batch"on its readerdrive it with
run()and read theResultreuse the same nodes offline and live
Proceed to Episode 17, where the same filter runs without any delay at all, which only a recording allows.
Nodes in this episode: Generator, CsvWriter, CsvReader, Bandpass, Collector
File s2e16_processing_a_recording_offline.py – View file on GitHub
1"""Season 2, Episode 16: record five seconds, then process them offline."""
2
3import glob
4import os
5import tempfile
6import time
7
8import gpype as gp
9
10# The recording goes to the temp directory; CsvWriter adds a timestamp
11RECORDING = os.path.join(tempfile.gettempdir(), "s2e16_recording.csv")
12PATTERN = os.path.join(tempfile.gettempdir(), "s2e16_recording*.csv")
13
14
15def record(seconds=5.0):
16 """Record the generator against the clock, and return the file."""
17 p = gp.Pipeline()
18 source = gp.Generator(sampling_rate=250, channel_count=4,
19 signal_frequency=10, signal_amplitude=10,
20 noise_amplitude=2)
21 writer = gp.CsvWriter(file_name=RECORDING)
22 p.connect(source, writer)
23
24 p.start()
25 time.sleep(seconds)
26 p.stop()
27 p.close()
28 return max(glob.glob(PATTERN), key=os.path.getmtime)
29
30
31def reprocess(path):
32 """Bandpass the whole recording in one pass, and return the Result."""
33 with gp.Pipeline() as p:
34 # mode="batch": the whole file is one frame, and the run has an end
35 reader = gp.CsvReader(file_name=path, mode="batch")
36 # The same node a realtime pipeline uses, unchanged
37 bandpass = gp.Bandpass(f_lo=9, f_hi=11)
38 # Keeps the result in memory instead of a second file
39 collector = gp.Collector()
40 p.connect(reader, bandpass)
41 p.connect(bandpass, collector)
42 # Returns when the last node is done
43 return p.run()
44
45
46if __name__ == "__main__":
47
48 existing = sorted(glob.glob(PATTERN))
49 path = existing[-1] if existing else record()
50 result = reprocess(path)
51
52 print(f"file : {os.path.basename(path)}")
53 print(f"shape : {result.data.shape}")
54 print(f"rate : {result.rate} Hz")
55 print(f"duration : {result.duration:.2f} s")
56 print(f"labels : {result.labels}")
57 channel = result.data[:, 0]
58 print(f"peak-to-peak of channel 1: {channel.max() - channel.min():.2f}")