shashashamti2008
shashashamti2008

Reputation: 2337

Pybind11 Issue for Eigen Return Type

I am trying to expose data_all which is a vector of data to python using pybind11:

struct data {
    std::vector<Eigen::ArrayXf> values;
    std::vector<int> indices;
    float x;
    float y;
    float z;
};

class dataBuffer {
public:
    std::vector<data> data_all;
    Eigen::ArrayXf getValues(ssize_t i, ssize_t j) { return data_all.at(i).values.at(j); };
};

I defined my pybind11 wrapper as follows:

PYBIND11_MODULE(example, m) {
    py::class_<data>(m, "data")
        .def(py::init<>())
        .def_readonly("values", &data::values)
        .def_readonly("indices", &data::indices)
        .def_readonly("x", &data::x)
        .def_readonly("y", &data::y)
        .def_readonly("z", &data::z);

    py::class_<dataBuffer>(m, "dataBuffer")
        .def(py::init<>())
        .def("getValues", &dataBuffer::getValues);
}

My C++ sample code is

namespace py = pybind11;
int main()
{
    data d;
    d.x = 1.1;
    d.y = 2.1;
    d.z = 3.1;
    d.indices.push_back(4);
    d.values.push_back(Eigen::ArrayXf::LinSpaced(50, 0.0, 50 - 1.0));
    d.indices.push_back(5);
    d.values.push_back(Eigen::ArrayXf::LinSpaced(60, 0.0, 60 - 1.0));
    d.indices.push_back(11);
    d.values.push_back(Eigen::ArrayXf::LinSpaced(70, 0.0, 70 - 1.0));

    dataBuffer D;
    D.data_all.push_back(d);
    D.data_all.push_back(d);
    std::cout << D.getValues(0,0) << "\n";

    py::scoped_interpreter guard{};
    py::object result = py::module::import("pybind11_test").attr("testData")(0,0);

}

Content of the file pybind11_test.py

import numpy as np
import example as m
def testData(buffer):
    help(buffer)
    a = buffer.getValues(0,0) # trying to retrieve the data buffer created in C++
    print(a)

help(buffer) prints the following signature:

Help on method getValues in module example:

getValues(...) method of example.dataBuffer instance
    getValues(self: example.dataBuffer, arg0: int, arg1: int) -> Eigen::Array<float,-1,1,0,-1,1>

However, when python reaches to executing buffer.getValues(0,0) it fails:

(in Visual Studio)
pybind11::error already set at memory location

(in Python)
Traceback (most recent call last):    
  File "<stdin>", line 1, in <module>
TypeError: getValues(): incompatible function arguments. The following argument types are supported:
    1. (self: example.dataBuffer, arg0: int, arg1: int) -> Eigen::Array<float,-1,1,0,-1,1>

I believe the Eigen return type is not appreciated by Python. Could someone help me what the issue could be? How should I help Python understands the return type for getValues so that I can further process it using numpy library?

Upvotes: 2

Views: 1145

Answers (1)

Wim Lavrijsen
Wim Lavrijsen

Reputation: 3778

I presume this line:

py::object result = py::module::import("pybind11_test").attr("testData")(0,0);

was meant to be:

py::object result = py::module::import("pybind11_test").attr("testData")(D);

Other than that, all you are missing is:

#include "pybind11/eigen.h"

at the top of your wrapper code.

Upvotes: 4

Related Questions