From f36245fb9600231f6575c2cbe8717d6fe778c240 Mon Sep 17 00:00:00 2001 From: Pierre Guetschel Date: Fri, 28 Mar 2025 16:17:44 +0100 Subject: [PATCH 1/6] Avoid partially matching a key-value --- mne_bids/path.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mne_bids/path.py b/mne_bids/path.py index 5d2f95bb0..b3591fac4 100644 --- a/mne_bids/path.py +++ b/mne_bids/path.py @@ -1390,7 +1390,9 @@ def _get_matching_bidspaths_from_filesystem(bids_path): search_str = op.join(search_str, datatype) else: search_str = op.join(search_str, "**") - search_str = op.join(search_str, f"{basename}*") + # The basename should end with a separator "_" or a period "." + # to avoid matching only the beggining of a value. + search_str = op.join(search_str, f"{basename}[_.]*") # Find all matching files in all supported formats. valid_exts = ALLOWED_FILENAME_EXTENSIONS From 94d38d4746a825de7644db8743591cf281d30ad9 Mon Sep 17 00:00:00 2001 From: Pierre Guetschel Date: Fri, 28 Mar 2025 16:17:49 +0100 Subject: [PATCH 2/6] Add test --- mne_bids/tests/test_path.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mne_bids/tests/test_path.py b/mne_bids/tests/test_path.py index 6225ca215..6610d4f3e 100644 --- a/mne_bids/tests/test_path.py +++ b/mne_bids/tests/test_path.py @@ -1648,3 +1648,22 @@ def test_dont_create_dirs_on_fpath_access(tmp_path): bp = BIDSPath(subject="01", datatype="eeg", root=tmp_path) bp.fpath # accessing .fpath is required for this regression test assert not (tmp_path / "sub-01").exists() + + +def test_fpath_common_prefix(tmp_path): + sub_dir = tmp_path / "sub-1" / "eeg" + sub_dir.mkdir(exist_ok=True, parents=True) + (sub_dir / "sub-1_run-1_raw.fif").touch() + (sub_dir / "sub-1_run-2.edf").touch() + # Other valid BIDS paths with the same basename prefix: + (sub_dir / "sub-1_run-10_raw.fif").touch() + (sub_dir / "sub-1_run-20.edf").touch() + + assert ( + BIDSPath(root=tmp_path, subject="1", run="1").fpath + == sub_dir / "sub-1_run-1_raw.fif" + ) + assert ( + BIDSPath(root=tmp_path, subject="1", run="2").fpath + == sub_dir / "sub-1_run-2.edf" + ) From 40175242561b09c99df97a6d59c0359534907286 Mon Sep 17 00:00:00 2001 From: Pierre Guetschel Date: Fri, 28 Mar 2025 16:26:07 +0100 Subject: [PATCH 3/6] Update whatsnew --- doc/whats_new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index da93f5690..b9db98f7d 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -26,6 +26,7 @@ The following authors had contributed before. Thank you for sticking around! * `Stefan Appelhoff`_ * `Daniel McCloy`_ * `Scott Huberty`_ +* `Pierre Guetschel`_ Detailed list of changes ~~~~~~~~~~~~~~~~~~~~~~~~ @@ -57,6 +58,7 @@ Detailed list of changes - BIDS dictates that the recording entity should be displayed as "_recording-" in the filename. This PR makes :class:`mne_bids.BIDSPath` correctly display "_recording-" (instead of "_rec-") in BIDSPath.fpath. By `Scott Huberty`_ (:gh:`1348`) - :func:`mne_bids.make_dataset_description` now correctly encodes the dataset description as UTF-8 on disk, by `Scott Huberty`_ (:gh:`1357`) - Corrects extension when filtering filenames in :meth:`mne_bids.BIDSPath.match()` and :func:`mne_bids.find_matching_paths()`, by `Arne Gottwald` (:gh:`1355`) +- Fix :class:`mne_bids.BIDSPath` partially matching a value, by `Pierre Guetschel` (:gh:`1388`) ⚕️ Code health ^^^^^^^^^^^^^^ From 7658c01fefd867294e681f46f9f9343bc55c85d9 Mon Sep 17 00:00:00 2001 From: Pierre Guetschel Date: Fri, 28 Mar 2025 16:30:28 +0100 Subject: [PATCH 4/6] Add test docstring --- mne_bids/tests/test_path.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mne_bids/tests/test_path.py b/mne_bids/tests/test_path.py index 6610d4f3e..d4ae1bf01 100644 --- a/mne_bids/tests/test_path.py +++ b/mne_bids/tests/test_path.py @@ -1651,6 +1651,8 @@ def test_dont_create_dirs_on_fpath_access(tmp_path): def test_fpath_common_prefix(tmp_path): + """Tests that fpath does not match multiple files with the same prefix. + This might happen if indices are not zero-paddded.""" sub_dir = tmp_path / "sub-1" / "eeg" sub_dir.mkdir(exist_ok=True, parents=True) (sub_dir / "sub-1_run-1_raw.fif").touch() From c884f49c4eb422d40e0a9e5355081fe178971352 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Mar 2025 15:31:11 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne_bids/tests/test_path.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mne_bids/tests/test_path.py b/mne_bids/tests/test_path.py index d4ae1bf01..8e9ed78d9 100644 --- a/mne_bids/tests/test_path.py +++ b/mne_bids/tests/test_path.py @@ -1652,7 +1652,8 @@ def test_dont_create_dirs_on_fpath_access(tmp_path): def test_fpath_common_prefix(tmp_path): """Tests that fpath does not match multiple files with the same prefix. - This might happen if indices are not zero-paddded.""" + This might happen if indices are not zero-paddded. + """ sub_dir = tmp_path / "sub-1" / "eeg" sub_dir.mkdir(exist_ok=True, parents=True) (sub_dir / "sub-1_run-1_raw.fif").touch() From 0120653c1178b17ec8a3010e4aa83048b2285cce Mon Sep 17 00:00:00 2001 From: Pierre Guetschel Date: Fri, 28 Mar 2025 16:34:05 +0100 Subject: [PATCH 6/6] Fix style (my pre-commit is broken) --- mne_bids/tests/test_path.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mne_bids/tests/test_path.py b/mne_bids/tests/test_path.py index 8e9ed78d9..5f0abeda9 100644 --- a/mne_bids/tests/test_path.py +++ b/mne_bids/tests/test_path.py @@ -1652,6 +1652,7 @@ def test_dont_create_dirs_on_fpath_access(tmp_path): def test_fpath_common_prefix(tmp_path): """Tests that fpath does not match multiple files with the same prefix. + This might happen if indices are not zero-paddded. """ sub_dir = tmp_path / "sub-1" / "eeg"