add_remaining_self_loops
¶
add_remaining_self_loops(
edge_index: array,
edge_features: Optional[array] = None,
fill_value: FillValue = None,
num_nodes: Optional[int] = None,
) -> tuple[array, Optional[array]]
Adds a self-loop (i, i) to every node of the graph that does not
already have one.
The existing self-loops are kept with their features (if a node has
several self-loops, only the last one is kept), while the new ones get
features computed from fill_value. All the self-loops are placed after
the other edges, in node order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
a [2, num_edges] array representing the source and target nodes of each edge |
required |
edge_features
|
Optional[array]
|
Optional features of each edge, of shape
|
None
|
fill_value
|
FillValue
|
Features of the added self-loops. Either |
None
|
num_nodes
|
Optional[int]
|
Number of nodes in the graph. Inferred from |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
Optional[array]
|
if it was not given). |
Example:
edge_index = mx.array([[0, 1, 1], [1, 0, 1]])
edge_features = mx.array([0.5, 0.5, 2.0])
add_remaining_self_loops(edge_index, edge_features, num_nodes=3)
# (mx.array([[0, 1, 0, 1, 2], [1, 0, 0, 1, 2]]),
# mx.array([0.5, 0.5, 1.0, 2.0, 1.0]))
Source code in mlx_graphs/utils/loop.py
from mlx_graphs.utils import add_remaining_self_loops · mlx_graphs/utils/loop.py