`read_forward_solution` loses subcortical source spaces

Aperta
#14,460 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
data

Direzione di ricerca

Start at mne/forward/forward.py lines 610-612, where read_forward_solution rebuilds fwd['src'], and compare it with the write path; the subcortical IDs (1018) suggest the source-space reading loses the extra surface entries. Trace how setup_subcortical_source_space (#14130) marks those spaces and how src.kind derives subcortical_surf/mixed. Done means the issue's round-trip script prints the expected kinds and IDs, and a forward write/read test is added alongside the existing subcortical tests.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

BUG
Description of the problem

A forward solution built from a subcortical surface source space (`setup_subcortical_source_space, added in #14130) doesn't survive a write/read round trip. The issue seems to be here:

https://github.com/mne-tools/mne-python/blob/main/mne/forward/forward.py#L610-L612

Steps to reproduce
# %% Imports
import tempfile
from pathlib import Path

import mne
from mne.datasets import sample

# %% Setup
data_path = sample.data_path()
subjects_dir = data_path / "subjects"
info = mne.io.read_info(data_path / "MEG" / "sample" / "sample_audvis_raw.fif")
trans = data_path / "MEG" / "sample" / "sample_audvis_raw-trans.fif"
bem = mne.read_bem_solution(subjects_dir / "sample" / "bem" / "sample-5120-bem-sol.fif")

tmp_dir = Path(tempfile.mkdtemp())

# %% Read cortex source space and create subcortical source space.
src_cortex = mne.read_source_spaces(
    subjects_dir / "sample" / "bem" / "sample-oct-6-src.fif"
)
src_subcortical = mne.setup_subcortical_source_space(
    "sample", label="Left-Amygdala", subjects_dir=subjects_dir
)


# %% Define helper function to create a forward solution, write it to disk, and read it back.
def meg_forward_round_trip(src, name):
    fwd = mne.make_forward_solution(
        info, trans, src, bem, meg=True, eeg=False, verbose=False
    )
    print(f"{name} in memory:", fwd["src"].kind, [s["id"] for s in fwd["src"]])

    fname = tmp_dir / f"{name}-fwd.fif"
    mne.write_forward_solution(fname, fwd, verbose=False)

    fwd_read = mne.read_forward_solution(fname, verbose=False)
    ids = [s["id"] for s in fwd_read["src"]]
    try:
        kind = fwd_read["src"].kind
    except RuntimeError as err:
        kind = f"RuntimeError: {err}"
    print(f"{name} read back:", kind, ids)


# %% Subcortical only: expected subcortical_surf [1018], read back as mixed [101].
meg_forward_round_trip(src_subcortical, "subcortical")

# %% Cortex + subcortical: expected mixed [101, 102, 1018], src.kind raises.
meg_forward_round_trip(src_cortex + src_subcortical, "mixed")
Link to data

No response

Expected results

subcortical in memory: subcortical_surf [1018]
subcortical read back: subcortical_surf [1018]
mixed in memory: mixed [101, 102, 1018]
mixed read back: mixed [101, 102, 1018]

Actual results

subcortical in memory: subcortical_surf [1018]
subcortical read back: mixed [101]
mixed in memory: mixed [101, 102, 1018]
mixed read back: RuntimeError: Invalid source space with kinds ['surf', 'surf', 'surf'] [101, 102, 101]

Additional information

A forward I/O round trip should probably be added to the subcortical tests too. Related: #14097, #14130, #14450.

mne.sys_info() output:
Platform Linux-6.17.0-1033-oem-x86_64-with-glibc2.39 (Wayland)
Python 3.13.13 (main, Jun 2 2026, 22:27:49) [Clang 22.1.3 ]
Executable /m/home/home6/69/taivait1/unix/dippa/mne-python/.venv/bin/python
CPU 12th Gen Intel(R) Core(TM) i5-12600K (16 cores)
Memory 31.0 GiB

Core
├☑ mne 1.14.0.dev135+gc16370a2b.d20261027 (development, latest release is 1.13.2)
├☑ numpy 2.5.4 (OpenBLAS 0.3.31.dev with 16 threads via pthreads)
├☑ scipy 1.18.1
└☑ matplotlib 3.11.2 (backend=module://matplotlib_inline.backend_inline)

Numerical (optional)
├☑ scikit-learn 1.9.1
├☑ threadpoolctl 3.7.0
├☑ nibabel 5.4.2
├☑ dipy 1.12.1
├☑ pandas 3.1.0
├☑ h5py 3.16.0
└☐ unavailable numba, nilearn, openmeeg, python-picard, jamica, cupy, h5io

Visualization (optional)
├☑ pyvista 0.49.1 (OpenGL 4.5.0 NVIDIA 580.196.07 via NVIDIA RTX A2000 12GB/PCIe/SSE2)
├☑ pyvistaqt 0.13.1
├☑ vtk 9.7.1
├☑ qtpy 2.4.3 (PySide6=6.12.0)
└☐ unavailable ipympl, pyqtgraph, mne-qt-browser, ipywidgets, trame, trame_client, trame_server, trame_pyvista, trame_vtk, trame_vuetify

Ecosystem (optional)
├☑ mne-bids 0.20.0
├☑ mne-connectivity 0.9.0
├☑ neo 0.14.6
├☑ eeglabio 0.1.4
├☑ edfio 0.4.18
├☑ pybv 0.8.1
├☑ pymef 1.4.8
├☑ defusedxml 0.7.1
└☐ unavailable mne-nirs, mne-features, mne-icalabel, mne-bids-pipeline, autoreject, curryreader, mffpy, antio

Lingua principale
Python
Stelle
3.6k
Fork
1.6k
Merge medio
1g 20h
PR unite (30g)
73

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di mne-tools/mne-python

Tutte le issue di mne-tools/mne-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.