Skip to content

Commit

Permalink
Changes for numpy 2.0 to fix test matrix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Jun 17, 2024
1 parent cdb6be2 commit d487b32
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 14 deletions.
5 changes: 5 additions & 0 deletions doc/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Latest Changes:
For editable installs, ``python setup.py --enable-tracing develop``
must now be done with ``python setup.py develop --enable-tracing``.

- Update for tests for numpy 2.0.

- Support of np.float16 conversion with arrays.


- **1.5.0 - 2023-04-03**

- Support for Python 3.12
Expand Down
76 changes: 76 additions & 0 deletions native/common/jp_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,61 @@
See NOTICE file for details.
*****************************************************************************/
#include "jpype.h"
#include <math.h>
#include <bitset>

namespace
{

template <jvalue func(void *c) >
class Half
{
public:
static jvalue convert(void* c)
{
uint16_t i = *(uint16_t*) c;
uint32_t sign = (i&0x8000)>>15;
uint32_t man = (i&0x7C00)>>10;
uint32_t frac = (i&0x03ff);
uint32_t k = sign<<31;

if (man == 0)
{
// subnormal numbers
if (frac != 0)
{
frac = frac | (frac >> 1);
frac = frac | (frac >> 2);
frac = frac | (frac >> 4);
frac = frac | (frac >> 8);
int zeros = std::bitset<32>(~frac).count();
man = 127-zeros+7;
man <<= 23;
frac <<= zeros-8;
frac &= 0x7fffff;
k |= man | frac;
}
}
else if (man < 31)
{
// normal numbers
man = man-15+127;
man <<= 23;
frac <<= 13;
k |= man | frac;
}
else
{
// to infinity and beyond!
if (frac == 0)
k |= 0x7f800000;
else
k |= 0x7f800001 | ((frac&0x200)<<12);
}
return func(&k);
}
};

template <class T>
class Convert
{
Expand Down Expand Up @@ -385,6 +436,31 @@ jconverter getConverter(const char* from, int itemsize, const char* to)
case 'd': return &Convert<double>::toD;
}
break;
case 'e':
if (reverse) switch (to[0])
{
case 'z': return &Reverse<Half<Convert<float>::toZ>::convert>::call4;
case 'b': return &Reverse<Half<Convert<float>::toB>::convert>::call4;
case 'c': return &Reverse<Half<Convert<float>::toC>::convert>::call4;
case 's': return &Reverse<Half<Convert<float>::toS>::convert>::call4;
case 'i': return &Reverse<Half<Convert<float>::toI>::convert>::call4;
case 'j': return &Reverse<Half<Convert<float>::toJ>::convert>::call4;
case 'f': return &Reverse<Half<Convert<float>::toF>::convert>::call4;
case 'd': return &Reverse<Half<Convert<float>::toD>::convert>::call4;

Check warning on line 449 in native/common/jp_convert.cpp

View check run for this annotation

Codecov / codecov/patch

native/common/jp_convert.cpp#L442-L449

Added lines #L442 - L449 were not covered by tests
}
else switch (to[0])
{
case 'z': return &Half<Convert<float>::toZ>::convert;
case 'b': return &Half<Convert<float>::toB>::convert;
case 'c': return &Half<Convert<float>::toC>::convert;
case 's': return &Half<Convert<float>::toS>::convert;
case 'i': return &Half<Convert<float>::toI>::convert;
case 'j': return &Half<Convert<float>::toJ>::convert;

Check warning on line 458 in native/common/jp_convert.cpp

View check run for this annotation

Codecov / codecov/patch

native/common/jp_convert.cpp#L453-L458

Added lines #L453 - L458 were not covered by tests
case 'f': return &Half<Convert<float>::toF>::convert;
case 'd': return &Half<Convert<float>::toD>::convert;
}
break;

Check warning on line 462 in native/common/jp_convert.cpp

View check run for this annotation

Codecov / codecov/patch

native/common/jp_convert.cpp#L462

Added line #L462 was not covered by tests

case 'n':
if (reverse) switch (to[0])
{
Expand Down
4 changes: 2 additions & 2 deletions test/jpypetest/test_conversionInt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def testIntFromFloat(self):
self.Test.callInt(float(2))

@common.unittest.skipUnless(haveNumpy(), "numpy not available")
def testIntFromNPFloat(self):
def testIntFromNPFloat16(self):
import numpy as np
with self.assertRaises(TypeError):
self.Test.callInt(np.float_(2))
self.Test.callInt(np.float16(2))

@common.unittest.skipUnless(haveNumpy(), "numpy not available")
def testIntFromNPFloat32(self):
Expand Down
4 changes: 2 additions & 2 deletions test/jpypetest/test_conversionLong.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def testLongFromFloat(self):
self.Test.callLong(float(2))

@common.unittest.skipUnless(haveNumpy(), "numpy not available")
def testLongFromNPFloat(self):
def testLongFromNPFloat16(self):
import numpy as np
with self.assertRaises(TypeError):
self.Test.callLong(np.float_(2))
self.Test.callLong(np.float16(2))

@common.unittest.skipUnless(haveNumpy(), "numpy not available")
def testLongFromNPFloat32(self):
Expand Down
4 changes: 2 additions & 2 deletions test/jpypetest/test_conversionShort.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def testShortFromFloat(self):
self.Test.callShort(float(2))

@common.unittest.skipUnless(haveNumpy(), "numpy not available")
def testShortFromNPFloat(self):
def testShortFromNPFloat16(self):
import numpy as np
with self.assertRaises(TypeError):
self.Test.callShort(np.float_(2))
self.Test.callShort(np.float16(2))

@common.unittest.skipUnless(haveNumpy(), "numpy not available")
def testShortFromNPFloat32(self):
Expand Down
4 changes: 2 additions & 2 deletions test/jpypetest/test_jboolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def testBooleanFromFloat(self):
self.Test.callBoolean(float(2))

@common.requireNumpy
def testBooleanFromNPFloat(self):
def testBooleanFromNPFloat16(self):
import numpy as np
with self.assertRaises(TypeError):
self.Test.callBoolean(np.float_(2))
self.Test.callBoolean(np.float16(2))

@common.requireNumpy
def testBooleanFromNPFloat32(self):
Expand Down
4 changes: 2 additions & 2 deletions test/jpypetest/test_jbyte.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def testByteFromFloat(self):
self.fixture.callByte(float(2))

@common.requireNumpy
def testByteFromNPFloat(self):
def testByteFromNPFloat16(self):
import numpy as np
with self.assertRaises(TypeError):
self.fixture.callByte(np.float_(2))
self.fixture.callByte(np.float16(2))

@common.requireNumpy
def testByteFromNPFloat32(self):
Expand Down
16 changes: 14 additions & 2 deletions test/jpypetest/test_jdouble.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ def testArraySetFromNPDouble(self):
self.assertElementsAlmostEqual(a, jarr)

@common.requireNumpy
def testArrayInitFromNPFloat(self):
a = np.random.random(100).astype(np.float_)
def testArrayInitFromNPFloat16(self):
a = np.random.random(100).astype(np.float16)
jarr = JArray(JDouble)(a)
self.assertElementsAlmostEqual(a, jarr)

Expand Down Expand Up @@ -436,3 +436,15 @@ def __len__(self):

def testCastBoolean(self):
self.assertEqual(JDouble._canConvertToJava(JBoolean(True)), "none")

@common.requireNumpy
def testNPFloat16(self):
v= [0.000000e+00, 5.960464e-08, 1.788139e-07, 1.788139e-07, 4.172325e-07, 8.940697e-07, 1.847744e-06, 3.755093e-06, 7.569790e-06, 1.519918e-05, 3.045797e-05, 6.097555e-05, 6.103516e-05, 3.332520e-01, 1.000000e+00, 6.550400e+04, np.inf, -np.inf]
a = np.array(v, dtype=np.float16)
jarr = JArray(JDouble)(a)
for v1,v2 in zip(a, jarr):
self.assertEqual(v1,v2)
a = np.array([np.nan], dtype=np.float16)
jarr = JArray(JDouble)(a)
self.assertTrue(np.isnan(jarr[0]))

16 changes: 14 additions & 2 deletions test/jpypetest/test_jfloat.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ def testArraySetFromNPDouble(self):
self.assertElementsAlmostEqual(a, jarr)

@common.requireNumpy
def testArrayInitFromNPFloat(self):
a = np.random.random(100).astype(np.float_)
def testArrayInitFromNPFloat16(self):
a = np.random.random(100).astype(np.float16)
jarr = JArray(JFloat)(a)
self.assertElementsAlmostEqual(a, jarr)

Expand Down Expand Up @@ -441,3 +441,15 @@ def __len__(self):
ja[:] = [1, 2, 3]
with self.assertRaisesRegex(ValueError, "mismatch"):
ja[:] = a

@common.requireNumpy
def testNPFloat16(self):
v= [0.000000e+00, 5.960464e-08, 1.788139e-07, 1.788139e-07, 4.172325e-07, 8.940697e-07, 1.847744e-06, 3.755093e-06, 7.569790e-06, 1.519918e-05, 3.045797e-05, 6.097555e-05, 6.103516e-05, 3.332520e-01, 1.000000e+00, 6.550400e+04, np.inf, -np.inf]
a = np.array(v, dtype=np.float16)
jarr = JArray(JFloat)(a)
for v1,v2 in zip(a, jarr):
self.assertEqual(v1,v2)
a = np.array([np.nan], dtype=np.float16)
jarr = JArray(JFloat)(a)
self.assertTrue(np.isnan(jarr[0]))

0 comments on commit d487b32

Please sign in to comment.