Debian Codegraph @

src/python/PyImathNumpyTest/pyImathNumpyTest.in

raw
1 #!@PYTHON@
2
3 #
4 # SPDX-License-Identifier: BSD-3-Clause
5 # Copyright Contributors to the OpenEXR Project.
6 #
7
8 from imath import *
9
10 import imathnumpy
11 import numpy as np
12
13 testList = []
14
15 def numpyType(ia):
16 podTypeDict = {
17 SignedCharArray : np.int8,
18 UnsignedCharArray : np.uint8,
19 ShortArray : np.int16,
20 UnsignedShortArray : np.uint16,
21 IntArray : np.int32,
22 UnsignedIntArray : np.uint32,
23 FloatArray : np.float32,
24 DoubleArray : np.float64,
25 V2sArray : np.int16,
26 V2iArray : np.int32,
27 V2fArray : np.float32,
28 V2dArray : np.float64,
29 V3sArray : np.int16,
30 V3iArray : np.int32,
31 V3fArray : np.float32,
32 V3dArray : np.float64,
33 V4sArray : np.int16,
34 V4iArray : np.int32,
35 V4fArray : np.float32,
36 V4dArray : np.float64,
37 C3cArray : np.uint8,
38 C3fArray : np.float32,
39 C4cArray : np.uint8,
40 C4fArray : np.float32,
41 IntArray2D : np.int32,
42 FloatArray2D : np.float32,
43 DoubleArray2D : np.float64,
44 Color4cArray2D : np.uint8,
45 Color4fArray2D : np.float32,
46 }
47
48 return podTypeDict[type(ia)]
49
50 def baseTypeSize(ia):
51 baseTypeSizeDict = {
52 SignedCharArray : 1,
53 UnsignedCharArray : 1,
54 ShortArray : 1,
55 UnsignedShortArray : 1,
56 IntArray : 1,
57 UnsignedIntArray : 1,
58 FloatArray : 1,
59 DoubleArray : 1,
60 V2sArray : 2,
61 V2iArray : 2,
62 V2fArray : 2,
63 V2dArray : 2,
64 V3sArray : 3,
65 V3iArray : 3,
66 V3fArray : 3,
67 V3dArray : 3,
68 V4sArray : 4,
69 V4iArray : 4,
70 V4fArray : 4,
71 V4dArray : 4,
72 C3cArray : 3,
73 C3fArray : 3,
74 C4cArray : 4,
75 C4fArray : 4,
76 Color4cArray2D : 4,
77 Color4fArray2D : 4
78 }
79
80 return baseTypeSizeDict[type(ia)]
81
82
83 ###############################################################################
84
85 def testScalarArrayToNumpy():
86 '''
87 Tests the wrapping of PyImath scalar array types to numpy arrays.
88 '''
89 aDim = 20
90
91 # Test all supported array types
92 arrayTestTypes = [
93 SignedCharArray,
94 UnsignedCharArray,
95 ShortArray,
96 UnsignedShortArray,
97 IntArray,
98 UnsignedIntArray,
99 FloatArray,
100 DoubleArray]
101
102 for arrayType in arrayTestTypes:
103 print("\nTesting: %s conversion to numpy" % arrayType.__name__)
104
105 ia = arrayType(aDim)
106 refType = numpyType(ia)
107 refShape = (aDim,)
108
109 for i in range(aDim):
110 ia[i] = i
111 na = imathnumpy.arrayToNumpy(ia)
112
113 print("%s == %s" % (str(na.dtype), str(refType)))
114 assert(na.dtype == refType)
115
116 print("%s == %s" % (str(na.shape), str(refShape)))
117 assert(na.shape == refShape)
118
119 for i in range(aDim):
120 assert(na[i] == ia[i])
121 print(na)
122
123 testList.append(testScalarArrayToNumpy)
124
125 ###############################################################################
126
127 def testVectorArrayToNumpy():
128 '''
129 Tests the wrapping of PyImath vector array types to numpy arrays.
130 '''
131 aDim = 20
132
133 # Test all supported array types
134 arrayTestTypes = [
135 V2sArray,
136 V2iArray,
137 V2fArray,
138 V2dArray,
139 V3sArray,
140 V3iArray,
141 V3fArray,
142 V3dArray,
143 V4sArray,
144 V4iArray,
145 V4fArray,
146 V4dArray,
147 C3cArray,
148 C3fArray,
149 C4cArray,
150 C4fArray]
151
152 for arrayType in arrayTestTypes:
153 print("\nTesting: %s conversion to numpy" % arrayType.__name__)
154
155 ia = arrayType(aDim)
156 refType = numpyType(ia)
157 refBaseTypeSize = baseTypeSize(ia)
158 refShape = (aDim, refBaseTypeSize)
159
160 for i in range(aDim):
161 for j in range(refBaseTypeSize):
162 ia[i][j] = i + 2 ** j
163 na = imathnumpy.arrayToNumpy(ia)
164
165 print("%s == %s" % (str(na.dtype), str(refType)))
166 assert(na.dtype == refType)
167
168 print("%s == %s" % (str(na.shape), str(refShape)))
169 assert(na.shape == refShape)
170
171 for i in range(aDim):
172 for j in range(refBaseTypeSize):
173 assert(na[i][j] == ia[i][j])
174 print(na)
175
176 testList.append(testVectorArrayToNumpy)
177
178 ###############################################################################
179
180 def testScalarArray2DToNumpy():
181 '''
182 Tests the wrapping of PyImath scalar2D array types to numpy arrays.
183 '''
184 aDim1 = 10
185 aDim2 = 5
186
187 # Test all supported array types
188 arrayTestTypes = [
189 IntArray2D,
190 FloatArray2D,
191 DoubleArray2D]
192
193 for arrayType in arrayTestTypes:
194 print("\nTesting: %s conversion to numpy" % arrayType.__name__)
195
196 ia = arrayType(aDim1, aDim2)
197 refType = numpyType(ia)
198 refShape = (aDim2, aDim1)
199
200 for y in range(aDim2):
201 for x in range(aDim1):
202 ia[(x, y)] = x + 2 * y
203 na = imathnumpy.arrayToNumpy(ia)
204
205 print("%s == %s" % (str(na.dtype), str(refType)))
206 assert(na.dtype == refType)
207
208 print("%s == %s" % (str(na.shape), str(refShape)))
209 assert(na.shape == refShape)
210
211 for y in range(aDim2):
212 for x in range(aDim1):
213 assert(na[y][x] == ia.item(x, y))
214 print(na)
215
216 testList.append(testScalarArray2DToNumpy)
217
218 ###############################################################################
219
220 def testVectorArray2DToNumpy():
221 '''
222 Tests the wrapping of PyImath vector2D array types to numpy arrays.
223 '''
224 aDim1 = 10
225 aDim2 = 5
226
227 # Test all supported array types
228 arrayTestTypes = [
229 Color4cArray2D,
230 Color4fArray2D]
231
232 for arrayType in arrayTestTypes:
233 print("\nTesting: %s conversion to numpy" % arrayType.__name__)
234
235 ia = arrayType(aDim1, aDim2)
236 refType = numpyType(ia)
237 refBaseTypeSize = baseTypeSize(ia)
238 refShape = (aDim2, aDim1, refBaseTypeSize)
239
240 for y in range(aDim2):
241 for x in range(aDim1):
242 for k in range(refBaseTypeSize):
243 ia.item(x, y)[k] = x + 2 * y + k
244 na = imathnumpy.arrayToNumpy(ia)
245
246 print("%s == %s" % (str(na.dtype), str(refType)))
247 assert(na.dtype == refType)
248
249 print("%s == %s" % (str(na.shape), str(refShape)))
250 assert(na.shape == refShape)
251
252 for y in range(aDim2):
253 for x in range(aDim1):
254 for k in range(refBaseTypeSize):
255 assert(na[y][x][k] == ia.item(x, y)[k])
256 print(na)
257
258 testList.append(testVectorArray2DToNumpy)
259
260 ###############################################################################
261
262 def testBufferProtocol():
263 '''
264 Numpy arrays and FixedArrays both now support the Python buffer protocol.
265 This test will exercise the FixedArray implementation of the protocol
266 against numpy arrays since only very limited testing of the protocol is
267 possible in PyImathTest .
268 '''
269 def verify (x, X, factor=1., shared=True):
270
271 assert (len(x) == len(X))
272 for i in range(10):
273 # Make sure each element is the proper factor of the index, this
274 # will help make sure all the indexing and striding is correct.
275 assert (X[i] == i*factor)
276 assert (X[i] == x[i])
277
278 # Buffers can be shared, so modifying the imath array should also
279 # be operating on the numpy array...
280 for i in range(10):
281 x[i] *= 10
282 for i in range(10):
283 assert ((shared and (X[i] == x[i])) or (not shared and (X[i]*10 == x[i])))
284
285
286 def verifyVector (v, V, cloned=False):
287
288 assert (len(v) == len(V))
289 for i in range(10):
290 assert (len(v[i]) == len(V[i]))
291 for j in range(len(v[i])):
292 assert (V[i][j] == v[i][j])
293
294 # Buffers should be shared, so modifying the imath array should also
295 # be operating on the numpy array...
296 for i in range(10):
297 v[i] *= 10
298 for i in range(10):
299 for j in range(len(v[i])):
300 assert ((not cloned and (V[i][j] == v[i][j])) or (cloned and (V[i][j]*10 == v[i][j])))
301
302 # Verify that imath striding is properly mapped to numpy...
303 # Note that we modified the elements by a factor of 10 above, so when
304 # we check each element against the index we need to account for this.
305 Vx = np.asarray (v.x)
306 Vy = np.asarray (v.y)
307 if cloned:
308 # Factors will have already been applied...
309 factor = 1000.
310 else:
311 factor = 10.
312 verify (v.x, Vx, factor, shared=True)
313 verify (v.y, Vy, factor*10., shared=True)
314
315 if (len(v[0]) == 3):
316 Vz = np.asarray (v.z)
317 verify (v.z, Vz, factor*10.*10., shared=True)
318
319 # .........................................................................
320
321 a = UnsignedCharArray (10)
322 for i in range(10):
323 a[i] = i
324 aNP = np.asarray (a)
325 verify (a, aNP)
326
327 a = IntArray (10)
328 for i in range(10):
329 a[i] = i
330 aNP = np.asarray (a)
331 verify (a, aNP)
332
333 # Use the BufferProtocol to construct a deepcopy of the numpy
334 # array as a FixedArray.
335 aNPclone = IntArrayFromBuffer (aNP)
336 verify (aNPclone, aNP, factor=10., shared=False)
337
338 a = FloatArray (10)
339 for i in range(10):
340 a[i] = float(i)
341 aNP = np.asarray (a)
342 verify (a, aNP)
343
344 # Use the BufferProtocol to construct a deepcopy of the numpy
345 # array as a FixedArray.
346 aNPclone = FloatArrayFromBuffer (aNP)
347 verify (aNPclone, aNP, factor=10., shared=False)
348
349 a = DoubleArray (10)
350 for i in range(10):
351 a[i] = float(i)
352 aNP = np.asarray (a)
353 verify (a, aNP)
354
355 # Use the BufferProtocol to construct a deepcopy of the numpy
356 # array as a FixedArray.
357 aNPclone = DoubleArrayFromBuffer (aNP)
358 verify (aNPclone, aNP, factor=10., shared=False)
359
360 v = V2iArray (10)
361 for i in range(10):
362 v[i] = V2i (i,i*10)
363 vNP = np.asarray (v)
364 verifyVector (v, vNP)
365
366 # Use the BufferProtocol to construct a deepcopy of the numpy
367 # array as a FixedArray.
368 vNPclone = V2iArrayFromBuffer (vNP)
369 verifyVector (vNPclone, vNP, cloned=True)
370
371 v = V2fArray (10)
372 for i in range(10):
373 v[i] = V2f (i,i*10)
374 vNP = np.asarray (v)
375 verifyVector (v, vNP)
376
377 # Use the BufferProtocol to construct a deepcopy of the numpy
378 # array as a FixedArray.
379 vNPclone = V2fArrayFromBuffer (vNP)
380 verifyVector (vNPclone, vNP, cloned=True)
381
382 v = V2dArray (10)
383 for i in range(10):
384 v[i] = V2d (i,i*10)
385 vNP = np.asarray (v)
386 verifyVector (v, vNP)
387
388 # Use the BufferProtocol to construct a deepcopy of the numpy
389 # array as a FixedArray.
390 vNPclone = V2dArrayFromBuffer (vNP)
391 verifyVector (vNPclone, vNP, cloned=True)
392
393 v = V3iArray (10)
394 for i in range(10):
395 v[i] = V3i (i,i*10,i*100)
396 vNP = np.asarray (v)
397 verifyVector (v, vNP)
398
399 # Use the BufferProtocol to construct a deepcopy of the numpy
400 # array as a FixedArray.
401 vNPclone = V3iArrayFromBuffer (vNP)
402 verifyVector (vNPclone, vNP, cloned=True)
403
404 v = V3fArray (10)
405 for i in range(10):
406 v[i] = V3f (i,i*10,i*100)
407 vNP = np.asarray (v)
408 verifyVector (v, vNP)
409
410 # Use the BufferProtocol to construct a deepcopy of the numpy
411 # array as a FixedArray.
412 vNPclone = V3fArrayFromBuffer (vNP)
413 verifyVector (vNPclone, vNP, cloned=True)
414
415 v = V3dArray (10)
416 for i in range(10):
417 v[i] = V3d (i,i*10,i*100)
418 vNP = np.asarray (v)
419 verifyVector (v, vNP)
420
421 # Use the BufferProtocol to construct a deepcopy of the numpy
422 # array as a FixedArray.
423 vNPclone = V3dArrayFromBuffer (vNP)
424 verifyVector (vNPclone, vNP, cloned=True)
425
426 testList.append (testBufferProtocol)
427
428 # -------------------------------------------------------------------------
429 # Main loop
430
431 for test in testList:
432 print("")
433 print("Running %s" % test.__name__)
434 test()
435
436 print("done.")
437
438 # Local Variables:
439 # mode:python
440 # End: