22
22
#
23
23
"""Miscellaneous utilities."""
24
24
import os
25
+ from typing import Optional
26
+ import warnings
25
27
26
28
27
29
__all__ = [
35
37
]
36
38
37
39
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
+ ):
39
46
"""
40
47
Parse template specifications
41
48
@@ -61,8 +68,17 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
61
68
RuntimeError:
62
69
...
63
70
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}
64
80
"""
65
- from templateflow .api import get as get_template
81
+ import templateflow .api as tf
66
82
67
83
# Massage spec (start creating if None)
68
84
template_spec = template_spec or {}
@@ -72,11 +88,31 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
72
88
"res" , template_spec .get ("resolution" , default_resolution )
73
89
)
74
90
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
+
75
111
common_spec = {"resolution" : template_spec ["resolution" ]}
76
112
if "cohort" in template_spec :
77
113
common_spec ["cohort" ] = template_spec ["cohort" ]
78
114
79
- tpl_target_path = get_template (in_template , ** template_spec )
115
+ tpl_target_path = tf . get (in_template , ** template_spec )
80
116
if not tpl_target_path :
81
117
raise RuntimeError (
82
118
"""\
0 commit comments