Frederic-vW / eeg_microstates

EEG microstate analysis
MIT License
36 stars 17 forks source link

Polarity flips randomly in some k clusters with kmeans. Can that be easily turned off/adjusted for? #6

Closed lorafanda closed 10 months ago

lorafanda commented 11 months ago

This is not really an issue but polarity flips randomly in some k clusters with means. The nmaps plotted show some differences between multiple runs of the clustering function within eeg_microstates. Can that be easily turned off/adjusted for? Example: The top two maps (custom for my needs) are one run of means and bottom to are another run of kmean (each iterated and ran multiple times until convergence).

EEG_microstates_example
Frederic-vW commented 11 months ago

Hi lorafanda, this is indeed a property of the modified k-means algorithm. The maps are eigenvectors of a matrix (Eq. 9 in Pascual-Marqui et al., 1995) and can point in one or the other direction, i.e. polarity is random.

You could a) flip the maps manually when you're done. If you are working with group-level microstate maps computed from subject maps (for example with T. Koenig's permutation algorithm), then you don't have to flip all subject maps, you can do it for the group-level maps only. It would be for visual purposes only, the microstate sequences are not affected as long as you ignore polarity. b) use canonical maps as provided in T. Koenig's EEGLAB plugin for example, re-sample them to your channel configuration, and get the correct polarity from the sign of the correlation coefficient between your maps and the canonical maps.

If you only want to make sure that each clustering run gives the same result, you can fix the random seed in Python. Not sure you want this though, as it prevents optimization, you literally get identical clusters every time.

Hope this makes sense and answers your question.

lorafanda commented 11 months ago

Yeah it almost fully made sense, thanks. Indeed, the microstate sequences arent changing much even when rerunning the clusterings multiple times. It is purely for visualization purposes. So the question is: If i want the clusters to visually mimic the data, how would i go about flipping the maps manually (i.e. where would this be done in the eeg_microstates?)

I'm thinking about a real brute force approach, and taking some values of the original data belonging to a cluster and, if the pattern is the same, don't flip. otherwise flip. Is there a better way to do this?

lorafanda commented 11 months ago

Sorry to answer twice, but I finally understand what point b) is saying and it is a good solution for my problem. Thanks!

Frederic-vW commented 11 months ago

Hi, I'm glad you found a solution that works for you. Sorry, I haven't implemented these steps in the eeg_microstates3.py script. I have only flipped maps to obtain uniform polarity for manuscript figures in the past. To do that, I simply load and plot my group-level maps, multiply by -1 if polarity flip is needed (visually assessed), and save as final maps. I wouldn't search for a single EEG data vector in the original data, simply because they are not the same thing as a cluster centroid, and they will be less smooth. Also, once you have a set of microstate maps, the other method (multiply by -1 if needed) is quick and simple (let me know if you need help).

I have to admit that my script is quite basic and doesn't offer many user amenities. Victor Férat's pycrostates (https://github.com/vferat/pycrostates/tree/main) package includes a microstate re-order function. You could give it a try, I'm planning to include the information-theoretic analyses there as well.