Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ignore_json, add ignore_nosub to file match/search funcs #1281

Merged
merged 13 commits into from
Aug 5, 2024
33 changes: 21 additions & 12 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,35 @@ Ariel Rokem <[email protected]>
Chris Holdgraf <[email protected]>
Chris Holdgraf <[email protected]> <[email protected]>
Clemens Brunner <[email protected]>
Dominik Welke <[email protected]>
Dominik Welke <[email protected]> dominikwelke <[email protected]>
Eduard Ort <[email protected]>
Eduard Ort <[email protected]> <[email protected]>
Evgenii Kalenkovich <[email protected]>
Ezequiel Mikulan <[email protected]>
Franziska von Albedyll <[email protected]>
Franziska von Albedyll <[email protected]> <[email protected]>
Fu-Te Wong <[email protected]>
Julius Welzel <[email protected]>
Kaare Mikkelsen <[email protected]>
Kaare Mikkelsen <[email protected]> <[email protected]>
Kambiz Tavabi <[email protected]>
Laetitia Fesselier <[email protected]>
Laetitia Fesselier <[email protected]> <[email protected]>
Mainak Jas <[email protected]>
Mainak Jas <[email protected]> <[email protected]>
Marijn van Vliet <[email protected]>
Mathieu Scheltienne <[email protected]>
Mathieu Scheltienne <[email protected]> <[email protected]>
Matt Sanderson <[email protected]>
Maximilien Chaumon <[email protected]>
Maximilien Chaumon <[email protected]> <[email protected]>
Romain Quentin <rom.quentin@gmail.com>
Pierre Guetschel <[email protected].com>
Richard Höchenberger <[email protected]>
Richard Koehler <[email protected]>
Robert Luke <[email protected]>
Romain Quentin <[email protected]>
Sophie Herbst <[email protected]>
Stefan Appelhoff <[email protected]>
Teon Brooks <[email protected]>
Dominik Welke <[email protected]>
Dominik Welke <[email protected]> dominikwelke <[email protected]>
Ezequiel Mikulan <[email protected]>
Fu-Te Wong <[email protected]>
Matt Sanderson <[email protected]>
Robert Luke <[email protected]>
Evgenii Kalenkovich <[email protected]>
Eduard Ort <[email protected]>
Eduard Ort <[email protected]> <[email protected]>
Tom Donoghue <[email protected]>
Franziska von Albedyll <[email protected]>
Franziska von Albedyll <[email protected]> <[email protected]>
4 changes: 4 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ authors:
family-names: Welzel
affiliation: 'Department of Neurology, Kiel University, Germany'
orcid: 'https://orcid.org/0000-0003-4782-5360'
- given-names: Kaare
family-names: Mikkelsen
affiliation: 'Department of Electrical and Computer Engineering, Aarhus University, Denmark'
orcid: 'https://orcid.org/0000-0002-7360-8629'
sappelhoff marked this conversation as resolved.
Show resolved Hide resolved
- given-names: Alexandre
family-names: Gramfort
affiliation: 'Université Paris-Saclay, Inria, CEA, Palaiseau, France'
Expand Down
1 change: 1 addition & 0 deletions doc/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
.. _Pierre Guetschel: https://github.com/PierreGtch
.. _Mara Wolter: https://github.com/marakw
.. _Julius Welzel: https://github.com/JuliusWelzel
.. _Kaare Mikkelsen: https://github.com/kaare-mikkelsen
4 changes: 2 additions & 2 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Version 0.16 (unreleased)

The following authors contributed for the first time. Thank you so much! 🤩

* nobody yet
* `Kaare Mikkelsen`_

The following authors had contributed before. Thank you for sticking around! 🤘

Expand All @@ -31,7 +31,7 @@ Detailed list of changes
🚀 Enhancements
^^^^^^^^^^^^^^^

- nothing yet
- :meth:`mne_bids.BIDSPath.match()` and :func:`mne_bids.find_matching_paths` now have additional parameters ``ignore_json`` and ``ignore_nosub``, to give users more control over which type of files are matched, by `Kaare Mikkelsen`_ (:gh:`1281`)

🧐 API and behavior changes
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
53 changes: 45 additions & 8 deletions mne_bids/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,9 @@ def rm(self, *, safe_remove=True, verbose=None):
raise RuntimeError("The root must not be None to remove files.")

