Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用boostpython将C结构作为numpy数组传递给python_Python_Boost_Numpy - Fatal编程技术网

使用boostpython将C结构作为numpy数组传递给python

使用boostpython将C结构作为numpy数组传递给python,python,boost,numpy,Python,Boost,Numpy,我试图通过使用Boosi::Python将C结构从C++传递到Python作为一个麻木数组。我知道有更简单的方法可以将C结构传递给python,但我之所以要使用numpy数组,是因为该解决方案是反射模型的一部分,所以需要从字符串动态生成数据类型 我可以序列化将其作为字符串传递给python的结构,并在python中调用numpy.fromstring(),从而成功地将其转换为numpy数组。但是,这不是很有效,因为将其转换为字符串会复制数据,而不是通过引用将其传递给python 例如: #inc

我试图通过使用Boosi::Python将C结构从C++传递到Python作为一个麻木数组。我知道有更简单的方法可以将C结构传递给python,但我之所以要使用numpy数组,是因为该解决方案是反射模型的一部分,所以需要从字符串动态生成数据类型

我可以序列化将其作为字符串传递给python的结构,并在python中调用numpy.fromstring(),从而成功地将其转换为numpy数组。但是,这不是很有效,因为将其转换为字符串会复制数据,而不是通过引用将其传递给python

例如:

#include <boost/python.hpp>

using namespace boost::python;

struct MyRecord
{
    uint32_t myInt;
    char myString[4];
    double myDouble;
};

class MyBaseClass
    :   public wrapper<MyBaseClass>
{
public:
    void myCallback(const MyRecord& data)
    {
        object func = get_override("myCallback");
        if (func) {
            std::string dataStr(reinterpret_cast<const char*>(data), sizeof(data));
            func(dataStr, "[('myInt','<u4'),('myString','|S4'),('myDouble','<f8')]");
        }
    }
};

BOOST_PYTHON_MODULE(example1)
{
    class_<MyBaseClass>("MyBaseClass")
        .def("myCallback", &MyBaseClass::myCallback);
}

<>我想做的是将结构转换成一个C++中的NUMPY数组,并将它直接引用到Python。

我曾用Booo::Python::Noto::Road类进行了实验,但我有问题将C++类型转换成一个NUMPY数组。构造函数正在抛出:“Type Error:没有C++的TyPython(按值)转换器:MyScess

下面是一些示例代码:

#include <boost/python.hpp>
#include <boost/python/exec.hpp>
#include <boost/python/numeric.hpp>

using namespace boost::python;

struct MyRecord
{
    uint32_t myInt;
    char myString[4];
    double myDouble;
};

class MyBaseClass
    :   public wrapper<MyBaseClass>
{
public:
    void myCallback(const MyRecord& data)
    {
        object func = get_override("myCallback");
        if (func) {
            object dtype = exec("eval(\"[('myInt','<u4'),('myString','|S4'),('myDouble','<f8')]\")");
            func(numeric::array(data, dtype));  // numeric::array throws
        }
    }
};

BOOST_PYTHON_MODULE(example2)
{
    class_<MyBaseClass>("MyBaseClass")
        .def("myCallback", &MyBaseClass::myCallback);
}

谢谢


保罗

好的,我已经设法回答了我自己的问题。这不是直截了当的,但它现在起作用了

#include <numpy/arrayobject.h>

void MyBaseClass::myCallback(const MyRecord& data)
{
    object func = get_override("myCallback");
    if (func) {
        PyArray_Descr* dtype;
        PyObject* op = Py_BuildValue("[(s,s),(s,s),(s,s)]", "myInt", "<u4", "myString", "|S4", "myDouble", "<f8");
        PyArray_DescrConverter(op, &dtype);
        Py_DECREF(op);
        PyObject* pya = PyArray_FromString(const_cast<char*>(reinterpret_cast<const char*>(&data)), sizeof(data), dtype, 1, NULL);
        // PyObject_Print(pya, stdout, 0);
        numeric::array bpa(static_cast<numeric::array>(handle<>(pya)));
        func(bpa);
    }
}

BOOST_PYTHON_MODULE(example3)
{
    import_array()
    class_<MyBaseClass>("MyBaseClass")
        .def("myCallback", &MyBaseClass::myCallback);
}
#包括
void MyBaseClass::myCallback(const MyRecord&data)
{
object func=get_override(“myCallback”);
if(func){
PyArray_Descr*dtype;

PyObject*op=Py_BuildValue(“[(s,s),(s),(s,s)]”,“myInt”,“不确定,但我认为您可能需要
numeric::array::factory
。类似问题:
#!/usr/bin/env python

import numpy
from example2 import MyBaseClass

class MyClass(MyBaseClass):
    def myCallback(self, data):
        print data # This is a numpy array passed from C++
#include <numpy/arrayobject.h>

void MyBaseClass::myCallback(const MyRecord& data)
{
    object func = get_override("myCallback");
    if (func) {
        PyArray_Descr* dtype;
        PyObject* op = Py_BuildValue("[(s,s),(s,s),(s,s)]", "myInt", "<u4", "myString", "|S4", "myDouble", "<f8");
        PyArray_DescrConverter(op, &dtype);
        Py_DECREF(op);
        PyObject* pya = PyArray_FromString(const_cast<char*>(reinterpret_cast<const char*>(&data)), sizeof(data), dtype, 1, NULL);
        // PyObject_Print(pya, stdout, 0);
        numeric::array bpa(static_cast<numeric::array>(handle<>(pya)));
        func(bpa);
    }
}

BOOST_PYTHON_MODULE(example3)
{
    import_array()
    class_<MyBaseClass>("MyBaseClass")
        .def("myCallback", &MyBaseClass::myCallback);
}