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,15 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
61
68
RuntimeError:
62
69
...
63
70
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}
64
78
"""
65
- from templateflow .api import get as get_template , get_metadata
79
+ import templateflow .api as tf
66
80
67
81
# Massage spec (start creating if None)
68
82
template_spec = template_spec or {}
@@ -72,16 +86,26 @@ def get_template_specs(in_template, template_spec=None, default_resolution=1):
72
86
"res" , template_spec .get ("resolution" , default_resolution )
73
87
)
74
88
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
79
103
80
104
common_spec = {"resolution" : template_spec ["resolution" ]}
81
105
if "cohort" in template_spec :
82
106
common_spec ["cohort" ] = template_spec ["cohort" ]
83
107
84
- tpl_target_path = get_template (in_template , ** template_spec )
108
+ tpl_target_path = tf . get (in_template , ** template_spec )
85
109
if not tpl_target_path :
86
110
raise RuntimeError (
87
111
"""\
0 commit comments