6 from pybind11_tests
import numpy_dtypes
as m
8 np = pytest.importorskip(
"numpy")
11 @pytest.fixture(scope=
"module")
13 ld = np.dtype(
"longdouble")
16 "names": [
"bool_",
"uint_",
"float_",
"ldbl_"],
17 "formats": [
"?",
"u4",
"f4", f
"f{ld.itemsize}"],
18 "offsets": [0, 4, 8, (16
if ld.alignment > 4
else 12)],
23 @pytest.fixture(scope=
"module")
25 return np.dtype([(
"bool_",
"?"), (
"uint_",
"u4"), (
"float_",
"f4"), (
"ldbl_",
"g")])
29 from sys
import byteorder
31 e =
"<" if byteorder ==
"little" else ">" 33 "{{'names':['bool_','uint_','float_','ldbl_']," 34 "'formats':['?','" + e +
"u4','" + e +
"f4','" + e +
"f{}']," 35 "'offsets':[0,4,8,{}],'itemsize':{}}}" 40 ld = np.dtype(
"longdouble")
41 simple_ld_off = 12 + 4 * (ld.alignment > 4)
42 return dt_fmt().
format(ld.itemsize, simple_ld_off, simple_ld_off + ld.itemsize)
46 from sys
import byteorder
48 return "[('bool_','?'),('uint_','{e}u4'),('float_','{e}f4'),('ldbl_','{e}f{}')]".
format(
49 np.dtype(
"longdouble").itemsize, e=
"<" if byteorder ==
"little" else ">" 56 + 4 * (np.dtype(
"uint64").alignment > 4)
58 + 8 * (np.dtype(
"longdouble").alignment > 8)
63 ld = np.dtype(
"longdouble")
65 partial_size = partial_ld_off + ld.itemsize
66 partial_end_padding = partial_size % np.dtype(
"uint64").alignment
68 ld.itemsize, partial_ld_off, partial_size + partial_end_padding
73 ld = np.dtype(
"longdouble")
74 partial_nested_off = 8 + 8 * (ld.alignment > 8)
76 partial_size = partial_ld_off + ld.itemsize
77 partial_end_padding = partial_size % np.dtype(
"uint64").alignment
78 partial_nested_size = partial_nested_off * 2 + partial_size + partial_end_padding
79 return "{{'names':['a'],'formats':[{}],'offsets':[{}],'itemsize':{}}}".
format(
85 np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype))
89 with pytest.raises(RuntimeError)
as excinfo:
90 m.get_format_unbound()
92 "^NumPy type info missing for .*UnboundStruct.*$",
str(excinfo.value)
95 ld = np.dtype(
"longdouble")
96 ldbl_fmt = (
"4x" if ld.alignment > 4
else "") + ld.char
97 ss_fmt =
"^T{?:bool_:3xI:uint_:f:float_:" + ldbl_fmt +
":ldbl_:}" 98 dbl = np.dtype(
"double")
99 end_padding = ld.itemsize % np.dtype(
"uint64").alignment
101 "^T{?:bool_:3xI:uint_:f:float_:" 102 +
str(4 * (dbl.alignment > 4) + dbl.itemsize + 8 * (ld.alignment > 8))
104 + (
str(end_padding) +
"x}" if end_padding > 0
else "}")
106 nested_extra =
str(
max(8, ld.alignment))
107 assert m.print_format_descriptors() == [
109 "^T{?:bool_:I:uint_:f:float_:g:ldbl_:}",
110 "^T{" + ss_fmt +
":a:^T{?:bool_:I:uint_:f:float_:g:ldbl_:}:b:}",
112 "^T{" + nested_extra +
"x" + partial_fmt +
":a:" + nested_extra +
"x}",
114 "^T{(3)4s:a:(2)i:b:(3)B:c:1x(4, 2)f:d:}",
116 "^T{Zf:cflt:Zd:cdbl:}",
121 from sys
import byteorder
123 e =
"<" if byteorder ==
"little" else ">" 125 assert [x.replace(
" ",
"")
for x
in m.print_dtypes()] == [
128 f
"[('a',{simple_dtype_fmt()}),('b',{packed_dtype_fmt()})]",
131 "[('a','S3'),('b','S3')]",
133 "{{'names':['a','b','c','d']," 134 +
"'formats':[('S4',(3,)),('" 136 +
"i4',(2,)),('u1',(3,)),('" 139 +
"'offsets':[0,12,20,24],'itemsize':56}}" 141 "[('e1','" + e +
"i8'),('e2','u1')]",
142 "[('x','i1'),('y','" + e +
"u8')]",
143 "[('cflt','" + e +
"c8'),('cdbl','" + e +
"c16')]",
149 "formats": [
"int32",
"float64"],
154 d2 = np.dtype([(
"a",
"i4"), (
"b",
"f4")])
155 assert m.test_dtype_ctors() == [
166 assert m.test_dtype_methods() == [
171 np.dtype(
"int32").itemsize,
172 simple_dtype.itemsize,
175 assert m.trailing_padding_dtype() == m.buffer_to_dtype(
176 np.zeros(1, m.trailing_padding_dtype())
179 expected_chars =
"bhilqBHILQefdgFDG?MmO" 180 assert m.test_dtype_kind() ==
list(
"iiiiiuuuuuffffcccbMmO")
181 assert m.test_dtype_char_() ==
list(expected_chars)
182 assert m.test_dtype_num() == [np.dtype(ch).num
for ch
in expected_chars]
183 assert m.test_dtype_byteorder() == [np.dtype(ch).byteorder
for ch
in expected_chars]
184 assert m.test_dtype_alignment() == [np.dtype(ch).alignment
for ch
in expected_chars]
185 assert m.test_dtype_flags() == [chr(np.dtype(ch).flags)
for ch
in expected_chars]
189 elements = [(
False, 0, 0.0, -0.0), (
True, 1, 1.5, -2.5), (
False, 2, 3.0, -5.0)]
192 (m.create_rec_simple, simple_dtype),
193 (m.create_rec_packed, packed_dtype),
196 assert arr.dtype == dtype
201 assert arr.dtype == dtype
206 assert type(arr[0]) == np.void
207 assert type(arr[0].item()) == tuple
209 if dtype == simple_dtype:
210 assert m.print_rec_simple(arr) == [
216 assert m.print_rec_packed(arr) == [
222 nested_dtype = np.dtype([(
"a", simple_dtype), (
"b", packed_dtype)])
224 arr = m.create_rec_nested(0)
225 assert arr.dtype == nested_dtype
228 arr = m.create_rec_nested(3)
229 assert arr.dtype == nested_dtype
233 ((
False, 0, 0.0, -0.0), (
True, 1, 1.5, -2.5)),
234 ((
True, 1, 1.5, -2.5), (
False, 2, 3.0, -5.0)),
235 ((
False, 2, 3.0, -5.0), (
True, 3, 4.5, -7.5)),
239 assert m.print_rec_nested(arr) == [
240 "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5",
241 "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
242 "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5",
245 arr = m.create_rec_partial(3)
247 partial_dtype = arr.dtype
248 assert "" not in arr.dtype.fields
249 assert partial_dtype.itemsize > simple_dtype.itemsize
253 arr = m.create_rec_partial_nested(3)
255 assert "" not in arr.dtype.fields
256 assert "" not in arr.dtype.fields[
"a"][0].fields
257 assert arr.dtype.itemsize > partial_dtype.itemsize
258 np.testing.assert_equal(arr[
"a"], m.create_rec_partial(3))
262 data = np.arange(1, 7, dtype=
"int32")
264 np.testing.assert_array_equal(m.test_array_ctors(10 + i), data.reshape((3, 2)))
265 np.testing.assert_array_equal(m.test_array_ctors(20 + i), data.reshape((3, 2)))
267 np.testing.assert_array_equal(m.test_array_ctors(30 + i), data)
268 np.testing.assert_array_equal(m.test_array_ctors(40 + i), data)
272 arr = m.create_string_array(
True)
273 assert str(arr.dtype) ==
"[('a', 'S3'), ('b', 'S3')]" 274 assert m.print_string_array(arr) == [
281 assert arr[
"a"].tolist() == [b
"", b
"a", b
"ab", b
"abc"]
282 assert arr[
"b"].tolist() == [b
"", b
"a", b
"ab", b
"abc"]
283 arr = m.create_string_array(
False)
284 assert dtype == arr.dtype
288 from sys
import byteorder
290 e =
"<" if byteorder ==
"little" else ">" 292 arr = m.create_array_array(3)
293 assert str(arr.dtype).replace(
" ",
"") == (
294 "{{'names':['a','b','c','d']," 295 +
"'formats':[('S4',(3,)),('" 297 +
"i4',(2,)),('u1',(3,)),('{e}f4',(4,2))]," 298 +
"'offsets':[0,12,20,24],'itemsize':56}}" 300 assert m.print_array_array(arr) == [
301 "a={{A,B,C,D},{K,L,M,N},{U,V,W,X}},b={0,1}," 302 +
"c={0,1,2},d={{0,1},{10,11},{20,21},{30,31}}",
303 "a={{W,X,Y,Z},{G,H,I,J},{Q,R,S,T}},b={1000,1001}," 304 +
"c={10,11,12},d={{100,101},{110,111},{120,121},{130,131}}",
305 "a={{S,T,U,V},{C,D,E,F},{M,N,O,P}},b={2000,2001}," 306 +
"c={20,21,22},d={{200,201},{210,211},{220,221},{230,231}}",
308 assert arr[
"a"].tolist() == [
309 [b
"ABCD", b
"KLMN", b
"UVWX"],
310 [b
"WXYZ", b
"GHIJ", b
"QRST"],
311 [b
"STUV", b
"CDEF", b
"MNOP"],
313 assert arr[
"b"].tolist() == [[0, 1], [1000, 1001], [2000, 2001]]
314 assert m.create_array_array(0).dtype == arr.dtype
318 from sys
import byteorder
320 e =
"<" if byteorder ==
"little" else ">" 322 arr = m.create_enum_array(3)
324 assert dtype == np.dtype([(
"e1", e +
"i8"), (
"e2",
"u1")])
325 assert m.print_enum_array(arr) == [
"e1=A,e2=X",
"e1=B,e2=Y",
"e1=A,e2=X"]
326 assert arr[
"e1"].tolist() == [-1, 1, -1]
327 assert arr[
"e2"].tolist() == [1, 2, 1]
328 assert m.create_enum_array(0).dtype == dtype
332 from sys
import byteorder
334 e =
"<" if byteorder ==
"little" else ">" 336 arr = m.create_complex_array(3)
338 assert dtype == np.dtype([(
"cflt", e +
"c8"), (
"cdbl", e +
"c16")])
339 assert m.print_complex_array(arr) == [
340 "c:(0,0.25),(0.5,0.75)",
341 "c:(1,1.25),(1.5,1.75)",
342 "c:(2,2.25),(2.5,2.75)",
344 assert arr[
"cflt"].tolist() == [0.0 + 0.25j, 1.0 + 1.25j, 2.0 + 2.25j]
345 assert arr[
"cdbl"].tolist() == [0.5 + 0.75j, 1.5 + 1.75j, 2.5 + 2.75j]
346 assert m.create_complex_array(0).dtype == dtype
351 doc(m.create_rec_nested)
352 ==
"create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]" 359 m.create_rec_simple(n),
360 m.create_rec_packed(n),
361 m.create_rec_nested(n),
362 m.create_enum_array(n),
364 funcs = [m.f_simple, m.f_packed, m.f_nested]
366 for i, func
in enumerate(funcs):
367 for j, arr
in enumerate(arrays):
369 assert [
func(arr[k])
for k
in range(n)] == [k * 10
for k
in range(n)]
371 with pytest.raises(TypeError)
as excinfo:
373 assert "incompatible function arguments" in str(excinfo.value)
378 array = m.create_rec_simple(n)
379 values = m.f_simple_vectorized(array)
380 np.testing.assert_array_equal(values, [0, 10, 20])
381 array_2 = m.f_simple_pass_thru_vectorized(array)
382 np.testing.assert_array_equal(array, array_2)
387 assert s.astuple() == (
False, 0, 0.0, 0.0)
388 assert m.SimpleStruct.fromtuple(s.astuple()).astuple() == s.astuple()
391 assert m.f_simple(s) == 20
394 s_recarray = np.array([(
False, 2, 0.0, 0.0)], dtype=simple_dtype)
396 np.testing.assert_array_equal(m.f_simple_vectorized(s_recarray), [20])
399 s_scalar = s_recarray[0]
401 assert m.f_simple(s_scalar) == 20
405 s_recarray_scalar = s_recarray.reshape(())
406 assert isinstance(s_recarray_scalar, np.ndarray)
407 assert s_recarray_scalar.dtype == simple_dtype
408 with pytest.raises(TypeError)
as excinfo:
409 m.f_simple(s_recarray_scalar)
410 assert "incompatible function arguments" in str(excinfo.value)
412 assert m.f_simple(m.SimpleStruct.fromtuple(s_recarray_scalar.item())) == 20
415 s_array_object = np.array([s])
416 assert s_array_object.dtype == object
417 with pytest.raises(TypeError)
as excinfo:
418 m.f_simple_vectorized(s_array_object)
419 assert "incompatible function arguments" in str(excinfo.value)
421 s_array = np.array([s.astuple()], dtype=simple_dtype)
422 np.testing.assert_array_equal(m.f_simple_vectorized(s_array), [20])
426 with pytest.raises(RuntimeError)
as excinfo:
428 assert "dtype is already registered" in str(excinfo.value)
431 @pytest.mark.xfail(
"env.PYPY")
433 from sys
import getrefcount
437 start = getrefcount(fmt)
438 d = m.dtype_wrapper(fmt)
439 assert d
is np.dtype(
"f4")
442 assert getrefcount(fmt) == start
446 assert all(m.compare_buffer_info())
def test_compare_buffer_info()
static const Eigen::internal::all_t all
def test_cls_and_dtype_conversion(simple_dtype)
Annotation for documentation.
def test_register_dtype()
bool isinstance(handle obj)
def test_dtype(simple_dtype)
def test_format_descriptors()
def test_array_constructors()
def assert_equal(actual, expected_data, expected_dtype)
Double_ range(const Point2_ &p, const Point2_ &q)
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
def test_scalar_conversion()
def test_recarray(simple_dtype, packed_dtype)