使用SWIG在Python中包装C ++类

时间:2017-05-13 15:31:31

标签: python c++ numpy swig

我有以下C ++类,我希望用SWIG包装在Python中。

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();
    };

我编写了以下SWIG接口文件,允许我将numpy数组传递给runRegression成员函数。

%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"

我相信我已经正确编写了SWIG类型图以允许我这样做。我编写了以下Python测试代码。

#!/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()

然而,我收到以下错误,暗示我实际上在我的numpy数组的类型映射中犯了一个错误。

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 *'

总而言之,我想知道我试图包装类及其成员函数的方式是否可以将numpy数组传递给const double*实际上是正确的。如果没有,那么惯例是什么?

编辑: - 我更新了SWIG文件以包含以下内容:

%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)};

并将要包装的函数的签名更改为以下内容:

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);

以便参数的排序与类型映射的排序相匹配。但是,我仍然收到以下错误:

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

2 个答案:

答案 0 :(得分:1)

如果查看do { let s = try String(contentsOf: URL(string: "https://www.google.com/")!, encoding: .utf8) print(s) } catch { print(error) } 标题中的示例,您会看到如何应用numpy.i类型地图的示例。

在您的情况下,您应该更改

NumPy

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

请注意%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)}; 的使用方式

答案 1 :(得分:0)

我认为,在将numpy ndarray提供给C ++函数时,不应添加ndarray维信息,在您的情况下为:2, 30, 30...

相反,只需提供诸如regressor.runRegression(X, Y, X_s, Y_s, {'a' : 0.0, 'b' : 0.0})之类的参数即可。