如何print在没有gil的Cython函数中使用?例如:
from libc.math cimport log, fabs
cpdef double f(double a, double b) nogil:
cdef double c = log( fabs(a - b) )
print c
return c
Run Code Online (Sandbox Code Playgroud)
编译时出现此错误:
Error compiling Cython file:
...
print c
^
------------------------------------------------------------
Python print statement not allowed without gil
...
Run Code Online (Sandbox Code Playgroud)
我知道如何使用C库而不是他们的python等价物(math例如这里的库),但我找不到类似的方法print.
我有一个循环修改water_depth类型为 2D numpy 数组的元素float。该阵列包含每个像素的水深,范围通常在 0 到 1.5m 之间。我想用这个不断变化的数组制作一个视频:每次迭代都可以是视频中的一个帧。我只发现这个链接解释了一个类似的问题,并建议使用 cv2 VideoWriter。问题是我的 numpy 数组是一个浮点数,而不是整数。这是否意味着我需要在每次迭代中对我的数组进行某种预处理?
import numpy as np
water_depth = np.zeros((500,700), dtype=float)
for i in range(1000):
random_locations = np.random.random_integers(200,450, size=(200, 2))
for item in random_locations:
water_depth[item[0], item[1]] += 0.1
#add this array to the video
Run Code Online (Sandbox Code Playgroud) 我想避免在以下代码中使用for循环来实现性能.矢量化适合这种问题吗?
a = np.array([[0,1,2,3,4],
[5,6,7,8,9],
[0,1,2,3,4],
[5,6,7,8,9],
[0,1,2,3,4]],dtype= np.float32)
temp_a = np.copy(a)
for i in range(1,a.shape[0]-1):
for j in range(1,a.shape[1]-1):
if a[i,j] > 3:
temp_a[i+1,j] += a[i,j] / 5.
temp_a[i-1,j] += a[i,j] / 5.
temp_a[i,j+1] += a[i,j] / 5.
temp_a[i,j-1] += a[i,j] / 5.
temp_a[i,j] -= a[i,j] * 4. / 5.
a = np.copy(temp_a)
Run Code Online (Sandbox Code Playgroud) 我有两个数据集如下:
ds1:一个 DEM(数字高程模型)文件作为 2d numpy 数组,
ds2:显示有一些多余水分的区域(像素)。
我有一个 while 循环,负责根据其 8 个相邻像素的高程及其自身的高度来扩展(和更改)每个像素中的多余体积,直到每个像素中的多余体积小于某个值 d = 0.05。因此,在每次迭代中,我需要在 ds2 中找到多余体积大于 0.05 的像素索引,如果没有像素剩余,则退出 while 循环:
exit_code == "No"
while exit_code == "No":
index_of_pixels_with_excess_volume = numpy.argwhere(ds2> 0.05) # find location of pixels where excess volume is greater than 0.05
if not index_of_pixels_with_excess_volume.size:
exit_code = "Yes"
else:
for pixel in index_of_pixels_with_excess_volume:
# spread those excess volumes to the neighbours and
# change the values of ds2
Run Code Online (Sandbox Code Playgroud)
问题是 numpy.argwhere(ds2> 0.05) 非常慢。我正在寻找更快的替代解决方案。
编译以下 Cython 代码后,我得到如下所示的 html 文件:
import numpy as np
cimport numpy as np
cpdef my_function(np.ndarray[np.double_t, ndim = 1] array_a,
np.ndarray[np.double_t, ndim = 1] array_b,
int n_rows,
int n_columns):
array_a[0:-1:n_columns] = 0
array_a[n_columns - 1:n_rows * n_columns:n_columns] = 0
array_a[0:n_columns] = 0
array_a[n_columns* (n_rows - 1):n_rows * n_columns] = 0
array_b[array_a == 3] = 0
return array_a, array_b
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么我的函数的那些操作仍然是黄色的?这是否意味着代码仍然没有使用 Cython 时那么快?
我有一个简单的函数,将一些计算的输出写入sqlite表中。我想在Python中通过多处理并行使用此函数。我的具体问题是,当每个进程尝试将其结果写入同一张表时,如何避免冲突?运行代码给我这个错误:sqlite3.OperationalError:数据库被锁定。
import sqlite3
from multiprocessing import Pool
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("CREATE TABLE table_1 (id int,output int)")
def write_to_file(a_tuple):
index = a_tuple[0]
input = a_tuple[1]
output = input + 1
c.execute('INSERT INTO table_1 (id, output)' 'VALUES (?,?)', (index,output))
if __name__ == "__main__":
p = Pool()
results = p.map(write_to_file, [(1,10),(2,11),(3,13),(4,14)])
p.close()
p.join()
Traceback (most recent call last):
sqlite3.OperationalError: database is locked
Run Code Online (Sandbox Code Playgroud)