Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose a numpy array from c array in cython?

cpdef myf():
    # pd has to be a c array.
    # Because it will then be consumed by some c function.
    cdef double pd[8000]
    # Do something with pd
    ...
    # Get a memoryview.
    cdef double[:] pd_view = pd 
    # Coercion the memoryview to numpy array. Not working.
    ret = np.asarray(pd)
    return ret

I would like it to return a numpy array. How can I do it?

For the moment I have to do

pd_np = np.zeros(8000, dtype=np.double)
cdef int i
for i in range(8000):
    pd_np[i] = pd[i]
like image 789
colinfang Avatar asked Mar 13 '15 15:03

colinfang


2 Answers

If you are just declaring your array in your function why not make it a numpy array to begin with, then when you need the c array you can just grab the data pointer.

cimport numpy as np
import numpy as np

def myf():
    cdef np.ndarray[double, ndim=1, mode="c"] pd_numpy = np.empty(8000)
    cdef double *pd = &pd_numpy[0]

    # Do something to fill pd with values
    for i in range(8000):
        pd[i] = i

    return pd_numpy
like image 92
Simon Gibbons Avatar answered Sep 21 '22 18:09

Simon Gibbons


In the memview example here http://docs.cython.org/src/userguide/memoryviews.html

# Memoryview on a C array
cdef int carr[3][3][3]
cdef int [:, :, :] carr_view = carr
carr_view[...] = narr_view  # np.arange(27, dtype=np.dtype("i")).reshape((3, 3, 3))
carr_view[0, 0, 0] = 100

I can create a numpy array from carr_view, the memory view on carr, a C array.

# print np.array(carr)    # cython error
print 'numpy array on carr_view'
print np.array(carr_view)
print np.array(carr_view).sum()   # match sum3d(carr)
# or np.asarray(carr_view)


print 'numpy copy from carr_view'
carr_copy = np.empty((3,3,3))
carr_copy[...] = carr_view[...]  # don't need indexed copy
print carr_copy
print carr_copy.sum()   # match sum3d(carr)
like image 39
hpaulj Avatar answered Sep 24 '22 18:09

hpaulj