Skip to content

radius_graph

radius_graph(
    x: array,
    r: float,
    batch_indices: Optional[array] = None,
    loop: bool = False,
    max_num_neighbors: Optional[int] = 32,
    flow: Literal["source_to_target", "target_to_source"] = "source_to_target",
    num_graphs: Optional[int] = None,
) -> array

Computes the graph connecting each point to the points within distance r.

Parameters:

Name Type Description Default
x array

Point coordinates (or features), of shape [N, num_features].

required
r float

The radius. Points at a distance strictly smaller than r are connected.

required
batch_indices Optional[array]

Graph of each point, of shape [N]. Only points of the same graph are connected. Default: None (a single graph).

None
loop bool

If True, the graph contains a self-loop for each point. Default: False

False
max_num_neighbors Optional[int]

The maximum number of neighbors of each point; the closest ones are kept. None for no limit. Default: 32

32
flow Literal['source_to_target', 'target_to_source']

Direction of the edges, for use with message passing. With "source_to_target", the edges go from the neighbors to the center point. Default: "source_to_target"

'source_to_target'
num_graphs Optional[int]

Number of graphs. Default: None (inferred from batch_indices).

None

Returns:

Type Description
array

The edge_index of shape [2, num_edges]. The edges are grouped by

array

center point, by increasing neighbor index.

Example:

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

x = mx.array([[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]])
edge_index = radius_graph(x, r=2.5)
>>> edge_index
array([[1, 2, 0, 3, 0, 3, 1, 2],
       [0, 0, 1, 1, 2, 2, 3, 3]], dtype=int32)
Source code in mlx_graphs/utils/spatial.py
def radius_graph(
    x: mx.array,
    r: float,
    batch_indices: Optional[mx.array] = None,
    loop: bool = False,
    max_num_neighbors: Optional[int] = 32,
    flow: Literal["source_to_target", "target_to_source"] = "source_to_target",
    num_graphs: Optional[int] = None,
) -> mx.array:
    """Computes the graph connecting each point to the points within distance
    ``r``.

    Args:
        x: Point coordinates (or features), of shape ``[N, num_features]``.
        r: The radius. Points at a distance strictly smaller than ``r`` are
            connected.
        batch_indices: Graph of each point, of shape ``[N]``. Only points of the
            same graph are connected. Default: ``None`` (a single graph).
        loop: If ``True``, the graph contains a self-loop for each point.
            Default: ``False``
        max_num_neighbors: The maximum number of neighbors of each point; the
            closest ones are kept. ``None`` for no limit. Default: ``32``
        flow: Direction of the edges, for use with message passing. With
            ``"source_to_target"``, the edges go from the neighbors to the
            center point. Default: ``"source_to_target"``
        num_graphs: Number of graphs. Default: ``None`` (inferred from
            ``batch_indices``).

    Returns:
        The ``edge_index`` of shape ``[2, num_edges]``. The edges are grouped by
        center point, by increasing neighbor index.

    Example:

    .. code-block:: python

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

        x = mx.array([[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]])
        edge_index = radius_graph(x, r=2.5)
        >>> edge_index
        array([[1, 2, 0, 3, 0, 3, 1, 2],
               [0, 0, 1, 1, 2, 2, 3, 3]], dtype=int32)
    """
    _check_flow(flow)
    rows = _neighbor_blocks(
        x, x, batch_indices, batch_indices, num_graphs, max_num_neighbors, not loop
    )
    edge_index = _sorted_pairs(_within_radius(list(rows), r), by_distance=False)
    return edge_index[::-1] if flow == "source_to_target" else edge_index

from mlx_graphs.nn import radius_graph · mlx_graphs/utils/spatial.py