Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return an std::vector to python as a numpy array

Using Pybind11, I am trying to pass a numpy array to c++ into a std::vector, multiply it by 2, and return this std::vector to python as a numpy array.

I have achieved the first step but the third is doing some strange things. For passing it back I have used: py::array ret = py::cast(vect_arr); By strange things I mean that the vector returned in Python doesn't have the correct dimensions nor the correct order.

As example, I have as array:

[[ 0.78114362  0.06873818  1.00364053  0.93029671]
 [ 1.50885413  0.38219005  0.87508337  2.01322396]
 [ 2.19912915  2.47706644  1.16032292 -0.39204517]]

and the code returns:

array([[ 1.56228724e+000,  3.01770826e+000,  4.39825830e+000,
         5.37804299e+161],
       [ 1.86059342e+000,  4.02644793e+000, -7.84090347e-001,
         1.38298992e-309],
       [ 1.75016674e+000,  2.32064585e+000,  0.00000000e+000,
         1.01370255e-316]])

I have read the documentation but I am having trouble understanding most of it.

Here an example to try:

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <Python.h>
namespace py = pybind11;
py::module nn = py::module::import("iteration");


py::array nump(py::array arr){

    auto arr_obj_prop = arr.request();
    //initialize values
    double *vals = (double*) arr_obj_prop.ptr;

    unsigned int shape_1 = arr_obj_prop.shape[0];
    unsigned int shape_2 = arr_obj_prop.shape[1];


    std::vector<std::vector <double>> vect_arr( shape_1, std::vector<double> (shape_2));

    for(unsigned int i = 0; i < shape_1; i++){
      for(unsigned int j = 0; j < shape_2; j++){
        vect_arr[i][j] = vals[i*shape_1 + j*shape_2] * 2;
      }
    }   

    py::array ret =  py::cast(vect_arr); //py::array(vect_arr.size(), vect_arr.data());
    return ret;

}

PYBIND11_MODULE(iteration_mod, m) {

    m.doc() = "pybind11 module for iterating over generations";

    m.def("nump", &nump,
      "the function which loops over a numpy array");
}

And the Python code:

import numpy as np
import iteration_mod as i_mod

class iteration(object):
    def __init__(self):
        self.iterator = np.random.normal(0,1,(3,4))

    def transform_to_dict(self):
        self.dict = {}
        for i in range(self.iterator.shape[0]):
            self.dict["key_number_{}".format(i)] = self.iterator[i,:]
        return self.dict

    def iterate_iterator(self):
        return i_mod.nump(self.iterator)

    def iterate_dict(self):
        return i_mod.dict(self)

a = iteration()
print(a.iterator)
print(a.iterate_iterator())

All of this compiled with: c++ -O3 -Wall -fopenmp -shared -std=c++11 -fPIC python3 -m pybind11 --includes iteration_mod.cpp -o iteration_mod.so

like image 417
Joachim Avatar asked Nov 07 '19 20:11

Joachim


1 Answers

std::vector<std::vector<double>> does not have the memory layout of a 2D builtin array, so that py::array(vect_arr.size(), vect_arr.data()); will not work.

It looks like the py::cast does do the proper copy conversions and propagates the values from the vector to a new numpy array, but this line:

vect_arr[i][j] = vals[i*shape_1 + j*shape_2] * 2;

is not right. It should be:

vect_arr[i][j] = vals[i*shape_2 + j] * 2;
like image 198
Wim Lavrijsen Avatar answered Sep 18 '22 00:09

Wim Lavrijsen