EEG Microstates#

In continually recorded human EEG, the spatial distribution of electric potentials across the cortex changes over time. Interestingly, for brief time periods (60 - 120ms), quasi-stable states of activity (i.e., microstates), characterized by unique spatial configurations of electrical activity distribution, have been observed. NeuroKit can be used to easily analyze them.

# Load the NeuroKit package and other useful packages
import neurokit2 as nk
import mne

EEG Preprocessing#

First, let’s download a raw eeg data in an MNE format and select teh EEG channels.

raw = nk.data("eeg_1min_200hz")
raw = raw.pick(["eeg"], verbose=False)

sampling_rate = raw.info["sfreq"]  # Store the sampling rate

EEG recordings measure the difference in electric potential between each electrode and a reference electrode. This means that the ideal reference electrode is one which records all the interfering noise from the environment but doesn’t pick up any fluctuating signals related to brain activity. The idea behind re-referencing is to express the voltage at the EEG scalp channels with respect to another, new reference. This “virtual reference” can be any recorded channel, or the average of all the channels, as we will use in the current analysis.

Below, we will apply a band-pass filter and re-reference the signals to remove power line noise, slow drifts and other large artifacts from the raw input.

# Apply band-pass filter (1-35Hz) and re-reference the raw signal
eeg = raw.copy().filter(1, 35, verbose = False)
eeg = nk.eeg_rereference(eeg, 'average')

nk.signal_plot([raw.get_data()[0, 0:500], eeg.get_data()[0, 0:500]], 
               labels =["Raw", "Preprocessed"], 
               sampling_rate = eeg.info["sfreq"])
../../_images/649aba78d8879b82e8b1a938cc4ae39b52ab77a4c6e8a26d5d13f30b9e21a057.png

Microstates Analysis#

Minimal Example#

Microstates can be extracted and analyzed like that:

# Extract microstates
microstates = nk.microstates_segment(eeg, n_microstates=4)

# Visualize the extracted microstates
nk.microstates_plot(microstates, epoch = (0, 500))
../../_images/2b94490f3551a3f9c5b921cb84fa2faa05530d0f3677bcf28a134fea69e6a936.png

This shows the aspect of the microstates and their sequence in the Global Field Power (GFP; see below). We can then proceed to a statistical analysis.

nk.microstates_static(microstates, sampling_rate=sampling_rate, show=True)
Microstate_0_Proportion Microstate_1_Proportion Microstate_2_Proportion Microstate_3_Proportion Microstate_0_LifetimeDistribution Microstate_1_LifetimeDistribution Microstate_2_LifetimeDistribution Microstate_3_LifetimeDistribution Microstate_0_DurationMean Microstate_0_DurationMedian Microstate_1_DurationMean Microstate_1_DurationMedian Microstate_2_DurationMean Microstate_2_DurationMedian Microstate_3_DurationMean Microstate_3_DurationMedian Microstate_Average_DurationMean Microstate_Average_DurationMedian
0 0.244583 0.28675 0.2995 0.169167 689.5 791.0 780.0 478.5 0.018838 0.015 0.019753 0.015 0.020537 0.015 0.017714 0.01 0.019367 0.015
../../_images/a83a1f1b575833704ba253148020a0f347798955a3afbe1e4ff01b29241b6fc8.png

This shows computes static statistics, such as the prevalence of each microstates and the median duration time (also shown in the graph).

nk.microstates_dynamic(microstates, show=True)
Microstate_0_to_0 Microstate_0_to_1 Microstate_0_to_2 Microstate_0_to_3 Microstate_1_to_0 Microstate_1_to_1 Microstate_1_to_2 Microstate_1_to_3 Microstate_2_to_0 Microstate_2_to_1 Microstate_2_to_2 Microstate_2_to_3 Microstate_3_to_0 Microstate_3_to_1 Microstate_3_to_2 Microstate_3_to_3
0 0.734583 0.103918 0.108348 0.053152 0.091253 0.746876 0.099971 0.061901 0.089037 0.097663 0.756539 0.056761 0.071464 0.105471 0.104978 0.718088
../../_images/1ccac387babd4f264d601df47bfdeb16af55572517b41ce4f17d8f9d31399eaa.png

