test_numpy_dtypes.cpp revision 12391:ceeca8b41e4b
112855Sgabeblack@google.com/* 212855Sgabeblack@google.com tests/test_numpy_dtypes.cpp -- Structured and compound NumPy dtypes 312855Sgabeblack@google.com 412855Sgabeblack@google.com Copyright (c) 2016 Ivan Smirnov 512855Sgabeblack@google.com 612855Sgabeblack@google.com All rights reserved. Use of this source code is governed by a 712855Sgabeblack@google.com BSD-style license that can be found in the LICENSE file. 812855Sgabeblack@google.com*/ 912855Sgabeblack@google.com 1012855Sgabeblack@google.com#include "pybind11_tests.h" 1112855Sgabeblack@google.com#include <pybind11/numpy.h> 1212855Sgabeblack@google.com 1312855Sgabeblack@google.com#ifdef __GNUC__ 1412855Sgabeblack@google.com#define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) 1512855Sgabeblack@google.com#else 1612855Sgabeblack@google.com#define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) 1712855Sgabeblack@google.com#endif 1812855Sgabeblack@google.com 1912855Sgabeblack@google.comnamespace py = pybind11; 2012855Sgabeblack@google.com 2112855Sgabeblack@google.comstruct SimpleStruct { 2212855Sgabeblack@google.com bool bool_; 2312855Sgabeblack@google.com uint32_t uint_; 2412855Sgabeblack@google.com float float_; 2512855Sgabeblack@google.com long double ldbl_; 2612855Sgabeblack@google.com}; 2712855Sgabeblack@google.com 2812855Sgabeblack@google.comstd::ostream& operator<<(std::ostream& os, const SimpleStruct& v) { 2912855Sgabeblack@google.com return os << "s:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_; 3012855Sgabeblack@google.com} 3112855Sgabeblack@google.com 3212855Sgabeblack@google.comPYBIND11_PACKED(struct PackedStruct { 3312855Sgabeblack@google.com bool bool_; 3412855Sgabeblack@google.com uint32_t uint_; 3512855Sgabeblack@google.com float float_; 3612855Sgabeblack@google.com long double ldbl_; 3712855Sgabeblack@google.com}); 3812855Sgabeblack@google.com 3912855Sgabeblack@google.comstd::ostream& operator<<(std::ostream& os, const PackedStruct& v) { 4012855Sgabeblack@google.com return os << "p:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_; 4112855Sgabeblack@google.com} 4212855Sgabeblack@google.com 4312855Sgabeblack@google.comPYBIND11_PACKED(struct NestedStruct { 4412855Sgabeblack@google.com SimpleStruct a; 4512855Sgabeblack@google.com PackedStruct b; 4612855Sgabeblack@google.com}); 4712855Sgabeblack@google.com 4812855Sgabeblack@google.comstd::ostream& operator<<(std::ostream& os, const NestedStruct& v) { 4912855Sgabeblack@google.com return os << "n:a=" << v.a << ";b=" << v.b; 5012855Sgabeblack@google.com} 5112855Sgabeblack@google.com 5212855Sgabeblack@google.comstruct PartialStruct { 5312855Sgabeblack@google.com bool bool_; 5412855Sgabeblack@google.com uint32_t uint_; 5512855Sgabeblack@google.com float float_; 5612855Sgabeblack@google.com uint64_t dummy2; 5712855Sgabeblack@google.com long double ldbl_; 5812855Sgabeblack@google.com}; 5912855Sgabeblack@google.com 6012855Sgabeblack@google.comstruct PartialNestedStruct { 61 uint64_t dummy1; 62 PartialStruct a; 63 uint64_t dummy2; 64}; 65 66struct UnboundStruct { }; 67 68struct StringStruct { 69 char a[3]; 70 std::array<char, 3> b; 71}; 72 73struct ComplexStruct { 74 std::complex<float> cflt; 75 std::complex<double> cdbl; 76}; 77 78std::ostream& operator<<(std::ostream& os, const ComplexStruct& v) { 79 return os << "c:" << v.cflt << "," << v.cdbl; 80} 81 82struct ArrayStruct { 83 char a[3][4]; 84 int32_t b[2]; 85 std::array<uint8_t, 3> c; 86 std::array<float, 2> d[4]; 87}; 88 89PYBIND11_PACKED(struct StructWithUglyNames { 90 int8_t __x__; 91 uint64_t __y__; 92}); 93 94enum class E1 : int64_t { A = -1, B = 1 }; 95enum E2 : uint8_t { X = 1, Y = 2 }; 96 97PYBIND11_PACKED(struct EnumStruct { 98 E1 e1; 99 E2 e2; 100}); 101 102std::ostream& operator<<(std::ostream& os, const StringStruct& v) { 103 os << "a='"; 104 for (size_t i = 0; i < 3 && v.a[i]; i++) os << v.a[i]; 105 os << "',b='"; 106 for (size_t i = 0; i < 3 && v.b[i]; i++) os << v.b[i]; 107 return os << "'"; 108} 109 110std::ostream& operator<<(std::ostream& os, const ArrayStruct& v) { 111 os << "a={"; 112 for (int i = 0; i < 3; i++) { 113 if (i > 0) 114 os << ','; 115 os << '{'; 116 for (int j = 0; j < 3; j++) 117 os << v.a[i][j] << ','; 118 os << v.a[i][3] << '}'; 119 } 120 os << "},b={" << v.b[0] << ',' << v.b[1]; 121 os << "},c={" << int(v.c[0]) << ',' << int(v.c[1]) << ',' << int(v.c[2]); 122 os << "},d={"; 123 for (int i = 0; i < 4; i++) { 124 if (i > 0) 125 os << ','; 126 os << '{' << v.d[i][0] << ',' << v.d[i][1] << '}'; 127 } 128 return os << '}'; 129} 130 131std::ostream& operator<<(std::ostream& os, const EnumStruct& v) { 132 return os << "e1=" << (v.e1 == E1::A ? "A" : "B") << ",e2=" << (v.e2 == E2::X ? "X" : "Y"); 133} 134 135template <typename T> 136py::array mkarray_via_buffer(size_t n) { 137 return py::array(py::buffer_info(nullptr, sizeof(T), 138 py::format_descriptor<T>::format(), 139 1, { n }, { sizeof(T) })); 140} 141 142#define SET_TEST_VALS(s, i) do { \ 143 s.bool_ = (i) % 2 != 0; \ 144 s.uint_ = (uint32_t) (i); \ 145 s.float_ = (float) (i) * 1.5f; \ 146 s.ldbl_ = (long double) (i) * -2.5L; } while (0) 147 148template <typename S> 149py::array_t<S, 0> create_recarray(size_t n) { 150 auto arr = mkarray_via_buffer<S>(n); 151 auto req = arr.request(); 152 auto ptr = static_cast<S*>(req.ptr); 153 for (size_t i = 0; i < n; i++) { 154 SET_TEST_VALS(ptr[i], i); 155 } 156 return arr; 157} 158 159template <typename S> 160py::list print_recarray(py::array_t<S, 0> arr) { 161 const auto req = arr.request(); 162 const auto ptr = static_cast<S*>(req.ptr); 163 auto l = py::list(); 164 for (ssize_t i = 0; i < req.size; i++) { 165 std::stringstream ss; 166 ss << ptr[i]; 167 l.append(py::str(ss.str())); 168 } 169 return l; 170} 171 172py::array_t<int32_t, 0> test_array_ctors(int i) { 173 using arr_t = py::array_t<int32_t, 0>; 174 175 std::vector<int32_t> data { 1, 2, 3, 4, 5, 6 }; 176 std::vector<ssize_t> shape { 3, 2 }; 177 std::vector<ssize_t> strides { 8, 4 }; 178 179 auto ptr = data.data(); 180 auto vptr = (void *) ptr; 181 auto dtype = py::dtype("int32"); 182 183 py::buffer_info buf_ndim1(vptr, 4, "i", 6); 184 py::buffer_info buf_ndim1_null(nullptr, 4, "i", 6); 185 py::buffer_info buf_ndim2(vptr, 4, "i", 2, shape, strides); 186 py::buffer_info buf_ndim2_null(nullptr, 4, "i", 2, shape, strides); 187 188 auto fill = [](py::array arr) { 189 auto req = arr.request(); 190 for (int i = 0; i < 6; i++) ((int32_t *) req.ptr)[i] = i + 1; 191 return arr; 192 }; 193 194 switch (i) { 195 // shape: (3, 2) 196 case 10: return arr_t(shape, strides, ptr); 197 case 11: return py::array(shape, strides, ptr); 198 case 12: return py::array(dtype, shape, strides, vptr); 199 case 13: return arr_t(shape, ptr); 200 case 14: return py::array(shape, ptr); 201 case 15: return py::array(dtype, shape, vptr); 202 case 16: return arr_t(buf_ndim2); 203 case 17: return py::array(buf_ndim2); 204 // shape: (3, 2) - post-fill 205 case 20: return fill(arr_t(shape, strides)); 206 case 21: return py::array(shape, strides, ptr); // can't have nullptr due to templated ctor 207 case 22: return fill(py::array(dtype, shape, strides)); 208 case 23: return fill(arr_t(shape)); 209 case 24: return py::array(shape, ptr); // can't have nullptr due to templated ctor 210 case 25: return fill(py::array(dtype, shape)); 211 case 26: return fill(arr_t(buf_ndim2_null)); 212 case 27: return fill(py::array(buf_ndim2_null)); 213 // shape: (6, ) 214 case 30: return arr_t(6, ptr); 215 case 31: return py::array(6, ptr); 216 case 32: return py::array(dtype, 6, vptr); 217 case 33: return arr_t(buf_ndim1); 218 case 34: return py::array(buf_ndim1); 219 // shape: (6, ) 220 case 40: return fill(arr_t(6)); 221 case 41: return py::array(6, ptr); // can't have nullptr due to templated ctor 222 case 42: return fill(py::array(dtype, 6)); 223 case 43: return fill(arr_t(buf_ndim1_null)); 224 case 44: return fill(py::array(buf_ndim1_null)); 225 } 226 return arr_t(); 227} 228 229py::list test_dtype_ctors() { 230 py::list list; 231 list.append(py::dtype("int32")); 232 list.append(py::dtype(std::string("float64"))); 233 list.append(py::dtype::from_args(py::str("bool"))); 234 py::list names, offsets, formats; 235 py::dict dict; 236 names.append(py::str("a")); names.append(py::str("b")); dict["names"] = names; 237 offsets.append(py::int_(1)); offsets.append(py::int_(10)); dict["offsets"] = offsets; 238 formats.append(py::dtype("int32")); formats.append(py::dtype("float64")); dict["formats"] = formats; 239 dict["itemsize"] = py::int_(20); 240 list.append(py::dtype::from_args(dict)); 241 list.append(py::dtype(names, formats, offsets, 20)); 242 list.append(py::dtype(py::buffer_info((void *) 0, sizeof(unsigned int), "I", 1))); 243 list.append(py::dtype(py::buffer_info((void *) 0, 0, "T{i:a:f:b:}", 1))); 244 return list; 245} 246 247TEST_SUBMODULE(numpy_dtypes, m) { 248 try { py::module::import("numpy"); } 249 catch (...) { return; } 250 251 // typeinfo may be registered before the dtype descriptor for scalar casts to work... 252 py::class_<SimpleStruct>(m, "SimpleStruct"); 253 254 PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); 255 PYBIND11_NUMPY_DTYPE(PackedStruct, bool_, uint_, float_, ldbl_); 256 PYBIND11_NUMPY_DTYPE(NestedStruct, a, b); 257 PYBIND11_NUMPY_DTYPE(PartialStruct, bool_, uint_, float_, ldbl_); 258 PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a); 259 PYBIND11_NUMPY_DTYPE(StringStruct, a, b); 260 PYBIND11_NUMPY_DTYPE(ArrayStruct, a, b, c, d); 261 PYBIND11_NUMPY_DTYPE(EnumStruct, e1, e2); 262 PYBIND11_NUMPY_DTYPE(ComplexStruct, cflt, cdbl); 263 264 // ... or after 265 py::class_<PackedStruct>(m, "PackedStruct"); 266 267 PYBIND11_NUMPY_DTYPE_EX(StructWithUglyNames, __x__, "x", __y__, "y"); 268 269 // If uncommented, this should produce a static_assert failure telling the user that the struct 270 // is not a POD type 271// struct NotPOD { std::string v; NotPOD() : v("hi") {}; }; 272// PYBIND11_NUMPY_DTYPE(NotPOD, v); 273 274 // test_recarray, test_scalar_conversion 275 m.def("create_rec_simple", &create_recarray<SimpleStruct>); 276 m.def("create_rec_packed", &create_recarray<PackedStruct>); 277 m.def("create_rec_nested", [](size_t n) { // test_signature 278 py::array_t<NestedStruct, 0> arr = mkarray_via_buffer<NestedStruct>(n); 279 auto req = arr.request(); 280 auto ptr = static_cast<NestedStruct*>(req.ptr); 281 for (size_t i = 0; i < n; i++) { 282 SET_TEST_VALS(ptr[i].a, i); 283 SET_TEST_VALS(ptr[i].b, i + 1); 284 } 285 return arr; 286 }); 287 m.def("create_rec_partial", &create_recarray<PartialStruct>); 288 m.def("create_rec_partial_nested", [](size_t n) { 289 py::array_t<PartialNestedStruct, 0> arr = mkarray_via_buffer<PartialNestedStruct>(n); 290 auto req = arr.request(); 291 auto ptr = static_cast<PartialNestedStruct*>(req.ptr); 292 for (size_t i = 0; i < n; i++) { 293 SET_TEST_VALS(ptr[i].a, i); 294 } 295 return arr; 296 }); 297 m.def("print_rec_simple", &print_recarray<SimpleStruct>); 298 m.def("print_rec_packed", &print_recarray<PackedStruct>); 299 m.def("print_rec_nested", &print_recarray<NestedStruct>); 300 301 // test_format_descriptors 302 m.def("get_format_unbound", []() { return py::format_descriptor<UnboundStruct>::format(); }); 303 m.def("print_format_descriptors", []() { 304 py::list l; 305 for (const auto &fmt : { 306 py::format_descriptor<SimpleStruct>::format(), 307 py::format_descriptor<PackedStruct>::format(), 308 py::format_descriptor<NestedStruct>::format(), 309 py::format_descriptor<PartialStruct>::format(), 310 py::format_descriptor<PartialNestedStruct>::format(), 311 py::format_descriptor<StringStruct>::format(), 312 py::format_descriptor<ArrayStruct>::format(), 313 py::format_descriptor<EnumStruct>::format(), 314 py::format_descriptor<ComplexStruct>::format() 315 }) { 316 l.append(py::cast(fmt)); 317 } 318 return l; 319 }); 320 321 // test_dtype 322 m.def("print_dtypes", []() { 323 py::list l; 324 for (const py::handle &d : { 325 py::dtype::of<SimpleStruct>(), 326 py::dtype::of<PackedStruct>(), 327 py::dtype::of<NestedStruct>(), 328 py::dtype::of<PartialStruct>(), 329 py::dtype::of<PartialNestedStruct>(), 330 py::dtype::of<StringStruct>(), 331 py::dtype::of<ArrayStruct>(), 332 py::dtype::of<EnumStruct>(), 333 py::dtype::of<StructWithUglyNames>(), 334 py::dtype::of<ComplexStruct>() 335 }) 336 l.append(py::str(d)); 337 return l; 338 }); 339 m.def("test_dtype_ctors", &test_dtype_ctors); 340 m.def("test_dtype_methods", []() { 341 py::list list; 342 auto dt1 = py::dtype::of<int32_t>(); 343 auto dt2 = py::dtype::of<SimpleStruct>(); 344 list.append(dt1); list.append(dt2); 345 list.append(py::bool_(dt1.has_fields())); list.append(py::bool_(dt2.has_fields())); 346 list.append(py::int_(dt1.itemsize())); list.append(py::int_(dt2.itemsize())); 347 return list; 348 }); 349 struct TrailingPaddingStruct { 350 int32_t a; 351 char b; 352 }; 353 PYBIND11_NUMPY_DTYPE(TrailingPaddingStruct, a, b); 354 m.def("trailing_padding_dtype", []() { return py::dtype::of<TrailingPaddingStruct>(); }); 355 356 // test_string_array 357 m.def("create_string_array", [](bool non_empty) { 358 py::array_t<StringStruct, 0> arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0); 359 if (non_empty) { 360 auto req = arr.request(); 361 auto ptr = static_cast<StringStruct*>(req.ptr); 362 for (ssize_t i = 0; i < req.size * req.itemsize; i++) 363 static_cast<char*>(req.ptr)[i] = 0; 364 ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a'; 365 ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a'; 366 ptr[3].a[0] = 'a'; ptr[3].b[0] = 'a'; 367 368 ptr[2].a[1] = 'b'; ptr[2].b[1] = 'b'; 369 ptr[3].a[1] = 'b'; ptr[3].b[1] = 'b'; 370 371 ptr[3].a[2] = 'c'; ptr[3].b[2] = 'c'; 372 } 373 return arr; 374 }); 375 m.def("print_string_array", &print_recarray<StringStruct>); 376 377 // test_array_array 378 m.def("create_array_array", [](size_t n) { 379 py::array_t<ArrayStruct, 0> arr = mkarray_via_buffer<ArrayStruct>(n); 380 auto ptr = (ArrayStruct *) arr.mutable_data(); 381 for (size_t i = 0; i < n; i++) { 382 for (size_t j = 0; j < 3; j++) 383 for (size_t k = 0; k < 4; k++) 384 ptr[i].a[j][k] = char('A' + (i * 100 + j * 10 + k) % 26); 385 for (size_t j = 0; j < 2; j++) 386 ptr[i].b[j] = int32_t(i * 1000 + j); 387 for (size_t j = 0; j < 3; j++) 388 ptr[i].c[j] = uint8_t(i * 10 + j); 389 for (size_t j = 0; j < 4; j++) 390 for (size_t k = 0; k < 2; k++) 391 ptr[i].d[j][k] = float(i) * 100.0f + float(j) * 10.0f + float(k); 392 } 393 return arr; 394 }); 395 m.def("print_array_array", &print_recarray<ArrayStruct>); 396 397 // test_enum_array 398 m.def("create_enum_array", [](size_t n) { 399 py::array_t<EnumStruct, 0> arr = mkarray_via_buffer<EnumStruct>(n); 400 auto ptr = (EnumStruct *) arr.mutable_data(); 401 for (size_t i = 0; i < n; i++) { 402 ptr[i].e1 = static_cast<E1>(-1 + ((int) i % 2) * 2); 403 ptr[i].e2 = static_cast<E2>(1 + (i % 2)); 404 } 405 return arr; 406 }); 407 m.def("print_enum_array", &print_recarray<EnumStruct>); 408 409 // test_complex_array 410 m.def("create_complex_array", [](size_t n) { 411 py::array_t<ComplexStruct, 0> arr = mkarray_via_buffer<ComplexStruct>(n); 412 auto ptr = (ComplexStruct *) arr.mutable_data(); 413 for (size_t i = 0; i < n; i++) { 414 ptr[i].cflt.real(float(i)); 415 ptr[i].cflt.imag(float(i) + 0.25f); 416 ptr[i].cdbl.real(double(i) + 0.5); 417 ptr[i].cdbl.imag(double(i) + 0.75); 418 } 419 return arr; 420 }); 421 m.def("print_complex_array", &print_recarray<ComplexStruct>); 422 423 // test_array_constructors 424 m.def("test_array_ctors", &test_array_ctors); 425 426 // test_compare_buffer_info 427 struct CompareStruct { 428 bool x; 429 uint32_t y; 430 float z; 431 }; 432 PYBIND11_NUMPY_DTYPE(CompareStruct, x, y, z); 433 m.def("compare_buffer_info", []() { 434 py::list list; 435 list.append(py::bool_(py::detail::compare_buffer_info<float>::compare(py::buffer_info(nullptr, sizeof(float), "f", 1)))); 436 list.append(py::bool_(py::detail::compare_buffer_info<unsigned>::compare(py::buffer_info(nullptr, sizeof(int), "I", 1)))); 437 list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), "l", 1)))); 438 list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), sizeof(long) == sizeof(int) ? "i" : "q", 1)))); 439 list.append(py::bool_(py::detail::compare_buffer_info<CompareStruct>::compare(py::buffer_info(nullptr, sizeof(CompareStruct), "T{?:x:3xI:y:f:z:}", 1)))); 440 return list; 441 }); 442 m.def("buffer_to_dtype", [](py::buffer& buf) { return py::dtype(buf.request()); }); 443 444 // test_scalar_conversion 445 m.def("f_simple", [](SimpleStruct s) { return s.uint_ * 10; }); 446 m.def("f_packed", [](PackedStruct s) { return s.uint_ * 10; }); 447 m.def("f_nested", [](NestedStruct s) { return s.a.uint_ * 10; }); 448 449 // test_register_dtype 450 m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); }); 451} 452