Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Modules/Bridge/NumPy/wrapping/PyBuffer.i.init
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ try:
if obj is None: return
self.itk_base = getattr(obj, 'itk_base', None)

def __reduce_ex__(self, protocol):
np_copy = np.array(self, copy=True)
return type(self)._reconstruct, (np_copy,), None

@classmethod
def _reconstruct(cls, obj):
with memoryview(obj) as m:
obj = m.obj
if type(obj) is np.ndarray:
return cls(obj, itk_base=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the discussion here:

The above two methods should be replaced with something like:

def __reduce__(self):
    np_copy = np.array(self, copy=True)
    pickled_state = super(NDArrayITKBase, np_copy).__reduce__()
    return pickled_state

except ImportError:
HAVE_NUMPY = False

Expand Down
29 changes: 29 additions & 0 deletions Modules/Bridge/NumPy/wrapping/test/itkPyBufferTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ class TestNumpyITKMemoryviewInterface(unittest.TestCase):
def setUp(self):
pass

def test_NDArrayITKBase_pickle(self):
"""
Test the serialization of itk.NDArrayITKBase
"""
Dimension = 3
ScalarImageType = itk.Image[itk.UC, Dimension]
RegionType = itk.ImageRegion[Dimension]

region = RegionType()
region.SetSize(0, 6);
region.SetSize(1, 6);
region.SetSize(2, 6);

scalarImage = ScalarImageType.New()
scalarImage.SetRegions(region);
scalarImage.Allocate(True);
scalarImage.SetPixel([0, 0, 0], 5)
scalarImage.SetPixel([0, 0, 1], 3)
scalarImage.SetPixel([5, 5, 5], 8)
ndarray_itk_base = itk.array_view_from_image(scalarImage)

import pickle

## test serialization of itk ndarrary itk base
pickled = pickle.dumps(ndarray_itk_base)
reloaded = pickle.loads(pickled)
equal = (reloaded == ndarray_itk_base).all()
assert equal, 'Different results before and after pickle'

def test_NumPyBridge_itkScalarImage(self):
"Try to convert all pixel types to NumPy array view"

Expand Down