memoryview

Passing/Returning Cython Memoryviews vs NumPy Arrays

ε祈祈猫儿з 提交于 2019-12-11 05:14:44
问题 I am writing Python code to accelerate a region properties function for labeled objects in a binary image. The following code will calculate the number of border pixels of a labeled object in a binary image given the indices of the object. The main() function will cycle through all labeled objects in a binary image 'mask' and calculate the number of border pixels for each one. I am wondering what the best way is to pass or return my variables in this Cython code. The variables are either in

Cython: size attribute of memoryviews

99封情书 提交于 2019-12-10 15:09:44
问题 I'm using a lot of 3D memoryviews in Cython, e.g. cython.declare(a='double[:, :, ::1]') a = np.empty((10, 20, 30), dtype='double') I often want to loop over all elements of a . I can do this using a triple loop like for i in range(a.shape[0]): for j in range(a.shape[1]): for k in range(a.shape[2]): a[i, j, k] = ... If I do not care about the indices i , j and k , it is more efficient to do a flat loop, like cython.declare(a_ptr='double*') a_ptr = cython.address(a[0, 0, 0]) for i in range(size

Increasing performance of highly repeated numpy array index operations

非 Y 不嫁゛ 提交于 2019-12-07 16:00:33
In my program code I've got numpy value arrays and numpy index arrays. Both kinds are preallocated and predefined during program initialization. Each part of the program has one array values on which calculations are performed, and three index arrays idx_from_exch , idx_values and idx_to_exch . There is on global value array to exchange the values of several parts: exch_arr . The index arrays most of the times have between 2 and 5 indices, seldom (most probably never) more indices are needed. dtype=np.int32 , shape and values are constant during the whole program run. Thus I set ndarray.flags

Cython: optimize native Python memoryview

懵懂的女人 提交于 2019-12-07 07:13:25
问题 I have a function (from an external Python library) that returns a memoryview object that I want to process in Cython. Is there a way to convert it to a typed memoryview of bytes (without copy) for efficiency? And how would I do that? This doesn't work: mv = memoryview(b'1234') cdef char[:] *tmv tmv = mv ^ ------------------------------------------------------------ /home/me/.cache/ipython/cython/_cython_magic_f9f608355444e2d6828580906623cea8.pyx:3:6: Cannot convert Python object to '__Pyx

Obtaining pointer to python memoryview on bytes object

随声附和 提交于 2019-12-06 08:47:40
问题 I have a python memoryview pointing to a bytes object on which I would like to perform some processing in cython. My problem is: because the bytes object is not writable, cython does not allow constructing a typed (cython) memoryview from it I cannot use pointers either because I cannot get a pointer to the memoryview start Example: In python: array = memoryview(b'abcdef')[3:] In cython: cdef char * my_ptr = &array[0] fails to compile with the message: Cannot take address of Python variable

Cython: optimize native Python memoryview

风格不统一 提交于 2019-12-05 16:06:35
I have a function (from an external Python library) that returns a memoryview object that I want to process in Cython. Is there a way to convert it to a typed memoryview of bytes (without copy) for efficiency? And how would I do that? This doesn't work: mv = memoryview(b'1234') cdef char[:] *tmv tmv = mv ^ ------------------------------------------------------------ /home/me/.cache/ipython/cython/_cython_magic_f9f608355444e2d6828580906623cea8.pyx:3:6: Cannot convert Python object to '__Pyx_memviewslice *' This answer linked by @CodeSurgeon is a possibility to do it. However, since Cython 0.28

Numpy set array memory

雨燕双飞 提交于 2019-12-05 13:23:41
I have a question regarding numpys memory views: Suppose we have two arrays with memory: import numpy as np import gc x = np.arange(4*3).reshape(4,3).astype(float) y = (np.arange(5) - 5).astype(float) y_ref = y We use these ( x , y ) in a framework, such that we cannot just redefine them, as the user may have linked them for himself (as in y_ref ). Now we want to combine their memory in one view. So, that the single view, say p shares the memory with both arrays. I did it in the following way, but do not know if this causes a memory leak: p = np.empty(x.size+y.size, dtype=float) # create new

Buffers and Memoryview Objects explained for the non-C programmer

拜拜、爱过 提交于 2019-12-04 19:47:19
问题 Python 2.7 has introduced a new API for buffers and memoryview objects. I read the documentation on them and I think I got the basic concept (accessing the internal data of an object in a raw form without copying it, which I suppose means a "faster and less memory-hungry" way to get object data), but to really understand the documentation, the reader should have a knowledge of C that is beyond the one I have. I would be very grateful if somebody would take the time to: explain buffers and

Cython: Convert memory view to NumPy array

不想你离开。 提交于 2019-12-04 16:10:53
问题 How to convert a typed memoryview to an NumPy array in cython? The docs have cimport numpy as np import numpy as np numpy_array = np.asarray(<np.int32_t[:10, :10]> my_pointer) I took this for my case np.asarray(<np.float_t[:, :]> my_memview) Using this the compiler tells me: Can only create cython.array from pointer or array Copying or not is not so decicive. I couldn't find any help on this. 回答1: You should just be able to use np.asarray directly on the memoryview itself, so something like:

Obtaining pointer to python memoryview on bytes object

老子叫甜甜 提交于 2019-12-04 14:54:24
I have a python memoryview pointing to a bytes object on which I would like to perform some processing in cython. My problem is: because the bytes object is not writable, cython does not allow constructing a typed (cython) memoryview from it I cannot use pointers either because I cannot get a pointer to the memoryview start Example: In python: array = memoryview(b'abcdef')[3:] In cython: cdef char * my_ptr = &array[0] fails to compile with the message: Cannot take address of Python variable cdef char[:] my_view = array fails at runtime with the message: BufferError: memoryview: underlying