Skip to main content

Reading data into Python via LSL

AJ Keller avatar
Written by AJ Keller
Updated over a month ago

If you are an advanced user and looking to work outside the machine learning provided by Crown, then you'll find this guide useful. We're writing this guide with the understanding that the reader knows how to run python3 and work with unfiltered EEG data.

Warning: LSL is experimental and time intervals can vary between samples due to resource constraints on the embedded Linux running within the Crown. Please consider using the Python SDK to send data through LSL instead of the embedded Linux if you run into issues.

Prerequisites

Install Virtual Environment

I use a virtual environment for usability and consistency. If you want to as well, I recommend installing virtualenv.

pip3 install virtualenv

If you run into issues, then you may want to run with sudo:

sudo -H pip3 install virtualenv

Create Virtual Environment

I've got a starter project if you want to follow along, you may download it from github.com/neurosity/notion-pylsl-starter. Otherwise, create a new directory for your project. Once you have the starter downloaded or new directory created, run:

virtualenv venv
source venv/bin/activate
Screen_Shot_2020-02-13_at_12.42.40_PM.png

Required steps for Notion

We assume you have already:

Working with PyLSL

Installing PyLSL

The first step is to install the PyLSL dependency. From your terminal, run:

pip install pylsl
Screen_Shot_2020-02-13_at_12.44.02_PM.png

Create a Python File

Make a new file called main.py.

Screen_Shot_2020-02-13_at_12.48.23_PM.png

Copy and paste the following into your main.py file, this file was directly taken from the main pylsl repo and is called ReceiveData.py.

"""Example program to show how to read a multi-channel time series from LSL."""

from pylsl import StreamInlet, resolve_stream

try:
# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = resolve_stream('type', 'EEG')
# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
while True:
# get a new sample (you can also omit the timestamp part if you're not
# interested in it)
sample, timestamp = inlet.pull_sample()
print(timestamp, sample)
except KeyboardInterrupt as e:
print("Ending program")
raise e

Running PyLSL

Make sure you have

Use the developer console to verify the WiFi network of your Notion:

Screen_Shot_2020-02-13_at_2.11.38_PM.png

Then in the terminal run:

python main.py

Which will automatically find the stream and read data from it!

Screen_Shot_2020-02-13_at_2.18.16_PM.png

If you run into any problems or have any suggestions on how to improve this tutorial, please comment below.

Did this answer your question?