Skip to content

Commit

Permalink
fix(pypipeline): Create a copy of output memoryviews
Browse files Browse the repository at this point in the history
When pipeline destructors are called, memoryviews can become invalid.
  • Loading branch information
thewtex committed Feb 24, 2023
1 parent 8299724 commit e2c8a22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/python/itkwasm/itkwasm/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@

def _memoryview_to_numpy_array(component_type, buf):
if component_type == IntTypes.UInt8:
return np.frombuffer(buf, dtype=np.uint8)
return np.frombuffer(buf, dtype=np.uint8).copy()
elif component_type == IntTypes.Int8:
return np.frombuffer(buf, dtype=np.int8)
return np.frombuffer(buf, dtype=np.int8).copy()
elif component_type == IntTypes.UInt16:
return np.frombuffer(buf, dtype=np.uint16)
return np.frombuffer(buf, dtype=np.uint16).copy()
elif component_type == IntTypes.Int16:
return np.frombuffer(buf, dtype=np.int16)
return np.frombuffer(buf, dtype=np.int16).copy()
elif component_type == IntTypes.UInt32:
return np.frombuffer(buf, dtype=np.uint32)
return np.frombuffer(buf, dtype=np.uint32).copy()
elif component_type == IntTypes.Int32:
return np.frombuffer(buf, dtype=np.int32)
return np.frombuffer(buf, dtype=np.int32).copy()
elif component_type == IntTypes.UInt64:
return np.frombuffer(buf, dtype=np.uint64)
return np.frombuffer(buf, dtype=np.uint64).copy()
elif component_type == IntTypes.Int64:
return np.frombuffer(buf, dtype=np.int64)
return np.frombuffer(buf, dtype=np.int64).copy()
elif component_type == FloatTypes.Float32:
return np.frombuffer(buf, dtype=np.float32)
return np.frombuffer(buf, dtype=np.float32).copy()
elif component_type == FloatTypes.Float64:
return np.frombuffer(buf, dtype=np.float64)
return np.frombuffer(buf, dtype=np.float64).copy()
else:
raise ValueError('Unsupported component type')

Expand Down
2 changes: 2 additions & 0 deletions src/python/itkwasm/test/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ def test_pipeline_write_read_polydata():
outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

out_mesh_dict = asdict(outputs[0].data)

# native itk python binaries require uint64
out_mesh_dict['cells'] = out_mesh_dict['cells'].astype(np.uint64)
out_mesh_dict['meshType']['cellComponentType'] = 'uint64'
assert np.isclose(out_mesh_dict['points'][0], 3.71636)
out_mesh = itk.mesh_from_dict(out_mesh_dict)

assert out_mesh.GetNumberOfPoints() == 2903
Expand Down

0 comments on commit e2c8a22

Please sign in to comment.