# Planning:
paths_matched = self.match(ignore_json=False, check=self.check)
paths_matched = self.match(
ignore_json=False, ignore_nosub=False, check=self.check
)
subjects = set()
paths_to_delete = list()
paths_to_update = {}
Expand Down Expand Up @@ -1005,7 +1007,7 @@ def update(self, *, check=None, **kwargs):
raise e
return self

def match(self, ignore_json=True, check=False):
def match(self, *, ignore_json=True, ignore_nosub=False, check=False):
"""Get a list of all matching paths in the root directory.

Performs a recursive search, starting in ``.root`` (if set), based on
Expand All @@ -1015,6 +1017,9 @@ def match(self, ignore_json=True, check=False):
----------
ignore_json : bool
If ``True``, ignores json files. Defaults to ``True``.
ignore_nosub : bool
If ``True``, ignores all files that are not of the form ``root/sub-*``.
Defaults to ``False``.
check : bool
If ``True``, only returns paths that conform to BIDS. If ``False``
(default), the ``.check`` attribute of the returned
Expand All @@ -1035,7 +1040,10 @@ def match(self, ignore_json=True, check=False):
)

paths = _return_root_paths(
self.root, datatype=self.datatype, ignore_json=ignore_json
self.root,
datatype=self.datatype,
ignore_json=ignore_json,
ignore_nosub=ignore_nosub,
)

fnames = _filter_fnames(
Expand Down Expand Up @@ -2325,6 +2333,9 @@ def find_matching_paths(
extensions=None,
datatypes=None,
check=False,
*,
ignore_json=False,
ignore_nosub=False,
):
"""Get list of all matching paths for all matching entity values.

Expand Down Expand Up @@ -2384,14 +2395,21 @@ def find_matching_paths(
(default), the ``.check`` attribute of the returned
:class:`mne_bids.BIDSPath` object will be set to ``True`` for paths that
do conform to BIDS, and to ``False`` for those that don't.
ignore_json : bool
If ``True``, ignores json files. Defaults to ``False``.
ignore_nosub : bool
If ``True``, ignores all files that are not of the form ``root/sub-*``.
Defaults to ``False``.

Returns
-------
bids_paths : list of mne_bids.BIDSPath
The matching paths.

"""
fpaths = _return_root_paths(root, datatype=datatypes, ignore_json=False)
fpaths = _return_root_paths(
root, datatype=datatypes, ignore_json=ignore_json, ignore_nosub=ignore_nosub
)

fpaths_filtered = _filter_fnames(
fpaths,
Expand All @@ -2413,16 +2431,29 @@ def find_matching_paths(
return bids_paths


def _return_root_paths(root, datatype=None, ignore_json=True):
"""Return all paths in root.
def _return_root_paths(root, datatype=None, ignore_json=True, ignore_nosub=False):
"""Return all file paths in root.

Can be filtered by datatype (which is present in the path but not in
the BIDSPath basename). Can also be list of datatypes.

Parameters
----------
root : pathlib.Path | str
The root of the BIDS path.
datatype : str | array-like of str | None
The BIDS data type, e.g., ``'anat'``, ``'func'``, ``'eeg'``, ``'meg'``,
``'ieeg'``.
ignore_json : bool
If ``True`` (default), do not return files ending with ``.json``.
ignore_nosub : bool
If ``True``, return only files of the form ``root/sub-*``. Defaults to
``False``.

Returns
-------
paths : list of pathlib.Path
All paths in `root`, filtered according to the function parameters.
"""
root = Path(root) # if root is str

Expand All @@ -2433,13 +2464,19 @@ def _return_root_paths(root, datatype=None, ignore_json=True):
search_str = "*.*"

paths = root.rglob(search_str)
# Only keep files (not directories), and omit the JSON sidecars
# if ignore_json is True.
# Only keep files (not directories), ...
# and omit the JSON sidecars if `ignore_json` is True.
if ignore_json:
paths = [p for p in paths if p.is_file() and p.suffix != ".json"]
else:
paths = [p for p in paths if p.is_file()]

# only keep files which are of the form root/sub-*,
# such that we only look in 'sub'-folders:
if ignore_nosub:
root_sub = str(root / "sub-")
paths = [p for p in paths if str(p).startswith(root_sub)]

return paths


Expand Down