You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ENH] Spatio-temporal cluster permutation testing: Define minimum "nodes" to define a cluster #10604
For spatio-temporal cluster permutation testing, it's important to consider how many "nodes" make up a cluster.
For example:
In the time domain (considering 1 "channel"): Are two neighboring significant time points already a cluster? Or does it start with "3", "4", "N"?
In the spatial domain: the same but for neighboring channels who have are significant at the same point in time
Currently I can only assume that in MNE-Python, any neighboring relationship (2 timepoints, or 2 channels) makes up a cluster.
It would be really nice to
document this
offer a parameter to customize this
In FieldTrip, this is done as so:
cfg.minnbchan =2; % minimum number of neighborhood channels that is% required for a selected sample to be included% in the clustering algorithm (default=0).
Currently I can only assume that in MNE-Python, any neighboring relationship (2 timepoints, or 2 channels) makes up a cluster.
Actually a single time point and single spatial location will even be a "cluster". The only thing that matters is that it exceeds the threshold.
Do you need to set minimum spatio/temporal/spatiotemporal "extent"s to get reasonable results? For data that are sufficiently smooth I don't think it shouldn't matter much in practice because single-node "clusters" will have very small counts and stat values (which you get/use depends on t_power) so won't be the largest cluster(s) for each permutation used in the end for the maximal statistic.
I think you are right with the assessment that "it won't matter" in most cases, and I think for my data presently it also doesn't matter too much.
However, I still remember this parameter from when I was using FieldTrip and would have liked to use it during exploration, as it also controls the number of "observed" clusters.
will have very small counts and stat values (which you get/use depends on t_power)
Now that we are talking about t_power: Is there actually a meaningful value for that param beyond 0 (for count of "extent") and 1 (for sum of cluster stats)?
it also controls the number of "observed" clusters.
This is a pretty easy operation (almost/possibly a one-liner) to do on the observed clusters after the fact, though. So I'd rather not add a parameter for this, but instead maybe a line in a tutorial/example.
Now that we are talking about t_power: Is there actually a meaningful value for that param beyond 0 (for count of "extent") and 1 (for sum of cluster stats)?
I guess that continuous values between 0 and 1 give a corresponding weight to the count vs stat. Then values greater than 1 give more importance to larger stat values, which at least reminds me of the difference between minimizing squared error versus absolute error / L2 vs L1 norms. But I'd suggest looking at the original papers/refs to see what they say, this is just my intuition from thinking about it.
This is a pretty easy operation (almost/possibly a one-liner) to do on the observed clusters after the fact, though. So I'd rather not add a parameter for this, but instead maybe a line in a tutorial/example.
Fair enough! But I also just thought of another way why such a parameter would be relevant -- I drew a quick sketch (you need to tilt your head to the right should, sorry 😉)
In the drawn scenario, this is 1 single big cluster that has a much larger extent simply because there are a few channel/time "bins" that extend like "fingers" from the "main cluster" ... this even has the potential to connect clusters that would actually be distinct.
With the proposed parameter, such "fingers" can be removed
I guess that continuous values between 0 and 1 give a corresponding weight to the count vs stat. Then values greater than 1 give more importance to larger stat values, which at least reminds me of the difference between minimizing squared error versus absolute error / L2 vs L1 norms. But I'd suggest looking at the original papers/refs to see what they say, this is just my intuition from thinking about it.
I've run into this problem with a cluster permutation in the time domain, where like @larsoner said a single time point can become a cluster. I now simply filter those out manually according to a user-defined threshold.
I've run into this problem with a cluster permutation in the time domain, where like @larsoner said a single time point can become a cluster. I now simply filter those out manually according to a user-defined threshold.
I'll make a PR for the study template soon that demonstrates this.
In the drawn scenario, this is 1 single big cluster that has a much larger extent simply because there are a few channel/time "bins" that extend like "fingers" from the "main cluster" ... this even has the potential to connect clusters that would actually be distinct.
This is not the case for your example according to a standard lattice adjacency, there are four clusters:
This is not the case for your example according to a standard lattice adjacency, there are four clusters:
I see - but does that make my point invalid? 🙂
Because we could tune my example such that it would result in a single cluster, ... and still look like something that I'd rather control to not be a single cluster (by a parameter that tunes a "minimum extent").
Full disclosure: I won't have time to do this PR myself at this time. But I still don't think (yet?) that this issue needs to be closed.
Because we could tune my example such that it would result in a single cluster, ... and still look like something that I'd rather control to not be a single cluster (by a parameter that tunes a "minimum extent").
Okay I'm not sure what you mean / how you want this "min" operation to operate. Can you provide an example of current behavior that would be bad, what you'd want the parameter to fix? For example, based on how I've been thinking about the problem, I would expect a min_spatial=2 or so to produce a cluster with 4 nodes for the thresholded map (time in rows, space in cols):
[[0, 1],
[0, 1],
[1, 1]]
But I'm starting to suspect you're saying that you'd want this to produce a cluster with just the last row, which is indeed different from how I've been thinking about the minimum extent... I'm thinking about a minimum after forming the cluster like an "any" operation, and you're thinking about a minimum during clustering like an "all" operation (i.e., "it has to maintain a minimum extent of X along dimension Y at all locations, rather than achieve it at any point")...?
Which way does the FieldTrip parameter actually operate?
So I think the default is identical to what we currently have in MNE.
with cfg.minnbchan=1 I would expect that for:
[[0, 1],
[0, 1],
[1, 1]]
... the first column would contain just a single channel significant at this timepoint 0, i.e., there are 0 neighbors -- so this does not go into the cluster, and -- as you say -- only the last column will be part of the cluster.
I think this can be helpful if one wants to avoid cases like this (just conceptually):
where 2 (or more) clusters are "bridged" by small connections (i.e., channels that are more sustainedly significant, when the rest of the cluster is already n.s. again).
Okay that makes sense. It seems doable in principle given our existing code. I think we can achieve minimums for:
Time points spanned by a single spatial node by making a preliminary pass over the columns (time series) of thresholded array to just zero out any contiguous series of 1's that is not long enough.
Spatial nodes by working within the clustering step to just drop any "cluster" within a time slice that is not big enough.
Just to clarify, what should happen in this case for min_t_span=2, min_spatial_span=2?
[[0, 1, 0],
[1, 1, 1],
[0, 1, 0]]
Should there even be 1 cluster? I think no, since the singletons should be culled by the algorithm...
As far as the naming goes, we could consider min_t_span=1(default) rather than min_t_neighbors=0. I at least find thinking about the span more natural than the number of neighbors (even though they just differ by 1).
Should there even be 1 cluster? I think no, since the singletons should be culled by the algorithm...
agreed
As far as the naming goes, we could consider min_t_span=1(default) rather than min_t_neighbors=0. I at least find thinking about the span more natural than the number of neighbors (even though they just differ by 1).
I agree that it is useful to have such parameters when doing cluster based permutation test. minnbchan can be very useful in avoiding birdges between clusters (ie two clusters merged into one because there is a relatively narrow path joining them).
I actually have some code for things like that in my borsar repository (which unfortunatelly, despite my best intentions, does not contain much good documentation apart from docstrings; but has good test coverage at least :) ). Unfortunatelly it does clustering differently to mne - uses separate functions (both numba and numpy) specialized for common scenarios (like 3d space with channel adjacency (TFR), 2d space with adjacency (ERP) etc.) - so making this code part of mne might not be straightforward.
If someone would like to try it out, here is a short example of finding clusters using minimum number of adjacent channels (min_adj_ch below):
this returns two boolean 2d arrays in clusters because there are two clusters when eliminating samples without at least one adjacent supra-threshold sample in the channels dimension. There are also higher level functions for performing the whole cluster based permutation test.
Interesting! Looking at this example of 3d data (observations X time X chs), only sensor adjacency is defined (without doing a "combine adjacency" to add "time adjacencies" as a lattice), and still it looks like clustering is happening over channels AND time (as I would expect!)
indeed the spatio_temporal_cluster_(1samp_)test functions automatically add a lattice connectivity along the time dimension based on the max_step parameter, as long as the value passed for adjacency is the correct shape (I think it's using combine_adjacency under the hood).
Hello all, so what's the plan here on how to proceed? I believe tackling this will be a major and widely applauded improvement. Maybe we could create some sort of milestone/task/todo list and distribute tasks? :)
To me this sounds like one PR that adds this feature, as Eric described in #10604 (comment) (although that comment only describes time and space, and we should also implement this for frequencies)
I volunteer to take over this task, targeting cluster_test.
Looking back at the onset of this discussion, what is the expected behavior: counting neighbors or cluster size? My intuition is that cluster size makes more sense but I may be missing something. Comment
The parameter should be named min_spatial_span
Covering frequency dimension as well as time
this will be applied to observed, as well as permuted maps
I think we settled on counting neighbors. The example (n_ch, n_times) array
[[1,0,0,1,1],[1,0,0,1,1],[1,1,1,1,1]]
would need to be pruned at the neighbor level (removing the second and third ones from the last row) before clustering happened. I think this is @sappelhoff 's use case
I suspect it can get tricky because the channel adjacency gets folded into a single combined adjacency: with adjacency="auto", cluster_test builds a (n_times * n_freqs * n_ch,) * 2-square matrix (the legacy spatio_temporal_* functions use (n_ch * n_freqs,) * 2). For "auto" we have the channel adjacency before combining, so we could start with that case and raise an error for user-supplied adjacency in higher dimensions, or describe / pass the dimensions through somehow to handle it in general...
The idea I think is that it's an operation between thresholding (after you have x_in) and _get_labels (which makes the clusters), not an operation on the clusters themselves. I think this is what minnbchan does. It might makes sense to make this an erosion that is applied repeatedly until it stops having an effect, I'm not sure if FieldTrip does this or just applies once. I am pretty sure minnbchan only counts spatial neighbors (channels) at the same time (and frequency) so if we want to be fieldtrip-like, we need to "un-ravel" (or be able to conceptually unravel) our adjacency matrix, which potentially collapses across multiple dimensions (e.g., space and frequency).
Keeping only clusters that are at least N points total is not something that we need, since this is easy enough to implement post-hoc on observed clusters, and isn't minnbchan-like.
Activity
larsoner commentedon May 6, 2022
Actually a single time point and single spatial location will even be a "cluster". The only thing that matters is that it exceeds the threshold.
Do you need to set minimum spatio/temporal/spatiotemporal "extent"s to get reasonable results? For data that are sufficiently smooth I don't think it shouldn't matter much in practice because single-node "clusters" will have very small counts and stat values (which you get/use depends on
t_power) so won't be the largest cluster(s) for each permutation used in the end for the maximal statistic.sappelhoff commentedon May 6, 2022
I think you are right with the assessment that "it won't matter" in most cases, and I think for my data presently it also doesn't matter too much.
However, I still remember this parameter from when I was using FieldTrip and would have liked to use it during exploration, as it also controls the number of "observed" clusters.
Now that we are talking about
t_power: Is there actually a meaningful value for that param beyond0(for count of "extent") and1(for sum of cluster stats)?larsoner commentedon May 6, 2022
This is a pretty easy operation (almost/possibly a one-liner) to do on the observed clusters after the fact, though. So I'd rather not add a parameter for this, but instead maybe a line in a tutorial/example.
I guess that continuous values between 0 and 1 give a corresponding weight to the count vs stat. Then values greater than 1 give more importance to larger stat values, which at least reminds me of the difference between minimizing squared error versus absolute error / L2 vs L1 norms. But I'd suggest looking at the original papers/refs to see what they say, this is just my intuition from thinking about it.
sappelhoff commentedon May 6, 2022
Fair enough! But I also just thought of another way why such a parameter would be relevant -- I drew a quick sketch (you need to tilt your head to the right should, sorry 😉)
In the drawn scenario, this is 1 single big cluster that has a much larger extent simply because there are a few channel/time "bins" that extend like "fingers" from the "main cluster" ... this even has the potential to connect clusters that would actually be distinct.
With the proposed parameter, such "fingers" can be removed
thanks for this intuition!
hoechenberger commentedon May 6, 2022
I've run into this problem with a cluster permutation in the time domain, where like @larsoner said a single time point can become a cluster. I now simply filter those out manually according to a user-defined threshold.
hoechenberger commentedon May 6, 2022
I'll make a PR for the study template soon that demonstrates this.
larsoner commentedon May 6, 2022
This is not the case for your example according to a standard lattice adjacency, there are four clusters:
This is because lattice adjacency (reasonably IMO) is a "cross" not a "filled 3x3 square" in the 2D case.
sappelhoff commentedon May 6, 2022
I see - but does that make my point invalid? 🙂
Because we could tune my example such that it would result in a single cluster, ... and still look like something that I'd rather control to not be a single cluster (by a parameter that tunes a "minimum extent").
Full disclosure: I won't have time to do this PR myself at this time. But I still don't think (yet?) that this issue needs to be closed.
larsoner commentedon May 6, 2022
Okay I'm not sure what you mean / how you want this "min" operation to operate. Can you provide an example of current behavior that would be bad, what you'd want the parameter to fix? For example, based on how I've been thinking about the problem, I would expect a
min_spatial=2or so to produce a cluster with 4 nodes for the thresholded map (time in rows, space in cols):But I'm starting to suspect you're saying that you'd want this to produce a cluster with just the last row, which is indeed different from how I've been thinking about the minimum extent... I'm thinking about a minimum after forming the cluster like an "any" operation, and you're thinking about a minimum during clustering like an "all" operation (i.e., "it has to maintain a minimum extent of X along dimension Y at all locations, rather than achieve it at any point")...?
Which way does the FieldTrip parameter actually operate?
sappelhoff commentedon May 6, 2022
The fieldtrip parameter description says:
So I think the default is identical to what we currently have in MNE.
with
cfg.minnbchan=1I would expect that for:... the first column would contain just a single channel significant at this timepoint 0, i.e., there are 0 neighbors -- so this does not go into the cluster, and -- as you say -- only the last column will be part of the cluster.
I think this can be helpful if one wants to avoid cases like this (just conceptually):
where 2 (or more) clusters are "bridged" by small connections (i.e., channels that are more sustainedly significant, when the rest of the cluster is already n.s. again).
larsoner commentedon May 6, 2022
Okay that makes sense. It seems doable in principle given our existing code. I think we can achieve minimums for:
Just to clarify, what should happen in this case for
min_t_span=2, min_spatial_span=2?Should there even be 1 cluster? I think no, since the singletons should be culled by the algorithm...
As far as the naming goes, we could consider
min_t_span=1(default) rather thanmin_t_neighbors=0. I at least find thinking about the span more natural than the number of neighbors (even though they just differ by 1).sappelhoff commentedon May 6, 2022
agreed
I think that most will agree with this as well 👍
larsoner commentedon May 6, 2022
Okay then we just need a volunteer :)
mmagnuski commentedon May 7, 2022
I agree that it is useful to have such parameters when doing cluster based permutation test.
minnbchancan be very useful in avoiding birdges between clusters (ie two clusters merged into one because there is a relatively narrow path joining them).I actually have some code for things like that in my
borsarrepository (which unfortunatelly, despite my best intentions, does not contain much good documentation apart from docstrings; but has good test coverage at least :) ). Unfortunatelly it does clustering differently to mne - uses separate functions (both numba and numpy) specialized for common scenarios (like 3d space with channel adjacency (TFR), 2d space with adjacency (ERP) etc.) - so making this code part of mne might not be straightforward.If someone would like to try it out, here is a short example of finding clusters using minimum number of adjacent channels (
min_adj_chbelow):this returns two boolean 2d arrays in
clustersbecause there are two clusters when eliminating samples without at least one adjacent supra-threshold sample in the channels dimension. There are also higher level functions for performing the whole cluster based permutation test.10 remaining items
hoechenberger commentedon May 17, 2022
I second that those sparse matrices are kinda odd to me ad a user
drammock commentedon May 20, 2022
indeed the
spatio_temporal_cluster_(1samp_)testfunctions automatically add a lattice connectivity along the time dimension based on themax_stepparameter, as long as the value passed foradjacencyis the correct shape (I think it's usingcombine_adjacencyunder the hood).hoechenberger commentedon Jun 11, 2022
Hello all, so what's the plan here on how to proceed? I believe tackling this will be a major and widely applauded improvement. Maybe we could create some sort of milestone/task/todo list and distribute tasks? :)
sappelhoff commentedon Jun 12, 2022
To me this sounds like one PR that adds this feature, as Eric described in #10604 (comment) (although that comment only describes time and space, and we should also implement this for frequencies)
kimcoco commentedon Jun 27, 2022
I would be interested in working on this during the code sprint.
dnacombo commentedon Nov 19, 2026
Hi,
I volunteer to take over this task, targeting
cluster_test.min_spatial_spanfollowing up on #14399 and #13774
My plan is to subsequently work on PRs for a between subject factorial anova and associated
cluster_test.@drammock @larsoner
Am I on track?
larsoner commentedon Nov 19, 2026
I think we settled on counting neighbors. The example
(n_ch, n_times)arraywould need to be pruned at the neighbor level (removing the second and third ones from the last row) before clustering happened. I think this is @sappelhoff 's use case
I suspect it can get tricky because the channel adjacency gets folded into a single combined adjacency: with
adjacency="auto",cluster_testbuilds a(n_times * n_freqs * n_ch,) * 2-square matrix (the legacyspatio_temporal_*functions use(n_ch * n_freqs,) * 2). For"auto"we have the channel adjacency before combining, so we could start with that case and raise an error for user-supplied adjacency in higher dimensions, or describe / pass the dimensions through somehow to handle it in general...dnacombo commentedon Nov 20, 2026
larsoner commentedon Nov 24, 2026
The idea I think is that it's an operation between thresholding (after you have
x_in) and_get_labels(which makes the clusters), not an operation on the clusters themselves. I think this is whatminnbchandoes. It might makes sense to make this an erosion that is applied repeatedly until it stops having an effect, I'm not sure if FieldTrip does this or just applies once. I am pretty sureminnbchanonly counts spatial neighbors (channels) at the same time (and frequency) so if we want to be fieldtrip-like, we need to "un-ravel" (or be able to conceptually unravel) our adjacency matrix, which potentially collapses across multiple dimensions (e.g., space and frequency).Keeping only clusters that are at least
Npoints total is not something that we need, since this is easy enough to implement post-hoc on observed clusters, and isn'tminnbchan-like.