如何将指向c数组的指针转换为python数组

2024-05-15 07:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个C++回调函数,它使用CyType调用Python。此函数的参数是指向double数组和元素数的指针。

有很多元素,大约200万。我需要把这个发送到scipy函数中。

< > C++原型是:

bool (*ptsetDataSource)(double*, long long);

以下是python代码:

CPF_setDataSource = CFUNCTYPE(c_bool, POINTER(c_double),c_longlong)
CPF_setSelection= CFUNCTYPE(c_bool,c_char_p, c_longlong,c_longlong)
CPF_ResetSequence = CFUNCTYPE(c_bool)

def setDataSource(Data, DataLength):
    Datalist=[0.0]*100
    for i in range(0,100):
        Datalist[i]=Data[i]

    print Datalist
    return True

问题是print datalist返回:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

这是不正确的(当在c++端检查时,数据中会填充很多其他数字)。

另外,如果我使用此代码将数据转换为python列表,它会在分配步骤锁定计算机。

无论如何,是否要从C++数组加载数据,然后将其转换成适合SCIPY的数组?


Tags: 数据函数代码元素data数组longbool
1条回答
网友
1楼 · 发布于 2024-05-15 07:47:39

如果Data(c_double*DataLength.value)数组,则可以:

a = np.frombuffer(Data) # no copy. Changes in `a` are reflected in `Data`

如果DataPOINTER(c_double),则可以使用^{}获得numpy数组。这与你的问题相同,但速度更快:

a = np.fromiter(Data, dtype=np.float, count=DataLength.value) # copy

要在不复制的情况下从POINTER(c_double)实例创建numpy数组,可以使用^{}方法:

ArrayType = ctypes.c_double*DataLength.value
addr = ctypes.addressof(Data.contents)
a = np.frombuffer(ArrayType.from_address(addr))

或者

array_pointer = ctypes.cast(Data, ctypes.POINTER(ArrayType))
a = np.frombuffer(array_pointer.contents)

两个方法都将POINTER(c_double)实例转换为(c_double*DataLength),然后将其传递给^{}

基于Cython的解决方案

Is there anyway to load the data from the C++ array and then convert it to an array fit for scipy?

下面是Python的C扩展模块(用Cython编写),它将转换函数作为C API提供:

cimport numpy as np
np.import_array() # initialize C API to call PyArray_SimpleNewFromData

cdef public api tonumpyarray(double* data, long long size) with gil:
    if not (data and size >= 0): raise ValueError
    cdef np.npy_intp dims = size
    #NOTE: it doesn't take ownership of `data`. You must free `data` yourself
    return np.PyArray_SimpleNewFromData(1, &dims, np.NPY_DOUBLE, <void*>data)

它可以与ctypes一起使用,如下所示:

from ctypes import (PYFUNCTYPE, py_object, POINTER, c_double, c_longlong,
                    pydll, CFUNCTYPE, c_bool, cdll)

import pointer2ndarray
tonumpyarray = PYFUNCTYPE(py_object, POINTER(c_double), c_longlong)(
    ("tonumpyarray", pydll.LoadLibrary(pointer2ndarray.__file__)))

@CFUNCTYPE(c_bool, POINTER(c_double), c_longlong)
def callback(data, size):
    a = tonumpyarray(data, size)
    # call scipy functions on the `a` array here
    return True

cpplib = cdll.LoadLibrary("call_callback.so") # your C++ lib goes here
cpplib.call_callback(callback)

其中call_callback是:^{}

相关问题 更多 >