psychopy.org | Reference | Downloads | Github

Real-time EEG experiment

I have a question regarding a real-time EEG experiment with PsychoPy.

Assume I have access to real-time EEG stream with labstreaminglayer (LSL). Using pylsl I have to be able to access to real-time EEG signals. Then, let’s say I have to process every 500 samples of EEG signals in real-time and do something after. Is it possible to keep in memory the last received 500 samples of EEG in Psychopy and process in real-time? If yes, how?

1 Like

I’d be interested in your solution.
You would probably have to run some numpy code in a seperate thread. Is it possible to open a new thread in PsychoPy3, or are there any obstacles to that?

1 Like

Hello! That is actually possible. There is an old library for doing NF in python - Wyrm. It has an object called a ringbuffer. Something that always takes the last X samples, regardless of where you are in the recording.

Another option is to look up python’s deque object.

One thing that would be good to look up, if ever you wish to do real-time stuff with python - is asynchronous programming. You basically start up the psychopy and your real-time as two separate async coroutines (i.e. functions that can run quasi-simultaneously). In your ‘while’ loop, you insert a yield statement that allows control to be switched to the other routine, which also has a yield, so that it can be switched back.

Thorough understanding of threading and multiprocessing also is necessary.

2 Likes

Hi, This is definitely possible!

This seems more of a how-to query for time series data in real time question than a PsychoPy one. It wouldn’t be the limiting factor on how much data could be held in memory. My lab has done this for Brain-Computer Interface (BCI) use and made our repository open source if you’re interested: https://github.com/BciPy/BciPy. It’d look at the acquisition and task modules.

As mentioned above most rely on a buffer implementation of some sort. Wyrm is a solid library, although seemingly abandoned? We also, in a separate process, write to a sqllite db which can be queried at any time. You will need to be multiprocessing aware, most of this cannot happen in a multithreaded way due to the GIL.

Good luck!

1 Like