Skip to content

Commit e33149d

Browse files
committed
Add image_like function for generic SpatialImage
1 parent 5a42cb3 commit e33149d

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

nibabel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from .freesurfer import MGHImage
5959
from .funcs import (squeeze_image, concat_images, four_to_three,
6060
as_closest_canonical)
61+
from .spatialimages import image_like
6162
from .orientations import (io_orientation, orientation_affine,
6263
flip_axis, OrientationError,
6364
apply_orientation, aff2axcodes)

nibabel/spatialimages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,10 @@ def orthoview(self):
679679
"""
680680
return OrthoSlicer3D(self.dataobj, self.affine,
681681
title=self.get_filename())
682+
683+
def image_like(img, data):
684+
''' Create new SpatialImage with metadata of `img`, and data
685+
contained in `data`.
686+
'''
687+
return img.__class__(data, img.affine, img.header.copy(),
688+
extra=img.extra.copy())

nibabel/tests/test_spatialimages.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from ..externals.six import BytesIO
1818
from ..spatialimages import (SpatialHeader, SpatialImage, HeaderDataError,
19-
Header, ImageDataError)
19+
Header, image_like)
2020

2121
from unittest import TestCase
2222
from nose.tools import (assert_true, assert_false, assert_equal,
@@ -402,3 +402,13 @@ class MyHeader(Header):
402402

403403
MyHeader()
404404
assert_equal(len(w), 1)
405+
406+
407+
def test_image_like():
408+
zeros = SpatialImage(np.zeros((2, 3, 4)), np.eye(4))
409+
ones = image_like(zeros, np.ones((2, 3, 4)))
410+
411+
assert np.all(ones.dataobj != zeros.dataobj)
412+
assert np.all(ones.affine == zeros.affine)
413+
assert ones.header == zeros.header
414+
assert ones.extra == zeros.extra

0 commit comments

Comments
 (0)