|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import numbers |
15 | 16 | import unittest |
16 | 17 |
|
17 | 18 | import numpy as np |
@@ -47,3 +48,102 @@ def test_functional_3x3triu(self): |
47 | 48 | new = deserialize_ndarray(obj) |
48 | 49 | np.testing.assert_array_equal(arr, new) |
49 | 50 | self.assertEqual(arr.dtype, new.dtype) |
| 51 | + |
| 52 | + def test_replacing_floats_with_ints(self): |
| 53 | + |
| 54 | + floating_dtypes = [np.float16, np.float32, np.float64] |
| 55 | + |
| 56 | + if int(np.__version__.split(".")[1]) >= 22: |
| 57 | + # Numpy<1.22.0 didn't support `is_integer()` on floating types |
| 58 | + # so float128 etc don't work out-of-the-box because `tolist()` |
| 59 | + # doesn't convert those to Python float. |
| 60 | + floating_dtypes.append(np.longdouble) |
| 61 | + |
| 62 | + for dtype in floating_dtypes: |
| 63 | + with self.subTest(f"{dtype}, all integer"): |
| 64 | + arr = np.ones(3, dtype=dtype) |
| 65 | + arr[0] = 2 |
| 66 | + arr[1] = -0.0 |
| 67 | + |
| 68 | + obj = serialize_ndarray(arr) |
| 69 | + |
| 70 | + # test the round trip |
| 71 | + new = deserialize_ndarray(obj) |
| 72 | + np.testing.assert_array_equal(arr, new) |
| 73 | + self.assertEqual(arr.dtype, new.dtype) # original vartype is restored |
| 74 | + |
| 75 | + # test the ones that can be are mapped to int |
| 76 | + self.assertIsInstance(obj["data"][0], int) |
| 77 | + self.assertIsInstance(obj["data"][1], int) |
| 78 | + self.assertIsInstance(obj["data"][2], int) |
| 79 | + |
| 80 | + with self.subTest(f"{dtype}, all float"): |
| 81 | + arr = np.empty(3, dtype=dtype) |
| 82 | + arr[0] = 1.5 |
| 83 | + arr[1] = float("inf") |
| 84 | + arr[2] = float("nan") |
| 85 | + |
| 86 | + obj = serialize_ndarray(arr) |
| 87 | + |
| 88 | + # test the round trip |
| 89 | + new = deserialize_ndarray(obj) |
| 90 | + np.testing.assert_array_equal(arr, new) |
| 91 | + self.assertEqual(arr.dtype, new.dtype) # original vartype is restored |
| 92 | + |
| 93 | + # test the ones that can be are mapped to int |
| 94 | + self.assertIsInstance(obj["data"][0], numbers.Real) |
| 95 | + self.assertIsInstance(obj["data"][1], numbers.Real) |
| 96 | + self.assertIsInstance(obj["data"][2], numbers.Real) |
| 97 | + |
| 98 | + with self.subTest(f"{dtype}, mixed"): |
| 99 | + arr = np.ones(3, dtype=dtype) |
| 100 | + arr[0] = 1.5 |
| 101 | + arr[1] = -0.0 |
| 102 | + |
| 103 | + obj = serialize_ndarray(arr) |
| 104 | + |
| 105 | + # test the round trip |
| 106 | + new = deserialize_ndarray(obj) |
| 107 | + np.testing.assert_array_equal(arr, new) |
| 108 | + self.assertEqual(arr.dtype, new.dtype) # original vartype is restored |
| 109 | + |
| 110 | + # test the ones that can be are mapped to int |
| 111 | + self.assertIsInstance(obj["data"][0], numbers.Real) |
| 112 | + self.assertIsInstance(obj["data"][1], int) |
| 113 | + self.assertIsInstance(obj["data"][2], int) |
| 114 | + |
| 115 | + with self.subTest("complex, mixed"): |
| 116 | + arr = np.ones(3, dtype=complex) |
| 117 | + arr[0] = 1.5 |
| 118 | + arr[1] = -0.0 |
| 119 | + |
| 120 | + obj = serialize_ndarray(arr) |
| 121 | + |
| 122 | + # test the round trip |
| 123 | + new = deserialize_ndarray(obj) |
| 124 | + np.testing.assert_array_equal(arr, new) |
| 125 | + self.assertEqual(arr.dtype, new.dtype) |
| 126 | + |
| 127 | + # in this case everything is kept as a complex number |
| 128 | + self.assertIsInstance(obj["data"][0], complex) |
| 129 | + self.assertIsInstance(obj["data"][1], complex) |
| 130 | + self.assertIsInstance(obj["data"][2], complex) |
| 131 | + |
| 132 | + for dtype in [np.int8, np.int16, np.int32, np.int64]: |
| 133 | + with self.subTest(dtype): |
| 134 | + arr = np.empty(3, dtype=dtype) |
| 135 | + arr[0] = 2 |
| 136 | + arr[1] = 0 |
| 137 | + arr[2] = -1 |
| 138 | + |
| 139 | + obj = serialize_ndarray(arr) |
| 140 | + |
| 141 | + # test the round trip |
| 142 | + new = deserialize_ndarray(obj) |
| 143 | + np.testing.assert_array_equal(arr, new) |
| 144 | + self.assertEqual(arr.dtype, new.dtype) # original vartype is restored |
| 145 | + |
| 146 | + # test the ones that can be are mapped to int |
| 147 | + self.assertIsInstance(obj["data"][0], int) |
| 148 | + self.assertIsInstance(obj["data"][1], int) |
| 149 | + self.assertIsInstance(obj["data"][2], int) |
0 commit comments