From a3bf0b0b1afc27fed7864d727269dc69aecfd2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20H=C3=B6chenberger?= Date: Sun, 28 May 2023 15:14:03 +0200 Subject: [PATCH] MRG: Don't create directories when accessing BIDSPath.fpath (#1139) --- doc/whats_new.rst | 1 + mne_bids/path.py | 5 +++-- mne_bids/tests/test_path.py | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index f06039788..fbdf0b37c 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -57,6 +57,7 @@ Detailed list of changes - Fix ``raw_to_bids`` CLI tool to properly recognize boolean and numeric values for the ``line_freq`` and ``overwrite`` parameters, by `Stefan Appelhoff`_ (:gh:`1125`) - Fix :func:`~mne_bids.copyfiles.copyfile_eeglab` to prevent data type conversion leading to an ``eeg_checkset`` failure when trying to load the file in EEGLAB, by `Laetitia Fesselier`_ (:gh:`1122`) - Improve compatibility with latest MNE-Python, by `Eric Larson`_ (:gh:`1128`) +- Working with :class:`~mne_bids.BIDSPath` would sometimes inadvertently create new directories, contaminating the BIDS dataset, by `Richard Höchenberger`_ (:gh:`1139`) :doc:`Find out what was new in previous releases ` diff --git a/mne_bids/path.py b/mne_bids/path.py index 81c4e7fd7..890ded288 100644 --- a/mne_bids/path.py +++ b/mne_bids/path.py @@ -1059,8 +1059,9 @@ def _get_matching_bidspaths_from_filesystem(bids_path): datatype = _infer_datatype(root=bids_root, sub=sub, ses=ses) - data_dir = BIDSPath(subject=sub, session=ses, datatype=datatype, - root=bids_root).mkdir().directory + data_dir = BIDSPath( + subject=sub, session=ses, datatype=datatype, root=bids_root + ).directory # For BTI data, just return the directory with a '.pdf' extension # to facilitate reading in mne-bids diff --git a/mne_bids/tests/test_path.py b/mne_bids/tests/test_path.py index 856c32263..d97294ce2 100644 --- a/mne_bids/tests/test_path.py +++ b/mne_bids/tests/test_path.py @@ -1,5 +1,6 @@ """Test for the MNE BIDSPath functions.""" # Authors: Adam Li +# Richard Höchenberger # # License: BSD-3-Clause import os @@ -1258,3 +1259,10 @@ def test_deprecation(): """Test deprecated behavior.""" with pytest.warns(FutureWarning, match='This will raise an exception'): BIDSPath(extension='vhdr') # no leading period + + +def test_dont_create_dirs_on_fpath_access(tmp_path): + """Regression test: don't create directories when accessing .fpath.""" + 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()