Skip to content

Numpy 2.5 deprecation warning: Setting the dtype on a NumPy array has been deprecated #62

Description

@johnthagen

Setting the dtype attribute is deprecated because mutating an array is unsafe if an array is shared, especially by multiple threads. As an alternative, you can create a view with a new dtype via array.view(dtype=new_dtype).
(gh-29244)

PointCloud.encode_rgb does this:

rgb_u32.dtype = np.float32 # type: ignore

As does PointCloud.decode_rgb

rgb.dtype = np.uint32 # type: ignore

This raises:

    @staticmethod
    def encode_rgb(rgb: npt.NDArray | list[npt.NDArray]) -> npt.NDArray:
        """
        Encode Nx3 uint8 array with RGB values to
        Nx1 float32 array with bit-packed RGB
        """
    
        if isinstance(rgb, np.ndarray):
            if rgb.ndim == 1:
                rgb = rgb[:, None]
        else:
            rgb = np.hstack([v[:, None] if v.ndim == 1 else v for v in rgb])
    
        rgb_u32 = rgb.astype(np.uint32)
        rgb_u32 = np.array(
            (rgb_u32[:, 0] << 16) | (rgb_u32[:, 1] << 8) | (rgb_u32[:, 2] << 0),
            dtype=np.uint32,
        )
>       rgb_u32.dtype = np.float32  # type: ignore
        ^^^^^^^^^^^^^
E       DeprecationWarning: Setting the dtype on a NumPy array has been deprecated in NumPy 2.5.
E       Instead of changing the dtype on an array x, create a new array with x.view(new_dtype)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions