Repository navigation
read_forward_solution loses subcortical source spaces #14460
Description
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
Activity