Skip to content

RemoveDuplicatedEdges

RemoveDuplicatedEdges(
    key: Optional[Union[str, list[str]]] = None, reduce: str = "add"
)

Bases: BaseTransform

Removes the duplicated edges of a graph and sorts the edges by source then destination (see coalesce()).

The attributes of merged edges are combined with reduce.

Parameters:

Name Type Description Default
key Optional[Union[str, list[str]]]

Name(s) of the per-edge attributes to merge with reduce. Default: None (all the per-edge attributes, e.g. edge_features, edge_weights and edge_labels, so that every per-edge attribute stays aligned with the edges).

None
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, 0], [1, 2, 1]]),
    edge_weights=mx.array([1.0, 2.0, 3.0]),
)
graph = RemoveDuplicatedEdges()(graph)
graph.edge_index
# mx.array([[0, 1], [1, 2]])
graph.edge_weights
# mx.array([4.0, 2.0])

Methods:

Name Description
process

Merges the duplicated edges of data.

Source code in mlx_graphs/transforms/edge_cleanup.py
def __init__(
    self, key: Optional[Union[str, list[str]]] = None, reduce: str = "add"
):
    self.keys = [key] if isinstance(key, str) else key
    self.reduce = reduce

process

process(data: GraphData) -> GraphData

Merges the duplicated edges of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph without duplicated edges.

Source code in mlx_graphs/transforms/edge_cleanup.py
def process(self, data: GraphData) -> GraphData:
    """Merges the duplicated edges of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph without duplicated edges.
    """
    num_nodes = _num_nodes(data)
    if self.keys is None:
        _, keys = _attribute_names(data, num_nodes)
    else:
        keys = [key for key in self.keys if getattr(data, key, None) is not None]
    edge_index = data.edge_index
    for key in keys:
        values = getattr(data, key)
        if not isinstance(values, mx.array):
            continue
        _, values = coalesce(edge_index, values, num_nodes, self.reduce)
        setattr(data, key, values)
    data.edge_index = coalesce(edge_index, num_nodes=num_nodes)
    return data

from mlx_graphs.transforms import RemoveDuplicatedEdges · mlx_graphs/transforms/edge_cleanup.py