MCPcopy Create free account
hub / github.com/Kitware/VTK / testNumpy2VTK

Method testNumpy2VTK

Common/Core/Testing/Python/TestNumpySupport.py:47–94  ·  view source on GitHub ↗

Test Numeric array to VTK array conversion and vice-versa.

(self)

Source from the content-addressed store, hash-verified

45 self.assertEqual(vtk_arr.GetTuple1(i), arr[i])
46
47 def testNumpy2VTK(self):
48 """Test Numeric array to VTK array conversion and vice-versa."""
49 # Put all the test arrays here.
50 t_z = []
51
52 # Test the different types of arrays.
53 t_z.append(numpy.array([-128, 0, 127], numpy.int8))
54 t_z.append(numpy.array([-32768, 0, 32767], numpy.int16))
55 t_z.append(numpy.array([-2147483648, 0, 2147483647], numpy.int32))
56 t_z.append(numpy.array([0, 255], numpy.uint8))
57 t_z.append(numpy.array([0, 65535], numpy.uint16))
58 t_z.append(numpy.array([0, 4294967295], numpy.uint32))
59 t_z.append(numpy.array([-1.0e38, 0, 1.0e38], 'f'))
60 t_z.append(numpy.array([-1.0e299, 0, 1.0e299], 'd'))
61
62 # Check multi-component arrays.
63 t_z.append(numpy.array([[1], [2], [300]], 'd'))
64 t_z.append(numpy.array([[1, 20], [300, 4000]], 'd'))
65 t_z.append(numpy.array([[1, 2, 3], [4, 5, 6]], 'f'))
66 t_z.append(numpy.array([[1, 2, 3],[4, 5, 6]], 'd'))
67 t_z.append(numpy.array([[1, 2, 3, 400],[4, 5, 6, 700]],
68 'd'))
69 t_z.append(numpy.array([range(9),range(10,19)], 'f'))
70
71 # Test if arrays with number of components not in [1,2,3,4,9] work.
72 t_z.append(numpy.array([[1, 2, 3, 400, 5000],
73 [4, 5, 6, 700, 8000]], 'd'))
74 t_z.append(numpy.array([range(10), range(10,20)], 'd'))
75
76 for z in t_z:
77 vtk_arr = numpy_to_vtk(z)
78 # Test for memory leaks.
79 self.assertEqual(vtk_arr.GetReferenceCount(), 1)
80 self._check_arrays(z, vtk_arr)
81 z1 = vtk_to_numpy(vtk_arr)
82 if len(z.shape) == 1:
83 self.assertEqual(len(z1.shape), 1)
84 self.assertEqual(sum(numpy.ravel(z) - numpy.ravel(z1)), 0)
85
86 vtk_arr = vtkBitArray()
87 bits = [0, 1, 0, 1, 1, 1, 0, 0, 1, 0]
88 for bit in bits:
89 vtk_arr.InsertNextValue(bit)
90
91 res = vtk_to_numpy(vtk_arr)
92 self._check_arrays(res, vtk_arr)
93 vtk_arr = numpy_to_vtk(res)
94 self._check_arrays(res, vtk_arr)
95
96 def testNumpyView(self):
97 "Test if the numpy and VTK array share the same data."

Callers

nothing calls this directly

Calls 9

_check_arraysMethod · 0.95
numpy_to_vtkFunction · 0.90
vtk_to_numpyFunction · 0.90
arrayMethod · 0.80
rangeClass · 0.50
sumFunction · 0.50
vtkBitArrayFunction · 0.50
appendMethod · 0.45
InsertNextValueMethod · 0.45

Tested by

no test coverage detected