Skip to content

Commit 5cc9966

Browse files
committed
Add map function for generic SpatialImage
1 parent 96d474c commit 5cc9966

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

nibabel/spatialimages.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,9 @@ def __getitem__(self):
872872
raise TypeError("Cannot slice image objects; consider slicing image "
873873
"array data with `img.dataobj[slice]` or "
874874
"`img.get_data()[slice]`")
875+
876+
877+
def mapImage(func, img):
878+
"""Map a function onto image data"""
879+
return img.__class__(func(img.get_data()), img.affine, img.header.copy(),
880+
extra=img.extra.copy())

nibabel/tests/test_spatialimages.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import numpy as np
1414

1515
from ..spatialimages import (Header, SpatialImage, HeaderDataError,
16-
ImageDataError)
16+
ImageDataError, mapImage)
1717

1818
from unittest import TestCase
1919

@@ -382,3 +382,9 @@ def test_load_mmap(self):
382382
# Check invalid values raise error
383383
assert_raises(ValueError, func, param1, mmap='rw')
384384
assert_raises(ValueError, func, param1, mmap='r+')
385+
386+
387+
def test_mapImage():
388+
img = SpatialImage(np.zeros((2, 3, 4)), np.eye(4))
389+
assert np.unique(mapImage(lambda x: x + 1, img).get_data()) == \
390+
np.array([1.0])

0 commit comments

Comments
 (0)