From d234d6d23009e74cde0743ac966a4184b9d617b0 Mon Sep 17 00:00:00 2001 From: yaylim Date: Mon, 1 Dec 2025 16:10:52 -0500 Subject: [PATCH 1/3] FIX: adding kit_system_id info to forward solution When raw.info contains a kit_system_id, the function make_forward_solution expects this value to be present during forward computation and raises an error if it is missing. Previously, the kit_system_id field was unintentionally dropped when constructing the Info object used for the forward solution, leading to this failure. This PR resolves the issue by preserving the kit_system_id field during Info creation. An explicit if check ensures that the field is added only when it is present in raw.info; if it is not present, no kit_system_id is included. This maintains consistency with the original metadata while avoiding unnecessary or incorrect fields. --- mne/forward/_make_forward.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/mne/forward/_make_forward.py b/mne/forward/_make_forward.py index e1264bd2fe6..b74feb64532 100644 --- a/mne/forward/_make_forward.py +++ b/mne/forward/_make_forward.py @@ -496,20 +496,24 @@ def _prepare_for_forward( mri_id = dict(machid=np.zeros(2, np.int32), version=0, secs=0, usecs=0) info_trans = str(trans) if isinstance(trans, Path) else trans - info = Info( - chs=info["chs"], - comps=info["comps"], - # The forward-writing code always wants a dev_head_t, so give an identity one - dev_head_t=info["dev_head_t"] or Transform("meg", "head"), - mri_file=info_trans, - mri_id=mri_id, - meas_file=info_extra, - meas_id=None, - working_dir=os.getcwd(), - command_line=cmd, - bads=info["bads"], - mri_head_t=mri_head_t, - ) + kwargs_fwd_info = { + "chs": info["chs"], + "comps": info["comps"], + "dev_head_t": info["dev_head_t"] or Transform("meg", "head"), + "mri_file": info_trans, + "mri_id": mri_id, + "meas_file": info_extra, + "meas_id": None, + "working_dir": os.getcwd(), + "command_line": cmd, + "bads": info["bads"], + "mri_head_t": mri_head_t, + } + + if "kit_system_id" in info: + kwargs_fwd_info["kit_system_id"] = info["kit_system_id"] + + info = Info(**kwargs_fwd_info) info._update_redundant() info._check_consistency() logger.info("") From 7bf436f87f8912ecafd0a7d9f47ba997bc2ede9a Mon Sep 17 00:00:00 2001 From: yaylim Date: Tue, 2 Dec 2025 16:37:57 -0500 Subject: [PATCH 2/3] Updates for PR # 13520 as requested - Created .rst file - Added test in test_make_forward.py - Updated the changes made in _make_forward.py --- doc/changes/dev/13520.bugfix.rst | 1 + doc/changes/names.inc | 1 + mne/forward/_make_forward.py | 27 +++++++++++++------------- mne/forward/tests/test_make_forward.py | 4 +++- 4 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 doc/changes/dev/13520.bugfix.rst diff --git a/doc/changes/dev/13520.bugfix.rst b/doc/changes/dev/13520.bugfix.rst new file mode 100644 index 00000000000..1fd6fa28700 --- /dev/null +++ b/doc/changes/dev/13520.bugfix.rst @@ -0,0 +1 @@ +Preserve ``kit_system_id`` in forward-solution ``Info``, by :newcontrib: 'Melih Yayli' \ No newline at end of file diff --git a/doc/changes/names.inc b/doc/changes/names.inc index 77e665ec6ed..b70da81fd98 100644 --- a/doc/changes/names.inc +++ b/doc/changes/names.inc @@ -210,6 +210,7 @@ .. _Matti Hämäläinen: https://research.aalto.fi/en/persons/matti-h%C3%A4m%C3%A4l%C3%A4inen/ .. _Matti Toivonen: https://github.com/mattitoi .. _Mauricio Cespedes Tenorio: https://github.com/mcespedes99 +.. _Melih Yayli: https://github.com/yaylim .. _Michael Straube: https://github.com/mistraube .. _Michal Žák: https://github.com/michalrzak .. _Michiru Kaneda: https://github.com/rcmdnk diff --git a/mne/forward/_make_forward.py b/mne/forward/_make_forward.py index b74feb64532..9c2f2552cc9 100644 --- a/mne/forward/_make_forward.py +++ b/mne/forward/_make_forward.py @@ -496,19 +496,20 @@ def _prepare_for_forward( mri_id = dict(machid=np.zeros(2, np.int32), version=0, secs=0, usecs=0) info_trans = str(trans) if isinstance(trans, Path) else trans - kwargs_fwd_info = { - "chs": info["chs"], - "comps": info["comps"], - "dev_head_t": info["dev_head_t"] or Transform("meg", "head"), - "mri_file": info_trans, - "mri_id": mri_id, - "meas_file": info_extra, - "meas_id": None, - "working_dir": os.getcwd(), - "command_line": cmd, - "bads": info["bads"], - "mri_head_t": mri_head_t, - } + kwargs_fwd_info = dict( + chs=info["chs"], + comps=info["comps"], + # The forward-writing code always wants a dev_head_t, so give an identity one + dev_head_t=info["dev_head_t"] or Transform("meg", "head"), + mri_file=info_trans, + mri_id=mri_id, + meas_file=info_extra, + meas_id=None, + working_dir=os.getcwd(), + command_line=cmd, + bads=info["bads"], + mri_head_t=mri_head_t, + ) if "kit_system_id" in info: kwargs_fwd_info["kit_system_id"] = info["kit_system_id"] diff --git a/mne/forward/tests/test_make_forward.py b/mne/forward/tests/test_make_forward.py index b584202465c..2ea479ae552 100644 --- a/mne/forward/tests/test_make_forward.py +++ b/mne/forward/tests/test_make_forward.py @@ -269,7 +269,9 @@ def test_make_forward_solution_kit(tmp_path, fname_src_small): ignore_ref=True, ) _compare_forwards(fwd, fwd_py, 157, n_src_small, meg_rtol=1e-3, meg_atol=1e-7) - + + # NEW TEST: ensure kit_system_id survives the forward-info rewrite + assert "kit_system_id" in fwd_py["info"] @requires_mne def test_make_forward_solution_bti(fname_src_small): From 7cd9986952dcd1141054ea29651041d4a5009365 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 2 Dec 2025 21:38:33 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/forward/tests/test_make_forward.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mne/forward/tests/test_make_forward.py b/mne/forward/tests/test_make_forward.py index 2ea479ae552..9e94f0cbd44 100644 --- a/mne/forward/tests/test_make_forward.py +++ b/mne/forward/tests/test_make_forward.py @@ -269,10 +269,11 @@ def test_make_forward_solution_kit(tmp_path, fname_src_small): ignore_ref=True, ) _compare_forwards(fwd, fwd_py, 157, n_src_small, meg_rtol=1e-3, meg_atol=1e-7) - + # NEW TEST: ensure kit_system_id survives the forward-info rewrite assert "kit_system_id" in fwd_py["info"] + @requires_mne def test_make_forward_solution_bti(fname_src_small): """Test BTI end-to-end versus C."""