Skip to content

Commit eaa89c2

Browse files
committed
ENH: Add option to raise exception on empty get query
1 parent 5a54d14 commit eaa89c2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

templateflow/api.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD
88

99

10-
def get(template, **kwargs):
10+
def get(template, raise_empty=False, **kwargs):
1111
"""
1212
Fetch one file from one particular template.
1313
1414
Parameters
1515
----------
1616
template : str
1717
A template identifier (e.g., ``MNI152NLin2009cAsym``).
18+
raise_empty : bool, optional
19+
Raise exception if no files were matched
1820
1921
Keyword Arguments
2022
-----------------
@@ -50,6 +52,14 @@ def get(template, **kwargs):
5052
... density='32k', suffix='sphere')) # doctest: +ELLIPSIS
5153
'.../tpl-fsLR_hemi-L_den-32k_sphere.surf.gii'
5254
55+
>>> get('fsLR', space='madeup')
56+
[]
57+
58+
>>> get('fsLR', raise_empty=True, space='madeup') # doctest: +IGNORE_EXCEPTION_DETAIL
59+
Traceback (most recent call last):
60+
Exception:
61+
...
62+
5363
"""
5464
out_file = [
5565
Path(p) for p in TF_LAYOUT.get(template=template, return_type="file", **kwargs)
@@ -93,6 +103,9 @@ def get(template, **kwargs):
93103

94104
raise RuntimeError(msg)
95105

106+
if not out_file and raise_empty:
107+
raise Exception("No results found")
108+
96109
if len(out_file) == 1:
97110
return out_file[0]
98111
return out_file

0 commit comments

Comments
 (0)