Skip to content

Commit 6d31a32

Browse files
authored
Merge pull request #92 from templateflow/fix/issue-86
FIX: Normalize extensions for leading dot & update doctests to new resources
2 parents b6cde5a + c33b9f7 commit 6d31a32

3 files changed

Lines changed: 62 additions & 15 deletions

File tree

.github/workflows/pythonpackage.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
matrix:
1919
python-version: [3.7, 3.8, 3.9]
20-
pip: ["pip~=20.0", "pip~=21.0"]
20+
pip: ["pip==21.2", "pip~=22.0"]
2121

2222
steps:
2323
- uses: actions/checkout@v2
@@ -87,15 +87,15 @@ jobs:
8787
TEMPLATEFLOW_HOME: /tmp/home/sdist
8888
run: |
8989
source /tmp/install_sdist/bin/activate
90-
python -m pip install /tmp/package/templateflow*.tar.gz
90+
python -m pip install /tmp/package/templateflow*.tar.gz --force-reinstall
9191
find ${TEMPLATEFLOW_HOME} >> /tmp/.sdist-install.txt
9292
- name: Re-install in confined environment [sdist - missing template]
9393
env:
9494
TEMPLATEFLOW_HOME: /tmp/home/sdist
9595
run: |
9696
rm -rf ${TEMPLATEFLOW_HOME}/tpl-MNI152NLin2009cAsym
9797
source /tmp/install_sdist/bin/activate
98-
python -m pip install /tmp/package/templateflow*.tar.gz
98+
python -m pip install /tmp/package/templateflow*.tar.gz --force-reinstall
9999
python -c "import templateflow; templateflow.update(overwrite=False)"
100100
find ${TEMPLATEFLOW_HOME} >> /tmp/.sdist-install-2.txt
101101
diff /tmp/.sdist-install.txt /tmp/.sdist-install-2.txt
@@ -118,15 +118,15 @@ jobs:
118118
TEMPLATEFLOW_HOME: /tmp/home/wheel
119119
run: |
120120
source /tmp/install_wheel/bin/activate
121-
python -m pip install /tmp/package/templateflow*.whl
121+
python -m pip install /tmp/package/templateflow*.whl --force-reinstall
122122
find ${TEMPLATEFLOW_HOME} >> /tmp/.wheel-install.txt
123123
- name: Re-install in confined environment [wheel - missing template]
124124
env:
125125
TEMPLATEFLOW_HOME: /tmp/home/wheel
126126
run: |
127127
rm -rf ${TEMPLATEFLOW_HOME}/tpl-MNI152NLin2009cAsym
128128
source /tmp/install_wheel/bin/activate
129-
python -m pip install /tmp/package/templateflow*.whl
129+
python -m pip install /tmp/package/templateflow*.whl --force-reinstall
130130
# Wheels do not run post-install hooks:
131131
test ! -d ${TEMPLATEFLOW_HOME}/tpl-MNI152NLin2009cAsym
132132
python -c "import templateflow; templateflow.update(overwrite=False)"
@@ -142,6 +142,7 @@ jobs:
142142
source /tmp/setup_install/bin/activate
143143
python -m pip install -U "setuptools >= 45" wheel "setuptools_scm >= 6.2" \
144144
setuptools_scm_git_archive "${{ matrix.pip }}"
145+
python -m pip install "numpy==1.20" Cython "pandas==1.3" "scipy==1.7"
145146
python setup.py install
146147
INSTALLED_VERSION=$(python -c 'import templateflow as tf; print(tf.__version__, end="")')
147148
echo "INSTALLED: \"${INSTALLED_VERSION}\""
@@ -174,6 +175,7 @@ jobs:
174175
source /tmp/setup_develop/bin/activate
175176
python -m pip install -U "setuptools >= 45" wheel "setuptools_scm >= 6.2" \
176177
setuptools_scm_git_archive "${{ matrix.pip }}"
178+
python -m pip install "numpy==1.20" Cython "pandas==1.3" "scipy==1.7"
177179
python setup.py develop
178180
INSTALLED_VERSION=$(python -c 'import templateflow as tf; print(tf.__version__, end="")')
179181
echo "INSTALLED: \"${INSTALLED_VERSION}\""

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]

templateflow/tests/test_api.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,26 @@
6565
"https://github.com/Washington-University/HCPpipelines/tree/master/global/templates"
6666
)
6767

68+
fsaverage_fbib = """\
69+
@article{Fischl_1999,
70+
\tdoi = {10.1002/(sici)1097-0193(1999)8:4<272::aid-hbm10>3.0.co;2-4},
71+
\turl = {https://doi.org/10.1002%2F%28sici%291097-0193%281999%298%3A4%3C272%3A%3Aaid-hbm10%3E3.0.co%3B2-4},
72+
\tyear = 1999,
73+
\tpublisher = {Wiley},
74+
\tvolume = {8},
75+
\tnumber = {4},
76+
\tpages = {272--284},
77+
\tauthor = {Bruce Fischl and Martin I. Sereno and Roger B.H. Tootell and Anders M. Dale},
78+
\ttitle = {High-resolution intersubject averaging and a coordinate system for the cortical surface},
79+
\tjournal = {Human Brain Mapping}
80+
}"""
6881

6982
@pytest.mark.parametrize(
7083
"template,urls,fbib,lbib",
7184
[
7285
("MNI152NLin2009cAsym", mni2009_urls, mni2009_fbib, mni2009_lbib),
7386
("fsLR", fslr_urls, fslr_fbib, fslr_lbib),
74-
("fsaverage", [], None, None),
87+
("fsaverage", ["https://doi.org/10.1002/(sici)1097-0193(1999)8:4%3C272::aid-hbm10%3E3.0.co;2-4"], fsaverage_fbib, None),
7588
],
7689
)
7790
def test_citations(tmp_path, template, urls, fbib, lbib):
@@ -80,7 +93,7 @@ def test_citations(tmp_path, template, urls, fbib, lbib):
8093
bibs = api.get_citations(template, bibtex=True)
8194
if bibs:
8295
assert "".join(bibs[0]) == fbib
83-
assert "".join(bibs[-1]) == lbib
96+
assert len(bibs) == 1 if lbib is None else "".join(bibs[-1]) == lbib
8497
else:
8598
# no citations currently
86-
assert template == "fsaverage"
99+
assert False

0 commit comments

Comments
 (0)