|
47 | 47 | SimpleInterface,
|
48 | 48 | )
|
49 | 49 | from nipype.interfaces.io import add_traits
|
50 |
| -from templateflow.api import templates as _get_template_list |
| 50 | +import templateflow as tf |
51 | 51 | from ..utils.bids import _init_layout, relative_to_root
|
52 | 52 | from ..utils.images import set_consumables, unsafe_write_nifti_header_and_data
|
53 | 53 | from ..utils.misc import _copy_any, unlink
|
|
57 | 57 | BIDS_DERIV_ENTITIES = frozenset({e["name"] for e in _pybids_spec["entities"]})
|
58 | 58 | BIDS_DERIV_PATTERNS = tuple(_pybids_spec["default_path_patterns"])
|
59 | 59 |
|
60 |
| -STANDARD_SPACES = _get_template_list() |
| 60 | +STANDARD_SPACES = tf.api.templates() |
61 | 61 | LOGGER = logging.getLogger("nipype.interface")
|
62 | 62 |
|
63 | 63 |
|
@@ -576,6 +576,17 @@ def _run_interface(self, runtime):
|
576 | 576 | if out_entities.get("resolution") == "native" and out_entities.get("space"):
|
577 | 577 | out_entities.pop("resolution", None)
|
578 | 578 |
|
| 579 | + # Expand templateflow resolutions |
| 580 | + resolution = out_entities.get("resolution") |
| 581 | + space = out_entities.get("space") |
| 582 | + if resolution: |
| 583 | + # Standard spaces |
| 584 | + if space in STANDARD_SPACES: |
| 585 | + res = _get_tf_resolution(space, resolution) |
| 586 | + else: # TODO: Nonstandard? |
| 587 | + res = "Unknown" |
| 588 | + self._metadata['Resolution'] = res |
| 589 | + |
579 | 590 | if len(set(out_entities["extension"])) == 1:
|
580 | 591 | out_entities["extension"] = out_entities["extension"][0]
|
581 | 592 |
|
@@ -926,3 +937,38 @@ def _run_interface(self, runtime):
|
926 | 937 | )
|
927 | 938 |
|
928 | 939 | return runtime
|
| 940 | + |
| 941 | + |
| 942 | +def _get_tf_resolution(space: str, resolution: str) -> str: |
| 943 | + """ |
| 944 | + Query templateflow template information to elaborate on template resolution. |
| 945 | +
|
| 946 | + Examples |
| 947 | + -------- |
| 948 | + >>> _get_tf_resolution('MNI152NLin2009cAsym', '01') # doctest: +ELLIPSIS |
| 949 | + 'Template MNI152NLin2009cAsym (1.0x1.0x1.0 mm^3)...' |
| 950 | + >>> _get_tf_resolution('MNI152NLin2009cAsym', '1') # doctest: +ELLIPSIS |
| 951 | + 'Template MNI152NLin2009cAsym (1.0x1.0x1.0 mm^3)...' |
| 952 | + >>> _get_tf_resolution('MNI152NLin2009cAsym', '10') |
| 953 | + 'Unknown' |
| 954 | + """ |
| 955 | + metadata = tf.api.get_metadata(space) |
| 956 | + resolutions = metadata.get('res', {}) |
| 957 | + res_meta = None |
| 958 | + |
| 959 | + # Due to inconsistencies, resolution keys may or may not be zero-padded |
| 960 | + padded_res = f'{str(resolution):0>2}' |
| 961 | + for r in (resolution, padded_res): |
| 962 | + if r in resolutions: |
| 963 | + res_meta = resolutions[r] |
| 964 | + if res_meta is None: |
| 965 | + return "Unknown" |
| 966 | + |
| 967 | + def _fmt_xyz(coords: list) -> str: |
| 968 | + xyz = "x".join([str(c) for c in coords]) |
| 969 | + return f"{xyz} mm^3" |
| 970 | + |
| 971 | + return ( |
| 972 | + f"Template {space} ({_fmt_xyz(res_meta['zooms'])})," |
| 973 | + f" curated by TemplateFlow {tf.__version__}" |
| 974 | + ) |
0 commit comments