简体   繁体   English

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

[英]"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 函数做以下两件事之一:

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

These two cases are normally incorrect:这两种情况通常是不正确的:

  1. Set an exception (or fail to clear one that already exists), but then return some value other than NULL .设置异常(或未能清除已存在的异常),但随后返回NULL以外的某个值。
  2. Don't set an exception, but then return 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.

相关问题 系统错误:<class 'pyodbc.error'> 返回带有错误集的结果</class> - SystemError: <class 'pyodbc.Error'> returned a result with an error set OpenCV 4.0.0 系统错误:<class 'cv2.CascadeClassifier'> 返回带有错误集的结果 - OpenCV 4.0.0 SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set Django:系统错误:<built-in function uwsgi_sendfile> 返回带有错误集的结果</built-in> - Django: SystemError: <built-in function uwsgi_sendfile> returned a result with an error set sklearn SimpelImputer 系统错误:<built-in function _abc_instancecheck> 返回带有错误集的结果 - sklearn SimpelImputer SystemError: <built-in function _abc_instancecheck> returned a result with an error set 系统错误:<built-in function xxx_iterator> 返回带有错误集的结果 - SystemError: <built-in function xxx_iterator> returned a result with an error set 为什么我会得到`SystemError:<built-in method __contains__ of dict object at ...> 导入“pydrake”东西时返回带有错误集的结果?</built-in> - Why do I get `SystemError: <built-in method __contains__ of dict object at ...> returned a result with an error set` when importing `pydrake` stuff? Tensorflow - 导入错误:系统错误:<built-in method __contains__ of dict object at 0x00000244b47addb8> 返回带有错误集的结果</built-in> - Tensorflow - ImportError: SystemError: <built-in method __contains__ of dict object at 0x00000244B47ADDB8> returned a result with an error set Python-SystemError:NULL结果,PyObject调用中没有错误 - Python - SystemError: NULL result without error in PyObject call SystemError:NULL结果多处理Python - SystemError: NULL result Multiprocessing Python Python 3.7.3 SystemError:错误返回,未设置异常 - Python 3.7.3 SystemError: error return without exception set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM