Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/dev/14460.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`mne.read_forward_solution` and :func:`mne.minimum_norm.read_inverse_operator` incorrectly overwriting subcortical surface source space IDs, by `Aditya Kumar Singh`_.
1 change: 1 addition & 0 deletions doc/changes/names.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.. _Adam Li: https://github.com/adam2392
.. _Adeline Fecker: https://github.com/adelinefecker
.. _Adina Wagner: https://github.com/adswa
.. _Aditya Kumar Singh: https://github.com/ADITYASINGH1206
.. _Aditya Pandey: https://github.com/adi-coderr
.. _Adonay Nunes: https://github.com/AdoNunes
.. _Akhilesh S. Yadav: https://github.com/YadavAkhileshh
Expand Down
7 changes: 6 additions & 1 deletion mne/forward/forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,12 @@ def read_forward_solution(fname, include=(), exclude=(), *, ordered=True, verbos

src = _read_source_spaces_from_tree(fid, tree, patch_stats=False)
for s in src:
s["id"] = find_source_space_hemi(s)
if (
s["type"] == "surf"
and s.get("id", FIFF.FIFFV_MNE_SURF_UNKNOWN)
== FIFF.FIFFV_MNE_SURF_UNKNOWN
):
s["id"] = find_source_space_hemi(s)

fwd = None

Expand Down
105 changes: 105 additions & 0 deletions mne/forward/tests/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

import copy
import gc
from pathlib import Path

Expand All @@ -21,11 +22,14 @@
apply_forward_raw,
average_forward_solutions,
convert_forward_solution,
create_info,
pick_types_forward,
read_evokeds,
read_forward_solution,
read_source_spaces,
write_forward_solution,
)
from mne._fiff.constants import FIFF
from mne._fiff.pick import pick_channels_forward
from mne.channels import equalize_channels
from mne.datasets import testing
Expand All @@ -39,6 +43,8 @@
)
from mne.io import read_info
from mne.label import read_label
from mne.source_space import SourceSpaces
from mne.transforms import Transform
from mne.utils import _record_warnings, requires_mne, run_subprocess

data_path = testing.data_path(download=False)
Expand Down Expand Up @@ -525,3 +531,102 @@ def test_equalize_channels():
fwd1, fwd2 = equalize_channels([fwd1, fwd2])
assert fwd1.ch_names == ["EEG 001", "EEG 002"]
assert fwd2.ch_names == ["EEG 001", "EEG 002"]


def test_forward_subcortical_io(tmp_path):
"""Test reading forward solution with subcortical and mixed source spaces."""
base_dir = Path(__file__).parents[2] / "io" / "tests" / "data"
src = read_source_spaces(base_dir / "small-src.fif.gz", verbose=False)

sub_s = copy.deepcopy(src[0])
sub_s["id"] = FIFF.FIFFV_MNE_SURF_SUBCORTICAL_OFFSET + 18
sub_s["seg_name"] = "Left-Amygdala"
mixed_src = SourceSpaces([src[0], src[1], sub_s])
sub_only_src = SourceSpaces([sub_s])

info = create_info(["MEG 001"], 1000.0, ["mag"])
with info._unlock():
info["mri_file"] = "mri"
info["mri_id"] = None
info["meas_file"] = "meas"
info["meas_id"] = None
info["mri_head_t"] = Transform("mri", "head", np.eye(4))
info["dev_head_t"] = Transform("meg", "head", np.eye(4))

for this_src, expected_kind in [
(mixed_src, "mixed"),
(sub_only_src, "subcortical_surf"),
]:
n_vert = sum(s["nuse"] for s in this_src)
sol_data = np.zeros((1, n_vert * 3))
fwd = Forward(
sol=dict(
data=sol_data,
nrow=1,
ncol=n_vert * 3,
row_names=["MEG 001"],
col_names=[],
),
source_ori=FIFF.FIFFV_MNE_FREE_ORI,
nsource=n_vert,
nchan=1,
coord_frame=FIFF.FIFFV_COORD_HEAD,
info=info,
src=this_src,
source_nn=np.zeros((n_vert * 3, 3)),
surf_ori=False,
mri_head_t=Transform("mri", "head", np.eye(4)),
sol_grad=None,
_orig_source_ori=FIFF.FIFFV_MNE_FREE_ORI,
_orig_sol=sol_data,
_orig_sol_grad=None,
)

fname = tmp_path / f"test-{expected_kind}-fwd.fif"
write_forward_solution(fname, fwd, overwrite=True, verbose=False)
fwd_read = read_forward_solution(fname, verbose=False)

assert fwd_read["src"].kind == expected_kind
assert [s["id"] for s in fwd_read["src"]] == [s["id"] for s in this_src]
assert [s.get("seg_name") for s in fwd_read["src"]] == [
s.get("seg_name") for s in this_src
]

# Legacy forward solution without FIFF_MNE_SOURCE_SPACE_ID
legacy_s0 = copy.deepcopy(src[0])
legacy_s0["id"] = FIFF.FIFFV_MNE_SURF_UNKNOWN
legacy_s1 = copy.deepcopy(src[1])
legacy_s1["id"] = FIFF.FIFFV_MNE_SURF_UNKNOWN
legacy_src = SourceSpaces([legacy_s0, legacy_s1])
n_vert = sum(s["nuse"] for s in legacy_src)
sol_data = np.zeros((1, n_vert * 3))
fwd_legacy = Forward(
sol=dict(
data=sol_data,
nrow=1,
ncol=n_vert * 3,
row_names=["MEG 001"],
col_names=[],
),
source_ori=FIFF.FIFFV_MNE_FREE_ORI,
nsource=n_vert,
nchan=1,
coord_frame=FIFF.FIFFV_COORD_HEAD,
info=info,
src=legacy_src,
source_nn=np.zeros((n_vert * 3, 3)),
surf_ori=False,
mri_head_t=Transform("mri", "head", np.eye(4)),
sol_grad=None,
_orig_source_ori=FIFF.FIFFV_MNE_FREE_ORI,
_orig_sol=sol_data,
_orig_sol_grad=None,
)
fname_legacy = tmp_path / "test-legacy-fwd.fif"
write_forward_solution(fname_legacy, fwd_legacy, overwrite=True, verbose=False)
fwd_legacy_read = read_forward_solution(fname_legacy, verbose=False)
assert fwd_legacy_read["src"].kind == "surface"
assert [s["id"] for s in fwd_legacy_read["src"]] == [
FIFF.FIFFV_MNE_SURF_LEFT_HEMI,
FIFF.FIFFV_MNE_SURF_RIGHT_HEMI,
]
7 changes: 6 additions & 1 deletion mne/minimum_norm/inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,12 @@ def read_inverse_operator(fname, *, verbose=None):
inv["src"] = _read_source_spaces_from_tree(fid, tree, patch_stats=False)

for s in inv["src"]:
s["id"] = find_source_space_hemi(s)
if (
s["type"] == "surf"
and s.get("id", FIFF.FIFFV_MNE_SURF_UNKNOWN)
== FIFF.FIFFV_MNE_SURF_UNKNOWN
):
s["id"] = find_source_space_hemi(s)

#
# Get the MRI <-> head coordinate transformation
Expand Down