Skip to content

Commit 7b7ef0f

Browse files
committed
ENH: Add fallback to query available resolutions
1 parent 6c65260 commit 7b7ef0f

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

niworkflows/utils/misc.py

Lines changed: 31 additions & 7 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,15 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
6168
RuntimeError:
6269
...
6370
71+
>>> get_template_specs('UNCInfant', {'suffix': 'T1w', 'res': 1})[1] # doctest: +IGNORE_EXCEPTION_DETAIL
72+
Traceback (most recent call last):
73+
RuntimeError:
74+
...
75+
76+
>>> get_template_specs('UNCInfant', {'cohort': 1, 'suffix': 'T1w', 'res': 1}, fallback=True)[1]
77+
{'resolution': None, 'cohort': 1}
6478
"""
65-
from templateflow.api import get as get_template, get_metadata
79+
import templateflow.api as tf
6680

6781
# Massage spec (start creating if None)
6882
template_spec = template_spec or {}
@@ -72,16 +86,26 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
7286
"res", template_spec.get("resolution", default_resolution)
7387
)
7488

75-
metadata = get_metadata(in_template)
76-
if "res" not in metadata and "resolution" not in metadata:
77-
# template does not have any resolution fields
78-
template_spec["resolution"] = None
89+
# Verify resolution is valid
90+
if fallback:
91+
res = template_spec['resolution']
92+
if res and not isinstance(res, list):
93+
res = [int(res)]
94+
95+
available_resolutions = tf.TF_LAYOUT.get_resolutions(template=in_template)
96+
if res and not set(res) & set(available_resolutions):
97+
fallback_res = available_resolutions[0] if available_resolutions else None
98+
warnings.warn(
99+
f"Template {in_template} does not have resolution: {res}."
100+
f"Falling back to resolution: {fallback_res}."
101+
)
102+
template_spec["resolution"] = fallback_res
79103

80104
common_spec = {"resolution": template_spec["resolution"]}
81105
if "cohort" in template_spec:
82106
common_spec["cohort"] = template_spec["cohort"]
83107

84-
tpl_target_path = get_template(in_template, **template_spec)
108+
tpl_target_path = tf.get(in_template, **template_spec)
85109
if not tpl_target_path:
86110
raise RuntimeError(
87111
"""\

0 commit comments

Comments
 (0)