验证NumPy中的错误和错误修复#

在本指南中,你将学习如何:

  • 验证NumPy中bug的存在

  • 验证为该bug所做的修复(如果有)

在您完成验证过程时,您将学习如何:

  • 设置 Python 虚拟环境(使用 virtualenv )

  • 安装适当版本的 NumPy,首先查看 bug 的实际效果,然后验证其修复

使用 Issue 16354 作为示例.

该问题为:

标题:当给定全零参数时,np.polymul 返回类型为 np.float64 或 np.complex128

当一个参数为全零,并且两个参数的类型为 np.int64 或 np.float32 时,np.polymul 返回一个类型为 np.float64 的对象.当所有零 np.complex64 时也会发生类似的情况,返回类型 np.complex128.

非零参数不会发生这种情况;结果如预期.

此错误不存在于 np.convolve 中.

重现代码示例:

>>> import numpy as np
>>> np.__version__
'1.18.4'
>>> a = np.array([1,2,3])
>>> z = np.array([0,0,0])
>>> np.polymul(a.astype(np.int64), a.astype(np.int64)).dtype
dtype('int64')
>>> np.polymul(a.astype(np.int64), z.astype(np.int64)).dtype
dtype('float64')
>>> np.polymul(a.astype(np.float32), z.astype(np.float32)).dtype
dtype('float64')
>>> np.polymul(a.astype(np.complex64), z.astype(np.complex64)).dtype
dtype('complex128')
Numpy/Python version information:
>>> import sys, numpy; print(numpy.__version__, sys.version)
1.18.4 3.7.5 (default, Nov  7 2019, 10:50:52) [GCC 8.3.0]

1. 设置虚拟环境#

创建一个新目录,进入该目录,并使用您首选的方法设置虚拟环境. 例如,这是在 Linux 或 macOS 上使用 virtualenv 的方法:

virtualenv venv_np_bug
source venv_np_bug/bin/activate

这确保了系统/全局/默认的 Python/NumPy 安装不会被更改.

2. 安装报告错误时使用的 NumPy 版本#

该报告引用了 NumPy 版本 1.18.4,因此在这种情况下您需要安装该版本.

由于此错误与发布版本相关,而不是与特定提交相关,因此通过 pip 安装在您的虚拟环境中的预构建 wheel 即可满足要求:

pip install numpy==1.18.4

某些错误可能需要您构建问题报告中引用的 NumPy 版本.要了解如何执行此操作,请访问 Building from source .

3. 重现该错误#

#16354 中报告的问题是,如果 numpy.polymul 方法的输入之一是零数组,则返回错误的 dtype .

要重现该错误,请启动 Python 终端,输入错误报告中显示的代码片段,并确保结果与问题中的结果匹配:

>>> import numpy as np
>>> np.__version__
'...' # 1.18.4
>>> a = np.array([1,2,3])
>>> z = np.array([0,0,0])
>>> np.polymul(a.astype(np.int64), a.astype(np.int64)).dtype
dtype('int64')
>>> np.polymul(a.astype(np.int64), z.astype(np.int64)).dtype
dtype('...') # float64
>>> np.polymul(a.astype(np.float32), z.astype(np.float32)).dtype
dtype('...') # float64
>>> np.polymul(a.astype(np.complex64), z.astype(np.complex64)).dtype
dtype('...') # complex128

如报告中所述,每当零数组(在上面的示例中为 z )是 numpy.polymul 的参数之一时,都会返回不正确的 dtype .

4. 检查最新版本的 NumPy 中是否存在修复#

如果您的错误的 issue report 尚未解决,则需要提交进一步的操作或补丁.

但是,在这种情况下,该问题已由 PR 17577 解决,现在已关闭.因此,您可以尝试验证修复.

要验证修复:

  1. 卸载仍然存在错误的 NumPy 版本:

    pip uninstall numpy
    
  2. 安装最新版本的 NumPy:

    pip install numpy
    
  3. 在 Python 终端中,运行您用于验证错误存在的报告的代码片段,并确认该问题已解决:

    >>> import numpy as np
    >>> np.__version__
    '...' # 1.18.4
    >>> a = np.array([1,2,3])
    >>> z = np.array([0,0,0])
    >>> np.polymul(a.astype(np.int64), a.astype(np.int64)).dtype
    dtype('int64')
    >>> np.polymul(a.astype(np.int64), z.astype(np.int64)).dtype
    dtype('int64')
    >>> np.polymul(a.astype(np.float32), z.astype(np.float32)).dtype
    dtype('float32')
    >>> np.polymul(a.astype(np.complex64), z.astype(np.complex64)).dtype
    dtype('complex64')
    

请注意,即使零数组是 numpy.polymul 的参数之一,现在也会返回正确的 dtype .

5. 通过验证和修复错误来支持 NumPy 开发#

转到 NumPy GitHub issues page ,看看您是否可以确认任何尚未确认的其它错误的的存在. 特别是,让开发人员知道是否可以在较新版本的 NumPy 上重现错误是很有用的.

验证错误存在的评论会提醒 NumPy 开发人员,多个用户可以重现该问题.