Don't know if I should use MNE for this project

Hello everyone! Im pretty new to MNE. Im currently working on a project which should detect micro awakenings and other sleep related events and then trigger things based on that. Im using the Muse S Athena headband for this, which also has EEG and fNIRS. I know how to record and stream the data, but I need a way to process it in real time. I came across MNE-Python and looked further into it. I saw (if Im correct) that its mainly used for offline analysis, but it can also be used for real time processing. For the EEG and fNIRS, what I basically need is filtering, and computing EEG bandpowers overtime, and for fNIRS being able to see HbO and HbE levels, so nothing fancy.

This project will run on a Raspberry Pi 4, but I heard MNE can be quite heavy. So I basically have two main options, the real time MNE way or doing it more manually by using numpy/scipy.

I hope someone can give me some advice on which approach is better (or maybe other approaches besides these two), and sorry if some wording is wrong Im pretty new to this (EEG/fNIRS processing with Python in general).

Hi @Dragadow, thanks for your question and welcome to the forums!

For real-time analysis, have you looked at MNE-LSL?

With this, you can connect to an LSL stream of your data and it will handle any re-referencing/filtering on data acquisition. This is very lightweight.

You can then easily compute things like bandpower over time by using scipy or some of the functions in MNE-Python.

Hello @tsbinns, thank you for your response! I did take a look at MNE-LSL, but I’m a bit concerned about latency. Because this project is for sleep-related stuff (lucid dreaming specifically), it needs to be able to process the data pretty fast, and I thought that MNE-LSL would be slow compared to other methods like a custom numpy/scipy pipeline. I also saw that MNE doesnt support PPG processing, so I was wondering, if I do go with the MNE-LSL approach and I use another library for PPG processing (NeuroKit2 for example), if that would come with certain problems.

MNE-LSL is designed to be used for real-time analyses, so it is fast by nature. In my own experience using this for real-time experiments, I haven’t had issues with it being too slow.

However, it’s also not the case that MNE-LSL is going to be handling the heaviest computational parts of your pipeline. Pulling chunks of new data takes only nanoseconds, and while there is an initial cost to generating filters, applying this to newly acquired data is very fast (internally it just uses scipy.signal.sosfilt).

It’s the analysis after this pre-processing of the data that is going to be most demanding. When you have the pre-processed data available, you’re free to use any combination of MNE-Python, SciPy, or other toolboxes for the analysis (like the rolling bandpower example I linked above shows).

I’m not familiar with PPG processing, but if this is a time series signal, you could still acquire this data using MNE-LSL (treating it as a misc channel type), and then apply whatever tools you need from NeuroKit2. I have used MNE-LSL in the past for working with a combination of supported (EEG) and unsupported (audio) data types (also with different sampling rates), where the unsupported data types get treated as misc.

Good to know. For streaming the data I use the OpenMuse library since it has to decode the raw bluetooth packets coming from the Muse S Athena device. So MNE-LSL only does the pre-processing? You said after acquiring the pre-processed data I can use any combination of toolboxes, including MNE-Python, but arent we going back to the original question of whether its an efficient method to run that on a Raspberry Pi 4 instead of a custom Numpy/Scipy script?

For streaming the data I use the OpenMuse library since it has to decode the raw bluetooth packets coming from the Muse S Athena device.

According to the OpenMuse README, the data is (or can be?) streamed with LSL (which actually uses MNE-LSL for this), so you can connect to these streams using MNE-LSL on whatever device you want to process the data.

So MNE-LSL only does the pre-processing?

Yes, data acquisition from streams, re-referencing, filtering (and optional epoching based on markers).

arent we going back to the original question of whether its an efficient method to run that on a Raspberry Pi 4 instead of a custom Numpy/Scipy script?

Beyond the pre-processing stuff, yes.

In the rolling bandpower example there is some benchmarking of SciPy’s periodogram and Welch methods vs. MNE’s multitaper method. The multitaper approach takes much longer (I’m sure in part because of some extra validation that takes part in MNE), but it is a more complex algorithm. A better comparison for your case would be SciPy’s Welch vs. MNE’s Welch.

Generally, I’d say if there’s functionality (like band power) that can be easily implemented using SciPy, go for it.

If at some point you need a more complex algorithm or some functionality that is harder to build from scratch, perhaps the performance hit is acceptable to have a verified implementation (from, e.g., MNE-Python, NeuroKit2) that you don’t have to build and maintain yourself.

I’m sorry I can’t give a definitive answer, but it’s difficult to do without knowing what the maximum acceptable output latency of your processing scripts are, and without some benchmarking on the specific hardware with the expected amount of data.

Fair enough. I’ll go with just SciPy for now since I already managed to apply bandpass filters and compute band powers with it, and see how it goes with real time processing. Thank you for the help, really appreciated!

Edit: Meant to say HbR instead of HbE in the original post :sweat_smile: