[Mne_analysis] How to get coherence between stc files?

Marijn van Vliet w.m.vanvliet at gmail.com
Sun Jun 18 09:41:07 EDT 2017
Search archives:

Hi,

from having discussed with Maria, I gathered she doesn’t want to compute connectivity at all. She just wants to compute the coherence (across time, so one value for each vertex) between two experimental conditions.

Going by Erics recommendations, it may be that the spectral_connectivity function may be able to be coaxed into doing that:

# Apply the inverse operator on each epoch
stcs_condition1 = apply_inverse_epochs(epochs_condition1, inverse_operator, …)
stcs_condition2 = apply_inverse_epochs(epochs_condition1, inverse_operator, …)

# Concatenate the epochs into a big 3D array
data_condition1 = np.data([s.data for s in secs_condition1])
data_condition2 = np.data([s.data for s in secs_condition2])

# The number of vertices is the size of one of the data arrays along the 2nd dimension
n_vertices = data_condition1.shape[1]

# Concatenate the two conditions along the spatial axis (what Eric suggested)
data = np.concatenate([data_condition1, data_condition2], axis=1)

# We are going to tell the spectral_connectivity function to compute coherence for each vertex between both conditions.
# So vertex 0 should be compared to vertex 0 + n_vertices, and vertex 1 to vertex 1 + n_vertices, etc.
indices = [np.arange(n_vertices), np.arange(n_vertices) + n_vertices]

# Finally, call the spectral_connectivity function
con, freqs, times, n_epochs, n_tapers = spectral_connectivity(data, indices=indices, ...)


I’ve no idea whether that will work, but it is my best guess :) Maybe someone with more experience in computing coherence can give better guidance.

Marijn.

