Skip to content

Commit 400bd7f

Browse files
committed
FIX: Simplify
1 parent b8c21a5 commit 400bd7f

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

mne_bids/path.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,7 @@ def _return_root_paths(root, datatype=None, ignore_json=True, ignore_nosub=False
25442544

25452545
if datatype is None and not ignore_nosub:
25462546
search_str = "*.*"
2547+
paths = root.rglob(search_str)
25472548
else:
25482549
if datatype is not None:
25492550
datatype = _ensure_tuple(datatype)
@@ -2555,8 +2556,11 @@ def _return_root_paths(root, datatype=None, ignore_json=True, ignore_nosub=False
25552556
# such that we truely only look in 'sub'-folders:
25562557
if ignore_nosub:
25572558
search_str = f"sub-*/{search_str}"
2558-
2559-
paths = root.rglob(search_str)
2559+
# TODO: Why is this not equivalent to list(root.rglob(search_str)) ?
2560+
paths = [
2561+
Path(root, fn)
2562+
for fn in glob.iglob(search_str, root_dir=root, recursive=True)
2563+
]
25602564

25612565
# Only keep files (not directories), ...
25622566
# and omit the JSON sidecars if `ignore_json` is True.

mne_bids/tests/test_path.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pathlib import Path
1313

1414
import mne
15-
import numpy as np
1615
import pytest
1716
from mne.datasets import testing
1817
from mne.io import anonymize_info
@@ -221,50 +220,36 @@ def test_path_benchmark(tmp_path_factory):
221220

222221
# apply nosub on find_matching_matchs with root level bids directory should
223222
# yield a performance boost of order of length from bids_subdirectories.
224-
timed_all, timed_ignored_nosub = (
225-
timeit.timeit(
226-
"mne_bids.find_matching_paths(tmp_bids_root)",
227-
setup="import mne_bids\ntmp_bids_root=r'" + str(tmp_bids_root) + "'",
228-
number=1,
229-
),
230-
timeit.timeit(
231-
"mne_bids.find_matching_paths(tmp_bids_root, ignore_nosub=True)",
232-
setup="import mne_bids\ntmp_bids_root=r'" + str(tmp_bids_root) + "'",
233-
number=10,
234-
)
235-
/ 10,
223+
setup = "import mne_bids\ntmp_bids_root=r'" + str(tmp_bids_root) + "'"
224+
timed_all = timeit.timeit(
225+
"mne_bids.find_matching_paths(tmp_bids_root)", setup=setup, number=1
236226
)
237-
238-
assert (
239-
timed_all / (10 * np.maximum(1, np.floor(len(bids_subdirectories) / 10)))
240-
# while this should be of same order, lets give it some space by a factor of 2
241-
> 0.5 * timed_ignored_nosub
227+
timed_ignored_nosub = timeit.timeit(
228+
"mne_bids.find_matching_paths(tmp_bids_root, ignore_nosub=True)",
229+
setup=setup,
230+
number=1,
242231
)
243232

233+
# while this should be of same order, lets give it some space by a factor of 2
234+
target = 2 * timed_all / len(bids_subdirectories)
235+
assert timed_ignored_nosub < target
236+
244237
# apply include_match on get_entity_vals with root level bids directory should
245238
# yield a performance boost of order of length from bids_subdirectories.
246-
timed_entity, timed_entity_match = (
247-
timeit.timeit(
248-
"mne_bids.get_entity_vals(tmp_bids_root, 'session')",
249-
setup="import mne_bids\ntmp_bids_root=r'" + str(tmp_bids_root) + "'",
250-
number=1,
251-
),
252-
timeit.timeit(
253-
"mne_bids.get_entity_vals(tmp_bids_root, 'session', include_match='sub-*/')", # noqa: E501
254-
setup="import mne_bids\ntmp_bids_root=r'" + str(tmp_bids_root) + "'",
255-
number=10,
256-
)
257-
/ 10,
239+
timed_entity = timeit.timeit(
240+
"mne_bids.get_entity_vals(tmp_bids_root, 'session')",
241+
setup=setup,
242+
number=1,
258243
)
259-
260-
assert (
261-
timed_entity / (10 * np.maximum(1, np.floor(len(bids_subdirectories) / 10)))
262-
# while this should be of same order, lets give it some space by a factor of 2
263-
> 0.5 * timed_entity_match
244+
timed_entity_match = timeit.timeit(
245+
"mne_bids.get_entity_vals(tmp_bids_root, 'session', include_match='sub-*/')", # noqa: E501
246+
setup=setup,
247+
number=1,
264248
)
265249

266-
# Clean up
267-
shutil.rmtree(tmp_bids_root)
250+
# while this should be of same order, lets give it some space by a factor of 2
251+
target = 2 * timed_entity / len(bids_subdirectories)
252+
assert timed_entity_match < target
268253

269254

270255
def test_search_folder_for_text(capsys):

0 commit comments

Comments
 (0)