Skip to content

Commit

Permalink
Update Pyomo for NumPy 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmundt committed Jun 17, 2024
1 parent 7bbe3a7 commit 1e7fabd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
10 changes: 8 additions & 2 deletions pyomo/common/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,10 +999,13 @@ def _finalize_numpy(np, available):
# registration here (to bypass the deprecation warning) until we
# finally remove all support for it
numeric_types._native_boolean_types.add(t)
_floats = [np.float_, np.float16, np.float32, np.float64]
_floats = [np.float16, np.float32, np.float64]
# float96 and float128 may or may not be defined in this particular
# numpy build (it depends on platform and version).
# Register them only if they are present
if hasattr(np, 'float_'):
# Prepend to preserve previous functionality
_floats = [np.float_] + _floats
if hasattr(np, 'float96'):
_floats.append(np.float96)
if hasattr(np, 'float128'):
Expand All @@ -1013,10 +1016,13 @@ def _finalize_numpy(np, available):
# registration here (to bypass the deprecation warning) until we
# finally remove all support for it
numeric_types._native_boolean_types.add(t)
_complex = [np.complex_, np.complex64, np.complex128]
_complex = [np.complex64, np.complex128]
# complex192 and complex256 may or may not be defined in this
# particular numpy build (it depends on platform and version).
# Register them only if they are present
if hasattr(np, 'np.complex_'):
# Prepend to preserve functionality
_complex = [np.complex_] + _complex
if hasattr(np, 'complex192'):
_complex.append(np.complex192)
if hasattr(np, 'complex256'):
Expand Down
10 changes: 6 additions & 4 deletions pyomo/core/kernel/register_numpy_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
# Historically, the lists included several numpy aliases
numpy_int_names.extend(('int_', 'intc', 'intp'))
numpy_int.extend((numpy.int_, numpy.intc, numpy.intp))
numpy_float_names.append('float_')
numpy_float.append(numpy.float_)
numpy_complex_names.append('complex_')
numpy_complex.append(numpy.complex_)
if hasattr(numpy, 'float_'):
numpy_float_names.append('float_')
numpy_float.append(numpy.float_)
if hasattr(numpy, 'complex_'):
numpy_complex_names.append('complex_')
numpy_complex.append(numpy.complex_)

# Re-build the old numpy_* lists
for t in native_boolean_types:
Expand Down
6 changes: 4 additions & 2 deletions pyomo/core/tests/unit/test_kernel_register_numpy_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
# Reals
numpy_float_names = []
if numpy_available:
numpy_float_names.append('float_')
if hasattr(numpy, 'float_'):
numpy_float_names.append('float_')
numpy_float_names.append('float16')
numpy_float_names.append('float32')
numpy_float_names.append('float64')
Expand All @@ -46,7 +47,8 @@
# Complex
numpy_complex_names = []
if numpy_available:
numpy_complex_names.append('complex_')
if hasattr(numpy, 'complex_'):
numpy_complex_names.append('complex_')
numpy_complex_names.append('complex64')
numpy_complex_names.append('complex128')
if hasattr(numpy, 'complex192'):
Expand Down
8 changes: 4 additions & 4 deletions pyomo/core/tests/unit/test_numvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,10 @@ def test_unknownNumericType(self):

@unittest.skipUnless(numpy_available, "This test requires NumPy")
def test_numpy_basic_float_registration(self):
self.assertIn(numpy.float_, native_numeric_types)
self.assertNotIn(numpy.float_, native_integer_types)
self.assertIn(numpy.float_, _native_boolean_types)
self.assertIn(numpy.float_, native_types)
self.assertIn(numpy.float64, native_numeric_types)
self.assertNotIn(numpy.float64, native_integer_types)
self.assertIn(numpy.float64, _native_boolean_types)
self.assertIn(numpy.float64, native_types)

@unittest.skipUnless(numpy_available, "This test requires NumPy")
def test_numpy_basic_int_registration(self):
Expand Down
12 changes: 6 additions & 6 deletions pyomo/core/tests/unit/test_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ def setUp(self):
self.instance = self.model.create_instance(currdir + "setA.dat")
self.e1 = numpy.bool_(1)
self.e2 = numpy.int_(2)
self.e3 = numpy.float_(3.0)
self.e3 = numpy.float64(3.0)
self.e4 = numpy.int_(4)
self.e5 = numpy.int_(5)
self.e6 = numpy.int_(6)
Expand All @@ -1068,7 +1068,7 @@ def test_numpy_int(self):

def test_numpy_float(self):
model = ConcreteModel()
model.A = Set(initialize=[numpy.float_(1.0), numpy.float_(0.0)])
model.A = Set(initialize=[numpy.float64(1.0), numpy.float64(0.0)])
self.assertEqual(model.A.bounds(), (0, 1))


Expand Down Expand Up @@ -3213,7 +3213,7 @@ def test_numpy_membership(self):
self.assertEqual(numpy.int_(1) in Boolean, True)
self.assertEqual(numpy.bool_(True) in Boolean, True)
self.assertEqual(numpy.bool_(False) in Boolean, True)
self.assertEqual(numpy.float_(1.1) in Boolean, False)
self.assertEqual(numpy.float64(1.1) in Boolean, False)
self.assertEqual(numpy.int_(2) in Boolean, False)

self.assertEqual(numpy.int_(0) in Integers, True)
Expand All @@ -3222,7 +3222,7 @@ def test_numpy_membership(self):
# identically to 1
self.assertEqual(numpy.bool_(True) in Integers, True)
self.assertEqual(numpy.bool_(False) in Integers, True)
self.assertEqual(numpy.float_(1.1) in Integers, False)
self.assertEqual(numpy.float64(1.1) in Integers, False)
self.assertEqual(numpy.int_(2) in Integers, True)

self.assertEqual(numpy.int_(0) in Reals, True)
Expand All @@ -3231,14 +3231,14 @@ def test_numpy_membership(self):
# identically to 1
self.assertEqual(numpy.bool_(True) in Reals, True)
self.assertEqual(numpy.bool_(False) in Reals, True)
self.assertEqual(numpy.float_(1.1) in Reals, True)
self.assertEqual(numpy.float64(1.1) in Reals, True)
self.assertEqual(numpy.int_(2) in Reals, True)

self.assertEqual(numpy.int_(0) in Any, True)
self.assertEqual(numpy.int_(1) in Any, True)
self.assertEqual(numpy.bool_(True) in Any, True)
self.assertEqual(numpy.bool_(False) in Any, True)
self.assertEqual(numpy.float_(1.1) in Any, True)
self.assertEqual(numpy.float64(1.1) in Any, True)
self.assertEqual(numpy.int_(2) in Any, True)

def test_setargs1(self):
Expand Down

0 comments on commit 1e7fabd

Please sign in to comment.