From 963aee021194bf1346502bb9200a0a758ada3ce7 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Tue, 24 Mar 2015 16:55:28 -0400 Subject: [PATCH 1/2] Add image_like function for generic SpatialImage --- nibabel/__init__.py | 1 + nibabel/spatialimages.py | 8 ++++++++ nibabel/tests/test_spatialimages.py | 12 +++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/nibabel/__init__.py b/nibabel/__init__.py index fca22ccc99..25b14d398f 100644 --- a/nibabel/__init__.py +++ b/nibabel/__init__.py @@ -72,6 +72,7 @@ def setup_test(): from .freesurfer import MGHImage from .funcs import (squeeze_image, concat_images, four_to_three, as_closest_canonical) +from .spatialimages import image_like from .orientations import (io_orientation, orientation_affine, flip_axis, OrientationError, apply_orientation, aff2axcodes) diff --git a/nibabel/spatialimages.py b/nibabel/spatialimages.py index ede0820065..b749cdc7b7 100644 --- a/nibabel/spatialimages.py +++ b/nibabel/spatialimages.py @@ -634,3 +634,11 @@ def as_reoriented(self, ornt): new_aff = self.affine.dot(inv_ornt_aff(ornt, self.shape)) return self.__class__(t_arr, new_aff, self.header) + + +def image_like(img, data): + ''' Create new SpatialImage with metadata of `img`, and data + contained in `data`. + ''' + return img.__class__(data, img.affine, img.header.copy(), + extra=img.extra.copy()) diff --git a/nibabel/tests/test_spatialimages.py b/nibabel/tests/test_spatialimages.py index b0f571023d..b8569543b4 100644 --- a/nibabel/tests/test_spatialimages.py +++ b/nibabel/tests/test_spatialimages.py @@ -16,7 +16,7 @@ from io import BytesIO from ..spatialimages import (SpatialHeader, SpatialImage, HeaderDataError, - Header, ImageDataError) + Header, ImageDataError, image_like) from ..imageclasses import spatial_axes_first from unittest import TestCase @@ -659,3 +659,13 @@ class MyHeader(Header): MyHeader() assert_equal(len(w), 1) + + +def test_image_like(): + zeros = SpatialImage(np.zeros((2, 3, 4)), np.eye(4)) + ones = image_like(zeros, np.ones((2, 3, 4))) + + assert np.all(ones.dataobj != zeros.dataobj) + assert np.all(ones.affine == zeros.affine) + assert ones.header == zeros.header + assert ones.extra == zeros.extra From cc5b2bb219995bf4578a8717db18fad67b89edb0 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Sat, 6 Jun 2015 23:10:55 -0400 Subject: [PATCH 2/2] Add optional data parameter to from_image, call from_image from image_like --- nibabel/spatialimages.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nibabel/spatialimages.py b/nibabel/spatialimages.py index b749cdc7b7..4f658fa91a 100644 --- a/nibabel/spatialimages.py +++ b/nibabel/spatialimages.py @@ -537,7 +537,7 @@ def get_affine(self): return self.affine @classmethod - def from_image(klass, img): + def from_image(klass, img, data=None): ''' Class method to create new instance of own class from `img` Parameters @@ -551,7 +551,7 @@ def from_image(klass, img): cimg : ``spatialimage`` instance Image, of our own class ''' - return klass(img.dataobj, + return klass(img.dataobj if data is None else data, img.affine, klass.header_class.from_header(img.header), extra=img.extra.copy()) @@ -640,5 +640,4 @@ def image_like(img, data): ''' Create new SpatialImage with metadata of `img`, and data contained in `data`. ''' - return img.__class__(data, img.affine, img.header.copy(), - extra=img.extra.copy()) + return img.from_image(img, data)