I was trying to do cluster-based permutation tests on single-subject sensor-space epoched data, and encountered an error relating to the adjacency matrix saying that “adjacency (len X) must be of the correct size, i.e. equal to or evenly divide the number of tests (Y)”.
A quick inspection suggests that the adjacency matrix created by channels.find_ch_adjacency(epoch_data.info, ch_type=‘grad’) was a 204x204 sparse matrix, despite that epoch_data.info had only 199 good grad channels. Also X/Y=204/199.
Could any expert here tell me if this is the source of the size error? Is the function creating the adjacency matrix from fixed templates? If so, what is the best practice to fix the error–should I use a different method to create an adjacency matrix that only includes good channels, or should I include/interpolate bad channels in the epoched data? How should I deal with this issue when doing such tests at the group level?
Hi!
If I am not mistaken, the adjacency doesn’t take account the ‘bads’ electrode. If you want, for some reason, to definitely get rid of these electrodes you should call the method drop_channels from your mne instance you created (either if it’s a mne.io.Raw object or mne.Epochs object or other).
If you want to perform a group study, (for example if you want to call mne.concatenate_epochs to concatenate your mne.Epochs objects of different subjects) you need to have the same number of electrodes across subjects (MNE will indicate you that).
So yes you should interpolate the bad channels.
However, I tried dropping one arbitrary channel from my epoch instance and it didn’t change the dimension of the adjacency matrix created from the epochs. I vaguely remember that in Fieldtrip there’s a way to manually create an adjacency matrix using channel indices. Is there such a method in MNE python as well (I’m very new to Python)? Or is it better to interpolate bad channels (and why)?
from mne import pick_types
from mne.channels import read_ch_adjacency
raw = ...
raw.info["bads"] = ["MEG0632", "MEG0642", "MEG0643"] # gradiometers
adj, ch_names = read_ch_adjacency("neuromag306planar") # full, similar to find_ch_adjacency
assert adj.shape == (204, 204)
idx = pick_types(raw.info, meg="grad", exclude="bads")
adj, ch_names = read_ch_adjacency("neuromag306planar", picks=[ch for k, ch in enumerate(raw.ch_names) if k in idx])
assert adj.shape == (201, 201)
assert not any(ch in ch_names for ch in raw.info["bads"])