Python中的“ SystemError:<class'int'>返回带有错误集的结果”

Ten*_*ero 1 python runtime-error scipy

我想使用ndimage.generic_filter()from 来应用一个非常简单的函数scipy。这是代码:

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)))
Run Code Online (Sandbox Code Playgroud)

当我执行它时,出现以下错误:

SystemError: <class 'int'> returned a result with an error set
Run Code Online (Sandbox Code Playgroud)

当试图胁迫values[4]一个人时int。如果我在test_func()不使用ndimage.generic_filter()随机数组的情况下运行该函数values,则该函数可以正常运行。

为什么会发生此错误?有办法使它起作用吗?

Kev*_*vin 5

对于您的情况:

这必须是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因为intPython标准库中的的实现尝试这样做(3),这可能是SciPy首先这样做的结果。这总是错误的,因此Python或它调用的SciPy代码中都必须存在错误。