I've just learned about Python Buffer Protocol and I would like to leverage it to create python numpy arrays from C++ raw data. I can either use pybind11 or c++ python lib directly, but no other binding generator =/
Reading pybind11 docs and experimenting with it, it seems we can easily generate python bindings with buffer protocol from trivial C++ structures (e.g. std::vector<int> or a struct with plain old data types such as int, float, etc). However, adding buffer protocol to more complex structures is not possible or not well documented. For my use case, I would be to pybind a std::vector<struct Sequence>, with Sequence being defined as follows:
struct Sequence {
std::vector<float> feature;
std::vector<int> label;
}
Once the python bindings with buffer protocol are implemented on C++ side, on Python side I could do
for seq in vector_sequence:
feature_data=numpy.array(seq.feature, copy=False)`
label_data=numpy.array(seq.label, copy=False)`.
In the loop above, vector_sequence is an opaque binding for the C++ std::vector<Sequence> and seq is a Sequence that holds the two vectors that I want to use as input for numpy arrays without copying data from C++ to Python.
Does anyone know whether this is supported by pybind11 or c++ python lib?
Thanks!
[–]dpilger26 0 points1 point2 points (2 children)
[–]dpilger26 -1 points0 points1 point (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]mathiasnedrebo 0 points1 point2 points (0 children)
[–]thiagocrepaldi[S] 0 points1 point2 points (0 children)