Skip to content

Commit c06497e

Browse files
authored
Merge pull request #336 from mgxd/fix/templateflow-select
FIX: Query templateflow files to see if resolution is available
2 parents fe17d3a + e375565 commit c06497e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ jobs:
272272
name: Pull TemplateFlow down, pre-cache some templates
273273
command: |
274274
export TEMPLATEFLOW_HOME=/tmp/templateflow
275-
pip install -U --user --no-cache-dir "$( grep templateflow /tmp/src/smriprep/setup.cfg | xargs )"
275+
pip install -U --user --no-cache-dir templateflow
276276
python -c "from templateflow import api as tfapi; \
277277
tfapi.get(['MNI152Lin', 'MNI152NLin2009cAsym', 'OASIS30ANTs'], suffix='T1w'); \
278278
tfapi.get(['MNI152Lin', 'MNI152NLin2009cAsym', 'OASIS30ANTs'], desc='brain', suffix='mask'); \

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ install_requires =
2525
matplotlib >= 2.2.0
2626
nibabel >= 4.0.1
2727
nipype >= 1.7.0
28-
niworkflows >= 1.7.0
28+
niworkflows @ git+https://github.com/nipreps/niworkflows.git@master
2929
numpy
3030
packaging
3131
pybids >= 0.11.1

smriprep/interfaces/templateflow.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# https://www.nipreps.org/community/licensing/
2222
#
2323
"""Interfaces to get templates from TemplateFlow."""
24+
import logging
2425
from templateflow import api as tf
2526
from nipype.interfaces.base import (
2627
SimpleInterface,
@@ -32,6 +33,8 @@
3233
InputMultiObject,
3334
)
3435

36+
LOGGER = logging.getLogger("nipype.interface")
37+
3538

3639
class _TemplateFlowSelectInputSpec(BaseInterfaceInputSpec):
3740
template = traits.Str("MNI152NLin2009cAsym", mandatory=True, desc="Template ID")
@@ -87,8 +90,11 @@ class TemplateFlowSelect(SimpleInterface):
8790
'.../tpl-MNIPediatricAsym_cohort-5_res-1_T1w.nii.gz'
8891
8992
>>> select = TemplateFlowSelect()
90-
>>> select.inputs.template = 'UNCInfant:cohort-1'
91-
>>> result = select.run() # doctest: +SKIP
93+
>>> select.inputs.template = 'MNI305'
94+
>>> select.inputs.template_spec = {'resolution': 1}
95+
>>> result = select.run()
96+
>>> result.outputs.t1w_file # doctest: +ELLIPSIS
97+
'.../tpl-MNI305_T1w.nii.gz'
9298
9399
"""
94100

@@ -115,6 +121,18 @@ def _run_interface(self, runtime):
115121
}
116122
)
117123

124+
if specs['resolution'] and not isinstance(specs['resolution'], list):
125+
specs['resolution'] = [specs['resolution']]
126+
127+
available_resolutions = tf.TF_LAYOUT.get_resolutions(template=name[0])
128+
if specs['resolution'] and not set(specs["resolution"]) & set(available_resolutions):
129+
fallback_res = available_resolutions[0] if available_resolutions else None
130+
LOGGER.warning(
131+
f"Template {name[0]} does not have resolution(s): {specs['resolution']}."
132+
f"Falling back to resolution: {fallback_res}."
133+
)
134+
specs["resolution"] = fallback_res
135+
118136
self._results["t1w_file"] = tf.get(name[0], desc=None, suffix="T1w", **specs)
119137

120138
self._results["brain_mask"] = (

0 commit comments

Comments
 (0)