我试图编写两个Cython函数来包装外部函数。功能是彼此相反的;一个接受一个字符串,并返回一个带有两个字段的结构:一个指向2D数组的void指针(第二个维度总是两个元素:[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], … ]
),以及数组的长度。另一个接受相同的结构,并返回一个字符串。到目前为止,我已经得到了以下内容。它编译,但嵌套列表的转换肯定是不正确的。
我的.pxd
:
cdef extern from "header.h":
struct _FFIArray:
void* data
size_t len
cdef _FFIArray decode_polyline_ffi(char* polyline, int precision);
cdef char* encode_coordinates_ffi(_FFIArray, int precision);
cdef void drop_float_array(_FFIArray coords);
cdef void drop_cstring(char* polyline)
我的.pyx
:
import numpy as np
from pypolyline_p cimport (
_FFIArray,
decode_polyline_ffi,
encode_coordinates_ffi,
drop_float_array,
drop_cstring
)
def encode_coordinates(coords, int precision):
""" coords looks like [[1.0, 2.0], [3.0, 4.0], …] """
cdef double[::1] ncoords = np.array(coords, dtype=np.float64)
cdef _FFIArray coords_ffi
# Wrong
coords_ffi.data = <void*>&ncoords[0]
# Wrong
coords_ffi.len = ncoords.shape[0]
cdef char* result = encode_coordinates_ffi(coords_ffi, precision)
cdef bytes polyline = result
drop_cstring(result)
return polyline
def decode_polyline(bytes polyline, int precision):
cdef char* to_send = polyline
cdef _FFIArray result = decode_polyline_ffi(to_send, precision)
# Wrong
cdef double* incoming_ptr = <double*>(result.data)
# Wrong
cdef double[::1] view = <double[:result.len:1]>incoming_ptr
coords = np.copy(view)
drop_float_array(result)
return coords
答案 0 :(得分:3)
我认为问题是你正在尝试使用2D数组和1D内存视图
在编码功能
中 # the coords are a 2D, C contiguous array
cdef double[:,::1] ncoords = np.array(coords, dtype=np.float64)
# ...
coords_ffi.data = <void*>&ncoords[0,0] # take the 0,0 element
# the rest stays the same
在解码功能
中 # specify it as a 2D, len by 2, C contiguous array
cdef double[:,::1] view = <double[:result.len,:2:1]>incoming_ptr
# the rest stays the same
(你的FFI函数可能期望Fortran连续数组。在这种情况下,::1
会进入内存视图的第一维,你也会更改incoming_ptr
)