From 0b2302f04037e5d77a8a0ad120396b74f2176af1 Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Tue, 23 Jul 2024 12:56:36 +0200 Subject: [PATCH] wip: initiate implementation --- nitransforms/resampling.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/nitransforms/resampling.py b/nitransforms/resampling.py index 9de0d2d6..2b2594b9 100644 --- a/nitransforms/resampling.py +++ b/nitransforms/resampling.py @@ -7,6 +7,7 @@ # ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## """Resampling utilities.""" +from warnings import warn from pathlib import Path import numpy as np from nibabel.loadsave import load as _nbload @@ -19,6 +20,9 @@ _as_homogeneous, ) +SERIALIZE_MIN_NUM_VOLUMES : int = 8 +"""Minimum number of volumes to automatically serialize 4D transforms.""" + def apply( transform, @@ -29,6 +33,8 @@ def apply( cval=0.0, prefilter=True, output_dtype=None, + serialize_nvols=SERIALIZE_MIN_NUM_VOLUMES, + njobs=None, ): """ Apply a transformation to an image, resampling on the reference spatial object. @@ -89,14 +95,20 @@ def apply( spatialimage = _nbload(str(spatialimage)) data = np.asanyarray(spatialimage.dataobj) + data_nvols = 1 if data.ndim < 4 else data.shape[-1] + xfm_nvols = len(transforms) - if data.ndim == 4 and data.shape[-1] != len(transform): + if data_nvols == 1 and xfm_nvols > 1: + data = data[..., np.newaxis] + elif data_nvols != xfm_nvols: raise ValueError( "The fourth dimension of the data does not match the tranform's shape." ) - if data.ndim < transform.ndim: - data = data[..., np.newaxis] + serialize_nvols = serialize_nvols if serialize_nvols and serialize_nvols > 1 else np.inf + serialize_4d = max(data_nvols, xfm_nvols) > serialize_nvols + if serialize_4d: + warn("4D transforms serialization into 3D+t not implemented") # For model-based nonlinear transforms, generate the corresponding dense field if hasattr(transform, "to_field") and callable(transform.to_field):