# Spatiotemporal cluster permutation on evoked data in a within subjects design

**URL:** <https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [January 17, 2018, 4:34pm UTC](https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398 "2018-01-17T16:34:26Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [January 17, 2018, 4:34pm UTC](https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398/1 "2018-01-17T16:34:26Z")

</div>

Hi all,

I'm trying to run spatiotemporal cluster permutation on evoked responses in  
a within subjects design where each subject is observed in two conditions:  
cond1 and cond2:

to do so I'm using the following:

# for 17 subjects and 100 ms time window

conditions= [cond1, cond2]  
for j, cond in enumerate(conditions):  
&nbsp;&nbsp;&nbsp;&nbsp;X\_tmp = np.zeros((17, 101, 102))

&nbsp;&nbsp;&nbsp;&nbsp;for i, subject in enumerate(subjects):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evoked = mne.read\_evokeds(fname)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grads= evoked.pick\_types(meg="grad")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rms = rms\_grad(grads.data)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X\_tmp[i, :] = np.transpose(rms)

&nbsp;&nbsp;&nbsp;&nbsp;if j == 0:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X = X\_tmp  
&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X = np.vstack((X, X\_tmp))

connectivity, ch\_names = find\_ch\_connectivity(evoked.info, ch\_type='mag')

T\_obs, clusters, p\_values, \_ = spatio\_temporal\_cluster\_test([X[:17],  
X[17:]],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n\_permutations=5000,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail=0,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n\_jobs=2,

connectivity=connectivity)

After around 20 mints of running the script, the returned p\_values look  
like this:

p\_values  
Out[68]:  
array([ 0.9476, 1. , 1. , 0.8854, 1. , 1. , 0.9966,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1. , 1. , 1. , 1. , 1. , 0.9974, 1. ,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1. , 1. , 1. , 1. ])

My question is, given the input data: [X[:17], X[17:]] where:  
X[17:] contains the data observed in cond1 from all 17 subjects and  
similarly X[17:] is the data observed in cond2.

are the permutations in this case done between cond1 and cond2 within the  
same subject (as should be) or randomly between subjects?

Many thanks in advance

Rasha

\<[https://www.avast.com/sig-email?utm\_medium=email&utm\_source=link&utm\_campaign=sig-email&utm\_content=webmail](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail)\>  
Virus-free.  
[www.avast.com](http://www.avast.com)  
\<[https://www.avast.com/sig-email?utm\_medium=email&utm\_source=link&utm\_campaign=sig-email&utm\_content=webmail](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail)\>  
\<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2\>  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180117/bde1bbe7/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180117/bde1bbe7/attachment.html)

---

<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:** [January 17, 2018, 7:07pm UTC](https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398/2 "2018-01-17T19:07:52Z")

</div>

Hi Rasha,

1. I'm not sure I follow your script. We don't have an rms\_grad function,  
but I guess you're taking the root mean square across gradiometers. If so,  
the shape of X\_tmp should be 2D (17 x n\_times) not 3D (17 \* 101 \* 102 in  
your script), shouldn't it?

2. I think the correct way to do the within subject analysis you mention is  
to use mne.stats.spatio\_temporal\_cluster\_1samp\_test: i.e. first you  
subtract the two condition within subject, second you run the permutations  
spatio\_temporal\_cluster\_1samp\_test(X[:17]-X[17:])

Hope that helps,

Jean-R?mi

---

<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:** [January 17, 2018, 8:18pm UTC](https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398/3 "2018-01-17T20:18:12Z")

</div>

Dear Jean,  
many thanks for your reply.

In fact \*rms\_gard\* is the same as \*\_merge\_grad\_data\* function imported from  
\*mne.channels.layout\* (\*from mne.channels.layout import \_merge\_grad\_data as  
rms\_grad\*). It merges the data of each pair of grads using the RMS and  
returns an array of shape (n\_grads/2, n\_times) so for the 204 grads I have  
102 arrays and the X\_temp has this shape (n\_subjects, n\_times, n\_grads/2).

Regarding your suggesting of subtracting the conditions first and then  
using mne.stats.spatio\_temporal\_cluster\_1samp\_test instead, could you  
please try to explain a bit more the logic behind using this method instead  
of the way I'm currently using now?

with best regards  
Rasha

\<[https://www.avast.com/sig-email?utm\_medium=email&utm\_source=link&utm\_campaign=sig-email&utm\_content=webmail](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail)\>  
Virus-free.  
[www.avast.com](http://www.avast.com)  
\<[https://www.avast.com/sig-email?utm\_medium=email&utm\_source=link&utm\_campaign=sig-email&utm\_content=webmail](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail)\>  
\<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2\>

---

<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:** [January 17, 2018, 9:29pm UTC](https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398/4 "2018-01-17T21:29:40Z")

</div>

> In fact \*rms\_gard\* is the same as \*\_merge\_grad\_data\* function imported  
> from \*mne.channels.layout\* (\*from mne.channels.layout import  
> \_merge\_grad\_data as rms\_grad\*). It merges the data of each pair of grads  
> using the RMS and returns an array of shape (n\_grads/2, n\_times) so for the  
> 204 grads I have 102 arrays and the X\_temp has this shape (n\_subjects,  
> n\_times, n\_grads/2).

Ok, that makes sense.

> Regarding your suggesting of subtracting the conditions first and then  
> using mne.stats.spatio\_temporal\_cluster\_1samp\_test instead, could you  
> please try to explain a bit more the logic behind using this method instead  
> of the way I'm currently using now?

IIRC the 1 sample cluster test permutes the sign (of the subtraction  
between the two conditions) across subjects. The test thus assesses the  
probability that your distribution is distinct from the distribution based  
on permuted signs.

Hope that helps,

Jean-R?mi

> with best regards  
> Rasha
> 
> \<[Safe emailing Mac | Avast](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&gt); Virus-free.  
> www.avast.com  
> \<[Safe emailing Mac | Avast](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&gt);  
> \<#m\_8627169073257330069\_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2\>
> 
> > Hi Rasha,
> > 
> > 1. I'm not sure I follow your script. We don't have an rms\_grad function,  
> > but I guess you're taking the root mean square across gradiometers. If so,  
> > the shape of X\_tmp should be 2D (17 x n\_times) not 3D (17 \* 101 \* 102 in  
> > your script), shouldn't it?
> > 
> > 2. I think the correct way to do the within subject analysis you mention  
> > is to use mne.stats.spatio\_temporal\_cluster\_1samp\_test: i.e. first you  
> > subtract the two condition within subject, second you run the permutations  
> > spatio\_temporal\_cluster\_1samp\_test(X[:17]-X[17:])
> > 
> > Hope that helps,
> > 
> > Jean-R?mi
> > 
> > > Hi all,
> > > 
> > > I'm trying to run spatiotemporal cluster permutation on evoked responses  
> > > in a within subjects design where each subject is observed in two  
> > > conditions: cond1 and cond2:
> > > 
> > > to do so I'm using the following:
> > > 
> > > # for 17 subjects and 100 ms time window
> > > 
> > > conditions= [cond1, cond2]  
> > > for j, cond in enumerate(conditions):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;X\_tmp = np.zeros((17, 101, 102))
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;for i, subject in enumerate(subjects):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evoked = mne.read\_evokeds(fname)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grads= evoked.pick\_types(meg="grad")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rms = rms\_grad(grads.data)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X\_tmp[i, :] = np.transpose(rms)
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;if j == 0:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X = X\_tmp  
> > > &nbsp;&nbsp;&nbsp;&nbsp;else:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X = np.vstack((X, X\_tmp))
> > > 
> > > connectivity, ch\_names = find\_ch\_connectivity(evoked.info,  
> > > ch\_type='mag')
> > > 
> > > T\_obs, clusters, p\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connectivity=connectivity)
> > > 
> > > After around 20 mints of running the script, the returned p\_values look  
> > > like this:
> > > 
> > > p\_values  
> > > Out[68]:  
> > > array([ 0.9476, 1. , 1. , 0.8854, 1. , 1. , 0.9966,  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1. , 1. , 1. , 1. , 1. , 0.9974, 1. ,  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1. , 1. , 1. , 1. ])
> > > 
> > > My question is, given the input data: [X[:17], X[17:]] where:  
> > > X[17:] contains the data observed in cond1 from all 17 subjects and  
> > > similarly X[17:] is the data observed in cond2.
> > > 
> > > are the permutations in this case done between cond1 and cond2 within  
> > > the same subject (as should be) or randomly between subjects?
> > > 
> > > Many thanks in advance
> > > 
> > > Rasha
> > > 
> > > \<[Safe emailing Mac | Avast](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&gt); Virus-free.  
> > > www.avast.com  
> > > \<[Safe emailing Mac | Avast](https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&gt);  
> > > \<#m\_8627169073257330069\_m\_-3461909942541472768\_m\_-8311896079980075393\_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2\>
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > Mne\_analysis mailing list  
> > > Mne\_analysis at nmr.mgh.harvard.edu  
> > > [Mne\_analysis Info Page](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)
> > > 
> > > The information in this e-mail is intended only for the person to whom  
> > > it is  
> > > addressed. If you believe this e-mail was sent to you in error and the  
> > > e-mail  
> > > contains patient information, please contact the Partners Compliance  
> > > HelpLine at  
> > > [MyComplianceReport.com: Compliance and Ethics Reporting](http://www.partners.org/complianceline) . If the e-mail was sent to you  
> > > in error  
> > > but does not contain patient information, please contact the sender and  
> > > properly  
> > > dispose of the e-mail.
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Mne\_analysis mailing list  
> > Mne\_analysis at nmr.mgh.harvard.edu  
> > [Mne\_analysis Info Page](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)
> > 
> > The information in this e-mail is intended only for the person to whom it  
> > is  
> > addressed. If you believe this e-mail was sent to you in error and the  
> > e-mail  
> > contains patient information, please contact the Partners Compliance  
> > HelpLine at  
> > [MyComplianceReport.com: Compliance and Ethics Reporting](http://www.partners.org/complianceline) . If the e-mail was sent to you  
> > in error  
> > but does not contain patient information, please contact the sender and  
> > properly  
> > dispose of the e-mail.
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mne\_analysis mailing list  
> Mne\_analysis at nmr.mgh.harvard.edu  
> [Mne\_analysis Info Page](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)
> 
> The information in this e-mail is intended only for the person to whom it  
> is  
> addressed. If you believe this e-mail was sent to you in error and the  
> e-mail  
> contains patient information, please contact the Partners Compliance  
> HelpLine at  
> [MyComplianceReport.com: Compliance and Ethics Reporting](http://www.partners.org/complianceline) . If the e-mail was sent to you in  
> error  
> but does not contain patient information, please contact the sender and  
> properly  
> dispose of the e-mail.

-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180117/6de6fd2b/attachment-0001.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180117/6de6fd2b/attachment-0001.html)

---

<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:** [January 17, 2018, 9:42pm UTC](https://mne.discourse.group/t/spatiotemporal-cluster-permutation-on-evoked-data-in-a-within-subjects-design/1398/5 "2018-01-17T21:42:43Z")

</div>

Thanks a lot Jean for your clarification. I will try doing it this way.

All the best  
Rasha
