memoryview

Cython “Cannot take address of memoryview slice”

狂风中的少年 提交于 2020-02-25 06:05:07
问题 I am having trouble creating a simple class in Cython. There is little documentation related to handling memoryviews for arrays in C++ wrappers. I want to create a data class with time, x, y, and z attributes. I need these attributes to be arrays, which will ultimately be callable in Python. I previously had this working using numpy types, but I would like to do this properly using memoryviews. My background is not strong in C++. For now, I am only trying to get the time attribute working

Cython “Cannot take address of memoryview slice”

可紊 提交于 2020-02-25 06:04:58
问题 I am having trouble creating a simple class in Cython. There is little documentation related to handling memoryviews for arrays in C++ wrappers. I want to create a data class with time, x, y, and z attributes. I need these attributes to be arrays, which will ultimately be callable in Python. I previously had this working using numpy types, but I would like to do this properly using memoryviews. My background is not strong in C++. For now, I am only trying to get the time attribute working

Cython: Create memoryview without NumPy array?

江枫思渺然 提交于 2020-01-12 03:30:16
问题 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 array but create a new one. In upper functions this is not noticeable, but in often called subroutines it is. Consider the following function #@cython.profile(False) @cython.boundscheck(False) @cython.wraparound(False) @cython.nonecheck(False) cdef double [:] vec_eq(double [:] v1, int [:] v2,

Why can't cython memory views be pickled?

妖精的绣舞 提交于 2020-01-04 13:46:34
问题 I have a cython module that uses memoryview arrays, that is... double[:,:] foo I want to run this module in parallel using multiprocessing. However I get the error: PicklingError: Can't pickle <type 'tile_class._memoryviewslice'>: attribute lookup tile_class._memoryviewslice failed Why can't I pickle a memory view and what can I do about it. 回答1: Maybe passing the actual array instead of the memory view can solve your problem. If you want to execute a function in parallel, all of it

Why is it not possible to get a Py_buffer from an array object?

怎甘沉沦 提交于 2020-01-03 09:48:19
问题 The python documentation on array clearly states that the array conforms to the buffer interface. It even suggest not using the buffer_info() method. But when I try to get a Py_Buffer from C/C++ code with PyObject_GetBuffer() or use python's memoryview, I get a failure. For example, in python (I use version 2.7): >>> a = array.array('c') >>> memoryview(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot make memory view because object does not have the

Why is it not possible to get a Py_buffer from an array object?

女生的网名这么多〃 提交于 2020-01-03 09:48:12
问题 The python documentation on array clearly states that the array conforms to the buffer interface. It even suggest not using the buffer_info() method. But when I try to get a Py_Buffer from C/C++ code with PyObject_GetBuffer() or use python's memoryview, I get a failure. For example, in python (I use version 2.7): >>> a = array.array('c') >>> memoryview(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot make memory view because object does not have the

Increasing performance of highly repeated numpy array index operations

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 03:07:19
问题 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.

Numpy set array memory

和自甴很熟 提交于 2019-12-22 09:02:25
问题 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

Assigning numpy data in cython to a view

試著忘記壹切 提交于 2019-12-13 01:26:34
问题 I am trying to assign the output of linalg inverse function (la.inv) to a view in cython. Unfortunately this does not work. I can always assign the output of la.inv() to a temporary ndarray object and then copy its content to the view. Is there a better way to do it. cpdef int testfunc1(np.ndarray[np.float_t, ndim=2] A, double [:,:] B) except -1: print("inverse of A:", la.inv(A)) if np.isnan(A).any(): return -1 else: B = la.inv(A) return 1 cpdef int testfunc2(np.ndarray[np.float_t, ndim=2] A)

Reading a binary file with memoryview

我们两清 提交于 2019-12-11 05:33:23
问题 I read a large file in the code below which has a special structure - among others two blocks that need be processed at the same time. Instead of seeking back and forth in the file I load these two blocks wrapped in memoryview calls with open(abs_path, 'rb') as bsa_file: # ... # load the file record block to parse later file_records_block = memoryview(bsa_file.read(file_records_block_size)) # load the file names block file_names_block = memoryview(bsa_file.read(total_file_name_length)) #