Skip to content

Commit

Permalink
BUG: Fix grow_labels (mne-tools#9317)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Apr 19, 2021
1 parent e8207ae commit 7c5448a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ Bugs

- Fix bug with :func:`mne.extract_label_time_course` where labels, STCs, and the source space were not checked for compatible ``subject`` attributes (:gh:`9284` by `Eric Larson`_)

- Fix bug with :func:`mne.grow_labels` where ``overlap=False`` could run forever or raise an error (:gh:`9317` by `Eric Larson`_)

- Fix compatibility bugs with :mod:`mne_realtime` (:gh:`8845` by `Eric Larson`_)

- Fix bug with `mne.viz.Brain` where non-inflated surfaces had an X-offset imposed by default (:gh:`8794` by `Eric Larson`_)
Expand Down
11 changes: 7 additions & 4 deletions mne/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,8 +1573,7 @@ def grow_labels(subject, seeds, extents, hemis, subjects_dir=None, n_jobs=1,
# make sure the inputs are arrays
if np.isscalar(seeds):
seeds = [seeds]
# these can have different sizes so need to use object array
seeds = np.asarray([np.atleast_1d(seed) for seed in seeds], dtype='O')
seeds = [np.atleast_1d(seed) for seed in seeds]
extents = np.atleast_1d(extents)
hemis = np.atleast_1d(hemis)
n_seeds = len(seeds)
Expand Down Expand Up @@ -1636,7 +1635,7 @@ def grow_labels(subject, seeds, extents, hemis, subjects_dir=None, n_jobs=1,
if overlap:
# create the patches
parallel, my_grow_labels, _ = parallel_func(_grow_labels, n_jobs)
seeds = np.array_split(seeds, n_jobs)
seeds = np.array_split(np.array(seeds, dtype='O'), n_jobs)
extents = np.array_split(extents, n_jobs)
hemis = np.array_split(hemis, n_jobs)
names = np.array_split(names, n_jobs)
Expand Down Expand Up @@ -1668,7 +1667,7 @@ def _grow_nonoverlapping_labels(subject, seeds_, extents_, hemis, vertices_,
labels = []
for hemi in set(hemis):
hemi_index = (hemis == hemi)
seeds = seeds_[hemi_index]
seeds = [seed for seed, h in zip(seeds_, hemis) if h == hemi]
extents = extents_[hemi_index]
names = names_[hemi_index]
graph = graphs[hemi] # distance graph
Expand Down Expand Up @@ -1698,6 +1697,10 @@ def _grow_nonoverlapping_labels(subject, seeds_, extents_, hemis, vertices_,
# add neighbors within allowable distance
row = graph[vert_from, :]
for vert_to, dist in zip(row.indices, row.data):
# Prevent adding a point that has already been used
# (prevents infinite loop)
if (vert_to == seeds[label]).any():
continue
new_dist = old_dist + dist

# abort if outside of extent
Expand Down
4 changes: 4 additions & 0 deletions mne/tests/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,10 @@ def test_grow_labels():
l1 = l11 + l12
assert_array_equal(l1.vertices, l0.vertices)

# non-overlapping (gh-8848)
for overlap in (False, True):
grow_labels('fsaverage', [0], 1, 1, subjects_dir, overlap=overlap)


@testing.requires_testing_data
def test_random_parcellation():
Expand Down

0 comments on commit 7c5448a

Please sign in to comment.