2023-05-17: Open-Source Solution for EEG Signal Data Processing and Analysis


In a prior blog post, I discussed how Neuropype commercial software can be used for EEG data processing and analysis. While this tool provides a lot of capability and an academic license can be obtained for free, the free license is not available to all and may not be suitable for some applications. EEGLAB is a popular, well-maintained free tool that has been used to create a number of EEG preprocessing pipelines and perform analysis. However, it requires Mathworks MATLAB commercial software to run so it indirectly comes with a cost and licensing requirements. 

MNE-Python

As an alternative for those looking for fully open-source and/or programmatic ways to perform EEG data preprocessing and analysis, the MNE-Python package enables exploring, visualizing, and analyzing human neurophysiological data without licensing fees. However, it does require some programming skills to use. If you already have Python installed on your computer, installing the MNE-Python library is fairly straightforward. You can use the package installer for python (pip) to add MNE-Python to your environment.

pip install --upgrade mne 

Loading Data

Once installed, a number of functions from this library can be utilized to load EEG signal data stored in a number of different file formats including Brainvision core format, Biosemi Data Format (BDF), European Data Format (EDF)General Data Format (GDF), Neuroscan (CNT), and MATLAB EEGLAB .set files. Each function allows data to be preloaded into memory, which can improve speeds but of course, has greater memory requirements. The data is loaded into an MNE Raw object, which contains has several helper functions for obtaining the sampling frequency, recording length, and channel location data. In just two lines of code, you can load and generate an image of your data:

    raw = mne.io.read_raw_brainvision('subject_002.vhdr', preload=True)
    raw.plot()


Fig 2. Plot of raw data generated using MNE-Python plot function

Preprocessing

MNE-Python provides a number of functions for removing unwanted artifacts from raw EEG signal data. MNE-Python can be used to perform bandpass filtering, remove bad channels, referencing, and independent component analysis (ICA). One standardized EEG data preprocessing pipeline, PREP, which was originally developed as an EEGLAB plugin, was later recreated using Python and some of these MNE-Python functions. This version, known as PyPREP, makes it fairly easily to complete EEG data preprocessing with just a few of lines of code. It has been used in conjunction with other methods to clean EEG data for Parkinson's Disease and motor imagery research.
Fig 3. Selected channel spectra before and after line noise removal using PREP Pipeline

Feature Extraction

MNE-Python can be used to find events in EEG signal data and perform epoching. Time-domain features like mean amplitude and variance can be extracted from the cleaned data.  Alternately, frequency domain features can be extracted after using MNE-Python to transform a dataset from the time domain to the frequency domain. This can be accomplished using Welch's method via the psd_welch function as follows.

    freqs, psd = mne.time_frequency.psd_welch(cleaned_eeg, 
                                              fmin=1, 
                                              fmax=50,
                                              n_fft=1024, 
                                              n_overlap=0)

The power spectral density (PSD) representing the power distribution of EEG data in the frequency domain can also be computed using the library's multitaper function, psd_multitaper. MNE-Python provides functions for computing time-frequency representation (TFR) using Stockwell Transform Morlet wavelets or discrete prolate spheroidal sequence (DPSS) tapers. It also includes many other functions for performing statistical analysis, computing time-frequency domain connectivity measures, and more.

Visualizations

MNE-Python is able to generate and save a variety of data visualizations with a little help from the pyvista and matplotlib libraries.  Figures 1, 2 and 4 were all generated with the MNE-Python library using EEG data from a prior study. However, there are more elaborate visualizations that can be generated to include 3D brain images.
Fig 4. Data visualization generated using MNE plot_psd function

Of course, this is not all that MNE-Python has to offer. Those curious about the full details of the library should refer to its full API. Interested parties would not be alone as there have been numerous studies that have utilized MNE-Python for EEG data processing and analysis. Recent studies that have incorporated the library include those led by Chen, Pandey, and Chiossi

MNE Enhancements

Custom python code and other libraries can be used in conjunction with MNE-Python to provide the capability needed for a particular application. For those working with data sets stored in the Brain Imaging Data Structure (BIDS) file directory structure, installing the MNE-BIDS Python library is an option to make reading and writing files in this convention easier. The BIDS standard for file storage and exchange is enforced by some online EEG data repositories like OpenNeuro. However, BIDS restricts the type of EEG files that can be used to four options.

Some researchers have developed other capabilities to augment MNE-Python like a graphical user interfaces for the library. Schiratti et al. created MNE-features to provide more feature extraction functions. Braindecode incorporates a good portion of MNE-Python functionality along with its own enhancements to provide additional deep learning and deep learning virtualization functionality. 

Alternatives to MNE

If programmatic EEG signal data processing and analysis is desired but MNE-Python does not meet the need, there are other options available. SciPy, which is one of MNE-Python's dependencies, has been used for EEG data preprocessing and analysis as well as to create a number of other EEG signal data and analysis libraries. PyEEG and eeglib both are built using SciPy, which are said to focus more on providing feature extraction capabilities. However, I would not cross MNE off the list of contenders if you have not already given it due consideration.

--Bathsheba Farrow (@sheisheba)

Comments