mne-tools / mne-tools/mne-python

Showing label borders is not possible on the flat brain surface

Open
#13,204 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

BUG
Dominant language
Python
Stars
3.5k
Forks
1.6k
Avg merge
1d 6h
Merged PRs (30d)
100

Description

Description of the problem

Hi and thank you for all the effort put into MNE-Python! Unfortunately, I ran into a problem when trying to plot borders of any region of the Desikan-Killiany parcellation on the flat brain surface. Somewhat surprisingly to me, plotting the parcellation as a whole works fine, although it also requires showing borders of all regions. Plotting the whole region (borders=False) also works, the problem only occurs with borders=True or borders set to an integer.

I would be happy to fix the issue but it might take a while for me to figure out the cause. If you have any hints about what went wrong, they would help a lot :)

Steps to reproduce
import mne

from mne.datasets import sample 

subjects_dir = sample.data_path() / "subjects"
subject = "fsaverage"

labels = mne.read_labels_from_annot(subject, "aparc", subjects_dir=subjects_dir)

Brain = mne.viz.get_brain_class()
brain = Brain(subject=subject, hemi="lh", surf="flat")
# brain.add_annotation("aparc")               # works fine
# brain.add_label(labels[0], borders=False)   # works fine
brain.add_label(labels[0], borders=True)      # crashes
brain._renderer.plotter.app.exec()
Link to data

No response

Expected results

A Pyvista figure of the flat surface of the left hemisphere with borders of one label (bankssts-lh) shown

Actual results

Error message:

Traceback (most recent call last):
  File "/home/willenjoy/Projects/roiextract-simulations/scripts/misc/bugs/mne_viz_brain_add_label_flat.py", line 14, in <module>
    brain.add_label(labels[0], borders=True)      # crashes
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/willenjoy/miniforge3/envs/roiextract-simulations/lib/python3.12/site-packages/mne/viz/_brain/_brain.py", line 2265, in add_label
    keep_idx = _mesh_borders(self.geo[hemi].faces, scalars)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/willenjoy/miniforge3/envs/roiextract-simulations/lib/python3.12/site-packages/mne/surface.py", line 1929, in _mesh_borders
    edges = mesh_edges(tris)
            ^^^^^^^^^^^^^^^^
  File "/home/willenjoy/miniforge3/envs/roiextract-simulations/lib/python3.12/site-packages/mne/surface.py", line 1713, in mesh_edges
    return _mesh_edges(tris=tris)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/willenjoy/miniforge3/envs/roiextract-simulations/lib/python3.12/site-packages/mne/surface.py", line 1719, in _mesh_edges
    raise ValueError("Cannot compute adjacency on a selection of triangles.")
ValueError: Cannot compute adjacency on a selection of triangles.
Additional information
Platform             Linux-6.8.0-57-generic-x86_64-with-glibc2.35
Python               3.12.0 | packaged by conda-forge | (main, Oct  3 2023, 08:43:22) [GCC 12.3.0]
Executable           /home/willenjoy/miniforge3/envs/roiextract-simulations/bin/python
CPU                  12th Gen Intel(R) Core(TM) i7-1255U (12 cores)
Memory               15.3 GiB

Core
├☑ mne               1.9.0 (latest release)
├☑ numpy             1.26.2 (OpenBLAS 0.3.23.dev with 12 threads)
├☑ scipy             1.14.1
└☑ matplotlib        3.8.2 (backend=QtAgg)

Numerical (optional)
├☑ sklearn           1.3.2
├☑ numba             0.61.0
├☑ nibabel           5.2.0
├☑ pandas            2.1.4
└☐ unavailable       nilearn, dipy, openmeeg, cupy, h5io, h5py

Visualization (optional)
├☑ pyvista           0.43.1 (OpenGL 4.6 (Core Profile) Mesa 23.2.1-1ubuntu3.1~22.04.2 via Mesa Intel(R) Graphics (ADL GT2))
├☑ pyvistaqt         0.11.0
├☑ vtk               9.3.0
├☑ qtpy              2.4.1 (PyQt6=6.8.1)
└☐ unavailable       ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
├☑ mne-bids          0.16.0
├☑ mne-connectivity  0.7.0
└☐ unavailable       mne-nirs, mne-features, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, mffpy, pybv

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the provided Brain.add_label example, then start in mne/viz/_brain/_brain.py at Brain.add_label and follow _mesh_borders in mne/surface.py. Check how the flat-surface label selection reaches mesh_edges. Done means borders=True or an integer renders the label without the reported ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.