本文介绍了"系统错误:<class 'int'>返回带有错误集的结果"在 Python 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to apply a very simple function using ndimage.generic_filter() from scipy. 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

当试图将 values[4] 强制转换为 int 时.如果我运行函数 test_func() 而不使用 ndimage.generic_filter() 随机数组 values,该函数工作正常.

when trying to coerce values[4] to an int. If I run the function test_func() without using ndimage.generic_filter() for a random array values, the function works alright.

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

Why is this error occurring? Is there a way to make it work?

推荐答案

对于您的情况:

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

This must be a bug in either Python or SciPy. Please file a bug at https://bugs.python.org and/or 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.

(另外,如果你能找到一种不需要使用随机性的方法来触发这个错误,他们可能会很感激.但如果你找不到这样的方法,那么请把它归档为-是.)

(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.)

一般来说:

[R] 返回带有错误集的结果"是只能在 C 级别完成的事情.通常,Python/C API 期望大多数 C 函数执行以下两种操作之一:

"[R]eturned a result with an error set" is something that can only be done at the C level. In general, the Python/C API expects most C functions to do one of two things:

  1. 使用这些函数之一设置异常并返回NULL(相当于抛出异常).
  2. 不要设置异常并返回真实"值,通常是 PyObject*(对应于返回值,包括 返回None).
  1. Set an exception using one of these functions and return NULL (corresponds to throwing an exception).
  2. Don't set an exception and return a "real" value, usually a PyObject* (corresponds to returning a value, including returning None).

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

These two cases are normally incorrect:

  1. 设置异常(或无法清除已存在的异常),但随后返回除 NULL 之外的某个值.
  2. 不要设置异常,然后返回NULL.
  1. Set an exception (or fail to clear one that already exists), but then return some value other than NULL.
  2. Don't set an exception, but then return NULL.

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

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. This is always wrong, so there must be a bug in either Python or the SciPy code that it called into.

这篇关于&quot;系统错误:&lt;class 'int'&gt;返回带有错误集的结果"在 Python 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 02:42