验证 NumPy 中的 bug 和 bug 修复#

在本教程中,您将学习如何

  • 验证 NumPy 中 bug 的存在

  • 验证 bug 的修复(如果已修复)

在您进行验证过程时,您将学习如何

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

  • 安装适当版本的 NumPy,首先查看 bug 的运行情况,然后验证其修复

我们以 issue 16354 作为示例。

该 issue 是

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

当其中一个参数为全零,且两个参数类型均为 np.int64 或 np.float32 时,np.polymul 返回一个类型为 np.float64 的对象。当全零 np.complex64 出现类似情况时,结果类型为 np.complex128。

对于非零参数,不会出现这种情况;此时结果符合预期。

np.convolve 不存在此 bug。

复现代码示例:

>>> 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. 安装出现 bug 的 NumPy 版本#

报告中引用的是 NumPy 版本 1.18.4,因此在这种情况下您需要安装此版本。

由于此 bug 与发布版本相关,而不是特定提交,因此在虚拟环境中通过 pip 安装的预编译 wheel 就足够了。

pip install numpy==1.18.4

有些 bug 可能需要您构建 issue 报告中引用的 NumPy 版本。要了解如何操作,请访问 从源码构建

3. 复现 bug#

#16354 中报告的 issue 是,当 numpy.polymul 方法的输入之一是零数组时,会返回错误的 dtype

要复现此 bug,请启动 Python 终端,输入 bug 报告中显示的 Snippet 代码,并确保结果与 issue 中的结果一致。

>>> 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 中的修复#

如果您的 bug 的 issue 报告尚未解决,则需要提交进一步的操作或补丁。

然而,在本例中,该 issue 已通过 PR 17577 解决并已关闭。因此,您可以尝试验证修复。

要验证修复

  1. 卸载仍存在 bug 的 NumPy 版本

    pip uninstall numpy
    
  2. 安装最新版 NumPy

    pip install numpy
    
  3. 在您的 Python 终端中,运行您之前用于验证 bug 存在的代码 Snippet,并确认该 issue 已被解决。

    >>> 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. 通过验证和修复 bug 来支持 NumPy 开发#

前往 NumPy GitHub issues 页面,看看您是否能确认其他尚未被确认的 bug 的存在。特别是,让开发者知道一个 bug 在新版本的 NumPy 上是否可以复现,对他们非常有帮助。

验证 bug 存在的评论会提醒 NumPy 开发者,有不止一个用户可以复现该问题。