-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_json_numpy.py
235 lines (183 loc) · 8.76 KB
/
test_json_numpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from __future__ import annotations
import json
import unittest
import uuid
from io import StringIO
from typing import Any, TypeVar, cast
import numpy as np
from numpy.testing import assert_array_equal, assert_equal
import json_numpy
T = TypeVar("T")
class NumpyJsonSerializationTest(unittest.TestCase):
def setUp(self) -> None:
json_numpy.patch()
@staticmethod
def dumps_loads(x: T) -> T:
return cast(T, json.loads(json.dumps(x)))
@staticmethod
def assert_equal_with_type(
actual: np.ndarray | np.generic | list[Any] | dict[Any, Any],
desired: np.ndarray | np.generic | list[Any] | dict[Any, Any],
sort_key: Any = None,
) -> None:
if isinstance(desired, np.ndarray) and isinstance(actual, np.ndarray):
assert_array_equal(actual, desired)
assert_equal(actual.dtype, desired.dtype)
elif isinstance(desired, list) and isinstance(actual, list):
assert_array_equal(actual, desired)
assert_array_equal([type(e) for e in actual], [type(e) for e in desired])
elif isinstance(desired, dict) and isinstance(actual, dict):
assert_array_equal(
sorted(actual.values(), key=sort_key),
sorted(desired.values(), key=sort_key),
)
assert_array_equal(
[type(e) for e in sorted(actual.values(), key=sort_key)],
[type(e) for e in sorted(desired.values(), key=sort_key)],
)
assert_array_equal(sorted(actual.keys()), sorted(desired.keys()))
assert_array_equal(
[type(e) for e in sorted(actual.keys())],
[type(e) for e in sorted(desired.keys())],
)
else:
assert_equal(actual, desired)
assert_equal(type(actual), type(desired))
def test_dump_load(self) -> None:
x = [np.float32(np.random.rand()) for _ in range(5)]
buff = StringIO()
json.dump(x, buff)
buff.seek(0)
self.assert_equal_with_type(json.load(buff), x)
def test_typeerror_on_cannot_encode(self) -> None:
self.assertRaises(TypeError, json.dumps, b"abc")
def test_numpy_scalar_bool(self) -> None:
for b in (True, False):
x = np.bool_(b)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_scalar_float(self) -> None:
x = np.float32(np.random.rand())
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_scalar_complex(self) -> None:
x = np.complex64(np.random.rand() + 1j * np.random.rand())
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_list_numpy_scalar_bool(self) -> None:
x = [np.bool_(True), np.bool_(False)] # noqa: FBT003
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_list_numpy_scalar_float(self) -> None:
x = [np.float32(np.random.rand()) for _ in range(5)]
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_list_numpy_scalar_complex(self) -> None:
x = [np.complex64(np.random.rand() + 1j * np.random.rand()) for _ in range(5)]
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_list_numpy_scalar_float_complex(self) -> None:
x = [np.float32(np.random.rand()) for _ in range(5)] + [
np.complex128(np.random.rand() + 1j * np.random.rand()) for _ in range(5)
]
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_list_mixed(self) -> None:
x = [1.0, np.float32(3.5), np.complex128(4.25), "foo"]
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_dict_numpy_float(self) -> None:
x = {"foo": np.float32(1.0), "bar": np.float32(2.0)}
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_dict_numpy_complex(self) -> None:
x = {"foo": np.complex128(1.0 + 1.0j), "bar": np.complex128(2.0 + 2.0j)}
self.assert_equal_with_type(self.dumps_loads(x), x, sort_key=np.linalg.norm)
def test_numpy_array_float(self) -> None:
x = np.random.rand(5).astype(np.float32)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_array_complex(self) -> None:
x = (np.random.rand(5) + 1j * np.random.rand(5)).astype(np.complex128)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_array_float_2d(self) -> None:
x = np.random.rand(5, 5).astype(np.float32)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_array_bytes(self) -> None:
x = np.array([b"abc", b"cba"])
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_array_mixed(self) -> None:
x = np.array(
[(1, 2, b"a", [1.0, 2.0])],
np.dtype(
[
("arg0", np.uint32),
("arg1", np.uint32),
("arg2", "S1"),
("arg3", np.float32, (2,)),
]
),
)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_array_non_contiguous(self) -> None:
x = np.ones((10, 10), np.uint32)[0:5, 0:5]
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_structured_array(self) -> None:
structured_dtype = np.dtype([("a", float), ("b", int)])
x = np.empty((10,), dtype=structured_dtype)
x["a"] = np.arange(10)
x["b"] = np.arange(10)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_shaped_structured_array(self) -> None:
shaped_structured_dtype = np.dtype([("a", float, 3), ("b", int)])
x = np.empty((10,), dtype=shaped_structured_dtype)
x["a"] = np.arange(30).reshape(10, 3)
x["b"] = np.arange(10)
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_numpy_nested_structured_array(self) -> None:
structured_dtype = np.dtype([("a", float), ("b", int)])
nested_dtype = np.dtype([("foo", structured_dtype), ("bar", structured_dtype)])
x = np.empty((10,), dtype=nested_dtype)
x["foo"]["a"] = np.arange(10)
x["foo"]["b"] = np.arange(10)
x["bar"]["a"] = np.arange(10) + 10
x["bar"]["b"] = np.arange(10) + 10
self.assert_equal_with_type(self.dumps_loads(x), x)
def test_dumps_no_cls_with_default(self) -> None:
sentinel = {"sentinel": str(uuid.uuid4())}
def user_default(_: Any) -> dict[str, Any]:
return sentinel
dumped = json.dumps(b"unserializable", default=user_default)
self.assert_equal_with_type(json.loads(dumped), sentinel)
x = np.random.rand(5).astype(np.float32)
dumped = json.dumps(x, default=user_default)
self.assert_equal_with_type(json.loads(dumped), x)
def test_dumps_with_cls_with_default(self) -> None:
# The `default` kwargs always overrides the `default` method of the `cls`.
sentinel = {"sentinel": [37, 42]}
class Encoder(json.JSONEncoder):
def __init__(self, *args: Any, **kwargs: Any) -> None:
del kwargs["separators"]
super().__init__(*args, separators=(", ", ": "), **kwargs)
def default(self, _: Any) -> None: # pragma: no cover
raise RuntimeError("Should never be called") # noqa: TRY003, EM101
def user_default(_: Any) -> dict[str, Any]:
return sentinel
dumped = json.dumps(b"unserializable", default=user_default, cls=Encoder)
self.assertEqual(dumped, '{"sentinel": [37, 42]}')
x = np.random.rand(5).astype(np.float32)
dumped = json.dumps(x, default=user_default, cls=Encoder)
self.assert_equal_with_type(json.loads(dumped), x)
def test_dumps_with_cls_no_default(self) -> None:
sentinel = {"sentinel": [37, 42]}
class Encoder(json.JSONEncoder):
def __init__(self, *args: Any, **kwargs: Any) -> None:
del kwargs["separators"]
super().__init__(*args, separators=(", ", ": "), **kwargs)
def default(self, _: Any) -> dict[str, Any]:
return sentinel
dumped = json.dumps(b"unserializable", cls=Encoder)
self.assertEqual(dumped, '{"sentinel": [37, 42]}')
x = np.random.rand(5).astype(np.float32)
dumped = json.dumps(x, cls=Encoder)
self.assert_equal_with_type(json.loads(dumped), x)
def test_loads_object_hook(self) -> None:
def hook(dct: dict) -> dict | int:
if "foo" in dct:
return dct["foo"]
return dct
foo = {"foo": "bar"}
x = np.random.rand(5).astype(np.float32)
result = json.loads(json.dumps([foo, x]), object_hook=hook)
self.assertEqual(result[0], "bar")
self.assert_equal_with_type(result[1], x)