TFR Adjacency error gives strange feedback

  • 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:

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

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?

Itā€™s hard to tell what you tried if you donā€™t share codeā€¦

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.
  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).

  • 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.

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

combine_adjacency(nFreqs, adjacency_mag)

or similar

2 Likes

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

many thanks!

2 Likes

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)

Thanks for your help,
Camille.

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.

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