Time and Synchronisation

A pipeline with several sources – an amplifier and a marker, say – needs one clock that decides which sample an event belongs to. g.Pype uses the samples of one source, the master: every sample and every event of a run is placed on its timeline. Where an event lands, and what the time of a sample means, both follow from how that timeline is built.

The master source's samples arrive in frames. Each frame is stamped with the host clock when it is first seen. An event's host instant is mapped through the fit of samples to stamps onto the sample it happened in.

One master per run

At start(), before any data flows, the pipeline asks every source whether it can be the master and picks the strongest claim. An amplifier wins over any other source, whichever starts first. A Generator or a recording reader comes next, and between two claims of the same rank the higher sampling rate wins: a 500 Hz generator is elected over a 250 Hz one. Sources that only emit events – Marker, Keyboard, UDPReceiver – never claim; their events are placed onto the master’s samples. The master is chosen once, and kept for the whole run.

Arrival stamps

Every live source adds one channel to what it produces: the instant each block was first seen on the host, read from the host’s high-resolution clock (time.perf_counter()). There is one value per block, because a block exists only once its last sample has arrived. The channel is found by its role, timestamp, never by its position, and the pipeline strips it before anything downstream sees it. That is why a source’s declared channel count is the device’s width while its output port is one wider: a two-channel generator’s port carries signal, signal, timestamp, and a Collector after it receives two channels. LslReceiver adds no stamp.

A stamp is a delivery instant, not an acquisition instant. It comes later than the moment the samples were measured, by however long the device and its link take. That delay is the same for two amplifiers of the same model, so it cancels when you compare them. It does not cancel between an amplifier’s samples and a marker your program stamps on the host, and g.Pype does not compensate it. For an absolute stimulus-to-EEG time, use the amplifier’s trigger input, which records the same electrical pulse in the data and in its digital channel.

How an event lands on a sample

A marker.emit(), a key press or a UDP packet is stamped with the host clock the moment it is observed. The pipeline keeps a fit between the master’s sample positions and the host clock, refined with every frame the master delivers, and maps the event’s instant through it onto the sample whose period contains it. The event waits for the master frame that reaches its instant: at most one frame, or longer when OSCAR, g.tec’s artifact removal, delays the signal.

Nodes that place events on a signal, such as a Router merging a marker channel or a Trigger, hold their output back by the event margin: 40 ms by default, 0 when OSCAR is running. An event that arrives slightly late still finds its sample that way. The margin is latency by design – those nodes emit every frame 40 ms later. p.event_margin_ms changes it, even while the pipeline runs, and 0 turns it off: less latency, and more events placed late. How closely events land, measured on each amplifier and across two machines, is in How to read this page.

What goes wrong is reported, not hidden: printed on the console, written to the session log under the placing node’s name, and the pipeline’s condition turns to Warning (Condition and Logging):

  • Two events in one sample: the first is kept, and the second is discarded with a warning – “1 observation(s) fell in a sample period that was already occupied and were discarded. Two events cannot be separated inside one sample – the first is kept. A source emitting a value and a reset back to back produces this: hold the reset for a sample or more, or raise the rate.”

  • An event before the master’s first sample is held, and placed once the master’s samples reach its instant.

  • An event too late for its sample goes on the newest sample, with a warning.

  • An event still waiting when the pipeline stops is lost, and the node says so – “Router: 1 event(s) were still waiting to be placed when the pipeline stopped, and are lost.”

A live source stamps with the host clock, while a recording read back by a reader brings the times it recorded – which is why the two cannot share a pipeline (Execution Modes). A replay with load_from is different: it is paced and stamped by the host clock, and Record and Replay covers what it reproduces.