summaryrefslogtreecommitdiff
path: root/tests/py_test.py
diff options
context:
space:
mode:
authorKevin Rose <kevin+gh@maypark.com>2017-08-01 10:34:00 -0500
committerWouter van Oortmerssen <aardappel@gmail.com>2017-08-01 08:34:00 -0700
commit3282a84e3068d2ff0ded5a683ceea0806da21ed6 (patch)
tree5ca756f0045bad32e24a6130dfd1985ec86ee9a2 /tests/py_test.py
parent89a68942acdeeb51ceb102d19153dcc23ba8c0dd (diff)
downloadflatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.tar.gz
flatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.tar.bz2
flatbuffers-3282a84e3068d2ff0ded5a683ceea0806da21ed6.zip
[Python] (scalar) vector reading speedup via numpy (#4390)
* Add numpy accessor to python flatbuffers scalar vectors * Update python tests to test numpy vector accessor * Update appveyor CI to run Python tests, save generated code as artifact * Update example generated python code * Add numpy info to python usage docs * Update test schema and python tests w/ multi-byte vector * did not mean to push profiling code * adding float64 numpy tests
Diffstat (limited to 'tests/py_test.py')
-rw-r--r--tests/py_test.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/py_test.py b/tests/py_test.py
index e6609449..b543d2d1 100644
--- a/tests/py_test.py
+++ b/tests/py_test.py
@@ -15,6 +15,7 @@
import os.path
import sys
+import imp
PY_VERSION = sys.version_info[:2]
import ctypes
@@ -25,6 +26,7 @@ import unittest
from flatbuffers import compat
from flatbuffers.compat import range_func as compat_range
+from flatbuffers.compat import NumpyRequiredForThisFeature
import flatbuffers
from flatbuffers import number_types as N
@@ -130,6 +132,40 @@ def CheckReadBuffer(buf, offset):
invsum += int(v)
asserter(invsum == 10)
+ for i in range(5):
+ asserter(monster.VectorOfLongs(i) == 10 ** (i * 2))
+
+ asserter(([-1.7976931348623157e+308, 0, 1.7976931348623157e+308]
+ == [monster.VectorOfDoubles(i)
+ for i in range(monster.VectorOfDoublesLength())]))
+
+ try:
+ imp.find_module('numpy')
+ # if numpy exists, then we should be able to get the
+ # vector as a numpy array
+ import numpy as np
+
+ asserter(monster.InventoryAsNumpy().sum() == 10)
+ asserter(monster.InventoryAsNumpy().dtype == np.dtype('uint8'))
+
+ VectorOfLongs = monster.VectorOfLongsAsNumpy()
+ asserter(VectorOfLongs.dtype == np.dtype('int64'))
+ for i in range(5):
+ asserter(VectorOfLongs[i] == 10 ** (i * 2))
+
+ VectorOfDoubles = monster.VectorOfDoublesAsNumpy()
+ asserter(VectorOfDoubles.dtype == np.dtype('float64'))
+ asserter(VectorOfDoubles[0] == np.finfo('float64').min)
+ asserter(VectorOfDoubles[1] == 0.0)
+ asserter(VectorOfDoubles[2] == np.finfo('float64').max)
+
+ except ImportError:
+ # If numpy does not exist, trying to get vector as numpy
+ # array should raise NumpyRequiredForThisFeature. The way
+ # assertRaises has been implemented prevents us from
+ # asserting this error is raised outside of a test case.
+ pass
+
asserter(monster.Test4Length() == 2)
# create a 'Test' object and populate it:
@@ -794,6 +830,20 @@ def make_monster_from_generated_code():
b.PrependUOffsetTRelative(test1)
testArrayOfString = b.EndVector(2)
+ MyGame.Example.Monster.MonsterStartVectorOfLongsVector(b, 5)
+ b.PrependInt64(100000000)
+ b.PrependInt64(1000000)
+ b.PrependInt64(10000)
+ b.PrependInt64(100)
+ b.PrependInt64(1)
+ VectorOfLongs = b.EndVector(5)
+
+ MyGame.Example.Monster.MonsterStartVectorOfDoublesVector(b, 3)
+ b.PrependFloat64(1.7976931348623157e+308)
+ b.PrependFloat64(0)
+ b.PrependFloat64(-1.7976931348623157e+308)
+ VectorOfDoubles = b.EndVector(3)
+
MyGame.Example.Monster.MonsterStart(b)
pos = MyGame.Example.Vec3.CreateVec3(b, 1.0, 2.0, 3.0, 3.0, 2, 5, 6)
@@ -806,6 +856,8 @@ def make_monster_from_generated_code():
MyGame.Example.Monster.MonsterAddTest(b, mon2)
MyGame.Example.Monster.MonsterAddTest4(b, test4)
MyGame.Example.Monster.MonsterAddTestarrayofstring(b, testArrayOfString)
+ MyGame.Example.Monster.MonsterAddVectorOfLongs(b, VectorOfLongs)
+ MyGame.Example.Monster.MonsterAddVectorOfDoubles(b, VectorOfDoubles)
mon = MyGame.Example.Monster.MonsterEnd(b)
b.Finish(mon)
@@ -962,6 +1014,15 @@ class TestAllCodePathsOfExampleSchema(unittest.TestCase):
self.assertEqual(0, mon2.Testnestedflatbuffer(0))
self.assertEqual(2, mon2.Testnestedflatbuffer(1))
self.assertEqual(4, mon2.Testnestedflatbuffer(2))
+ try:
+ imp.find_module('numpy')
+ # if numpy exists, then we should be able to get the
+ # vector as a numpy array
+ self.assertEqual([0, 2, 4], mon2.TestnestedflatbufferAsNumpy().tolist())
+ except ImportError:
+ assertRaises(self,
+ lambda: mon2.TestnestedflatbufferAsNumpy(),
+ NumpyRequiredForThisFeature)
def test_nondefault_monster_testempty(self):
b = flatbuffers.Builder(0)