我正在尝试编译pyBKT项目,它使用的boost::python
库在删除boost/python/numeric.hpp
时在版本1.63中发生了重大变化。我已按照建议将其更新为boost/numpy.hpp
,但是我无法将代码的其他部分更新为新的API。
特别是,我对下面的将结果包装到numpy
对象中的代码感到麻烦。
//wrapping results in numpy objects.
npy_intp all_stateseqs_dims[2] = {1, bigT};
PyObject * all_stateseqs_pyObj = PyArray_SimpleNewFromData(2, all_stateseqs_dims, NPY_INT, all_stateseqs);
boost::python::handle<> all_stateseqs_handle( all_stateseqs_pyObj );
boost::python::numpy::ndarray all_stateseqs_handle_arr( all_stateseqs_pyObj );
npy_intp all_data_dims[2] = {num_subparts, bigT};
PyObject * all_data_pyObj = PyArray_SimpleNewFromData(2, all_data_dims, NPY_INT, all_data);
boost::python::handle<> all_data_handle( all_data_pyObj );
boost::python::numpy::ndarray all_data_arr( all_data_handle );
报告的错误是
generate/synthetic_data_helper.cpp:161:65: error: no matching function for call to ‘boost::python::numpy::ndarray::ndarray(boost::python::handle<>&)’
我知道这意味着找不到具有此参数的构造函数,但由于我不是C ++程序员,所以我不知道如何更改它。
答案 0 :(得分:0)
我只是直接使用numpy API,因为我必须支持Numpy 1.4(默认情况下是RHEL6)。事实证明还不错,您只需返回一个新对象即可。假设您已经暴露了一个图像对象:
// accessor to wrap data from image up into read-only numpy array
//
static inline object image_get_data(object &imageobj) {
// extract image base
const image &img = extract<const image&>(imageobj);
// build read-only array around data
npy_intp dims[2] = {img.width, img.height};
PyObject* array = PyArray_New(
&PyArray_Type, 2, dims, NPY_COMPLEX64, NULL, (void*)img.data(), 0,
NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED, NULL
);
// increment reference to image object and set base pointer
// of array, this will establish an ownership link so that
// image instance won't be destroyed before the array.
incref(image.ptr());
NPY_SET_BASE(array, image.ptr());
return object(handle<>(array));
}
然后将以下内容添加到图像对象:
.add_property("data", &image_get_data, "read-only numpy array containing image data")