[英]"SystemError: <class 'int'> returned a result with an error set" in Python
I wanted to apply a very simple function using ndimage.generic_filter()
from scipy
.我想使用 scipy 中的
scipy
ndimage.generic_filter()
应用一个非常简单的函数。 This is the code:这是代码:
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)))
When I execute it though, the following error shows up:但是,当我执行它时,出现以下错误:
SystemError: <class 'int'> returned a result with an error set
when trying to coerce values[4]
to an int
.试图将
values[4]
强制转换为int
时。 If I run the function test_func()
without using ndimage.generic_filter()
for a random array values
, the function works alright.如果我运行函数
test_func()
而不对随机数组values
使用ndimage.generic_filter()
,则该函数可以正常工作。
Why is this error occurring?为什么会出现此错误? Is there a way to make it work?
有没有办法让它工作?
For your case:对于你的情况:
This must be a bug in either Python or SciPy.这一定是 Python 或 SciPy 中的错误。 Please file a bug at https://bugs.python.org and/or https://www.scipy.org/bug-report.html .
请在https://bugs.python.org和/或https://www.scipy.org/bug-report.html提交错误。 Include the version numbers of Python and NumPy/SciPy, the full code that you have here, and the entire traceback.
包括 Python 和 NumPy/SciPy 的版本号、您在此处拥有的完整代码以及整个回溯。
(Also, if you can find a way to trigger this bug that doesn't require the use of randomness, they will likely appreciate it. But if you can't find such a method, then please do file it as-is.) (另外,如果你能找到一种不需要使用随机性的方法来触发这个错误,他们可能会很感激。但如果你找不到这样的方法,请按原样提交。)
In general:一般来说:
"[R]eturned a result with an error set" is something that can only be done at the C level. “[R]返回了一个错误集的结果”是只能在 C 级别完成的事情。 In general, the Python/C API expects most C functions to do one of two things:
通常,Python/C API 期望大多数 C 函数做以下两件事之一:
NULL
(corresponds to throwing an exception).NULL
(对应于抛出异常)。PyObject*
(corresponds to returning a value, including returning None
).PyObject*
(对应于返回值,包括返回None
)。 These two cases are normally incorrect:这两种情况通常是不正确的:
NULL
.NULL
以外的某个值。NULL
.NULL
。 Python is raising a SystemError
because the implementation of int
, in the Python standard library, tried to do (3), possibly as a result of SciPy doing it first. Python 正在引发
SystemError
,因为int
的实现在 Python 标准库中尝试执行 (3),这可能是 SciPy 首先执行它的结果。 This is always wrong, so there must be a bug in either Python or the SciPy code that it called into.这总是错误的,因此 Python 或它调用的 SciPy 代码中一定存在错误。
I was having a very similar experience with Python 3.8.1 and SciPy 1.4.1 on Linux.我在 Linux 上使用 Python 3.8.1 和 SciPy 1.4.1 的经历非常相似。 A workaround was to introduce
np.floor
so that:解决方法是引入
np.floor
以便:
centre = int(window.size / 2)
becomes centre = int(np.floor(window.size/2))
centre = int(window.size / 2)
变成centre = int(np.floor(window.size/2))
which seems to have resolved the issue.这似乎已经解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.