patmejia/contrails_vision

pyvista: DeprecationWarning for np.bool in test session

Closed this issue · 2 comments

Title: "DeprecationWarning: Usage of np.bool in test_plot_dataset (test_main.py)"

Error Description:

  • File: "src/test_main.py", Test: "test_plot_dataset"
  • Line 74: DeprecatedWarning: np.bool is a deprecated alias.
  • Suggestion: Use bool instead to silence the warning.
  • Deprecated in NumPy 1.20; details: [URL].
  • Code Line: _vtk_np = {vtkConstants.VTK_BIT: numpy.bool,.
========================================= test session starts =========================================
platform darwin -- Python 3.9.16, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/ops/code/contrails-vision
plugins: mock-3.10.0
collected 3 items                                                                                     

src/test_main.py ...                                                                            [100%]

========================================== warnings summary ===========================================
src/test_main.py::test_compute_normals_and_warp
src/test_main.py::test_compute_normals_and_warp
src/test_main.py::test_compute_normals_and_warp
src/test_main.py::test_compute_normals_and_warp
src/test_main.py::test_plot_dataset
src/test_main.py::test_plot_dataset
src/test_main.py::test_plot_dataset
src/test_main.py::test_plot_dataset
  /Users/ops/opt/anaconda3/envs/contrail_env/lib/python3.9/site-packages/vtkmodules/util/numpy_support.py:74: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
  Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
    _vtk_np = {vtkConstants.VTK_BIT:numpy.bool,

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
==================================== 3 passed, 8 warnings in 5.24s ====================================

Solution: Replacing numpy.bool with bool resolves the deprecation warning.

open: .../anaconda3/envs/contrail_env/lib/python3.9/site-packages/vtkmodules/util/numpy_support.py
change
def get_vtk_to_numpy_typemap():
    """Returns the VTK array type to numpy array type mapping."""
    _vtk_np = {vtkConstants.VTK_BIT:numpy.bool,
                vtkConstants.VTK_CHAR:numpy.int8,
                vtkConstants.VTK_SIGNED_CHAR:numpy.int8,
                vtkConstants.VTK_UNSIGNED_CHAR:numpy.uint8,
                vtkConstants.VTK_SHORT:numpy.int16,
                vtkConstants.VTK_UNSIGNED_SHORT:numpy.uint16,
                vtkConstants.VTK_INT:numpy.int32,
                vtkConstants.VTK_UNSIGNED_INT:numpy.uint32,
                vtkConstants.VTK_LONG:LONG_TYPE_CODE,
                vtkConstants.VTK_LONG_LONG:numpy.int64,
                vtkConstants.VTK_UNSIGNED_LONG:ULONG_TYPE_CODE,
                vtkConstants.VTK_UNSIGNED_LONG_LONG:numpy.uint64,
                vtkConstants.VTK_ID_TYPE:ID_TYPE_CODE,
                vtkConstants.VTK_FLOAT:numpy.float32,
                vtkConstants.VTK_DOUBLE:numpy.float64}
    return _vtk_np

to

def get_vtk_to_numpy_typemap():
    """Returns the VTK array type to numpy array type mapping."""
    _vtk_np = {vtkConstants.VTK_BIT:bool,
                vtkConstants.VTK_CHAR:numpy.int8,
                vtkConstants.VTK_SIGNED_CHAR:numpy.int8,
                vtkConstants.VTK_UNSIGNED_CHAR:numpy.uint8,
                vtkConstants.VTK_SHORT:numpy.int16,
                vtkConstants.VTK_UNSIGNED_SHORT:numpy.uint16,
                vtkConstants.VTK_INT:numpy.int32,
                vtkConstants.VTK_UNSIGNED_INT:numpy.uint32,
                vtkConstants.VTK_LONG:LONG_TYPE_CODE,
                vtkConstants.VTK_LONG_LONG:numpy.int64,
                vtkConstants.VTK_UNSIGNED_LONG:ULONG_TYPE_CODE,
                vtkConstants.VTK_UNSIGNED_LONG_LONG:numpy.uint64,
                vtkConstants.VTK_ID_TYPE:ID_TYPE_CODE,
                vtkConstants.VTK_FLOAT:numpy.float32,
                vtkConst

Explanation:

Removing numpy. from the dictionary values fixes the issue because the code already imports the numpy module, so there's no need to prefix the values with numpy. The prefix causes a warning because numpy.bool is a deprecated alias for the built-in bool type. By directly referencing the appropriate data types (bool, int8, uint8, ...) without the prefix, the warning is resolved, and the code uses the correct data types without deprecation issues.