Last active
November 30, 2021 21:02
-
-
Save EtienneCmb/24897bb4acaeb16a8cffef209293f491 to your computer and use it in GitHub Desktop.
Frites -> MNE-Connectivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Converting Frites' outputs to MNE-connectivity. | |
""" | |
import numpy as np | |
import xarray as xr | |
def conn_frites_to_mne(conn): | |
"""Conversion of Frites' connectivity outputs to MNE-connectivity. | |
For performing the conversion to MNE-Python this function requires the | |
MNE-Connectivity package. | |
Parameters | |
---------- | |
conn : xr.DataArray | |
Connectivity array (output of of a frites.conn function) | |
Returns | |
------- | |
conn_mne : mne_connectivity | |
Connectivity object compatible with MNE-Python | |
""" | |
assert isinstance(conn, xr.DataArray) | |
# getting universal attributes | |
indices = (conn.attrs['sources'], conn.attrs['targets']) | |
method = conn.name | |
attrs = conn.attrs | |
# conversion according to connectivity type | |
if conn.attrs['type'] in ['dfc']: | |
from mne_connectivity import EpochTemporalConnectivity | |
times = conn['times'].data | |
names = conn['roi'].data | |
conn_mne = EpochTemporalConnectivity( | |
conn.data, times, len(names), names=names, indices=indices, | |
method=method, attrs=attrs | |
) | |
return conn_mne | |
if __name__ == '__main__': | |
import matplotlib.pyplot as plt | |
from frites.conn import define_windows | |
from frites.conn import conn_dfc | |
from frites.simulations import StimSpecAR | |
# data simulation using AR model | |
ss = StimSpecAR() | |
x = ss.fit(ar_type='hga', n_epochs=100, n_stim=2) | |
# sliding window definition | |
slwin_len = .3 | |
slwin_step = .02 | |
win_sample = define_windows( | |
x['times'].data, slwin_len=slwin_len, slwin_step=slwin_step | |
)[0] | |
# compute amplitude "correlation" | |
conn = conn_dfc(x, win_sample=win_sample, times='times', roi='roi') | |
# plot the results | |
# conn.groupby('trials').mean().plot(x='times', hue='trials') | |
# plt.show() | |
conn_mne = conn_frites_to_mne(conn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment