blob: 2c24e6dd3e4a3a90da86f552739843ab815649e6 [file] [log] [blame]
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01001/*
2 example/example20.cpp -- Usage of structured numpy dtypes
3
4 Copyright (c) 2016 Ivan Smirnov
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#include "example.h"
11
12#include <pybind11/numpy.h>
13#include <cstdint>
14#include <iostream>
15
16namespace py = pybind11;
17
Ivan Smirnov4f164212016-06-22 01:07:20 +010018struct SimpleStruct {
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010019 bool x;
20 uint32_t y;
21 float z;
22};
23
Ivan Smirnov4f164212016-06-22 01:07:20 +010024std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) {
25 return os << "s:" << v.x << "," << v.y << "," << v.z;
Ivan Smirnov669e1422016-06-21 21:05:29 +010026}
27
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010028struct PackedStruct {
29 bool x;
30 uint32_t y;
31 float z;
32} __attribute__((packed));
33
Ivan Smirnov669e1422016-06-21 21:05:29 +010034std::ostream& operator<<(std::ostream& os, const PackedStruct& v) {
Ivan Smirnov4f164212016-06-22 01:07:20 +010035 return os << "p:" << v.x << "," << v.y << "," << v.z;
Ivan Smirnov669e1422016-06-21 21:05:29 +010036}
37
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010038struct NestedStruct {
Ivan Smirnov4f164212016-06-22 01:07:20 +010039 SimpleStruct a;
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010040 PackedStruct b;
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010041} __attribute__((packed));
42
Ivan Smirnov669e1422016-06-21 21:05:29 +010043std::ostream& operator<<(std::ostream& os, const NestedStruct& v) {
Ivan Smirnov4f164212016-06-22 01:07:20 +010044 return os << "n:a=" << v.a << ";b=" << v.b;
Ivan Smirnov669e1422016-06-21 21:05:29 +010045}
46
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010047struct UnboundStruct { };
48
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010049template <typename T>
50py::array mkarray_via_buffer(size_t n) {
51 return py::array(py::buffer_info(nullptr, sizeof(T),
Ivan Smirnov5e71e172016-06-26 12:42:34 +010052 py::format_descriptor<T>::format(),
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010053 1, { n }, { sizeof(T) }));
54}
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010055
56template <typename S>
57py::array_t<S> create_recarray(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010058 auto arr = mkarray_via_buffer<S>(n);
59 auto ptr = static_cast<S*>(arr.request().ptr);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010060 for (size_t i = 0; i < n; i++) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010061 ptr[i].x = i % 2; ptr[i].y = (uint32_t) i; ptr[i].z = (float) i * 1.5f;
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010062 }
63 return arr;
64}
65
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010066std::string get_format_unbound() {
67 return py::format_descriptor<UnboundStruct>::format();
68}
69
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010070py::array_t<NestedStruct> create_nested(size_t n) {
71 auto arr = mkarray_via_buffer<NestedStruct>(n);
72 auto ptr = static_cast<NestedStruct*>(arr.request().ptr);
73 for (size_t i = 0; i < n; i++) {
74 ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f;
75 ptr[i].b.x = (i + 1) % 2; ptr[i].b.y = (uint32_t) (i + 1); ptr[i].b.z = (float) (i + 1) * 1.5f;
76 }
77 return arr;
Ivan Smirnovbdc99022016-06-19 16:54:07 +010078}
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010079
Ivan Smirnov669e1422016-06-21 21:05:29 +010080template <typename S>
81void print_recarray(py::array_t<S> arr) {
82 auto buf = arr.request();
83 auto ptr = static_cast<S*>(buf.ptr);
84 for (size_t i = 0; i < buf.size; i++)
85 std::cout << ptr[i] << std::endl;
86}
Ivan Smirnovbdc99022016-06-19 16:54:07 +010087
88void print_format_descriptors() {
Ivan Smirnov5e71e172016-06-26 12:42:34 +010089 std::cout << py::format_descriptor<SimpleStruct>::format() << std::endl;
90 std::cout << py::format_descriptor<PackedStruct>::format() << std::endl;
91 std::cout << py::format_descriptor<NestedStruct>::format() << std::endl;
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010092}
93
Ivan Smirnov4f164212016-06-22 01:07:20 +010094void print_dtypes() {
95 auto to_str = [](py::object obj) {
96 return (std::string) (py::str) ((py::object) obj.attr("__str__"))();
97 };
98 std::cout << to_str(py::dtype_of<SimpleStruct>()) << std::endl;
99 std::cout << to_str(py::dtype_of<PackedStruct>()) << std::endl;
100 std::cout << to_str(py::dtype_of<NestedStruct>()) << std::endl;
101}
102
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100103void init_ex20(py::module &m) {
Ivan Smirnov5412a052016-07-02 16:18:42 +0100104 PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z);
105 PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z);
106 PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100107
Ivan Smirnov4f164212016-06-22 01:07:20 +0100108 m.def("create_rec_simple", &create_recarray<SimpleStruct>);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100109 m.def("create_rec_packed", &create_recarray<PackedStruct>);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100110 m.def("create_rec_nested", &create_nested);
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100111 m.def("print_format_descriptors", &print_format_descriptors);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100112 m.def("print_rec_simple", &print_recarray<SimpleStruct>);
Ivan Smirnov669e1422016-06-21 21:05:29 +0100113 m.def("print_rec_packed", &print_recarray<PackedStruct>);
114 m.def("print_rec_nested", &print_recarray<NestedStruct>);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100115 m.def("print_dtypes", &print_dtypes);
Ivan Smirnovd0bafd92016-06-26 16:35:28 +0100116 m.def("get_format_unbound", &get_format_unbound);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100117}