Skip to content Skip to sidebar Skip to footer

Casting An Array Of C Structs To A Numpy Array

A function I'm calling from a shared library returns a structure called info similar to this: typedef struct cmplx { double real; double imag; } cmplx; typedef struct info{

Solution 1:

Define your field as double and make a complex view with numpy:

class info(Structure):
    _fields_ = [("name", c_char_p),
                ("arr_len", c_int),
                ("real_data", POINTER(c_double)),
                ("cmplx_data", POINTER(c_double))]

c_func.restype = info
ret_val = c_func()
data = np.ctypeslib.as_array(ret_val.contents.real_data, shape=(info.contents.arr_len,))
complex_data = np.ctypeslib.as_array(ret_val.contents.cmplx_data, shape=(info.contents.arr_len,2)).view('complex128')

Post a Comment for "Casting An Array Of C Structs To A Numpy Array"