Skip to content

RadiusGraph

RadiusGraph(
    r: float,
    loop: bool = False,
    max_num_neighbors: Optional[int] = 32,
    flow: Literal["source_to_target", "target_to_source"] = "source_to_target",
)

Bases: BaseTransform

Creates the graph connecting the nodes whose positions pos are within a given distance.

The previous edges of the graph are replaced, and its edge_features and edge_weights are dropped. For a GraphDataBatch, only the nodes of the same graph are connected.

Parameters:

Name Type Description Default
r float

The distance.

required
loop bool

If True, the graph contains self-loops. Default: False

False
max_num_neighbors Optional[int]

The maximum number of neighbors of each node; 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 node. Default: "source_to_target"

'source_to_target'

Example:

import mlx.core as mx
from mlx_graphs.data import GraphData
from mlx_graphs.transforms import RadiusGraph

pos = mx.array([[0.0, 0.0], [1.0, 0.0], [3.0, 0.0]])
graph = GraphData(edge_index=mx.zeros((2, 0), dtype=mx.int32), pos=pos)
graph = RadiusGraph(r=1.5)(graph)
>>> graph.edge_index
array([[1, 0],
       [0, 1]], dtype=int32)

Methods:

Name Description
process

Replaces the edges of data by its radius graph.

Source code in mlx_graphs/transforms/spatial.py
def __init__(
    self,
    r: float,
    loop: bool = False,
    max_num_neighbors: Optional[int] = 32,
    flow: Literal["source_to_target", "target_to_source"] = "source_to_target",
):
    self.r = r
    self.loop = loop
    self.max_num_neighbors = max_num_neighbors
    self.flow = flow

process

process(data: GraphData) -> GraphData

Replaces the edges of data by its radius graph.

Parameters:

Name Type Description Default
data GraphData

The graph to transform. It must have a pos attribute.

required

Returns:

Type Description
GraphData

The graph with the radius edges.

Source code in mlx_graphs/transforms/spatial.py
def process(self, data: GraphData) -> GraphData:
    """Replaces the edges of ``data`` by its radius graph.

    Args:
        data: The graph to transform. It must have a ``pos`` attribute.

    Returns:
        The graph with the radius edges.
    """
    pos = _positions(data)
    edge_index = radius_graph(
        pos,
        self.r,
        _batch_indices(data),
        loop=self.loop,
        max_num_neighbors=self.max_num_neighbors,
        flow=self.flow,
    )
    return _set_edges(data, edge_index, pos.shape[0])

from mlx_graphs.transforms import RadiusGraph · mlx_graphs/transforms/spatial.py