LSL Integration

Lab Streaming Layer (LSL) is how g.Pype talks to the rest of a lab: an LSLSender publishes a pipeline’s stream, an LslReceiver reads any LSL stream in. Both come with the lsl extra, pip install "gpype[lsl]", which installs pylsl. Season 4, Episode 2 and Episode 8 walk through each direction; this page is the reference.

Sending

sender = gp.LSLSender(stream_name="my_eeg")
p.connect(filtered, sender)

Field

What g.Pype writes

Stream name

stream_name, or gpype_lsl; the source_id is the same.

Stream type

EEG.

Rate, channel count, format

The input port’s, as double64.

Channel description

Written when the input names its channels or any channel has a role other than signal: one channel entry per column under desc/channels, the XDF convention, with its label where the channel is named, its type from the role (signal is EEG, trigger is Markers, auxiliary is AUX, the rest Misc) and its unit where recorded, uV for EEG.

Timestamp

Each chunk is pushed with the pipeline timeline’s host time at the moment of the push, the master’s current position in seconds of the host’s monotonic clock: the last sample’s own time when the stream is the master’s and nothing between the source and the sender holds frames back, a little later otherwise. Before the timeline has a relation, LSL stamps at push time.

Markers

A trigger channel goes out as a column of the same stream, typed Markers, with its codes and zeros between them. There is no separate marker stream.

Chunking

One push per frame, so a frame of 4 samples at 250 Hz is a chunk every 16 ms.

Receiving

source = gp.LslReceiver(stream_name="my_eeg")      # or stream_type="EEG"
p.connect(source, scope)

The receiver resolves the stream by name or by type, waiting up to five seconds, then pulls chunks as they come and feeds the pipeline sample by sample. Labels, types and units are read from the stream description, so a g.Pype stream read back keeps what it was published with, and a Markers column arrives as a trigger channel. The node is built while the stream is offline when channel_count= and sampling_rate= are given, which a server or an unassigned edge requires; without them, construction resolves the stream on the network and fails if none answers. Samples are timed by their arrival on this host, not by the stream’s own stamps; the receiver numbers them as they come and holds a time base of its own, so it can be a pipeline’s only source. A stream carries no authentication; it says what it is and cannot prove it.

Recording with LabRecorder

A g.Pype outlet is an ordinary LSL stream, so LabRecorder records it into an XDF file beside every other stream on the network, with LSL’s own clock offsets per stream. pyxdf reads the file in Python:

import pyxdf
streams, header = pyxdf.load_xdf("session.xdf")
for s in streams:
    print(s["info"]["name"][0], s["time_series"].shape)

pyxdf, or MNELAB’s read_raw, turns the XDF into an MNE Raw; MNE’s mne_lsl reads a live LSL stream instead, which is the live route into MNE, see the MNE workflow. These are LSL’s standard tools and not part of g.Pype.

Two g.Pype pipelines

An LSLSender in one pipeline and an LslReceiver in another, on one machine or across a network, is the loosest way to couple two pipelines: the receiving side gets the samples, the labels and the markers, timed by arrival. For two processes that must share one timeline, sample for sample, use a distributed pipeline instead, Deployment Scenarios.

When a stream is not found

  • LSL discovers streams by UDP multicast on port 16571 and moves data over TCP; a firewall that blocks either leaves the resolve empty after five seconds. Allow Python through it, on both machines.

  • Two outlets with one name on the network: the receiver takes the first it resolves. Give streams distinct names or resolve by type.

  • A stream declared at an irregular rate (nominal_srate=0) cannot be a continuous source; pass sampling_rate= if it is in fact regular.

  • The lsl extra is not installed: the node names it when it is built.