/
test_numpy.py
210 lines (165 loc) · 6.97 KB
/
test_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
# pylint: disable=unused-argument
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from functools import partial
import numpy as np
import pytest
from bentoml.io import NumpyNdarray
from bentoml.exceptions import BadInput
from bentoml.exceptions import InvalidArgument
from bentoml.exceptions import BentoMLException
from bentoml._internal.service.openapi.specification import Schema
if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture
from bentoml.grpc.v1alpha1 import service_pb2 as pb
else:
from bentoml.grpc.utils import import_generated_stubs
pb, _ = import_generated_stubs()
class ExampleGeneric(str, np.generic):
pass
example = np.zeros((2, 2, 3, 2))
from_example = NumpyNdarray.from_sample(example)
def test_invalid_dtype():
with pytest.raises(BentoMLException) as e:
NumpyNdarray(dtype="asdf")
assert "Invalid dtype" in str(e.value)
generic = ExampleGeneric("asdf")
with pytest.raises(BentoMLException) as e:
_ = NumpyNdarray.from_sample(generic) # type: ignore (test exception)
assert "expects a 'numpy.array'" in str(e.value)
def test_invalid_init():
with pytest.raises(InvalidArgument) as exc_info:
NumpyNdarray(enforce_dtype=True)
assert "'dtype' must be specified" in str(exc_info.value)
with pytest.raises(InvalidArgument) as exc_info:
NumpyNdarray(enforce_shape=True)
assert "'shape' must be specified" in str(exc_info.value)
@pytest.mark.parametrize("dtype, expected", [("float", "number"), (">U8", "integer")])
def test_numpy_to_openapi_types(dtype: str, expected: str):
assert NumpyNdarray(dtype=dtype)._openapi_types() == expected # type: ignore (private functions warning)
def test_numpy_openapi_schema():
nparray = NumpyNdarray().openapi_schema()
assert nparray.type == "array"
assert nparray.nullable
assert nparray.items and nparray.items.type == "integer"
ndarray = from_example.openapi_schema()
assert nparray.type == "array"
assert isinstance(nparray.items, Schema)
items = ndarray.items
assert items.type == "array"
assert items.items and items.items.type == "number"
def test_numpy_openapi_request_body():
nparray = NumpyNdarray().openapi_request_body()
assert nparray["required"]
assert nparray["content"]
assert "application/json" in nparray["content"]
ndarray = from_example.openapi_request_body()
assert ndarray["required"]
assert ndarray["content"]
assert ndarray["content"]["application/json"].example == example.tolist()
nparray = NumpyNdarray(dtype="float")
nparray.sample_input = ExampleGeneric("asdf") # type: ignore (test exception)
with pytest.raises(BadInput):
nparray.openapi_example()
def test_numpy_openapi_responses():
responses = NumpyNdarray().openapi_responses()
assert responses["content"]
assert "application/json" in responses["content"]
assert not responses["content"]["application/json"].example
def test_verify_numpy_ndarray(caplog: LogCaptureFixture):
partial_check = partial(from_example.validate_array, exception_cls=BentoMLException)
with pytest.raises(BentoMLException) as ex:
partial_check(np.array(["asdf"]))
assert f'Expecting ndarray of dtype "{from_example._dtype}"' in str(ex.value) # type: ignore (testing message)
with pytest.raises(BentoMLException) as e:
partial_check(np.array([[1]]))
assert f'Expecting ndarray of shape "{from_example._shape}"' in str(e.value) # type: ignore (testing message)
# test cases where reshape is failed
example = NumpyNdarray.from_sample(np.ones((2, 2, 3)))
example._enforce_shape = False # type: ignore (test internal check)
example._enforce_dtype = False # type: ignore (test internal check)
with caplog.at_level(logging.DEBUG):
example.validate_array(np.array("asdf"))
assert "Failed to reshape" in caplog.text
def generate_1d_array(dtype: pb.NDArray.DType.ValueType, length: int = 3):
if dtype == pb.NDArray.DTYPE_BOOL:
return [True] * length
elif dtype == pb.NDArray.DTYPE_STRING:
return ["a"] * length
else:
return [1] * length
@pytest.mark.asyncio
@pytest.mark.parametrize(
"dtype",
filter(lambda x: x > 0, [v.number for v in pb.NDArray.DType.DESCRIPTOR.values]),
)
async def test_from_proto(dtype: pb.NDArray.DType.ValueType) -> None:
from bentoml._internal.io_descriptors.numpy import dtypepb_to_fieldpb_map
from bentoml._internal.io_descriptors.numpy import dtypepb_to_npdtype_map
np.testing.assert_array_equal(
await NumpyNdarray(dtype=example.dtype, shape=example.shape).from_proto(
example.ravel().tobytes(),
),
example,
)
# DTYPE_UNSPECIFIED
np.testing.assert_array_equal(
await NumpyNdarray().from_proto(
pb.NDArray(dtype=pb.NDArray.DType.DTYPE_UNSPECIFIED),
),
np.empty(0),
)
np.testing.assert_array_equal(
await NumpyNdarray().from_proto(
pb.NDArray(shape=tuple(example.shape)),
),
np.empty(tuple(example.shape)),
)
# different DTYPE
np.testing.assert_array_equal(
await NumpyNdarray().from_proto(
pb.NDArray(
dtype=dtype,
**{dtypepb_to_fieldpb_map()[dtype]: generate_1d_array(dtype)},
),
),
np.array(generate_1d_array(dtype), dtype=dtypepb_to_npdtype_map()[dtype]),
)
# given shape from message.
np.testing.assert_array_equal(
await NumpyNdarray().from_proto(
pb.NDArray(shape=[3, 3], float_values=[1.0] * 9),
),
np.array([[1.0] * 3] * 3),
)
@pytest.mark.asyncio
async def test_exception_from_proto():
with pytest.raises(AssertionError):
await NumpyNdarray().from_proto(pb.NDArray(string_values="asdf"))
await NumpyNdarray().from_proto(pb.File(content=b"asdf")) # type: ignore (testing exception)
with pytest.raises(BadInput):
await NumpyNdarray().from_proto(b"asdf")
with pytest.raises(BadInput) as exc_info:
await NumpyNdarray().from_proto(pb.NDArray(dtype=123, string_values="asdf")) # type: ignore (testing exception)
assert "123 is invalid." == str(exc_info.value)
with pytest.raises(BadInput) as exc_info:
await NumpyNdarray().from_proto(
pb.NDArray(string_values="asdf", float_values=[1.0, 2.0])
)
assert "Array contents can only be one of" in str(exc_info.value)
@pytest.mark.asyncio
async def test_exception_to_proto():
with pytest.raises(BadInput):
await NumpyNdarray(dtype=np.float32, enforce_dtype=True).to_proto(
np.array("asdf")
)
with pytest.raises(BadInput):
await NumpyNdarray(dtype=np.dtype(np.void)).to_proto(np.array("asdf"))
@pytest.mark.asyncio
async def test_to_proto() -> None:
assert await NumpyNdarray().to_proto(example) == pb.NDArray(
shape=example.shape,
dtype=pb.NDArray.DType.DTYPE_DOUBLE,
double_values=example.ravel().tolist(),
)