All Questions
Tagged with cython memoryview
21 questions
43
votes
4
answers
23k
views
Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView
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
...
10
votes
2
answers
13k
views
How to declare 2D c-arrays dynamically in Cython
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 ...
30
votes
1
answer
21k
views
Cython: Convert memory view to NumPy array
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 ...
27
votes
1
answer
10k
views
Cython typed memoryviews: what they really are?
The Cython documentation explains very well what they allow for, how you can declare them, and how to use them.
However, it is still not clear to me what they really are. For example, a simple ...
9
votes
1
answer
6k
views
Cython: Should I use np.float_t rather than double for typed memory views
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 ...
8
votes
2
answers
15k
views
Which one is faster np.vstack, np.append, np.concatenate or a manual function made in cython?
I coded some program which updates a numpy list in each iteration and does some operations on it. the number of iterations depends on time. for example in 1 second, there might be 1000 to 2500 ...
8
votes
3
answers
3k
views
How to wrap a C pointer and length in a new-style buffer object in Cython?
I'm writing a Python 2.7 extension module in Cython. How do I create a Python object implementing the new-style buffer interface that wraps a chunk of memory given to me by a C library? The chunk of ...
6
votes
1
answer
3k
views
Sort memoryview in Cython
How can I sort a memoryview in-place in Cython? Is there a built-in function that can do it? Right now I have to use a numpy array instead and use numpy's sort, which is very slow.
15
votes
1
answer
3k
views
Cython Memoryview as return value
Consider this dummy Cython code:
#!python
#cython: boundscheck=False
#cython: wraparound=False
#cython: initializedcheck=False
#cython: cdivision=True
#cython: nonecheck=False
import numpy as np
# ...
14
votes
2
answers
4k
views
How to use Cython typed memoryviews to accept strings from Python?
How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview?
According to the Unicode ...
12
votes
4
answers
5k
views
Cython: Create memoryview without NumPy array?
Since I found memory-views handy and fast, I try to avoid creating NumPy arrays in cython and work with the views of the given arrays. However, sometimes it cannot be avoided, not to alter an existing ...
8
votes
4
answers
9k
views
Obtaining pointer to python memoryview on bytes object
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 ...
7
votes
2
answers
2k
views
Cython: size attribute of memoryviews
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 ...
5
votes
1
answer
3k
views
Copy Numpy array to a memoryview
I have a memoryview on a numpy array and want to copy the content of another numpy array into it by using this memoryview:
import numpy as np
cimport numpy as np
cdef double[:,::1] test = np.array([[...
5
votes
3
answers
5k
views
Initialise Cython Memoryview efficiently
I'm currently setting my MemoryViews in my Cython pyx file as follows:
@cython.boundscheck(False)
cdef int[:] fill_memview():
# This happens inside a big loop so needs to be fast
cdef int[:] ...