Skip to content

Commit 976e28e

Browse files
committed
Add tests for label adjacency
1 parent 3d154de commit 976e28e

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

mne/tests/test_label.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121
from scipy import sparse
2222

23+
import mne
2324
from mne import (
2425
grow_labels,
2526
labels_to_stc,
@@ -57,13 +58,17 @@
5758
real_label_fname = data_path / "MEG" / "sample" / "labels" / "Aud-lh.label"
5859
v1_label_fname = subjects_dir / "sample" / "label" / "lh.V1.label"
5960

61+
fname_vsrc = data_path / "MEG" / "sample" / "sample_audvis_trunc-meg-vol-7-fwd.fif"
62+
fname_src_fs = data_path / "subjects" / "fsaverage" / "bem" / "fsaverage-ico-5-src.fif"
63+
6064
fwd_fname = data_path / "MEG" / "sample" / "sample_audvis_trunc-meg-eeg-oct-6-fwd.fif"
6165
src_bad_fname = data_path / "subjects" / "fsaverage" / "bem" / "fsaverage-ico-5-src.fif"
6266
label_dir = subjects_dir / "sample" / "label" / "aparc"
6367

6468
test_path = Path(__file__).parents[1] / "io" / "tests" / "data"
6569
label_fname = test_path / "test-lh.label"
6670

71+
6772
# This code was used to generate the "fake" test labels:
6873
# for hemi in ['lh', 'rh']:
6974
# label = Label(np.unique((np.random.rand(100) * 10242).astype(int)),
@@ -1255,3 +1260,121 @@ def test_label_geometry(fname, area):
12551260
)
12561261
assert_array_less(inside_euc, inside_dist)
12571262
assert_array_less(0.25 * inside_dist, inside_euc)
1263+
1264+
1265+
@testing.requires_testing_data
1266+
def test_volume_label_adjacency():
1267+
"""Test label adjacency."""
1268+
pytest.importorskip("sklearn")
1269+
src = read_source_spaces(fname_vsrc)
1270+
1271+
# aseg=auto uses the aparc+aseg atlas, which does not exist in the testing datasets
1272+
adj, labels = mne.volume_label_adjacency(
1273+
src, subject="sample", subjects_dir=subjects_dir, aseg="aseg"
1274+
)
1275+
n_neighbors = adj.sum(axis=1)
1276+
1277+
assert_equal(len(labels), 46) # default number of labels in aseg.mgz
1278+
assert_equal(adj.shape, (len(labels), len(labels)))
1279+
1280+
assert_equal(n_neighbors.min(), 0)
1281+
assert_equal(np.sum(n_neighbors == 0), 4)
1282+
1283+
# example: 'Left-Thalamus-Proper'
1284+
label_idx = 7
1285+
connected_labels_idx = adj.toarray()[label_idx, :]
1286+
connected_labels = np.array(labels)[np.where(connected_labels_idx == 1)[0]]
1287+
1288+
assert_equal(
1289+
np.sort(connected_labels).tolist(),
1290+
[
1291+
"3rd-Ventricle",
1292+
"Brain-Stem",
1293+
"CSF",
1294+
"Left-Accumbens-area",
1295+
"Left-Cerebral-Cortex",
1296+
"Left-Cerebral-White-Matter",
1297+
"Left-Hippocampus",
1298+
"Left-Lateral-Ventricle",
1299+
"Left-Thalamus-Proper",
1300+
"Left-VentralDC",
1301+
"Unknown",
1302+
],
1303+
)
1304+
1305+
input_labels = [
1306+
"Left-Thalamus-Proper",
1307+
"Left-Hippocampus",
1308+
"Right-Hippocampus",
1309+
]
1310+
adj, labels = mne.volume_label_adjacency(
1311+
src,
1312+
subject="sample",
1313+
subjects_dir=subjects_dir,
1314+
aseg="aseg",
1315+
labels=input_labels,
1316+
)
1317+
1318+
assert_equal(
1319+
adj.toarray(),
1320+
np.array(
1321+
[
1322+
[1, 1, 0],
1323+
[1, 1, 0],
1324+
[0, 0, 1],
1325+
]
1326+
),
1327+
)
1328+
1329+
assert_equal(labels, input_labels)
1330+
1331+
with pytest.raises(FileNotFoundError):
1332+
mne.volume_label_adjacency(
1333+
src,
1334+
subject="sample",
1335+
subjects_dir=subjects_dir,
1336+
aseg="my-aseg",
1337+
labels=input_labels,
1338+
)
1339+
1340+
1341+
@testing.requires_testing_data
1342+
def test_label_adjacency():
1343+
"""Test label adjacency."""
1344+
pytest.importorskip("sklearn")
1345+
src = read_source_spaces(fname_src_fs)
1346+
mne.add_source_space_distances(src, dist_limit=0.01, n_jobs=-1)
1347+
1348+
labels = mne.read_labels_from_annot(
1349+
subject="fsaverage",
1350+
subjects_dir=subjects_dir,
1351+
)
1352+
adj = mne.label_adjacency(labels, src)
1353+
1354+
n_neighbors = adj.sum(axis=1)
1355+
1356+
assert_equal(len(labels), 69) # default number of labels in aseg.mgz
1357+
assert_equal(adj.shape, (len(labels), len(labels)))
1358+
1359+
assert_equal(n_neighbors.min(), 0)
1360+
assert_equal(np.sum(n_neighbors == 0), 1)
1361+
1362+
input_labels = [
1363+
"cuneus-lh",
1364+
"cuneus-rh",
1365+
"precuneus-lh",
1366+
]
1367+
adj = mne.label_adjacency(
1368+
[lab for lab in labels if lab.name in input_labels],
1369+
src
1370+
)
1371+
assert_equal(
1372+
adj.toarray(),
1373+
np.array(
1374+
[
1375+
[1, 0, 1],
1376+
[0, 1, 0],
1377+
[1, 0, 1],
1378+
]
1379+
),
1380+
)

0 commit comments

Comments
 (0)