Skip to content

Commit 87c762c

Browse files
committed
intra and inter hemispheric connection selection in conn_links
1 parent d615424 commit 87c762c

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

frites/conn/conn_utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def conn_get_pairs(roi, directed=False, nb_min_suj=-np.inf, verbose=None):
144144

145145
def conn_links(roi, directed=False, net=False, within_roi=True, sep='auto',
146146
nb_min_links=None, pairs=None, sort=True, triu_k=1,
147-
verbose=None):
147+
hemisphere=None, hemi_links='both', verbose=None):
148148
"""Construct pairwise links for functional connectivity.
149149
150150
This function can be used for defining the pairwise links for computing
@@ -179,6 +179,17 @@ def conn_links(roi, directed=False, net=False, within_roi=True, sep='auto',
179179
triu_k : int | 1
180180
Diagonal offset when estimating the undirected links to use. By
181181
default, triu_k=1 means that we skip auto-connections
182+
hemisphere : array_like | None
183+
List of hemisphere names
184+
hemi_links : {'both', 'intra', 'inter'}
185+
Specify whether connectivity links should be :
186+
187+
* 'both': intra-hemispheric and inter-hemispheric (default)
188+
* 'intra': intra-hemispheric
189+
* 'inter': inter-hemispheric
190+
191+
In order to work, you should provide the hemisphere name using the
192+
input `hemisphere`
182193
183194
Returns
184195
-------
@@ -244,6 +255,19 @@ def conn_links(roi, directed=False, net=False, within_roi=True, sep='auto',
244255
keep = [df.loc[r] >= nb_min_links for r in roi_st]
245256
x_s, x_t = x_s[keep], x_t[keep]
246257

258+
# hemisphere selection
259+
if isinstance(hemisphere, (list, np.ndarray)):
260+
assert hemi_links in ['both', 'intra', 'inter']
261+
hemisphere = np.asarray(hemisphere)
262+
h_s, h_t = hemisphere[x_s], hemisphere[x_t]
263+
if hemi_links in ['intra', 'inter']:
264+
keep = h_s == h_t if hemi_links == 'intra' else h_s != h_t
265+
x_s, x_t = x_s[keep], x_t[keep]
266+
else:
267+
keep = np.array([True] * len(x_s))
268+
logger.info(f" Hemispheric selection (hemi_links={hemi_links}, "
269+
f"dropped={(~keep).sum()} links)")
270+
247271
# build pairs of brain region names
248272
roi_st = np.asarray([f"{s}{sep}{t}" for s, t in zip(roi[x_s], roi[x_t])])
249273

frites/conn/tests/test_conn_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,15 @@ def test_conn_links(self):
284284
_, roi_st = conn_links(roi, pairs=np.c_[p_1, p_2], directed=True)
285285
np.testing.assert_array_equal(
286286
roi_st, ['dlPFC->aINS', 'dlPFC->vmPFC'])
287+
288+
# test hemispheric selection
289+
hemi = ['R', 'R', 'L', 'L']
290+
roi_2 = ['r0', 'r1', 'r2', 'r3']
291+
_, roi_st = conn_links(roi_2, hemisphere=hemi, hemi_links='both')
292+
np.testing.assert_array_equal(
293+
roi_st, ['r0-r1', 'r0-r2', 'r0-r3', 'r1-r2', 'r1-r3', 'r2-r3'])
294+
_, roi_st = conn_links(roi_2, hemisphere=hemi, hemi_links='intra')
295+
np.testing.assert_array_equal(roi_st, ['r0-r1', 'r2-r3'])
296+
_, roi_st = conn_links(roi_2, hemisphere=hemi, hemi_links='inter')
297+
np.testing.assert_array_equal(
298+
roi_st, ['r0-r2', 'r0-r3', 'r1-r2', 'r1-r3'])

0 commit comments

Comments
 (0)