【发布时间】:2019-05-16 16:49:14
【问题描述】:
我想使用来自scipy 的ndimage.generic_filter() 应用一个非常简单的函数。这是代码:
import numpy as np
import scipy.ndimage as ndimage
data = np.random.rand(400,128)
dimx = int(np.sqrt(np.size(data,0)))
dimy = dimx
coord = np.random.randint(np.size(data,0), size=(dimx,dimy))
def test_func(values):
idx_center = int(values[4])
weight_center = data[idx_center]
weights_around = data[values]
differences = weights_around - weight_center
distances = np.linalg.norm(differences, axis=1)
return np.max(distances)
results = ndimage.generic_filter(coord,
test_func,
footprint = np.ones((3,3)))
当我执行它时,会出现以下错误:
SystemError: <class 'int'> returned a result with an error set
当试图将values[4] 强制转换为int 时。如果我运行函数test_func() 而不使用ndimage.generic_filter() 作为随机数组values,则该函数可以正常工作。
为什么会出现这个错误?有没有办法让它工作?
【问题讨论】:
-
idx_center = int(values[4])想要达到什么目的?当您将coord传递给test_func时,values[4]是一个nparray,因此您无法将其转换为anint,并且int(coord[4])返回错误:only size-1 arrays can be converted to Python scalars。 -
我怀疑这是一个 SciPy 错误;
values将是一个浮点值数组,因此weights_around = data[values]行应该生成一个IndexError。显然有些东西破坏了ndimage.generic_filter中的错误处理。您能否在github.com/scipy/scipy/issues 上为此创建一个新问题?
标签: python scipy runtime-error