> On 18 Jun 2017, at 10:56, Maria Hakonen <maria.hakonen at gmail.com> wrote:
> 
> Hi,
> 
> Unfortunately, I think that I can’t do that so easily since apply_inverse_epochs gives a list of stcs and
> len(stcs) is 160, so it has only one dimension. If I try
> data=np.concatenate((stcs_1,stcs_2),axis=1)
> 
> I get:
> IndexError: axis 1 out of bounds [0, 1)
> 
> Only this works:
> data=np.concatenate((stcs_1,stcs_2),axis=0)
> 
> In [45]: data.shape
> Out[45]: (320,)
> 
> data[0]
> <SourceEstimate  |  20482 vertices, subject : ah, tmin : -99.9936018905 (ms), tmax : 2899.81445482 (ms), tstep : 0.833280015754 (ms), data size : 20482 x 3601>
> 
> However, I can concatenate data of stc files from two lists of stcs:
> data=np.concatenate((stcs_1[0].data,stcs_2[0].data),axis=0)
> data.shape
> Out[39]: (40964, 3601) (vertices=40964 and time=3601)
> 
> Thereafter, I tried to create a new stc as follows:
> stc_3=stc_1
> stc_3.data = data
> 
> but got:
> AttributeError: can't set attribute
> 
> I wonder if there is any way to replace “data” attribute of stc file? I haven’t managed to find any example about this.  
> 
> If I could create new stc files of the concatenated data, I could probably create a list of them and give it as an argument to mne.connectivity.spectral_connectivity. In this case, the indices would be
> indices = (np.array([1, 2, … ,20482]), np.array([20483, … 40964]))
> 
> Best,
> Maria
> 
> 2017-06-18 0:20 GMT+03:00 Eric Larson <larson.eric.d at gmail.com>:
> Concatenate stcs1 and stcs2 along axis=1 (spatial dimension), similar to before. The logic for vertices here is the same as it was for channels in your previous example. The connectivity code has no notion of datasets, only single signals (axis=1) -- so whatever you want to compute connectivity between (channels, sources, label means, whatever) needs to be defined along that axis (with the epochs along axis=0 being meaningfully paired across signals).
> 
> Eric
> 
> 
> 
> On Jun 17, 2017 15:07, "Maria Hakonen" <maria.hakonen at gmail.com> wrote:
> Hi,
> 
> Many thanks for your answers! This clarified the matter for me.
> 
> However, instead of calculating coherence in sensor space, I would like to calculate coherence in source space (i.e. I would need to get coherence between each vertex from the first dataset and their counterparts from the second dataset). Therefore, I have applied inverse operator for each epoch of condition 1 and condition 2:
> 
> stcs1 = apply_inverse_epochs(epochs1, inverse_operator1, lambda2, method,
>                             pick_ori="normal")
> stcs2 = apply_inverse_epochs(epochs2, inverse_operator2, lambda2, method,
>                             pick_ori="normal")
> 
> This gives me a list of 160 stcs that are as follows:
> stcs1[0]
> Out[25]: <SourceEstimate  |  20482 vertices, subject : ah, tmin : -99.9936018905 (ms), tmax : 2899.81445482 (ms), tstep : 0.833280015754 (ms), data size : 20482 x 3601> 
> 
> Then, I could combine these two list of stcs and give the resulting list as an argument to mne.connectivity.spectral_connectivity:
> coh, freqs, times, n_epochs, n_tapers = spectral_connectivity(
>     stcs, method='coh', mode='fourier', indices=indices,
>     sfreq=sfreq, fmin=fmin, fmax=fmax, faverage=True, n_jobs=1)
> 
> However, I would need to estimate coherence between each vertex from the first dataset and each vertex in the second dataset. As far as I understand, “indices” can only determine the vertices between which to compute coherence within a dataset but not between two datasets. 
> 
> The documentation says:
> 
> "By default, the connectivity between all signals is computed (only connections corresponding to the lower-triangular part of the connectivity matrix). If one is only interested in the connectivity between some signals, the “indices” parameter can be used. For example, to compute the connectivity between the signal with index 0 and signals “2, 3, 4” (a total of 3 connections) one can use the following:
> 
> indices = (np.array([0, 0, 0]),    # row indices
>            np.array([2, 3, 4]))    # col indices
> 
> con_flat = spectral_connectivity(data, method='coh',
>                                  indices=indices, ...)"
> 
> Could you please let me know if there is any way in mne to calculate coherence between two datasets in source space?
> 
> Best,
> Maria
> 
> 
> 2017-06-16 18:32 GMT+03:00 Eric Larson <larson.eric.d at gmail.com>:
> data=np.concatenate((epochs_1.get_data(),epochs_2.get_data()),axis=0)
> 
> data.shape
> 
> Out[28]: (320, 308, 3721)
> 
> 
> Following the nomenclature from the connectivity docs, your input data need to be formatted as (n_epochs, n_signals, n_times). Keep in mind that the n_signals dimension is usually the spatial one, like source space vertices, or in your case, channels. So the way you have this set up, you have 308 spatial channels / signals, each with 320 epochs / repeats.
> 
> I am not sure how the parameter “indices” should be set in my case.
> 
> 
> Indices should be into the (typically) spatial dimension, i.e. the n_signals dimension.
> 
> I would like to calculate the coherence between each data point in the file 1 and the corresponding data points in the file 2.
> 
> 
> "data point" is a bit ambiguous -- I assume you mean you want to estimate the connectivity between each channel from the first dataset and each channel in the second dataset....
> 
> Since I have 320 epochs after concatenation, I tried:
> 
> indices=(np.arange(1,160),np.arange(161,320)) (i.e. 160 epochs in each file).
> 
>  
> ... which means this probably isn't set up to do what you want.
> 
> What you might want is this:
> data=np.concatenate((epochs_1.get_data(),epochs_2.get_data()
> ),axis=1)
> data.shape
> (160, 616, 3721)
> 
>> This way, you have 616 signals of interest (308 signals / channels from one condition, 308 from signals / channels from another), each with 160 epochs / repeats.
> 
> Hopefully from there you can come up with how to get connectivity you want. For example, if you want each channel only with its corresponding channel from the other condition (308 estimates), this would be something like:
> (np.arange(308), np.arange(308) + 308)
> 
>> If you wanted all channels from condition 1 with all channels from condition 2 (94864 estimates), this would be something like:
> (np.repeat(np.arange(308), 308), np.tile(np.arange(308) + 308, 308))
> 
>> And 308 is a somewhat rare number of data channels -- you might want to check to make sure you have only data channels picked as Jaakko suggests.
> 
> HTH,
> Eric
> 
> 
> _______________________________________________
> Mne_analysis mailing list
> Mne_analysis at nmr.mgh.harvard.edu
> 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
> 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
> 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
> 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
> 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
> 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
> 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
> 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.




More information about the Mne_analysis mailing list