Skip to content

RemoveSelfLoops

Bases: BaseTransform

Removes the self-loops of a graph, together with their per-edge attributes (e.g. edge_features, edge_labels).

Example:

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

Methods:

Name Description
process

Removes the self-loops of data.

process

process(data: GraphData) -> GraphData

Removes the self-loops of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph without self-loops. Its number of nodes is unchanged.

Source code in mlx_graphs/transforms/structure.py
def process(self, data: GraphData) -> GraphData:
    """Removes the self-loops of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph without self-loops. Its number of nodes is unchanged.
    """
    num_nodes = _num_nodes(data)
    _, edge_keys = _attribute_names(data, num_nodes)
    edges = np.asarray(data.edge_index)
    _select_edges(data, np.nonzero(edges[0] != edges[1])[0], edge_keys)
    data.num_nodes = num_nodes
    return data

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