Skip to content

Commit 93d30b6

Browse files
committed
FIX: More
1 parent 400bd7f commit 93d30b6

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

doc/whats_new.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Detailed list of changes
3636
- :func:`mne_bids.write_raw_bids()` can now handle mne `Raw` objects with `eyegaze` and `pupil` channels, by `Christian O'Reilly`_ (:gh:`1344`)
3737
- :func:`mne_bids.get_entity_vals()` has a new parameter ``ignore_suffixes`` to easily ignore sidecar files, by `Daniel McCloy`_ (:gh:`1362`)
3838
- Empty-room matching now preferentially finds recordings in the subject directory tagged as `task-noise` before looking in the `sub-emptyroom` directories. This adds support for a part of the BIDS specification for ER recordings, by `Berk Gerçek`_ (:gh:`1364`)
39-
- Path matching is now implemenented in a more efficient manner within `_return_root_paths`, by `Arne Gottwald` (:gh:`1355`)
39+
- Path matching is now implemenented in a more efficient manner within :meth:`mne_bids.BIDSPath.match()` and :func:`mne_bids.find_matching_paths()`, by `Arne Gottwald` (:gh:`1355`)
4040
- :func:`mne_bids.get_entity_vals()` has a new parameter ``include_match`` to prefilter item matching and ignore non-matched items from begin of directory scan, by `Arne Gottwald` (:gh:`1355`)
4141

4242

@@ -56,7 +56,7 @@ Detailed list of changes
5656
- :func:`mne_bids.read_raw_bids` can optionally return an ``event_id`` dictionary suitable for use with :func:`mne.events_from_annotations`, and if a ``values`` column is present in ``events.tsv`` it will be used as the source of the integer event ID codes, by `Daniel McCloy`_ (:gh:`1349`)
5757
- 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`)
5858
- :func:`mne_bids.make_dataset_description` now correctly encodes the dataset description as UTF-8 on disk, by `Scott Huberty`_ (:gh:`1357`)
59-
- Corrects extension match within `_filter_fnames` at end of filename, by `Arne Gottwald` (:gh:`1355`)
59+
- Corrects extension when filtering filenames in :meth:`mne_bids.BIDSPath.match()` and :func:`mne_bids.find_matching_paths()`, by `Arne Gottwald` (:gh:`1355`)
6060

6161
⚕️ Code health
6262
^^^^^^^^^^^^^^

mne_bids/path.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from copy import deepcopy
1313
from datetime import datetime
1414
from io import StringIO
15-
from itertools import chain as iter_chain
1615
from os import path as op
1716
from pathlib import Path
1817
from textwrap import indent
@@ -2017,8 +2016,9 @@ def get_entity_vals(
20172016
20182017
.. versionadded:: 0.17
20192018
include_match : str | array-like of str | None
2020-
Apply a starting match pragma following Unix style pattern syntax from
2021-
package glob to prefilter search criterion.
2019+
Glob-style match string(s) of directories to include in the search (i.e., each
2020+
must end with ``"/"``). ``None`` (the default) is equivalent to ``"**/"``
2021+
(within any subdirectory of the BIDS root).
20222022
20232023
.. versionadded:: 0.17
20242024
with_key : bool
@@ -2119,8 +2119,7 @@ def get_entity_vals(
21192119
search_str = f"**/*{entity_long_abbr_map[entity_key]}-*_*"
21202120
if include_match is not None:
21212121
include_match = _ensure_tuple(include_match)
2122-
filenames = [root.glob(im + search_str) for im in include_match]
2123-
filenames = iter_chain(*filenames)
2122+
filenames = [f for im in include_match for f in root.glob(im + search_str)]
21242123
else:
21252124
filenames = root.glob(search_str)
21262125

@@ -2543,8 +2542,7 @@ def _return_root_paths(root, datatype=None, ignore_json=True, ignore_nosub=False
25432542
root = Path(root) # if root is str
25442543

25452544
if datatype is None and not ignore_nosub:
2546-
search_str = "*.*"
2547-
paths = root.rglob(search_str)
2545+
paths = root.rglob("*.*")
25482546
else:
25492547
if datatype is not None:
25502548
datatype = _ensure_tuple(datatype)
@@ -2557,6 +2555,7 @@ def _return_root_paths(root, datatype=None, ignore_json=True, ignore_nosub=False
25572555
if ignore_nosub:
25582556
search_str = f"sub-*/{search_str}"
25592557
# TODO: Why is this not equivalent to list(root.rglob(search_str)) ?
2558+
# Most of the speedup is from using glob.iglob here.
25602559
paths = [
25612560
Path(root, fn)
25622561
for fn in glob.iglob(search_str, root_dir=root, recursive=True)

mne_bids/tests/test_path.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def test_path_benchmark(tmp_path_factory):
171171
"""Benchmark exploring bids tree."""
172172
# This benchmark is to verify the speed-up in function call get_entity_vals with
173173
# `include_match=sub-*/` in face of a bids tree hosting derivatives and sourcedata.
174-
n_subjects = 20
175-
n_sessions = 10
174+
n_subjects = 10
175+
n_sessions = 5
176176
n_derivatives = 17
177177
tmp_bids_root = tmp_path_factory.mktemp("mnebids_utils_test_bids_ds")
178178

@@ -251,6 +251,15 @@ def test_path_benchmark(tmp_path_factory):
251251
target = 2 * timed_entity / len(bids_subdirectories)
252252
assert timed_entity_match < target
253253

254+
# and these should be equivalent
255+
out_1 = get_entity_vals(tmp_bids_root, "session")
256+
out_2 = get_entity_vals(tmp_bids_root, "session", include_match="**/")
257+
assert out_1 == out_2
258+
out_3 = get_entity_vals(tmp_bids_root, "session", include_match="sub-*/")
259+
assert out_2 == out_3 # all are sub-* vals
260+
out_4 = get_entity_vals(tmp_bids_root, "session", include_match="none/")
261+
assert out_4 == []
262+
254263

255264
def test_search_folder_for_text(capsys):
256265
"""Test finding entries."""

0 commit comments

Comments
 (0)