Skip to content

Commit 01cc9c8

Browse files
committed
ENH: Add Resolution field to metadata if data has res entity
1 parent 5b898c4 commit 01cc9c8

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

niworkflows/interfaces/bids.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
SimpleInterface,
4848
)
4949
from nipype.interfaces.io import add_traits
50-
from templateflow.api import templates as _get_template_list
50+
import templateflow as tf
5151
from ..utils.bids import _init_layout, relative_to_root
5252
from ..utils.images import set_consumables, unsafe_write_nifti_header_and_data
5353
from ..utils.misc import _copy_any, unlink
@@ -57,7 +57,7 @@
5757
BIDS_DERIV_ENTITIES = frozenset({e["name"] for e in _pybids_spec["entities"]})
5858
BIDS_DERIV_PATTERNS = tuple(_pybids_spec["default_path_patterns"])
5959

60-
STANDARD_SPACES = _get_template_list()
60+
STANDARD_SPACES = tf.api.templates()
6161
LOGGER = logging.getLogger("nipype.interface")
6262

6363

@@ -576,6 +576,17 @@ def _run_interface(self, runtime):
576576
if out_entities.get("resolution") == "native" and out_entities.get("space"):
577577
out_entities.pop("resolution", None)
578578

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+
579590
if len(set(out_entities["extension"])) == 1:
580591
out_entities["extension"] = out_entities["extension"][0]
581592

@@ -926,3 +937,38 @@ def _run_interface(self, runtime):
926937
)
927938

928939
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

Comments
 (0)