Skip to content

Commit d67a7c4

Browse files
authored
Merge pull request #803 from mgxd/fix/templateflow-default-res
FIX: Inspect if template has resolution instead of assuming
2 parents a8d0c5a + 3c43d31 commit d67a7c4

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

niworkflows/anat/ants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def init_brain_extraction_wf(
196196
template_spec["suffix"] = template_spec.get("suffix", bids_suffix)
197197

198198
tpl_target_path, common_spec = get_template_specs(
199-
in_template, template_spec=template_spec
199+
in_template, template_spec=template_spec, fallback=True,
200200
)
201201

202202
# Get probabilistic brain mask if available

niworkflows/interfaces/norm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ def _get_ants_args(self):
429429
self.inputs.template,
430430
template_spec=template_spec,
431431
default_resolution=default_resolution,
432+
fallback=True,
432433
)
433434

434435
# Set reference image

niworkflows/utils/misc.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#
2323
"""Miscellaneous utilities."""
2424
import os
25+
from typing import Optional
26+
import warnings
2527

2628

2729
__all__ = [
@@ -35,7 +37,12 @@
3537
]
3638

3739

38-
def get_template_specs(in_template, template_spec=None, default_resolution=1):
40+
def get_template_specs(
41+
in_template: str,
42+
template_spec: Optional[dict] = None,
43+
default_resolution: int = 1,
44+
fallback: bool = False
45+
):
3946
"""
4047
Parse template specifications
4148
@@ -61,8 +68,17 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
6168
RuntimeError:
6269
...
6370
71+
>>> get_template_specs('UNCInfant',
72+
... {'suffix': 'T1w', 'res': 1})[1] # doctest: +IGNORE_EXCEPTION_DETAIL
73+
Traceback (most recent call last):
74+
RuntimeError:
75+
...
76+
77+
>>> get_template_specs('UNCInfant',
78+
... {'cohort': 1, 'suffix': 'T1w', 'res': 1}, fallback=True)[1]
79+
{'resolution': None, 'cohort': 1}
6480
"""
65-
from templateflow.api import get as get_template
81+
import templateflow.api as tf
6682

6783
# Massage spec (start creating if None)
6884
template_spec = template_spec or {}
@@ -72,11 +88,31 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
7288
"res", template_spec.get("resolution", default_resolution)
7389
)
7490

91+
# Verify resolution is valid
92+
if fallback:
93+
res = template_spec['resolution']
94+
if not isinstance(res, list):
95+
try:
96+
res = [int(res)]
97+
except Exception:
98+
res = None
99+
if res is None:
100+
res = []
101+
102+
available_resolutions = tf.TF_LAYOUT.get_resolutions(template=in_template)
103+
if not (set(res) & set(available_resolutions)):
104+
fallback_res = available_resolutions[0] if available_resolutions else None
105+
warnings.warn(
106+
f"Template {in_template} does not have resolution: {res}."
107+
f"Falling back to resolution: {fallback_res}."
108+
)
109+
template_spec["resolution"] = fallback_res
110+
75111
common_spec = {"resolution": template_spec["resolution"]}
76112
if "cohort" in template_spec:
77113
common_spec["cohort"] = template_spec["cohort"]
78114

79-
tpl_target_path = get_template(in_template, **template_spec)
115+
tpl_target_path = tf.get(in_template, **template_spec)
80116
if not tpl_target_path:
81117
raise RuntimeError(
82118
"""\

0 commit comments

Comments
 (0)