Skip to content

ToUndirected

ToUndirected(reduce: str = 'add')

Bases: BaseTransform

Makes a graph undirected by adding the reverse of every edge.

Duplicate edges are merged, so that (i, j) and (j, i) are each present exactly once, and the edges are sorted by source then destination. Every per-edge attribute (e.g. edge_features, edge_labels) follows: the values of merged edges are combined with reduce.

Parameters:

Name Type Description Default
reduce str

How the attributes of merged edges are combined: "add", "mean", "max" or "min". Default: "add"

'add'

Example:

graph = GraphData(edge_index=mx.array([[0, 1, 1], [1, 0, 2]]))
graph = ToUndirected()(graph)
graph.edge_index
# mx.array([[0, 1, 1, 2], [1, 0, 2, 1]])

Methods:

Name Description
process

Adds the reverse edges to data.

Source code in mlx_graphs/transforms/structure.py
def __init__(self, reduce: str = "add"):
    self.reduce = reduce

process

process(data: GraphData) -> GraphData

Adds the reverse edges to data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The undirected graph.

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

    Args:
        data: The graph to transform.

    Returns:
        The undirected graph.
    """
    num_nodes = _num_nodes(data)
    _, edge_keys = _attribute_names(data, num_nodes)
    edge_index = data.edge_index
    for key in edge_keys:
        _, values = to_undirected(
            edge_index,
            getattr(data, key),
            coalesce=True,
            num_nodes=num_nodes,
            reduce=self.reduce,
        )
        setattr(data, key, values)
    data.edge_index = to_undirected(edge_index, coalesce=True, num_nodes=num_nodes)
    return data

from mlx_graphs.transforms import ToUndirected · mlx_graphs/transforms/structure.py