# TFR Adjacency error gives strange feedback

**URL:** <https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024>\
**Category:** Support & Discussions\
**Created:** [July 2, 2024, 3:50pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024 "2024-07-02T15:50:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andesha](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andesha/32/2524_2.png) [@Andesha](https://mne.discourse.group/u/Andesha)\
**Post date:** [July 2, 2024, 3:50pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/1 "2024-07-02T15:50:28Z")

</div>

- MNE version: 1.7.0
- operating system: ubuntu

Hi all,

Having a problem with doing some clustering across channels via `spatio_temporal_cluster_test`.

I have set up the adjacency variables much like the tutorial with `find_ch_adjacency` and `combine_adjacency`. However, once I attempt to run the above cluster function I get the following error:

```python
ValueError: adjacency (len 131425) must be of the correct size, i.e. be equal to or evenly divide the number of tests (18775).

If adjacency was computed for a source space, try using the fwd["src"] or inv["src"] as some original source space vertices can be excluded during forward computation

```

This does equally divide though, so I am confused as to how to proceed.

Thanks,  
Tyler

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [July 2, 2024, 9:12pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/2 "2024-07-02T21:12:59Z")

</div>

The error message confused me as well, but after peeking at the source code, I think that you are interpreting the message backwards: the adjacency needs to evenly divide the number of tests, rather than be evenly divisible _by_ the number of the tests. In other words, the number of tests (`18_775`) needs to be evenly divisible _by_ the adjacency (`131_425`).

So: `divmod(18_775, 131_425)` (not the other way around), would need to return a remainder of 0 (but doesn’t)

As for a fix, unfortunately I won’t be of much help… other than telling you to make sure that your ‘adjacency’ is not be greater than your number of tests (`18_775`)

Maybe @CarinaFo , or @larsoner (the last person who touched that line of code) can be of more help?

---

<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:** [July 3, 2024, 6:10am UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/3 "2024-07-03T06:10:49Z")

</div>

It’s hard to tell what you tried if you don’t share code…

---

<div class="post-metadata">

**Author:** ![CamilleFakche](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/camillefakche/32/3209_2.png) [@CamilleFakche](https://mne.discourse.group/u/CamilleFakche)\
**Post date:** [July 3, 2024, 12:24pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/4 "2024-07-03T12:24:53Z")

</div>

Hello,

I have the same problem.  
I am using MNE 1.0.3, on Spyder, Python 3.10, on Windows.

My input matrix X has the format [nParticipants \* nTimes \* nFreqs \* nMag], corresponding to [36 \* 500 \* 29 \* 102].

I have also followed the tutorial to compute the adjacency matrix.

1. adjacency\_mag, ch\_names\_mag = find\_ch\_adjacency(Epoch.info, ch\_type=“mag”). I obtained a sparse.\_csr.csr\_matrix object [102 \* 102].  
Here is the plot I obtained.  
 ![Adjacency_Mag](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/b/bb84ca564f0af9625245881480703ad134edc65f.png)
2. tf\_adjacency = combine\_adjacency(nTimes, nFreqs, adjacency\_mag). I obtained again a sparse.\_csr.csr\_matrix object [1479000 \* 1479000] which I think is correct because 500x29x102=1479000.

Then when I try to run the cluster permutations test, I used the following code:

cluster\_stats = spatio\_temporal\_cluster\_test(  
X,  
n\_permutations=1000,  
tail=0,  
n\_jobs=None,  
buffer\_size=None,  
adjacency=tf\_adjacency,  
)

And I received the following error:

ValueError: adjacency (len 1479000) must be of the correct size, i.e. be equal to or evenly divide the number of tests (2958).

If adjacency was computed for a source space, try using the fwd[“src”] or inv[“src”] as some original source space vertices can be excluded during forward computation

I have a look to the source code ([mne-python/mne/stats/cluster\_level.py at maint/1.7 · mne-tools/mne-python · GitHub](https://github.com/mne-tools/mne-python/blob/maint/1.7/mne/stats/cluster_level.py#L895)).

- Line 935: The input matrix X is transformed to form a list of 36 array [500 \* 29 \* 102].

- Line 936: The number of samples is defined as followed:  
n\_samples = X[0].shape[0]. I obtained n\_samples = 500.

- Line 937: The number of times is defined as followed:  
n\_times = X[0].shape[1] I obtained n\_times = 29.

- Line 945: The X matrix is transformed again to a list of 36 array [500 \* 2958] with 29x102 = 2958.

- Line 946: The number of tests is defined as:  
n\_tests = X[0].shape[1]. I obtained n\_tests = 2958.

- Line 949: A new adjacency is defined as: adjacency = \_setup\_adjacency(adjacency, n\_tests, n\_times) (definition line 619).

- Line 627: got\_times, mod = divmod(n\_tests, adjacency.shape[0]). I obtained got\_times = 29, and mod = 0.

- Line 628-636: if got\_times != n\_times or mod != 0:  
raise ValueError(  
"adjacency (len %d) must be of the correct size, i.e. be "  
“equal to or evenly divide the number of tests (%d).\n\n”  
"If adjacency was computed for a source space, try using "  
'the fwd[“src”] or inv[“src”] as some original source space ’  
“vertices can be excluded during forward computation”  
% (adjacency.shape[0], n\_tests)  
)  
But my variables got\_times = n\_times, and mod = 0. So I do not understand why the if statement is validated…

I hope this help and that we’ll be abe to find a solution.  
Many thanks,  
Camille.

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [July 3, 2024, 8:23pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/5 "2024-07-03T20:23:38Z")

</div>

Try not creating an adjacency that includes the time dimension – that should be done automatically in the `spatio_temporal_*` versions of the function. You should just need to provide the spatio-spectral connectivity like

```python
combine_adjacency(nFreqs, adjacency_mag)

```

or similar

---

<div class="post-metadata">

**Author:** ![Andesha](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andesha/32/2524_2.png) [@Andesha](https://mne.discourse.group/u/Andesha)\
**Post date:** [July 4, 2024, 7:14pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/6 "2024-07-04T19:14:34Z")

</div>

Thanks @larsoner, dropping the one parameter as you suggested worked right away

many thanks!

---

<div class="post-metadata">

**Author:** ![CamilleFakche](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/camillefakche/32/3209_2.png) [@CamilleFakche](https://mne.discourse.group/u/CamilleFakche)\
**Post date:** [July 11, 2024, 2:46pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/7 "2024-07-11T14:46:06Z")

</div>

Hi,

Indeed, with your solution @larsoner the code works.  
However, the times at which the clusters are significant are missing in the output…

clusters\_stats = spatio\_temporal\_cluster\_test(  
X,  
n\_permutations=nperms,  
tail=tail,  
n\_jobs=None,  
buffer\_size=None,  
adjacency=tf\_adjacency)

F\_obs, clusters, p\_values, \_ = cluster\_stats

From the function’s output cluster\_stats, we extracted the variable clusters.  
From what I understood, this variable should contain freqs, times, sensors information. But it does not contain times info.

freq\_inds, time\_inds, space\_inds = clusters[0]  
ValueError: not enough values to unpack (expected 3, got 2)

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/0/017b5d54a914ad1b5ada579d4da6e42c6df812c1.png)

Thanks for your help,  
Camille.

---

<div class="post-metadata">

**Author:** ![larsoner](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/larsoner/32/3_2.png) [@larsoner](https://mne.discourse.group/u/larsoner)\
**Post date:** [July 12, 2024, 7:10pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/8 "2024-07-12T19:10:44Z")

</div>

> [@CamilleFakche](#):
>
> From what I understood, this variable should contain freqs, times, sensors information.

Yes in principle, but if you collapsed dimensions of your `X` array originally, they will be collapsed in the output as well, so you’ll have to `reshape` (properly!) to get the original dimensionality back.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [July 19, 2024, 7:10pm UTC](https://mne.discourse.group/t/tfr-adjacency-error-gives-strange-feedback/9024/9 "2024-07-19T19:10:58Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
