本文介绍了传递/返回Cython Memoryviews与NumPy Arrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在编写Python代码来加速二进制图像中标记对象的区域属性功能。以下代码将根据对象的索引计算二进制图像中标记对象的边界像素数。 main()函数将遍历二进制图像mask中的所有标记对象,并计算每个对象的边框像素数。

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.

我想知道在这个Cython代码中传递或返回变量的最佳方法是什么。变量可以是NumPy数组,也可以是类型化的Memoryviews。我已经弄乱了以不同格式传递/返回变量,但无法推断出最佳/最有效的方式。我是Cython的新手,所以Memoryviews对我来说仍然相当抽象,两种方法之间是否存在差异仍然是个谜。我正在使用的图像包含100,000多个标记对象,因此这些操作需要相当高效。

I am wondering what the best way is to pass or return my variables in this Cython code. The variables are either in NumPy arrays or typed Memoryviews. I've messed around with passing/returning the variables in the different formats, but cannot deduce what the best/most efficient way is. I am new to Cython so Memoryviews are still fairly abstract to me and whether there is a different between the two methods remains a mystery. The images I am working with contain 100,000+ labeled objects so operations such as these need to be fairly efficient.

总结:

何时/应该将我的变量作为类型化的Memoryviews而不是NumPy数组传递/返回非常重复的计算?有没有最好的方式或者它们完全一样?

When/should I pass/return my variables as typed Memoryviews rather than NumPy arrays for very repetitive computations? Is there a way that is best or are they exactly the same?

 c> np.get_include()中设置包含目录code> setup.py 文件。您不必使用memoryviews执行此操作,这通常意味着您可以跳过 setup.py 并使用 cythonize 命令行命令或 pyximport 用于更简单的项目。

Ease of compiling

If you're using np.ndarray you have to get the set the include directory with np.get_include() in your setup.py file. You don't have to do this with memoryviews, which often means you can skip setup.py and just use the cythonize command line command or pyximport for simpler projects.

与numpy数组(如果你想使用它)相比,这是memoryviews的优势。它不需要全局解释器锁来获取内存视图的片段,但它适用于numpy数组。这意味着以下代码大纲可以与内存视图并行工作:

This is the big advantage of memoryviews compared to numpy arrays (if you want to use it). It does not require the global interpreter lock to take slices of a memoryview but it does for a numpy array. This means that the following code outline can work in parallel with a memoryview:

cdef void somefunc(double[:] x) nogil:
     # implementation goes here

cdef double[:,:] 2d_array = np.array(...)
for i in prange(2d_array.shape[0]):
    somefunc(2d_array[i,:])

如果您不使用Cython的并行功能不适用。

If you aren't using Cython's parallel functionality this doesn't apply.

您可以将memoryviews用作 cdef 类的属性,但不能使用 np.ndarray s。您当然可以(当然)使用numpy数组作为无类型 object 属性。

You can use memoryviews as attributes of cdef classes but not np.ndarrays. You can (of course) use numpy arrays as untyped object attributes instead.

这篇关于传递/返回Cython Memoryviews与NumPy Arrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:30