Photo by Robina Weermeijer on Unsplash

Neuroscience Meets Data Science: Exploring Common Spatial Pattern (CSP) and Its Applications in Healthcare Analytics

Utkarsh Lal
Geek Culture
Published in
6 min readApr 8, 2023

--

Common Spatial Pattern (CSP) is a popular technique in the field of biomedical signal processing and has been widely used in various applications, particularly in the healthcare industry. It is a spatial filtering technique that is used to extract features from multi-channel biomedical signals such as electroencephalogram (EEG) or magnetoencephalogram (MEG). The goal of CSP is to find a set of spatial filters that can effectively differentiate between two classes of signals based on their covariance matrices.

The mathematical foundation of CSP is based on linear algebra and multivariate statistical methods. CSP involves transforming the EEG data from the time domain to the spatial domain using a spatial filter. It applies a spatial filter to the multi-channel EEG data to enhance the signal variance of one class while reducing it for the other within the same domain. This process results in new features (components) that are linear combinations of the original channels. The goal of the spatial filter is to find a set of spatial weights that maximally discriminate between two or more classes of EEG data.

source: author

For example, in a typical CSP analysis, the EEG data may be divided into two classes: one class representing a task-related cognitive state (e.g., imagining the movement of the left hand) and the other class representing a baseline state (e.g., resting with eyes closed). The CSP method then finds a set of spatial weights that maximally discriminate between these two classes of EEG data.

The spatial filter used in CSP is typically computed by solving a generalized eigenvalue problem. The generalized eigenvalue problem involves finding the eigenvectors of a matrix that maximally discriminates between the two classes of EEG data. The process involves calculating the eigenvectors of the composite covariance matrices of the two classes. These eigenvectors (spatial filters) project the EEG signals into a new space where the classes are optimally separated in terms of variance. In other words, the resulting spatial filters are basically a set of weights that can be applied to the EEG data to obtain a new set of spatially filtered data that emphasizes the differences between the two classes.

The CSP method can be further refined by applying spatial filtering to both the original EEG data and the spatially filtered data. This results in a set of spatial filters that can be used to extract spatial patterns of brain activity that are associated with specific cognitive states or tasks.

In summary, the CSP method is a signal-processing technique that uses linear algebra and multivariate statistical methods to identify spatial patterns of brain activity in EEG data. By identifying spatial patterns that are associated with specific cognitive states or tasks, CSP can be used to develop EEG-based BCIs that can be used for a variety of applications, including motor rehabilitation, communication, and control of external devices.

CSP has been widely used in various applications in the healthcare industry, including the detection of various neurological diseases such as epilepsy and Parkinson’s disease, the classification of sleep stages, and the classification of motor intentions from EEG signals.

In this article, we will focus on the applications of CSP in the healthcare industry and provide a detailed example along with Python code to show how CSP is used in a real-world project.

Applications of Common Spatial Pattern in the Healthcare Industry:

  1. Detection of Epilepsy: CSP has been used in the detection of epilepsy by analyzing the EEG signals recorded during seizures. CSP is used to extract features from the EEG signals, which are then used as input to a classifier to differentiate between seizures and normal EEG signals.
  2. Classification of Sleep Stages: CSP has been used in the classification of sleep stages by analyzing the EEG signals recorded during sleep. CSP is used to extract features from the EEG signals, which are then used as input to a classifier to differentiate between the different sleep stages.
  3. Classification of Motor Intentions: CSP has been used in the classification of motor intentions by analyzing the EEG signals recorded during movement. CSP is used to extract features from the EEG signals, which are then used as input to a classifier to differentiate between the different motor intentions.

Example: Let’s consider a real-world example where CSP is used to detect epileptic seizures from EEG data.

Here’s the Python code to implement CSP for a model that is trained to distinguish between epileptic and non-epileptic patients. The code is for illustrative purposes and will need to be adapted for different types of EEG datasets.

# Import necessary libraries
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

# Load EEG data for epileptic and non-epileptic patients
eeg_epileptic = np.loadtxt('eeg_epileptic.csv', delimiter=',')
eeg_non_epileptic = np.loadtxt('eeg_non_epileptic.csv', delimiter=',')

# Define CSP function
def csp(X, y, n_components):
# Calculate covariance matrices for each class
covs = [np.cov(X[y==i].T) for i in np.unique(y)]

