繁体   English   中英

“系统错误:<class 'int'> 在 Python 中返回了带有错误集的结果”</class>

[英]"SystemError: <class 'int'> returned a result with an error set" in Python

我想使用 scipy 中的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()而不对随机数组values使用ndimage.generic_filter() ,则该函数可以正常工作。

为什么会出现此错误? 有没有办法让它工作?

对于你的情况:

这一定是 Python 或 SciPy 中的错误。 请在https://bugs.python.org和/或https://www.scipy.org/bug-report.html提交错误。 包括 Python 和 NumPy/SciPy 的版本号、您在此处拥有的完整代码以及整个回溯。

(另外,如果你能找到一种不需要使用随机性的方法来触发这个错误,他们可能会很感激。但如果你找不到这样的方法,请按原样提交。)

一般来说:

“[R]返回了一个错误集的结果”是只能在 C 级别完成的事情。 通常,Python/C API 期望大多数 C 函数做以下两件事之一:

  1. 使用这些函数之一设置异常并返回NULL (对应于抛出异常)。
  2. 不要设置异常并返回“真实”值,通常是PyObject* (对应于返回值,包括返回None )。

这两种情况通常是不正确的:

  1. 设置异常(或未能清除已存在的异常),但随后返回NULL以外的某个值。
  2. 不要设置异常,然后返回NULL

Python 正在引发SystemError ,因为int的实现在 Python 标准库中尝试执行 (3),这可能是 SciPy 首先执行它的结果。 这总是错误的,因此 Python 或它调用的 SciPy 代码中一定存在错误。

我在 Linux 上使用 Python 3.8.1 和 SciPy 1.4.1 的经历非常相似。 解决方法是引入np.floor以便:

centre = int(window.size / 2)变成centre = int(np.floor(window.size/2))

这似乎已经解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM