Miles
Miles

Reputation: 2537

Wrapping C++ class in Python with SWIG

I have the following C++ class that I wish to wrap in Python with SWIG.

typedef std::map<std::string, double> ParamaterSet;

class GPRegressor{
public:
    double runRegression(const double *trainData, const double *trainTruth, int trainRows, int trainCols,
                             const double *testData, const double *testTruth, int testRows, int testCols,
                             const ParamaterSet &params);

    GPRegressor(KernelType kernType = SQUARED_EXPONENTIAL);
    ~GPRegressor();
    };

I have written the following SWIG interface file to allow me to pass numpy arrays to the runRegression member function.

%module pyGP
%{
#define SWIG_FILE_WITH_INIT
#include "lib/typedefs.h"
#include "lib/Kernels.h"
#include "lib/GPRegressor.h"
%}

%include "numpy.i"
%include "std_string.i"
%include "std_map.i"
%template(map_string_double) std::map<std::string, double>;

%init %{
  import_array();
%}

%apply (const double *arr, int dim1, int dim2) {(const double *data, int dimx, int dimy)}
%apply (double *arr, int dim1, int dim2) {(double *data, int dimx, int dimy)}

%include "lib/typedefs.h"
%include "lib/Kernels.h"
%include "lib/GPRegressor.h"

I believe that I have correctly written the SWIG typemaps to allow me to do this. I have written the following Python test code.

#!/usr/bin/python3
import sys, os
sys.path.append(os.path.realpath('../build'))
import numpy as np

from pyGP import *

def runRegression():
    X = np.random.rand(30, 2)
    Y = np.random.rand(30, 1)
    X_s = np.random.rand(30, 2)
    Y_s = np.random.rand(30, 1)

    regressor = GPRegressor()
    regressor.runRegression(X, Y, 30, 2, X_s, Y_s, 30, 1, {'a' : 0.0, 'b' : 0.0})

if __name__ == "__main__":
    runRegression()

However I receive the following error, implying that I have in fact made a mistake with my typemaps for the numpy arrays.

Traceback (most recent call last):
  File "./demo.py", line 22, in <module>
    runRegression()
  File "./demo.py", line 18, in runRegression
    regressor.runRegression(X, Y, 30, 2, X_s, Y_s, 30, 1, {'a' : 0.0, 'b' : 0.0})
  File "/home/jack/GitRepos/GaussianProcess/build/pyGP.py", line 337, in runRegression
    return _pyGP.GPRegressor_runRegression(self, trainData, trainTruth, trainRows, trainCols, testData, testTruth, testRows, testCols, params)
TypeError: in method 'GPRegressor_runRegression', argument 2 of type 'double const *'

To summarise, I would like to know if the way I am attempting to wrap the class and it's member functions such that I can pass numpy arrays to const double* is in fact correct. If not, what is the convention?

EDIT:- I have updated the SWIG file to contain the following:

%apply (double *IN_ARRAY2, int DIM1, int DIM2) {(const double *trainData, int trainCols, int trainRows)};
%apply (double *IN_ARRAY1, int DIM1) {(const double *trainTruth, int trainRows)};
%apply (double *IN_ARRAY2, int DIM1, int DIM2) {(const double *testData, int testCols, int testRows)};
%apply (double *IN_ARRAY1, int DIM1) {(const double *testTruth, int testRows)};

and have changed the signature of the function to be wrapped to the following:

double runRegression(const double *trainData, int trainCols, int trainRows, const double *trainTruth,
                     int trainTruthRows, const double *testData, int testCols, int testRows,
                     const double *testTruth, int testTruthRows, const ParamaterSet &params);

so that the ordering of the parameters matches that of the typemaps. However, I am still receiving the following error:

Traceback (most recent call last):
  File "./demo.py", line 23, in <module>
    runRegression()
  File "./demo.py", line 19, in runRegression
    regressor.runRegression(X, 2, 30, Y, 30, X_s, 2, 30, Y_s, 30, {'a' : 0.0, 'b' : 0.0})
TypeError: runRegression() takes 6 positional arguments but 12 were given

Upvotes: 2

Views: 909

Answers (2)

Here
Here

Reputation: 51

I think when you supply your numpy ndarray to your C++ function, you are not supposed to add your ndarray dimension information, which in your case is:2, 30, 30....

Instead , just provide arguments like: regressor.runRegression(X, Y, X_s, Y_s, {'a' : 0.0, 'b' : 0.0}).

Upvotes: 0

Jens Munk
Jens Munk

Reputation: 4725

If look into the examples in the numpy.i header, you will see examples of how to apply the NumPy typemaps.

In your case, you should change

%apply (double *arr, int dim1, int dim2) {(double *data, int dimx, int dimy)}

into

%apply (double* IN_ARRAY2, int DIM1, int DIM2) {(const double *testTruth, int testRows, int testColsdouble)};

%apply (double* IN_ARRAY2, int DIM1, int DIM2) {(const double *trainTruth, int trainRows, int trainCols)};

Note how IN_ARRAY2 is used

Upvotes: 1

Related Questions