Skip to content

Lab Streaming Layer

Download Latest Version

Overview

Lab Streaming Layer (LSL) is an open-source networked middleware ecosystem to stream, receive, synchronize, and record neural, physiological, and behavioral data streams acquired from diverse sensor hardware.

See here for a list of supported devices

Can also search for supported devices and apps here

With LSL, SightLab can both receive data from and send data to third party EEG and physiological devices and software. The example scripts are in ExampleScripts/LSL_Sample, which also has a README.md with the same steps as this page.

Note: To use LSL you need the pylsl Python library. If it isn't installed, install it via Tools - Package Manager in the Vizard IDE.

Example Scripts

File What it does
lsl_sample_receive.py Receives one or more LSL streams, summarises each channel per trial and saves the results to the experiment summary. Can also send trial markers and save the raw data. With eeg=True, saves every sample of the session for EEG analysis.
lsl_list_streams.py Lists the LSL streams on the network and prints a line to paste into lsl_sample_receive.py.
lsl_sample_stream.py Dummy 8-channel EEG stream for testing without a device.
LSLpupil_diameter.py Sends pupil diameter (combined, left and right) at 100 Hz, e.g. into Biopac AcqKnowledge.
LSLmousecoordinates.py Sends the mouse position at 100 Hz, e.g. into Biopac AcqKnowledge.
LSL_Biopac_Stream.py Template for sending your own values. Sends head position and the trial number by default.
lsl_receiver.py, lsl_sender.py Helper modules used by the scripts above. Keep them in the same folder as your script.

Streaming Into SightLab (Stream Inlet)

SightLab can receive data from devices such as EEG (Wearable Sensing, Muse, BrainVision, BioSemi, etc.), GSR, ECG and other physiological recorders, and save it with your session. Any device or software that sends an LSL stream works the same way. For EEG, also see EEG Devices to save the raw signal for analysis.

  1. Start the device streaming to LSL from its own software.
  2. Run lsl_list_streams.py. For each stream on the network it prints the stream's details and a line to copy.
  3. Paste that line into STREAMS at the top of lsl_sample_receive.py, one line per stream you want to record.
  4. Run lsl_sample_receive.py.

For example, lsl_list_streams.py prints this for the dummy EEG stream:

[1] DummyEEG
    type       : 'EEG'
    channels   : 8
    rate       : 100 Hz
    format     : float32
    source_id  : 'dummy1234'
    host       : MY-PC
    labels     : ['Fp1', 'Fp2', 'C3', 'C4', 'P3', 'P4', 'O1', 'O2']

# --- paste into STREAMS in lsl_sample_receive.py ---
dict(name='DummyEEG', type='EEG', source_id=None, labels=None, channels=None, prefix='DummyEEG', eeg=True),
# eeg=True saves every sample of the session to DummyEEG_lsl_session.csv - see README

eeg=True is only added for EEG streams. See EEG Devices.

The settings at the top of lsl_sample_receive.py:

STREAMS = [
    dict(name='DummyEEG', type='EEG', source_id=None, labels=None, channels=['C3', 'O2'], prefix='EEG'),
]
STATS = ['mean']            # saved per channel - any of 'mean', 'std', 'rms', 'min', 'max'
EEG_STATS = ['std']         # the same for eeg=True streams; rms/mean mostly measure electrode offset on unfiltered EEG
EEG_EXCLUDE = ['TRG', 'X1', 'X2', 'X3']  # left out of eeg=True summaries (still in the session file) unless channels is set
SAVE_RAW_CSV = False        # True = also save every sample to <prefix>_trial_<n>_lsl.csv in the data folder
SEND_MARKERS = True         # send trial_start_<n> / trial_end_<n> on an LSL 'Markers' stream
USE_MOCK_IF_MISSING = True  # False = stop with an error instead of using random data
TRIAL_DURATION = 5          # the demo ends each trial after this many seconds

In each STREAMS entry:

  • name, type, source_id choose the stream. None matches anything. source_id is only needed if several streams share the same name and type.
  • labels names the channels if the stream doesn't send its own names. None uses the stream's names, or Ch0, Ch1, ... if it has none.
  • channels chooses which channels to save, by name or number, e.g. ['GSR'] or [0, 2]. None saves all of them.
  • prefix starts the column names in the experiment summary.
  • eeg=True (optional) turns on EEG mode for that stream. See EEG Devices.

The results are saved to the experiment summary file, which has one row per trial, in columns such as EEG_C3_mean. An EEG_Samples column gives the number of samples received in each trial, and an EEG_Source column records which stream was used.

To try it without a device, run lsl_sample_stream.py in a separate window first. The default STREAMS entry in lsl_sample_receive.py matches it.

