oce*_*hug 2 c python swig numpy cython
我正在尝试加速我的Numpy代码,并决定我想实现一个特定的函数,我的代码大部分时间都在C中.
我实际上是C中的新手,但我设法编写了一个函数,它将矩阵中的每一行规范化为1.我可以编译它并用一些数据(在C中)测试它并且它做我想要的.那时我为自己感到骄傲.
现在我试图从Python中调用我的光荣函数,它应该接受一个2d-Numpy数组.
我尝试过的各种事情都是
痛饮
SWIG + numpy.i
ctypes的
我的功能有原型
void normalize_logspace_matrix(size_t nrow, size_t ncol, double mat[nrow][ncol]);
Run Code Online (Sandbox Code Playgroud)
因此它需要一个指向可变长度数组的指针并将其修改到位.
我尝试了以下纯SWIG接口文件:
%module c_utils
%{
extern void normalize_logspace_matrix(size_t, size_t, double mat[*][*]);
%}
extern void normalize_logspace_matrix(size_t, size_t, double** mat);
Run Code Online (Sandbox Code Playgroud)
然后我会做(在Mac OS X 64bit上):
> swig -python c-utils.i
> gcc -fPIC c-utils_wrap.c -o c-utils_wrap.o \
-I/Library/Frameworks/Python.framework/Versions/6.2/include/python2.6/ \
-L/Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/ -c
c-utils_wrap.c: In function ‘_wrap_normalize_logspace_matrix’:
c-utils_wrap.c:2867: warning: passing argument 3 of ‘normalize_logspace_matrix’ from incompatible pointer type
> g++ -dynamiclib c-utils.o -o _c_utils.so
Run Code Online (Sandbox Code Playgroud)
在Python中,我在导入模块时遇到以下错误:
>>> import c_utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initc_utils)
Run Code Online (Sandbox Code Playgroud)
接下来我尝试使用SWIG +这种方法numpy.i:
%module c_utils
%{
#define SWIG_FILE_WITH_INIT
#include "c-utils.h"
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply ( int DIM1, int DIM2, DATA_TYPE* INPLACE_ARRAY2 )
{(size_t nrow, size_t ncol, double* mat)};
%include "c-utils.h"
Run Code Online (Sandbox Code Playgroud)
但是,我没有比这更进一步:
> swig -python c-utils.i
c-utils.i:13: Warning 453: Can't apply (int DIM1,int DIM2,DATA_TYPE *INPLACE_ARRAY2). No typemaps are defined.
Run Code Online (Sandbox Code Playgroud)
SWIG似乎没有找到定义的类型映射numpy.i,但我不明白为什么,因为numpy.i在同一目录中,SWIG不会抱怨它无法找到它.
使用ctypes我没有走得太远,但很快就迷失在文档中,因为我无法弄清楚如何将它传递给2d阵列然后得到结果.
那么有人可以向我展示如何在Python/Numpy中使用我的函数的神奇技巧吗?
除非你有充分的理由不这样做,否则你应该使用cython来连接C和python.(我们开始在numpy/scipy中使用cython而不是raw C).
你可以在我的scikits talkbox中看到一个简单的例子(因为从那时起cython已经有了很大的改进,我想你今天可以写得更好).
def cslfilter(c_np.ndarray b, c_np.ndarray a, c_np.ndarray x):
"""Fast version of slfilter for a set of frames and filter coefficients.
More precisely, given rank 2 arrays for coefficients and input, this
computes:
for i in range(x.shape[0]):
y[i] = lfilter(b[i], a[i], x[i])
This is mostly useful for processing on a set of windows with variable
filters, e.g. to compute LPC residual from a signal chopped into a set of
windows.
Parameters
----------
b: array
recursive coefficients
a: array
non-recursive coefficients
x: array
signal to filter
Note
----
This is a specialized function, and does not handle other types than
double, nor initial conditions."""
cdef int na, nb, nfr, i, nx
cdef double *raw_x, *raw_a, *raw_b, *raw_y
cdef c_np.ndarray[double, ndim=2] tb
cdef c_np.ndarray[double, ndim=2] ta
cdef c_np.ndarray[double, ndim=2] tx
cdef c_np.ndarray[double, ndim=2] ty
dt = np.common_type(a, b, x)
if not dt == np.float64:
raise ValueError("Only float64 supported for now")
if not x.ndim == 2:
raise ValueError("Only input of rank 2 support")
if not b.ndim == 2:
raise ValueError("Only b of rank 2 support")
if not a.ndim == 2:
raise ValueError("Only a of rank 2 support")
nfr = a.shape[0]
if not nfr == b.shape[0]:
raise ValueError("Number of filters should be the same")
if not nfr == x.shape[0]:
raise ValueError, \
"Number of filters and number of frames should be the same"
tx = np.ascontiguousarray(x, dtype=dt)
ty = np.ones((x.shape[0], x.shape[1]), dt)
na = a.shape[1]
nb = b.shape[1]
nx = x.shape[1]
ta = np.ascontiguousarray(np.copy(a), dtype=dt)
tb = np.ascontiguousarray(np.copy(b), dtype=dt)
raw_x = <double*>tx.data
raw_b = <double*>tb.data
raw_a = <double*>ta.data
raw_y = <double*>ty.data
for i in range(nfr):
filter_double(raw_b, nb, raw_a, na, raw_x, nx, raw_y)
raw_b += nb
raw_a += na
raw_x += nx
raw_y += nx
return ty
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,除了通常的参数检查你会在python中做什么,它几乎是一样的(filter_double是一个函数,如果你愿意,可以在一个单独的库中用纯C编写).当然,因为它是编译代码,所以未能检查你的参数会使你的解释器崩溃而不是引发异常(尽管最近的cython有几个级别的安全与速度权衡).
| 归档时间: |
|
| 查看次数: |
3270 次 |
| 最近记录: |