Finally, one can compute complexity features of the sequence of microstates, such as the entropy.

nk.microstates_complexity(microstates, show=True)
Microstates_Entropy_Shannon
0 1.968256
../../_images/376cd48c34989f364256d454c2755a0c1a1f840ee6be82b1be4a306ae694b7b3.png

Options and features#

Global Field Power (GFP)#

Under the hood, microstates_segment() starts by computing the GFP.

The Global Field Power (GFP) is a reference-independent measure of potential field strength. It is thought to quantify the integrated electrical activity of the brain and is mathematically defined as the standard deviation of all electrodes at a given time.

The GFP time series periodically shows peaks, where the EEG topographies are most clearly defined (i.e., signal-to-noise ratio is maximized at GFP peaks). As such, GFP peak samples are often used to extract microstates.

This can be visualized manually:

gfp = nk.eeg_gfp(eeg)

peaks = nk.microstates_peaks(eeg, gfp=gfp)

# Plot the peaks in the first 200 data points
nk.events_plot(events = peaks[peaks < 200], signal = gfp[0:200])
../../_images/bb1c6a5ccaf568bee60c5f1aafa1813eeb4fe70bd3b81194252715a86e00f2ee.png

By default, the microstates clustering algorithm is trained on the EEG activity at these peaks, and then applied to back-predict the state at all data points. However, this behaviour can be changed. For instance, one can decide to train the algorithm directly on all data points.

microstates_all = nk.microstates_segment(eeg, n_microstates=4, train="all")
nk.microstates_plot(microstates_all, epoch = (0, 500))
../../_images/d5d03f4a767ddb54bc4c0f977550e23661954154d8a2a835b712222da725075e.png

How many microstates?#

Most of the clustering algorithms used in microstates analysis require the number of clusters to extract to be specified beforehand.

However, one can attempt at statistically estimating the optimal number of microstates. A variety of indices of fit can be used.

# Note: we cropped the data as this function takes some time to compute 
n_optimal, scores = nk.microstates_findnumber(eeg.crop(0, 5), n_max=8, show=True)  
print("Optimal number of microstates: ", n_optimal)
[........................................] 0/7
[█████...................................] 1/7
[███████████.............................] 2/7
[█████████████████.......................] 3/7
[██████████████████████..................] 4/7
[████████████████████████████............] 5/7
[██████████████████████████████████......] 6/7
[████████████████████████████████████████] 7/7

Optimal number of microstates:  4.0
../../_images/a1c168c035eef6973abbc403218286c25ebe8397a23d1e2f1bd7435265fe6d7c.png

Microstates clustering algorithms#

Several different clustering algorithms can be used to segment your EEG recordings into microstates. These algorithms mainly differ in how they define cluster membership and the cost functionals to be optimized (Xu & Tian, 2015). The method to use hence depends on your data and the underlying assumptions of the methods (e.g., some methods ignore polarity). There is no one true method that gives the best results but you can refer to Poulsen et al., 2018 if you would like a more detailed review of the different clustering methods.

In the example below, we will compare the two most commonly applied clustering algorithms - the K-means and modified K-means. Other methods that can be applied using nk.microstates_segment include kmedoids, pca, ica, aahc.

# Extract microstates
microstates_kmeans = nk.microstates_segment(eeg, n_microstates=4, method="kmeans")
microstates_kmod = nk.microstates_segment(eeg, n_microstates=4, method="kmod")  

# Global Explained Variance
gev_kmeans = microstates_kmeans['GEV']
gev_kmod = microstates_kmod['GEV']
print( f' Using conventional Kmeans,  GEV = {gev_kmeans*100:.2f}%')
print( f' Using modified Kmeans,  GEV = {gev_kmod*100:.2f}%')

# Visualize the extracted microstates
nk.microstates_plot(microstates_kmeans, epoch = (150, 450))
nk.microstates_plot(microstates_kmod, epoch = (150, 450))
 Using conventional Kmeans,  GEV = 61.88%
 Using modified Kmeans,  GEV = 71.40%
../../_images/50b1e810a249bf2b362b8766c70ed65621e12602d5762e41dfc2534e64a93093.png ../../_images/cc4943f3bb61686092bfbb2217bc04ec798efb1fcc6f0cf73be4ff15b381a8ed.png