- Notifications
You must be signed in to change notification settings - Fork 67
Open
Description
I'm trying to wrap a custom type using pybind11 and xtensor-python. Although I can create a numpy array of my wrapped type from Python, I cannot pass this numpy array to C++ functions or pass xt::xarray to Python.
The C++ part is
#include<pybind11/numpy.h> #include<pybind11/pybind11.h> #include<xtensor/xarray.hpp> #defineFORCE_IMPORT_ARRAY #include<xtensor-python/pyarray.hpp>structQuantity{double q; Quantity(double q=0) : q(q){} }; PYBIND11_MODULE(test_array, test_array){xt::import_numpy(); pybind11::class_<Quantity>(test_array, "Quantity") .def(pybind11::init<double>()) .def_readwrite("q", &Quantity::q); test_array.def("create", [](){return xt::xarray<Quantity>{1.1, 2.2}}); test_array.def( "consume", [](xt::xarray<Quantity> const & array){for(auto && item: array){std::cout << item.q << "\n"} })}And the Python code
importnumpyimporttest_arrayq1=test_array.Quantity(42) q2=test_array.Quantity(3.14) a=numpy.array([q1, q2]) print([x.qforxina]) try: test_array.create() exceptExceptionase: print(e) try: test_array.consume(a) exceptExceptionase: print(e)Both try/except blocks in Python yield a RuntimeError saying "NumPy type info missing for 8Quantity"
Is this use case supported by xtensor-python? If so, what am I missing? If not, what alternative would you suggest?
Metadata
Metadata
Assignees
Labels
No labels