Good to know:

  • Data is collected for the whole trial, from startTrial to endTrial. The demo ends each trial after TRIAL_DURATION seconds; replace that line with however your trials end.
  • If no matching stream is found, random mock data is used so the script can still be tested, and a large warning is printed. The <prefix>_Source column then reads MOCK DATA, and an eeg=True stream's session file ends in _MOCK. Set USE_MOCK_IF_MISSING = False to stop with an error instead.
  • A warning is printed if a trial received less than 90% of the expected number of samples, for example if the device disconnected.
  • Fast, high-channel devices are fine: everything that arrived is read every frame and only running totals are kept, so memory doesn't grow over long trials.
  • Timestamps are converted to this PC's LSL clock, so data from a device running on another PC lines up too.
  • Text or marker streams are skipped, because they have no values to average. Record them with LabRecorder instead.

Event Markers and LabRecorder

With SEND_MARKERS = True, lsl_sample_receive.py sends trial_start_<n> and trial_end_<n> on a stream called SightLab Markers. Record it with LabRecorder (or your device's own LSL recorder) together with the device stream to line up trials with the raw data afterwards.

To mark your own events, call markers.send() anywhere in the experiment:

markers.send('door_opened')

Use LSL's clock for LSL timestamps

Markers are timestamped with pylsl.local_clock(). Don't timestamp LSL data with viz.tick(). It's a different clock, so the data won't line up with other LSL streams.

EEG Devices

A summary per trial isn't enough for EEG: you'll want every sample to analyse afterwards. Add eeg=True to the stream's line in STREAMS (lsl_list_streams.py already adds it for streams of type EEG):

dict(name='MyEEG', type='EEG', source_id=None, labels=None, channels=None, prefix='EEG', eeg=True),

Getting the EEG onto LSL

How the EEG gets onto LSL depends on the device. Most setups use one of these:

  • LSL output in the manufacturer's software: many EEG recording programs can send their data to LSL. Turn it on (it may be a setting, a plug-in or a separate licence), then start recording or monitoring.
  • An LSL app for the device: a small program, from the manufacturer or the LSL community, that connects to the headset and streams its data. See the list of supported devices.
  • Other recording software: if the EEG is recorded by another program, that program may be able to pass the data on over LSL.
  • No LSL support: a small bridge script can read the device through its own API and send the data to LSL. iworx_lsl_bridge.py in ExampleScripts/LSL_Sample does this for iWorx recorders.

Once the stream is running, run lsl_list_streams.py and paste the line it prints into STREAMS. Check two things in its output:

  • eeg=True is only added when the stream's type is EEG. If your software uses another type (for example ExG), add eeg=True yourself.
  • If the stream sends no channel names (labels : (none sent)), set labels in the STREAMS line so the columns get proper names.

What eeg=True saves

  • EEG_lsl_session.csv (named after the stream's prefix; EEG_lsl_session_MOCK.csv if mock data was used) in the session's data folder: every sample from the start of the first trial until just after the last trial, between trials too. Columns:
    • lsl_timestamp: the sample's time on this PC's LSL clock.
    • trial: the trial the sample belongs to, 0 between trials.
    • one column per channel, in the units the device sends (usually microvolts).
    • marker: trial_start_<n>, trial_end_<n> and anything you send with markers.send('door_opened'), on the first sample at or after the event. Needs SEND_MARKERS = True (the default).
  • In the summary file, EEG_STATS per channel instead of STATS. The default is std: the size of the signal's swings over the trial. mean and rms aren't useful for raw EEG, as they mostly measure each electrode's offset.
  • Channels named in EEG_EXCLUDE are left out of the summary but kept in the session file. It's for channels that aren't EEG, such as trigger and auxiliary inputs. The defaults (TRG, X1-X3) are the names some headsets use; add the names your device uses. Setting channels yourself overrides this.
  • File size: roughly 330 MB per hour for 24 channels at 300 Hz. Values are saved with 9 significant digits, which keeps float32 data (what most EEG streams send) exact.

To try it without a headset, add eeg=True to the default STREAMS line and run lsl_sample_stream.py first.

Analysing the session file

Cut out trials with the trial column, e.g. in Python with pandas:

import pandas as pd
eeg = pd.read_csv('EEG_lsl_session.csv')
trial_2 = eeg[eeg.trial == 2]

If the session was quit early, the last rows can hold only a marker, with empty channel values. Drop them before analysing: eeg = eeg.dropna(subset=eeg.columns[2:-1]).

For MNE, pass the channel columns to mne.io.RawArray (MNE expects volts: multiply microvolts by 1e-6). You can also record the EEG stream and SightLab Markers together in LabRecorder, which saves both in one XDF file that MNE and EEGLAB can read.

Timing

Markers and samples use the same LSL clock, so they line up within the session file. LSL can't see the delay before a sample reaches the PC (e.g. a wireless link to the headset), so each sample's timestamp is a little later than the moment it was measured. That is fine for whole trials; for ERPs, measure the delay (e.g. with a photodiode) or use the amplifier's hardware trigger input, if it has one.

Streaming Out of SightLab (Stream Outlet)

Need to visualise VR gaze paths in AcqKnowledge or MATLAB, or record SightLab data alongside your EEG? SightLab can send its own data as an LSL outlet. This could be things such as:

  • Face tracking data
  • Participant position data
  • Participant head orientation data
  • Pupil diameter
  • Gaze coordinates

LSL_Biopac_Stream.py is a template for sending your own values. Copy it and edit CHANNELS and readValues(). readValues() returns one value per channel and is called once per frame:

import lsl_sender

CHANNELS = ['HeadX', 'HeadY', 'HeadZ']

def readValues():
    x, y, z = viz.MainView.getPosition()
    return [x, y, z]

outlet = lsl_sender.FixedRateOutlet('Head Position', 'Position', CHANNELS, read=readValues, srate=100)
vizact.onupdate(0, outlet.update)

FixedRateOutlet sends at exactly the rate you set, whatever the headset's frame rate. Every frame it sends the current values once for each sample that has come due, so the stream has no gaps and doesn't drift. Any value that is None is sent as the pad value (-1 by default). A vizact.ontimer(0.01, ...) timer can't do this, because Vizard timers run at most once per frame (60 to 120 times a second).

See also below for the detailed workflow with Biopac AcqKnowledge.

Streaming from SightLab to Biopac Acqknowledge

Acqknowledge supports LSL streaming natively, making SightLab ↔ Biopac setup painless. It needs Biopac's LSL license for AcqKnowledge (AcqKnowledge 5.0.10 or later).

See the examples in the ExampleScripts/LSL_Sample folder:

  • LSLpupil_diameter.py sends pupil diameter as 3 channels (combined, left and right). It uses the same pupil readings SightLab records for each supported eye tracker. Until the eye tracker is ready, or on hardware without pupil data, it sends -1.
  • LSLmousecoordinates.py sends the mouse position (x and y, from 0 to 1 across the window).
  • LSL_Biopac_Stream.py sends head position and a Trial channel (the trial number, 0 between trials), so you can see in AcqKnowledge when each trial ran. Edit it to send any other data.

All three send at a steady 100 Hz with no gaps, and start streaming as soon as the script starts, so AcqKnowledge can connect before the experiment begins.

To set up AcqKnowledge:

In Acqknowledge go to Display - Preferences- Hardware- Data Acquisition and enable "LSL"

Click the “+” Button to add hardware to show

Restart Acqknowledge and create a new experiment

Needs to be a new graph (go to File- New)

Hardware dropdown and choose LSL stream

When you restart Acqknowledge you can refresh your stream (or just start the SightLab script using something like the Pupil Diameter streaming example)

You can then click "Refresh" at the start stream

For event markers inside AcqKnowledge, see Sending Events to Biopac and Biopac Integration.

Earlier versions of these examples

The pupil diameter stream used to send 1 channel and now sends 3, so add the stream to your AcqKnowledge graph again. The summary columns written by lsl_sample_receive.py are now named <prefix>_<channel>_<stat> (for example EEG_C3_mean), with each trial's values on its own row, instead of Trial_<n>_LSL_Avg. The old per-trial column names only had a header for trial 1, so later trials' values had no column name.

Troubleshooting

Receiving streams in SightLab

  • "NO MATCHING LSL STREAM FOUND - USING MOCK (RANDOM) DATA": run lsl_list_streams.py and compare the stream's name and type with your STREAMS entry.
  • No streams listed: check the device software is streaming. If it's on another PC, both PCs must be on the same network and the firewall must allow LSL.
  • "2 streams match - using the first": set source_id to the value shown by lsl_list_streams.py.
  • Device without LSL support (for example iWorx through its Python API): a small bridge script has to read the device and send the data to an LSL outlet. lsl_sample_receive.py then works as described above.
  • The device's LSL app can't connect to the headset: some headsets allow only one program to connect at a time, so close the manufacturer's own software first.
  • An EEG stream isn't saved to a session file: check that its STREAMS line has eeg=True. lsl_list_streams.py only adds it for streams of type EEG.

Acqknowledge

When you first launch Acqknowledge if you see "No Stream Available"

You should be able to start whatever stream you’re expecting to find, and press the Refresh button to find it. That will cause Acq to look for streams again.

If the outlet gets closed for any reason and Acq notices (e.g., here, I quit the program that was generating SimpleStream and then pressed the Start button in a graph window that was connected to SimpleStream)

You can go to “Manage Hardware Connections” to look for new streams to connect to. You can get there from the main hardware menu, which might be “MP160”, “MP200”, or in the case of a graph window receiving an LSL stream, it will have the name of the stream.