From d92f8e0ff8ae89761a61169cb4859a00c1c048da Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Mon, 2 Dec 2019 09:36:08 +0000 Subject: [PATCH] BF: Fix up adjust method --- fsl/data/image.py | 26 +++++++++++++++----------- fsl/transform/affine.py | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/fsl/data/image.py b/fsl/data/image.py index 7eaf53f4f..39f1f7fac 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -864,22 +864,26 @@ class Nifti(notifier.Notifier, meta.Meta): raise ValueError('Exactly one of pixdim or ' 'shape must be specified') + if shape is not None: ndim = len(shape) + else: ndim = len(pixdim) + + # We only allow adjustment of + # the spatial dimensions + if ndim != 3: + raise ValueError('Three dimensions must be specified') + + oldShape = np.array(self.shape[ :ndim]) + oldPixdim = np.array(self.pixdim[:ndim]) newShape = shape newPixdim = pixdim # if pixdims were specified, - # convert them into a shape + # convert them into a shape, + # and vice versa if newPixdim is not None: - npixdim = len(newPixdim) - newPixdim = np.array(newPixdim) - oldShape = np.array(self.shape[ :npixdim]) - oldPixdim = np.array(self.pixdim[:npixdim]) - newShape = oldShape * (oldPixdim / newPixdim) - - # We only allow adjustment of - # the spatial dimensions - if len(newShape) != 3: - raise ValueError('Three dimensions must be specified') + newShape = oldShape * (oldPixdim / newPixdim) + else: + newPixdim = oldPixdim * (oldShape / newShape) # Rescale the voxel-to-world affine xform = affine.rescale(oldShape, newShape, origin) diff --git a/fsl/transform/affine.py b/fsl/transform/affine.py index 92a16ddd4..c7fb71f2a 100644 --- a/fsl/transform/affine.py +++ b/fsl/transform/affine.py @@ -588,7 +588,7 @@ def rmsdev(T1, T2, R=None, xc=None): def rescale(oldShape, newShape, origin=None): """Calculates an affine matrix to use for resampling. - This function generates an affine transformation matreix that can be used + This function generates an affine transformation matrix that can be used to resample an N-D array from ``oldShape`` to ``newShape`` using, for example, ``scipy.ndimage.affine_transform``. -- GitLab