Skip to content

ToDense

ToDense(num_nodes: Optional[int] = None)

Bases: BaseTransform

Converts the edges of a graph to a dense adjacency matrix, as PyG's ToDense.

The adjacency matrix is stored in adj, with shape [num_nodes, num_nodes, *]: entry adj[i, j] holds the edge_features of the edge \((i, j)\) (summed over duplicate edges), or 1 if the graph has no edge features. edge_index and edge_features are then set to None.

If num_nodes is given, the graph is padded to that many nodes: node_features, pos and node_labels get rows of zeros, and a boolean mask attribute tells which nodes are real. The number of nodes is stored explicitly.

Parameters:

Name Type Description Default
num_nodes Optional[int]

Number of nodes to pad the graph to. Defaults to the number of nodes of the graph.

None

Example:

graph = GraphData(edge_index=mx.array([[0, 1], [1, 2]]))
graph = ToDense(num_nodes=4)(graph)
graph.adj
# mx.array([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
graph.mask
# mx.array([True, True, True, False])

Methods:

Name Description
process

Converts the edges of data to a dense adjacency matrix.

Source code in mlx_graphs/transforms/topology.py
def __init__(self, num_nodes: Optional[int] = None):
    self.num_nodes = num_nodes

process

process(data: GraphData) -> GraphData

Converts the edges of data to a dense adjacency matrix.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with adj and mask.

Source code in mlx_graphs/transforms/topology.py
def process(self, data: GraphData) -> GraphData:
    """Converts the edges of ``data`` to a dense adjacency matrix.

    Args:
        data: The graph to transform.

    Returns:
        The graph with ``adj`` and ``mask``.
    """
    orig_num_nodes = _num_nodes(data)
    num_nodes = orig_num_nodes if self.num_nodes is None else self.num_nodes
    if orig_num_nodes > num_nodes:
        raise ValueError(
            f"The graph has {orig_num_nodes} nodes, more than "
            f"num_nodes={num_nodes}"
        )
    edge_features = data.edge_features
    if edge_features is None:
        edge_features = mx.ones((data.num_edges,), dtype=mx.float32)
    src, dst = mx.array(_host_edges(data))
    adj = mx.zeros(
        (num_nodes, num_nodes, *edge_features.shape[1:]), dtype=edge_features.dtype
    )
    if data.num_edges > 0:
        adj = adj.at[src, dst].add(edge_features)

    data.adj = adj
    data.edge_index = None
    data.edge_features = None
    data.mask = mx.arange(num_nodes) < orig_num_nodes

    pad = num_nodes - orig_num_nodes
    for key in ("node_features", "pos", "node_labels"):
        value = getattr(data, key, None)
        if value is None or value.ndim == 0 or value.shape[0] != orig_num_nodes:
            continue
        zeros = mx.zeros((pad, *value.shape[1:]), dtype=value.dtype)
        setattr(data, key, mx.concatenate([value, zeros]))
    data.num_nodes = num_nodes
    return data

from mlx_graphs.transforms import ToDense · mlx_graphs/transforms/topology.py