1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
"""Test buffer protocol for VTK arrays
Python 2.6 introduced a new buffer protocol that can expose raw
memory as a multi-dimensional array. This is used, for example,
by numpy in order to automatically generate arrays from memory
exposed by other python extension modules.
Created on Aug 1, 2015 by David Gobbi
"""
import sys
import struct
import vtk
from vtk.test import Testing
# array types and the corresponding format
lsize = vtk.VTK_SIZEOF_LONG
idsize = vtk.VTK_SIZEOF_ID_TYPE
if idsize == 8:
idchar = 'q'
else:
idchar = 'i'
arrayType = {
'SignedChar':('b', 1), 'UnsignedChar':('B', 1),
'Short':('h', 2), 'UnsignedShort':('H', 2),
'Int':('i', 4), 'UnsignedInt':('I', 4),
'Long':('l', lsize), 'UnsignedLong':('L', lsize),
'IdType':(idchar, idsize),
'LongLong':('q', 8), 'UnsignedLongLong':('Q', 8)
}
class TestBuffer(Testing.vtkTest):
def testOneDimensionalDataArray(self):
"""Test one-dimensional data array."""
if sys.hexversion < 0x02070000:
return
for atype,ainfo in arrayType.items():
aclass = getattr(vtk, 'vtk' + atype + 'Array')
a = aclass()
a.InsertNextValue(10)
a.InsertNextValue(7)
a.InsertNextValue(85)
m = memoryview(a)
self.assertEqual(m.format, ainfo[0])
self.assertEqual(m.itemsize, ainfo[1])
self.assertEqual(m.strides, (ainfo[1],))
self.assertEqual(m.shape, (3,))
self.assertEqual(m.ndim, 1)
# test the contents of the memoryview
tp = struct.unpack(3*ainfo[0], m.tobytes())
self.assertEqual(tp, (10, 7, 85))
# now test re-creating the array from a buffer
b = aclass()
b.SetVoidArray(m, 3, True)
self.assertEqual(b.GetValue(0), 10)
self.assertEqual(b.GetValue(1), 7)
self.assertEqual(b.GetValue(2), 85)
def testTwoDimensionalDataArray(self):
"""Test data array with components."""
if sys.hexversion < 0x02070000:
return
for atype,ainfo in arrayType.items():
aclass = getattr(vtk, 'vtk' + atype + 'Array')
a = aclass()
a.SetNumberOfComponents(3)
a.InsertNextTuple((10, 7, 4))
a.InsertNextTuple((85, 8, 2))
m = memoryview(a)
self.assertEqual(m.format, ainfo[0])
self.assertEqual(m.itemsize, ainfo[1])
self.assertEqual(m.shape, (2, 3))
self.assertEqual(m.strides, (ainfo[1]*3, ainfo[1]))
self.assertEqual(m.ndim, 2)
# test the contents of the memoryview
tp = struct.unpack(6*ainfo[0], m.tobytes())
self.assertEqual(tp, (10, 7, 4, 85, 8, 2))
def testCharArray(self):
"""Test the special case of the char array."""
if sys.hexversion < 0x02070000:
return
# bit array is actually stored as a byte array
a = vtk.vtkCharArray()
a.SetNumberOfComponents(5)
a.InsertNextTypedTuple("hello")
a.InsertNextTypedTuple("world")
m = memoryview(a)
self.assertEqual(m.format, 'c')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.shape, (2, 5))
self.assertEqual(m.strides, (5, 1))
self.assertEqual(m.ndim, 2)
# test the contents of the memoryview
self.assertEqual(m.tobytes(), b"helloworld")
def testBitArray(self):
"""Test the special case of the bit array."""
if sys.hexversion < 0x02070000:
return
# bit array is actually stored as a byte array
a = vtk.vtkBitArray()
a.InsertNextValue(0)
a.InsertNextValue(1)
a.InsertNextValue(1)
a.InsertNextValue(0)
a.InsertNextValue(1)
m = memoryview(a)
self.assertEqual(m.format, 'B')
self.assertEqual(m.itemsize, 1)
self.assertEqual(m.shape, (1,))
# test the contents of the memoryview
self.assertEqual(ord(m.tobytes()) & 0xF8, 0x68)
def testBufferShared(self):
"""Test the special buffer_shared() check that VTK provides."""
a = bytearray(b'hello')
self.assertEqual(vtk.buffer_shared(a, a), True)
b = bytearray(b'hello')
self.assertEqual(vtk.buffer_shared(a, b), False)
a = vtk.vtkFloatArray()
a.SetNumberOfComponents(3)
a.InsertNextTuple((10, 7, 4))
a.InsertNextTuple((85, 8, 2))
b = vtk.vtkFloatArray()
b.SetVoidArray(a, 6, True)
self.assertEqual(vtk.buffer_shared(a, b), True)
c = vtk.vtkFloatArray()
c.DeepCopy(a)
self.assertEqual(vtk.buffer_shared(a, c), False)
if sys.hexversion >= 0x02070000:
m = memoryview(a)
self.assertEqual(vtk.buffer_shared(a, m), True)
if sys.hexversion < 0x03000000:
m = buffer(a)
self.assertEqual(vtk.buffer_shared(a, m), True)
if __name__ == "__main__":
Testing.main([(TestBuffer, 'test')])
|