[BUG]: Fix SpaceWarper to handle native space data conversion in "auto" mode #482

Merged
synchon merged 5 commits from fix/native-data-with-auto-space-warper into main 2026-03-12 13:32:01 +00:00
2 changed files with 29 additions and 33 deletions

View file

@ -0,0 +1 @@
Allow :class:`.SpaceWarper` to convert data from native to template space when ``using="auto"`` by `Fede Raimondo`_ and `Synchon Mandal`_

View file

@ -129,20 +129,21 @@ class SpaceWarper(BasePreprocessor):
------ ------
ValueError ValueError
If ``extra_input`` is None when transforming to native space If ``extra_input`` is None when transforming to native space
i.e., using ``"T1w"`` as reference. i.e., using ``"T1w"`` as reference or converting from native to
template space or
if the ``reference`` key is missing from ``input`` when converting
from native to template space.
RuntimeError RuntimeError
If warper could not be found in ``extra_input`` when If warper could not be found in ``extra_input`` when
``using="auto"`` or converting from native space or ``using="auto"`` or converting from native space or
if the data is in the correct space and does not require if the data is in the correct space and does not require
warping or warping or
if FSL is used when ``reference="T1w"``. if FSL or "auto" is used when ``reference!="T1w"``.
""" """
logger.info(f"Warping to {self.reference} space using SpaceWarper") logger.info(f"Warping to {self.reference} space using SpaceWarper")
# Transform to native space # Transform to native space
if ( if self.reference == "T1w": # pragma: no cover
self.using in ["fsl", "ants", "auto"] and self.reference == "T1w"
): # pragma: no cover
# Check for extra inputs # Check for extra inputs
if extra_input is None: if extra_input is None:
raise_error( raise_error(
@ -150,20 +151,8 @@ class SpaceWarper(BasePreprocessor):
f"`{self.reference}` data types in particular." f"`{self.reference}` data types in particular."
) )
# Conditional preprocessor # Conditional preprocessor
if self.using == "fsl": warper = None
input = FSLWarper().preprocess( if self.using == "auto":
input=input,
extra_input=extra_input,
reference=self.reference,
)
elif self.using == "ants":
input = ANTsWarper().preprocess(
input=input,
extra_input=extra_input,
reference=self.reference,
)
elif self.using == "auto":
warper = None
for entry in extra_input["Warp"]: for entry in extra_input["Warp"]:
if entry["dst"] == "native": if entry["dst"] == "native":
warper = entry["warper"] warper = entry["warper"]
@ -171,20 +160,21 @@ class SpaceWarper(BasePreprocessor):
raise_error( raise_error(
klass=RuntimeError, msg="Could not find correct warper" klass=RuntimeError, msg="Could not find correct warper"
) )
if warper == "fsl": else:
input = FSLWarper().preprocess( warper = self.using
input=input, if warper == "fsl":
extra_input=extra_input, input = FSLWarper().preprocess(
reference=self.reference, input=input,
) extra_input=extra_input,
elif warper == "ants": reference=self.reference,
input = ANTsWarper().preprocess( )
input=input, elif warper == "ants":
extra_input=extra_input, input = ANTsWarper().preprocess(
reference=self.reference, input=input,
) extra_input=extra_input,
# Transform to template space reference=self.reference,
if self.using in ["fsl", "ants"] and self.reference != "T1w": )
else:
input_space = input["space"] input_space = input["space"]
# Check pre-requirements for space manipulation # Check pre-requirements for space manipulation
if self.using == "ants" and self.reference == input_space: if self.using == "ants" and self.reference == input_space:
@ -241,12 +231,12 @@ class SpaceWarper(BasePreprocessor):
) )
else: else:
# Transform from MNI to MNI template space not possible # Transform from MNI to MNI template space not possible
if self.using == "fsl": if self.using in ["fsl", "auto"]:
raise_error( raise_error(
( (
f"Warping from {input_space} space to " f"Warping from {input_space} space to "
f"{self.reference} space not possible with " f"{self.reference} space not possible with "
"FSL, use ANTs instead." f"{self.using}, use ANTs instead."
), ),
klass=RuntimeError, klass=RuntimeError,
) )
@ -257,5 +247,10 @@ class SpaceWarper(BasePreprocessor):
extra_input={}, extra_input={},
reference=self.reference, reference=self.reference,
) )
logger.debug("Completed warping step")
logger.debug("Warped data types: ")
for k, v in input.items():
if k in ["data", "meta"]:
continue
logger.debug(f"\t{k}: {v}")
return input return input