2016-01-09 61 views
0

我想将这个问题How to access arrays passed to ctypes callbacks as numpy arrays?扩展为复数的情况。NumPy复数组和回调

test.py

#!/usr/bin/env python 

import numpy as np 
import numpy.ctypeslib as npct 
import ctypes 
import os.path 

array_1d_double = npct.ndpointer(dtype=np.complex128, ndim=1, flags='CONTIGUOUS') 

callback_func = ctypes.CFUNCTYPE(
    None,   # return 
    ctypes.POINTER(np.complex128), # x 
    ctypes.c_int  # n 
) 

libtest = npct.load_library('libtest', os.path.dirname(__file__)) 
libtest.callback_test.restype = None 
libtest.callback_test.argtypes = [array_1d_double, ctypes.c_int, callback_func] 


@callback_func 
def callback(x, n): 
    x = npct.as_array(x, (n,)) 
    print("x: {0}, n: {1}".format(x[:n], n)) 


if __name__ == '__main__': 
    x = np.array([20, 13, 8, 100, 1, 3], dtype=np.complex128) 
    libtest.callback_test(x, x.shape[0], callback) 

test.c的

#include <complex.h> 

typedef void (*callback_t)(
    void* *x, 
    int n 
); 

void callback_test(void** x, int n, callback_t callback) { 
    _Complex double* cx = (_Complex double*)x; 
     for(int i = 1; i <= 5; i++) { 

       for(int j = 0; j < n; j++) { 
         cx[j] = cx[j]/i; 
       } 

       callback(x, n); 
     } 
} 

给我:

Traceback (most recent call last):
File "test.py", line 12, in <module>
ctypes.POINTER(np.complex128), # x
TypeError: type must have storage info

关于如何解决此问题的任何想法?

回答

1

ctypes无法直接处理numpy类型。因此“POINTER(np.complex128)”导致错误。您可以使用ndpointer结果作为类型,并在as_array中移除shape参数。这适用于我:

#!/usr/bin/env python 

import numpy as np 
import numpy.ctypeslib as npct 
import ctypes 
import os.path 

array_1d_double = npct.ndpointer(dtype=np.complex128, ndim=1, flags='CONTIGUOUS') 

callback_func = ctypes.CFUNCTYPE(
    None,   # return 
    array_1d_double, 
    ctypes.c_int  # n 
) 

libtest = npct.load_library('libtest', os.path.dirname(__file__)) 
libtest.callback_test.restype = None 
libtest.callback_test.argtypes = [array_1d_double, ctypes.c_int, callback_func] 


@callback_func 
def callback(x, n): 
    x._shape_ = (n,) 
    x = npct.as_array(x) 
    print("x: {0}, n: {1}".format(x[:n], n)) 



if __name__ == '__main__': 
    x = np.array([20, 13, 8, 100, 1, 3, 11, 12, 13], dtype=np.complex128) 
    libtest.callback_test(x, x.shape[0], callback) 
+0

感谢您的回答。当'shape(6,)'没有设置时,这不起作用(所以我不能用不同的数组大小)。 – BigDawg

+1

我添加了一个“hack”来将形状分配给返回的ndpointer的private _shape_属性。我认为应该有一个更干净的方式来做到这一点,但至少这适用于我的numpy版本。 – Niemerds

+0

谢谢,这似乎在我的应用程序中工作。我不是NumPy的专家,能否让我知道使用“黑客”有什么危险? – BigDawg