Skip to content

VirtualNode

Bases: BaseTransform

Appends a virtual node connected to all the other nodes, as described in "Neural Message Passing for Quantum Chemistry", as PyG's VirtualNode.

The virtual node gets the id num_nodes. An edge from every node to the virtual node and an edge from the virtual node to every node are appended, in this order. The type of each edge is stored in edge_type: existing edges keep their edge_type (0 if there was none), and with \(t\) the largest existing type, edges to the virtual node get type \(t + 1\) and edges from it type \(t + 2\).

Every per-node attribute (e.g. node_features) gets a row of zeros for the virtual node, and every per-edge attribute (e.g. edge_features) gets rows of zeros for the new edges, except edge_weights which is extended with ones.

Example:

graph = GraphData(
    edge_index=mx.array([[0, 1], [1, 2]]),
    node_features=mx.ones((3, 2)),
)
graph = VirtualNode()(graph)
graph.edge_index
# mx.array([[0, 1, 0, 1, 2, 3, 3, 3], [1, 2, 3, 3, 3, 0, 1, 2]])
graph.edge_type
# mx.array([0, 0, 1, 1, 1, 2, 2, 2])
graph.node_features.shape
# (4, 2)

Methods:

Name Description
process

Adds the virtual node to data.

process

process(data: GraphData) -> GraphData

Adds the virtual node to data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the virtual node.

Source code in mlx_graphs/transforms/topology.py
def process(self, data: GraphData) -> GraphData:
    """Adds the virtual node to ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the virtual node.
    """
    num_nodes = _num_nodes(data)
    node_keys, edge_keys = _attribute_names(data, num_nodes)
    dtype = data.edge_index.dtype
    edge_type = getattr(data, "edge_type", None)
    if edge_type is None:
        edge_type = mx.zeros((data.num_edges,), dtype=dtype)

    arange = mx.arange(num_nodes, dtype=dtype)
    full = mx.full((num_nodes,), num_nodes, dtype=dtype)
    edge_index = mx.concatenate(
        [
            data.edge_index,
            mx.stack([arange, full]),
            mx.stack([full, arange]),
        ],
        axis=1,
    )
    max_type = int(mx.max(edge_type).item()) if edge_type.size > 0 else 0
    new_type = mx.full((num_nodes,), max_type + 1, dtype=edge_type.dtype)
    edge_type = mx.concatenate([edge_type, new_type, new_type + 1])

    for key in edge_keys:
        if key == "edge_type":
            continue
        value = getattr(data, key)
        fill = 1 if key == "edge_weights" else 0
        new_value = mx.full((2 * num_nodes, *value.shape[1:]), fill, value.dtype)
        setattr(data, key, mx.concatenate([value, new_value.astype(value.dtype)]))
    for key in node_keys:
        value = getattr(data, key)
        new_value = mx.zeros((1, *value.shape[1:]), dtype=value.dtype)
        setattr(data, key, mx.concatenate([value, new_value]))

    data.edge_index = edge_index
    data.edge_type = edge_type
    if _keeps_explicit_num_nodes(data):
        data.num_nodes = num_nodes + 1
    return data

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