Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for numpy 2.0 to fix test matrix. #1196

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) >
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a crude implementation of the "half" floating type. There is no native type in Java to support this but it is possible that someone could pass a half floating in for promotion to either float or double. This code is exercised in the test suite.

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 @@
case 'd': return &Convert<double>::toD;
}
break;
case 'e':
if (reverse) switch (to[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if there is every a case in which a reversed byte order half floating value would be converted. Through the order of this operation is reasonable to define. Reverse the bytes, promote to float then convert to Java type.

{
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
31 changes: 16 additions & 15 deletions test/jpypetest/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ def testMemoryByte(self):
jtype = jpype.JByte[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -328,11 +328,12 @@ def testMemoryInt(self):
jtype = jpype.JInt[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
print(dtype)
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -341,11 +342,11 @@ def testMemoryShort(self):
jtype = jpype.JShort[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -354,11 +355,11 @@ def testMemoryLong(self):
jtype = jpype.JLong[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -367,11 +368,11 @@ def testMemoryFloat(self):
jtype = jpype.JFloat[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -380,11 +381,11 @@ def testMemoryDouble(self):
jtype = jpype.JDouble[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -393,11 +394,11 @@ def testMemoryBoolean(self):
jtype = jpype.JBoolean[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d", "n", "N"):
for dtype in "c?bBhHiIlLqQfdnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

for dtype in ("s", "p", "P", "e"):
for dtype in "spP":
with self.assertRaises(Exception):
jtype(mv.cast(dtype))

Expand All @@ -406,7 +407,7 @@ def testMemoryChar(self):
jtype = jpype.JChar[:]

# Simple checks
for dtype in ("c", "?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "n", "N"):
for dtype in "c?bBhHiIlLqQnN":
jtype(mv.cast(dtype))
jtype(mv.cast("@" + dtype))

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]))

Loading