# Calculate whitening transformation matrix
D = np.concatenate(covs)
E, U = np.linalg.eigh(D)
W = np.dot(np.diag(np.sqrt(1/(E + 1e-6))), U.T)

# Whiten data
X_white = np.dot(X, W.T)

# Calculate spatial filters
S1 = np.dot(np.dot(covs[0], W.T), W)
S2 = np.dot(np.dot(covs[1], W.T), W)
E, U = np.linalg.eigh(S1, S1 + S2)
W_csp = np.dot(U.T, W)

# Apply spatial filters
X_csp = np.dot(X_white, W_csp.T)

# Select top CSP components
X_csp = X_csp[:, :n_components]

return X_csp

# Apply CSP to EEG data
X_epileptic_csp = csp(eeg_epileptic[:, :-1], eeg_epileptic[:, -1], 4)
X_non_epileptic_csp = csp(eeg_non_epileptic[:, :-1], eeg_non_epileptic[:, -1], 4)

# Combine data and labels
X = np.concatenate([X_epileptic_csp, X_non_epileptic_csp])
y = np.concatenate([np.ones(len(X_epileptic_csp)), np.zeros(len(X_non_epileptic_csp))])

# Train LDA classifier
lda = LDA()
lda.fit(X, y)

# Load test EEG data
eeg_test = np.loadtxt('eeg_test.csv', delimiter=',')

# Apply CSP to test EEG data
X_test_csp = csp(eeg_test[:, :-1], eeg_test[:, -1], 4)

# Classify test EEG data using LDA
y_pred = lda.predict(X_test_csp)

# Print predicted class labels
print(y_pred)

In this example, the EEG data for epileptic and non-epileptic patients is loaded from two separate CSV files. The csp() function is then defined to apply CSP to the EEG data and return the spatially filtered data. The LDA() classifier is used to train a linear discriminant analysis (LDA) model on the combined data and labels. The eeg_test.csv file contains test EEG data, which is passed through the csp() function and then classified using the trained LDA model.

The same task can be easily achieved using the MNE python library as shown in the example below: -

import numpy as np
import mne
from mne.decoding import CSP
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA

# Load EEG data from EDF files
eeg_epileptic = mne.io.read_raw_edf('eeg_epileptic.edf', preload=True)
eeg_non_epileptic = mne.io.read_raw_edf('eeg_non_epileptic.edf', preload=True)
eeg_test = mne.io.read_raw_edf('eeg_test.edf', preload=True)

# Extract data (assuming the data is preprocessed)
# The way of extracting labels might change based on how they are stored in the EDF files
X_epileptic = eeg_epileptic.get_data().T # Transpose to get correct shape
y_epileptic = np.ones(X_epileptic.shape[0]) # Replace with actual method of obtaining labels

X_non_epileptic = eeg_non_epileptic.get_data().T
y_non_epileptic = np.zeros(X_non_epileptic.shape[0]) # Replace with actual method of obtaining labels

X_test = eeg_test.get_data().T
# y_test for evaluation (if available)

# Combine data and labels for training
X_train = np.concatenate([X_epileptic, X_non_epileptic])
y_train = np.concatenate([y_epileptic, y_non_epileptic])

# Define and apply CSP
n_components = 4
csp = CSP(n_components=n_components, reg=None, log=None, norm_trace=False)
X_train_csp = csp.fit_transform(X_train, y_train)

# Train LDA classifier
lda = LDA()
lda.fit(X_train_csp, y_train)

# Apply CSP to test data and make predictions
X_test_csp = csp.transform(X_test)
y_pred = lda.predict(X_test_csp)

# Print predicted class labels
print("Predicted labels:", y_pred)

# Optional: Evaluate model performance (if y_test labels are available)
# y_test = ... # Extract test labels similar to training labels
# print("Accuracy:", accuracy_score(y_test, y_pred))
# print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

The mne.decoding.CSP function is used to apply Common Spatial Pattern to the EEG data and return the spatially filtered data.

References

  1. https://mne.tools/stable/auto_examples/decoding/decoding_csp_eeg.html#sphx-glr-auto-examples-decoding-decoding-csp-eeg-py

--

--

Utkarsh Lal
Geek Culture

Product Development Analyst at American Express