# How to build an adjacency matrix for spatio-temporal cluster tests?

**URL:** <https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924>\
**Category:** Support & Discussions\
**Tags:** time-frequency, statistics\
**Created:** [May 22, 2022, 11:40pm UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924 "2022-05-22T23:40:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anapesq](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@anapesq](https://mne.discourse.group/u/anapesq)\
**Post date:** [May 22, 2022, 11:40pm UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924/1 "2022-05-22T23:40:10Z")

</div>

Hello,

I am comparing the time-frequency matrices (n\_freqs, n\_times) between two conditions using mne.stats.spatio\_temporal\_cluster\_1samp\_test.

If I understand correctly setting the adjacency parameter to None (default) will connect adjacent neighbors in the time dimension (horizontal). I would like to consider adjacent neighbors in both the frequency (vertical) and time (horizontal) dimensions.

Is there an example I could follow to set up such an adjacency matrix?

Many thanks!

Ana P

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [May 23, 2022, 5:35am UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924/2 "2022-05-23T05:35:51Z")

</div>

Hello @anapesq, if the input data has the shape (observations, times, frequencies), passing `adjacency=None` should do exactly what you want and form clusters across immediately neighboring time and frequency bins. Clusters are always implicitly formed over neighboring time points.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![anapesq](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@anapesq](https://mne.discourse.group/u/anapesq)\
**Post date:** [May 23, 2022, 11:32am UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924/3 "2022-05-23T11:32:09Z")

</div>

Hello @richard!

Thank you for the quick reply and clarification.  
Do you know how I would have to set-up the adjacency if I wished to check not only the immediately neighboring time and frequency bins (next cells) but also the bins at a distance of two cells?  
Meaning, how could I expand what is considered a neighboring cell.

Thank you!

Ana P

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [May 23, 2022, 11:50am UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924/4 "2022-05-23T11:50:08Z")

</div>

I’m sure @sappelhoff is now in a good position to help here — he just reworked a big chunk of the clustering documentation 😁

---

<div class="post-metadata">

**Author:** ![sappelhoff](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/sappelhoff/32/3276_2.png) [@sappelhoff](https://mne.discourse.group/u/sappelhoff)\
**Post date:** [May 23, 2022, 12:10pm UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924/5 "2022-05-23T12:10:18Z")

</div>

> [@anapesq](#):
>
> I am comparing the time-frequency matrices (n\_freqs, n\_times) between two conditions using mne.stats.spatio\_temporal\_cluster\_1samp\_test.

Do you mean you don’t have “channels” as a dimension? If your data is really just `observations * n_freqs * n_times`, then [mne.stats.spatio\_temporal\_cluster\_1samp\_test](https://mne.tools/stable/generated/mne.stats.spatio_temporal_cluster_1samp_test.html) is the wrong test to choose, see the docstring:

> This function provides a convenient wrapper for [`mne.stats.permutation_cluster_1samp_test()`](https://mne.tools/stable/generated/mne.stats.permutation_cluster_1samp_test.html#mne.stats.permutation_cluster_1samp_test), for use with data organized in the form (observations × time × space).

I think I would rather advise you to use: [mne.stats.permutation\_cluster\_1samp\_test](https://mne.tools/stable/generated/mne.stats.permutation_cluster_1samp_test.html)

> [@anapesq](#):
>
> Do you know how I would have to set-up the adjacency if I wished to check not only the immediately neighboring time and frequency bins (next cells) but also the bins at a distance of two cells?

You could have a look at the `max_step` parameter in the cluster permutation functions. **However** I haven’t looked into the source code for that, and the parameter description makes me think that:

1. this param only works when you specify adjacency for **channels** as obtained by [mne.channels.find\_ch\_adjacency](https://mne.tools/stable/generated/mne.channels.find_ch_adjacency.html)
2. this param only works for the 2nd dimension of the input data … and not as you’d like to have it, for several input dimensions

So overall I don’t think this is possible right now, but it sounds like a very reasonable feature request to me to expand what `max_step` can do.

---

<div class="post-metadata">

**Author:** ![anapesq](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@anapesq](https://mne.discourse.group/u/anapesq)\
**Post date:** [May 23, 2022, 1:14pm UTC](https://mne.discourse.group/t/how-to-build-an-adjacency-matrix-for-spatio-temporal-cluster-tests/4924/7 "2022-05-23T13:14:22Z")

</div>

Dear @sappelhoff,

Thank you for your quick and informative reply.

I re-run the analysis using the suggested `mne.stats.permutation_cluster_1samp_test()` and the results are the same as when using ‘mne.stats.spatio\_temporal\_cluster\_1samp\_test()’. Possibly the later function (which is not the preferred as you suggest) adapts to an input without the channel dimension.

I will look into how to request mne features.

Many thanks!
