Permalink
Browse files

Merge pull request #327 from Thrameos/numpy_array

Numpy array
  • Loading branch information...
marscher committed Nov 4, 2018
2 parents ac0e179 + 6af8047 commit 7802a538d61b1c0c925d0309356c4af230c7b043
Showing with 56 additions and 2 deletions.
  1. +7 −1 jpype/_jarray.py
  2. +49 −1 test/jpypetest/array.py
@@ -90,6 +90,12 @@ def __setattr__(self, attr, value):
else:
raise AttributeError("%s does not have field %s"%(self.__name__, attr), self)
def _isIterable(obj):
if isinstance(obj, collections.Sequence):
return True
if hasattr(obj,'__len__') and hasattr(obj,'__iter__'):
return True
return False
def _jarrayInit(self, *args):
if len(args) == 2 and args[0] == _jclass._SPECIAL_CONSTRUCTOR_KEY:
@@ -100,7 +106,7 @@ def _jarrayInit(self, *args):
.format(len(args) + 1))
else:
values = None
if isinstance(args[0], collections.Sequence):
if _isIterable(args[0]):
sz = len(args[0])
values = args[0]
else:
@@ -286,6 +286,55 @@ def testSetFromNPDoubleArray(self):
jarr[:] = a
self.assertCountEqual(a, jarr)
@unittest.skipUnless(haveNumpy(), "numpy not available")
def testInitFromNPIntArray(self):
import numpy as np
n = 100
a = np.random.random(n).astype(np.int)
jarr = jpype.JArray(jpype.JInt)(a)
self.assertCountEqual(a, jarr)
@unittest.skipUnless(haveNumpy(), "numpy not available")
def testInitFromNPDoubleArray(self):
import numpy as np
n = 100
a = np.random.random(n).astype(np.float)
jarr = jpype.JArray(jpype.JDouble)(a)
self.assertCountEqual(a, jarr)
@unittest.skipUnless(haveNumpy(), "numpy not available")
def testInitFromNPDoubleArrayFloat32(self):
import numpy as np
n = 100
a = np.random.random(n).astype(np.float32)
jarr = jpype.JArray(jpype.JDouble)(a)
self.assertCountEqual(a, jarr)
@unittest.skipUnless(haveNumpy(), "numpy not available")
def testInitFromNPDoubleArrayFloat64(self):
import numpy as np
n = 100
a = np.random.random(n).astype(np.float64)
jarr = jpype.JArray(jpype.JDouble)(a)
self.assertCountEqual(a, jarr)
@unittest.skipUnless(haveNumpy(), "numpy not available")
def testInitFromNPFloatArrayInt(self):
import numpy as np
a = np.array([1,2,3],dtype=np.int32)
jarr = jpype.JArray(jpype.JFloat)(a)
print(a)
print(jarr)
self.assertCountEqual(a, jarr)
@unittest.skipUnless(haveNumpy(), "numpy not available")
def testSetFromNPFloatArrayInt(self):
import numpy as np
a = np.array([1,2,3],np.int32)
jarr = jpype.JArray(jpype.JFloat)(len(a))
jarr[:] = a
self.assertCountEqual(a, jarr)
def testArrayCtor1(self):
jobject = jpype.JClass('java.lang.Object')
jarray = jpype.JArray(jobject)
@@ -303,4 +352,3 @@ def testArrayCtor3(self):
def testArrayCtor4(self):
jarray = jpype.JArray(jpype.JObject)
self.assertTrue( isinstance(jarray, jpype._jarray._JavaArray))

0 comments on commit 7802a53

Please sign in to comment.