Skip to content

Commit 6f73137

Browse files
committed
fix: normalize extensions / update doctests to new resources
Resolves: #86.
1 parent b6cde5a commit 6f73137

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

templateflow/api.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def get(template, raise_empty=False, **kwargs):
3838
3939
Examples
4040
--------
41-
>>> str(get('MNI152Lin', resolution=1, suffix='T1w')) # doctest: +ELLIPSIS
41+
>>> str(get('MNI152Lin', resolution=1, suffix='T1w', desc=None)) # doctest: +ELLIPSIS
4242
'.../tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz'
4343
44-
>>> str(get('MNI152Lin', resolution=2, suffix='T1w')) # doctest: +ELLIPSIS
44+
>>> str(get('MNI152Lin', resolution=2, suffix='T1w', desc=None)) # doctest: +ELLIPSIS
4545
'.../tpl-MNI152Lin/tpl-MNI152Lin_res-02_T1w.nii.gz'
4646
4747
>>> [str(p) for p in get(
48-
... 'MNI152Lin', suffix='T1w')] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
48+
... 'MNI152Lin', suffix='T1w', desc=None)] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
4949
['.../tpl-MNI152Lin/tpl-MNI152Lin_res-01_T1w.nii.gz',
5050
'.../tpl-MNI152Lin/tpl-MNI152Lin_res-02_T1w.nii.gz']
5151
@@ -62,6 +62,10 @@ def get(template, raise_empty=False, **kwargs):
6262
...
6363
6464
"""
65+
# Normalize extensions to always have leading dot
66+
if "extension" in kwargs:
67+
kwargs["extension"] = _normalize_ext(kwargs["extension"])
68+
6569
out_file = [
6670
Path(p) for p in TF_LAYOUT.get(template=template, return_type="file", **kwargs)
6771
]
@@ -135,8 +139,8 @@ def templates(**kwargs):
135139
>>> all([t in tpls for t in base])
136140
True
137141
138-
>>> templates(suffix='PD')
139-
['MNI152Lin', 'MNI152NLin2009cAsym', 'MNI152NLin2009cSym', 'MNIInfant', 'MNIPediatricAsym']
142+
>>> sorted(set(base).intersection(templates(suffix='PD')))
143+
['MNI152Lin', 'MNI152NLin2009cAsym']
140144
141145
"""
142146
return sorted(TF_LAYOUT.get_templates(**kwargs))
@@ -247,8 +251,7 @@ def _to_bibtex(doi, template, idx):
247251
import requests
248252

249253
response = requests.post(
250-
doi,
251-
headers={"Accept": "application/x-bibtex; charset=utf-8"}
254+
doi, headers={"Accept": "application/x-bibtex; charset=utf-8"}
252255
)
253256
if not response.ok:
254257
print(
@@ -258,3 +261,32 @@ def _to_bibtex(doi, template, idx):
258261
return doi
259262

260263
return response.text
264+
265+
266+
def _normalize_ext(value):
267+
"""
268+
Normalize extensions to have a leading dot.
269+
270+
Examples
271+
--------
272+
>>> _normalize_ext(".nii.gz")
273+
'.nii.gz'
274+
>>> _normalize_ext("nii.gz")
275+
'.nii.gz'
276+
>>> _normalize_ext(("nii", ".nii.gz"))
277+
['.nii', '.nii.gz']
278+
>>> _normalize_ext(("", ".nii.gz"))
279+
['', '.nii.gz']
280+
>>> _normalize_ext((None, ".nii.gz"))
281+
[None, '.nii.gz']
282+
>>> _normalize_ext([])
283+
[]
284+
285+
"""
286+
287+
if not value:
288+
return value
289+
290+
if isinstance(value, str):
291+
return f"{'' if value.startswith('.') else '.'}{value}"
292+
return [_normalize_ext(v) for v in value]

0 commit comments

Comments
 (0)