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

ENH: Add option to raise exception on empty get query #57

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion templateflow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD


def get(template, **kwargs):
def get(template, raise_empty=False, **kwargs):
"""
Fetch one file from one particular template.

Parameters
----------
template : str
A template identifier (e.g., ``MNI152NLin2009cAsym``).
raise_empty : bool, optional
Raise exception if no files were matched

Keyword Arguments
-----------------
Expand Down Expand Up @@ -50,6 +52,14 @@ def get(template, **kwargs):
... density='32k', suffix='sphere')) # doctest: +ELLIPSIS
'.../tpl-fsLR_hemi-L_den-32k_sphere.surf.gii'

>>> get('fsLR', space='madeup')
[]

>>> get('fsLR', raise_empty=True, space='madeup') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
Exception:
...

"""
out_file = [
Path(p) for p in TF_LAYOUT.get(template=template, return_type="file", **kwargs)
Expand Down Expand Up @@ -93,6 +103,9 @@ def get(template, **kwargs):

raise RuntimeError(msg)

if not out_file and raise_empty:
raise Exception("No results found")

if len(out_file) == 1:
return out_file[0]
return out_file
Expand Down