Skip to content

voxel_grid

voxel_grid(
    pos: array,
    size: Union[float, Sequence[float], array],
    batch: Optional[array] = None,
    start: Optional[Union[float, Sequence[float], array]] = None,
    end: Optional[Union[float, Sequence[float], array]] = None,
) -> array

Voxel grid pooling from the "Dynamic Edge-Conditioned Filters in Convolutional Networks on Graphs" paper: overlays a regular grid of user-defined size over a point cloud and clusters all the points within the same voxel.

Parameters:

Name Type Description Default
pos array

Node positions, of shape [N, D].

required
size Union[float, Sequence[float], array]

Size of a voxel (in each dimension, or the same for all).

required
batch Optional[array]

Graph of each node, of shape [N]. Nodes of different graphs are never in the same voxel. Default: None (a single graph).

None
start Optional[Union[float, Sequence[float], array]]

Start coordinates of the grid (in each dimension, or the same for all). Default: None (the minimum of pos).

None
end Optional[Union[float, Sequence[float], array]]

End coordinates of the grid. Default: None (the maximum of pos).

None

Returns:

Type Description
array

The voxel (cluster) id of each node, of shape [N] (int64). Ids

array

are not consecutive, see consecutive_cluster().

Example:

import mlx.core as mx
from mlx_graphs.utils import voxel_grid

pos = mx.array([[0.0, 0.0], [0.4, 0.2], [1.5, 0.1], [0.2, 1.7]])
>>> voxel_grid(pos, size=1.0)
array([0, 0, 1, 2], dtype=int64)
Source code in mlx_graphs/utils/clustering.py
def voxel_grid(
    pos: mx.array,
    size: Union[float, Sequence[float], mx.array],
    batch: Optional[mx.array] = None,
    start: Optional[Union[float, Sequence[float], mx.array]] = None,
    end: Optional[Union[float, Sequence[float], mx.array]] = None,
) -> mx.array:
    r"""Voxel grid pooling from the `"Dynamic Edge-Conditioned Filters in
    Convolutional Networks on Graphs" <https://arxiv.org/abs/1704.02901>`_
    paper: overlays a regular grid of user-defined size over a point cloud
    and clusters all the points within the same voxel.

    Args:
        pos: Node positions, of shape ``[N, D]``.
        size: Size of a voxel (in each dimension, or the same for all).
        batch: Graph of each node, of shape ``[N]``. Nodes of different graphs
            are never in the same voxel. Default: ``None`` (a single graph).
        start: Start coordinates of the grid (in each dimension, or the same
            for all). Default: ``None`` (the minimum of ``pos``).
        end: End coordinates of the grid. Default: ``None`` (the maximum of
            ``pos``).

    Returns:
        The voxel (cluster) id of each node, of shape ``[N]`` (``int64``). Ids
        are not consecutive, see :func:`consecutive_cluster`.

    Example:

    .. code-block:: python

        import mlx.core as mx
        from mlx_graphs.utils import voxel_grid

        pos = mx.array([[0.0, 0.0], [0.4, 0.2], [1.5, 0.1], [0.2, 1.7]])
        >>> voxel_grid(pos, size=1.0)
        array([0, 0, 1, 2], dtype=int64)
    """
    pos = pos.reshape(-1, 1) if pos.ndim == 1 else pos
    if not mx.issubdtype(pos.dtype, mx.floating):
        pos = pos.astype(mx.float32)
    num_points, dim = pos.shape
    if batch is None:
        batch_column = mx.zeros((num_points, 1), dtype=pos.dtype)
    else:
        batch_column = batch.reshape(-1, 1).astype(pos.dtype)
    pos = mx.concatenate([pos, batch_column], axis=1)
    if num_points == 0:
        return mx.zeros((0,), dtype=mx.int64)

    size_ = np.concatenate([_repeat(size, dim, "size"), [1.0]])
    size_arr = mx.array(size_, dtype=pos.dtype)
    if start is None:
        start_arr = pos.min(axis=0)
    else:
        start_arr = mx.array(
            np.concatenate([_repeat(start, dim, "start"), [0.0]]), dtype=pos.dtype
        )
    if end is None:
        end_arr = pos.max(axis=0)
    else:
        end_arr = mx.concatenate(
            [
                mx.array(_repeat(end, dim, "end"), dtype=pos.dtype),
                batch_column.max(axis=0),
            ]
        )
    # Same operations as PyG's grid_cluster (truncation towards zero).
    pos = pos - start_arr[None, :]
    num_voxels = ((end_arr - start_arr) / size_arr).astype(mx.int64) + 1
    num_voxels = mx.cumprod(num_voxels)
    num_voxels = mx.concatenate([mx.ones((1,), dtype=mx.int64), num_voxels[:-1]])
    voxel = (pos / size_arr[None, :]).astype(mx.int64)
    return (voxel * num_voxels[None, :]).sum(axis=1)

from mlx_graphs.nn import voxel_grid · mlx_graphs/utils/clustering.py