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
16 changes: 16 additions & 0 deletions Modules/Bridge/NumPy/wrapping/PyBuffer.i.init
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ try:

except ImportError:
HAVE_NUMPY = False
try:
import numpy as np
from distributed.protocol import dask_serialize, dask_deserialize
from typing import Dict, List, Tuple
except ImportError:
pass
else:
@dask_serialize.register(NDArrayITKBase)
def serialize(ndarray_itk_base: NDArrayITKBase) -> Tuple[Dict, List[bytes]]:
dumps = dask_serialize.dispatch(np.ndarray)
return dumps(ndarray_itk_base)

@dask_deserialize.register(NDArrayITKBase)
def deserialize(header: Dict, frames: List[bytes]) -> NDArrayITKBase:
loads = dask_deserialize.dispatch(np.ndarray)
return NDArrayITKBase(loads(header, frames))

def _get_numpy_pixelid(itk_Image_type):
"""Returns a ITK PixelID given a numpy array."""
Expand Down
39 changes: 39 additions & 0 deletions Modules/Bridge/NumPy/wrapping/test/itkPyBufferTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,45 @@ 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'
Comment on lines +55 to +58
Copy link
Contributor

Choose a reason for hiding this comment

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

This test doesn't engage the code that you've written above. Pickle is the standard python serialization technique. Dask's serialization is different. If you wanted to test it you would try the following:

from distributed.protocol import serialize, deserialize

header, frames = serialize(ndarray_itk_base)
obj = deserialize(header, frames)
assert ...


try:
import dask
from distributed.protocol.serialize import dask_dumps, dask_loads
except ImportError:
pass
else:
header, frames = dask_dumps(ndarray_itk_base)
recon_obj = dask_loads(header, frames)
equal = (recon_obj == 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