clustering across chnls for epochs using pycrostates(template matching)

  • MNE version: e.g. 1.8.0
  • operating system: e.g. macOS sequoia 15.1 (24B83)

i did epoch each subjects (1 till 79) into 3133 1 sec epochs across 18 channels for each individual subject. using Pycrostates, i observe putting clusters=4 topo plots for subject1, but i want to know whetehjr is it microstte A or B or C or D as in standard literature, how to template match, from where can i get this predefined template to say it belong to A or B or C or D , i want to know sequence of these states for all subkects 3133 epochs like A,B , c,d,a so on till 3133? how to do this guys, please help me

import pycrostates
from pycrostates.cluster import ModKMeans

Select the first subject’s data

first_subject_raw = raw_list[0]
first_subject_raw.set_montage(‘standard_1020’)

Create 1-second epochs for the first subject

epochs = mne.make_fixed_length_epochs(first_subject_raw, duration=1.0, preload=True)
print(f"Created {len(epochs)} 1-second epochs for the first subject.")

Define the number of microstates to cluster into

n_clusters = 4 # Adjust as needed

Initialize the modified K-means clustering

clustering = ModKMeans(n_clusters=n_clusters, random_state=42)

Fit the clustering on the data for the first subject’s epochs

clustering.fit(epochs, picks=‘eeg’, tmin=None, tmax=None, reject_by_annotation=True, n_jobs=5, verbose=None)

Access the derived cluster centers (microstate maps)

microstate_maps = clustering.cluster_centers_

print(“Derived microstate maps for the first subject:”)
clustering.rename_clusters(new_names=[‘A’,‘B’,‘C’,‘D’]

manually i did i feel this is wrong

IIRC, this function pycrostates.cluster.utils.optimize_order — pycrostates 0.7.0.dev0 documentation was added to figure out the order of the microstates in inst to match the order in template_inst. And it can also be used directly in the reorder_clusters method: pycrostates.cluster.ModKMeans — pycrostates 0.7.0.dev0 documentation

So:

  1. Take one of your subject/cluster, plot the microstates and manually re-order and label the maps.
  2. Use this cluster as a ‘template’ to automatically re-order the others

For reference, here is the PR which added the functionality: Auto-label/order microstates maps by vferat · Pull Request #105 · vferat/pycrostates · GitHub

Mathieu