# Spatio-temporal cluster permutation at the group level

**URL:** <https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949>\
**Category:** Support & Discussions\
**Tags:** eeg, visualization, evoked\
**Created:** [November 17, 2022, 3:04pm UTC](https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949 "2022-11-17T15:04:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdg4](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@rdg4](https://mne.discourse.group/u/rdg4)\
**Post date:** [November 17, 2022, 3:04pm UTC](https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949/1 "2022-11-17T15:04:51Z")

</div>

Hello.

I have a within-subjects experiment with N=20 and 3 conditions and I want to run a spatio-temporal cluster permutation test. I have the evokeds `-ave.fif` file for each participant, but I’m confused about how to construct the input array X. Here is what I’m currently doing:

```python
event_id = ['Neutral', 'Rise', 'Fall']

evokeds_files = sorted(glob.glob('./path_to_evokeds/*-ave.fif')) #returns a list of the evoked file of 20 subjects

X = []
for subj in evokeds_files:
    X.append([mne.read_evokeds(subj, condition=event_name, verbose=False).data for event_name in event_id])

X = [np.transpose(x, (0, 2, 1)) for x in X]

```

I’m not sure if what I’m doing is correct because the clusters returned are either a huge cluster of all electrodes, or very small clusters of 1 or 2 electrodes.

Moreover, is there a way to visualize clusters for a particular time window, say between 250ms-350ms (for P300 ERP) or visualize clusters only for a single channel?

Thanks!

 ![stcp_viz](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/e/e832e701f0f5db48f03fd839dd6877055310f556.png)

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [December 2, 2022, 8:58pm UTC](https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949/2 "2022-12-02T20:58:49Z")

</div>

- You have 20 subjects and 3 conditions
- Your evokeds shape is `(n_channels, n_times)`
- the desired shape for `X` is, according to the docstring, `(n_observations, p[, q], n_vertices)`

here `n_observations` will be the subjects, and channels will be our “vertices”. That means “time” and “condition” are our `p` and `q` dimensions. So your X should end up as `(n_subj, n_cond, n_time, n_chan)` or given what you’ve told us, `(20, 3, n_time, n_chan)`. So something like this should work:

```python
X = list()
for subj in subjs:
    this_x = list()
    for cond in conditions:
        evk = mne.read_evokeds(subj, condition=cond)
        this_x.append(evk.data.T)
    X.append(this_x)

```

I’m pretty sure that’s equivalent to what you’re already doing, but double-check. If they are the same, you’ll have to dig into the data to see why the clusters don’t look how you expect them to.

For your other questions, I don’t understand what it would mean to “visualize clusters only for a single channel”. A cluster is, by definition, a collection of at least 2 vertices/channels. For particular time windows, yes it ought to be possible to do that; we have a helper function [mne.stats.summarize\_clusters\_stc — MNE 1.2.2 documentation](https://mne.tools/stable/generated/mne.stats.summarize_clusters_stc.html) for when the clustering is done in source space, but not for sensors I’m afraid. I don’t have time at the moment to work up an example of how you would do that for sensor data (partly because I’ve never clustered in sensor space so I can’t just copy some old code and tweak it to work with fake/sample data).

@mscheltienne @mmagnuski have either of you done sensor-space clustering, and have some useful sample code for visualizing the results?

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [December 2, 2022, 9:09pm UTC](https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949/3 "2022-12-02T21:09:05Z")

</div>

Sorry, I don’t have code snippets matching this use case. Good luck!

---

<div class="post-metadata">

**Author:** ![rdg4](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@rdg4](https://mne.discourse.group/u/rdg4)\
**Post date:** [December 3, 2022, 1:58pm UTC](https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949/4 "2022-12-03T13:58:39Z")

</div>

Thank you! It’s clearer now.

> [@drammock](#):
>
> For your other questions, I don’t understand what it would mean to “visualize clusters only for a single channel”. A cluster is, by definition, a collection of at least 2 vertices/channels.

I meant something like only temporal clustering instead of spatio-temporal clustering. So if I’m interested in only a parietal channel (say Pz), I could ‘select’ it and find temporal clusters in the EEG timecourse of Pz.

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [December 3, 2022, 3:10pm UTC](https://mne.discourse.group/t/spatio-temporal-cluster-permutation-at-the-group-level/5949/5 "2022-12-03T15:10:49Z")

</div>

> [@rdg4](#):
>
> I meant something like only temporal clustering instead of spatio-temporal clustering

Ah yes ok. That is certainly possible. You can, as you say, pick just the channel you’re interested in, such that your n\_vertices dimension is just 1 element. Don’t completely remove that dimension though! Shape should be (20, 3, n\_times, 1) for it to work properly.
