Skip to content

Commit a66faa7

Browse files
committed
DFC supports channel aggregation
1 parent ad2e6e6 commit a66faa7

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

frites/conn/conn_dfc.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def conn_dfc(data, win_sample=None, times=None, roi=None, n_jobs=1, gcrn=True,
14-
verbose=None):
14+
agg_ch=False, verbose=None):
1515
"""Single trial Dynamic Functional Connectivity.
1616
1717
This function computes the Dynamic Functional Connectivity (DFC) using the
@@ -46,6 +46,13 @@ def conn_dfc(data, win_sample=None, times=None, roi=None, n_jobs=1, gcrn=True,
4646
Specify if the Gaussian Copula Rank Normalization should be applied.
4747
If the data are normalized (e.g z-score) this parameter can be set to
4848
False because the data can be considered as gaussian over time.
49+
agg_ch : bool | False
50+
In case there are multiple electrodes, channels, contacts or sources
51+
inside a brain region, specify how the data has to be aggregated. Use
52+
either :
53+
54+
* agg_ch=False : compute the pairwise DFC aross all possible pairs
55+
* agg_ch=True : compute the multivariate MI
4956
5057
Returns
5158
-------
@@ -78,13 +85,20 @@ def conn_dfc(data, win_sample=None, times=None, roi=None, n_jobs=1, gcrn=True,
7885

7986
# -------------------------------------------------------------------------
8087
# find group of brain regions
81-
gp = pd.DataFrame({'roi': roi}).groupby('roi').groups
82-
roi_gp, roi_idx = list(gp.keys()), list(gp.values())
88+
if agg_ch:
89+
logger.info(' Grouping pairs of brain regions')
90+
gp = pd.DataFrame({'roi': roi}).groupby('roi').groups
91+
roi_gp = np.array(list(gp.keys()))
92+
roi_idx = np.array(list(gp.values()))
93+
else:
94+
roi_gp, roi_idx = roi, np.arange(len(roi)).reshape(-1, 1)
8395
n_roi = len(roi_gp)
8496
x_s, x_t = np.triu_indices(n_roi, k=1)
8597
n_pairs = len(x_s)
86-
pairs = np.c_[x_s, x_t]
87-
roi_p = [f"{roi_gp[s]}-{roi_gp[t]}" for s, t in zip(x_s, x_t)]
98+
# build names of pairs of brain regions
99+
roi_s, roi_t = roi_gp[x_s], roi_gp[x_t]
100+
roi_s, roi_t = np.sort(np.c_[roi_s, roi_t], axis=1).T
101+
roi_p = [f"{s}-{t}" for s, t in zip(roi_s, roi_t)]
88102

89103
# -------------------------------------------------------------------------
90104
# prepare outputs and elements

frites/conn/tests/test_conn.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ def test_conn_dfc(self):
3838
dfc = conn_dfc(x, win_sample, times=times, roi=roi)
3939
assert isinstance(dfc, xr.DataArray)
4040

41+
# test empty window definition + sorted channel aggregation
42+
x = np.random.rand(10, 3, 100)
43+
trials = np.arange(10)
44+
roi = ['roi_1', 'roi_0', 'roi_0']
45+
times = (np.arange(100) - 10) / 64.
46+
x = xr.DataArray(x, dims=('trials', 'roi', 'times'),
47+
coords=(trials, roi, times))
48+
dfc = conn_dfc(x, times='times', roi='roi', agg_ch=False)
49+
assert dfc.shape == (10, 3, 1)
50+
np.testing.assert_array_equal(
51+
dfc['roi'].data, ['roi_0-roi_1', 'roi_0-roi_1', 'roi_0-roi_0'])
52+
53+
dfc = conn_dfc(x, times='times', roi='roi', agg_ch=True)
54+
assert dfc.shape == (10, 1, 1)
55+
np.testing.assert_array_equal(dfc['roi'].data, ['roi_0-roi_1'])
56+
4157
def test_conn_covgc(self):
4258
"""Test function conn_covgc."""
4359
n_epochs = 5

0 commit comments

Comments
 (0)