memoryview

Cython: Should I use np.float_t rather than double for typed memory views

时光总嘲笑我的痴心妄想 提交于 2019-12-01 00:12:23
问题 Concerning memoryviews in cython, is there any advantage of typing a view with NumPy types such as np.float_t instead of simply do double if I'm working with numpy float arrays? And should I type the cdef then the same way, doing e. g. ctypedef np.float64_t np_float_t ... @cython.profile(False) @cython.wraparound(False) @cython.boundscheck(False) cdef np_float_t mean_1d(np_float_t [:] v) nogil: cdef unsigned int n = v.shape[0] cdef np_float_t n_sum = 0. cdef Py_ssize_t i for i in range(n): n

Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView

不想你离开。 提交于 2019-11-30 07:44:54
问题 I am converting a Cython memoryview to a numpy array (to be able to use it in pure Python code): from libc.stdlib cimport realloc cimport numpy as np DTYPE = np.float64 ctypedef np.float64_t DTYPE_t cpdef np.ndarray[DTYPE_t] compute(DTYPE_t[:,::1] data): cdef unsigned int Nchannels = data.shape[0] cdef unsigned int Ndata = data.shape[1] cdef DTYPE_t* output = NULL cdef DTYPE_t[::1] mv output = <DTYPE_t*>realloc(output, Ndata*sizeof(output)) if not output: raise MemoryError() mv = <DTYPE_t[

Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView

馋奶兔 提交于 2019-11-29 05:24:51
I am converting a Cython memoryview to a numpy array (to be able to use it in pure Python code): from libc.stdlib cimport realloc cimport numpy as np DTYPE = np.float64 ctypedef np.float64_t DTYPE_t cpdef np.ndarray[DTYPE_t] compute(DTYPE_t[:,::1] data): cdef unsigned int Nchannels = data.shape[0] cdef unsigned int Ndata = data.shape[1] cdef DTYPE_t* output = NULL cdef DTYPE_t[::1] mv output = <DTYPE_t*>realloc(output, Ndata*sizeof(output)) if not output: raise MemoryError() mv = <DTYPE_t[:Ndata]>output mv[10:Ndata-10] = 0.0 # various calculations... return np.asarray(mv, dtype=DTYPE, order='C

When should a memoryview be used? [duplicate]

大憨熊 提交于 2019-11-28 17:14:52
This question already has an answer here: What exactly is the point of memoryview in Python 5 answers The full description of memoryview can be found here : Create a memoryview that references obj . obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray . A memoryview has the notion of an element , which is the atomic memory unit handled by the originating object obj . For many simple types such as bytes and bytearray , an element is a single byte, but other types such as array.array may have bigger elements. A memoryview is

How to declare 2D c-arrays dynamically in Cython

爷,独闯天下 提交于 2019-11-27 16:46:52
问题 I need to perform a lot of work using 2D numpy arrays of various sizes and I would like to offload these calculations onto cython. The idea is that my 2D numpy arrays would be passed from python to cython where it would be converted into c-array or memory view and used in a cascade of other c-level functions to do the calculations. After some profiling I ruled out using numpy arrays in cython due to some serious python overhead. Using memory views was MUCH faster and quite easy to use, but I

When should a memoryview be used? [duplicate]

空扰寡人 提交于 2019-11-27 10:31:29
问题 This question already has answers here : What exactly is the point of memoryview in Python (5 answers) Closed 3 years ago . The full description of memoryview can be found here: Create a memoryview that references obj . obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray . A memoryview has the notion of an element , which is the atomic memory unit handled by the originating object obj . For many simple types such as bytes and

What exactly is the point of memoryview in Python

若如初见. 提交于 2019-11-26 06:29:42
问题 Checking the documentation on memoryview: memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying. class memoryview (obj) Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray. Then we are given the sample code: >>> v = memoryview(b\'abcefg\') >>> v[1] 98 >>> v[-1] 103 >>> v[1:4] <memory at 0x7f3ddc9f4350> >>> bytes(v[1