subgraph
¶
Functions:
| Name | Description |
|---|---|
subgraph |
Returns the subgraph induced by the nodes in |
k_hop_subgraph |
Computes the |
bipartite_subgraph |
Returns the subgraph of a bipartite graph induced by a subset of the |
subgraph
¶
subgraph(
subset: NodeSubset,
edge_index: array,
edge_features: Optional[array] = None,
relabel_nodes: bool = False,
num_nodes: Optional[int] = None,
return_edge_mask: bool = False,
) -> Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
Returns the subgraph induced by the nodes in subset, i.e. the edges of
edge_index whose source and destination are both in subset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
NodeSubset
|
The nodes to keep, either as an array (or list) of node
indices or as a boolean mask of shape |
required |
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
|
relabel_nodes
|
bool
|
If |
False
|
num_nodes
|
Optional[int]
|
Number of nodes in the graph. Inferred from |
None
|
return_edge_mask
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
|
A tuple |
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
|
and their features ( |
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
|
followed by the edge mask if |
Example:
edge_index = mx.array([[0, 1, 2, 3], [1, 2, 3, 0]])
subgraph(mx.array([1, 2, 3]), edge_index)
# (mx.array([[1, 2], [2, 3]]), None)
subset = mx.array([False, True, True, True])
subgraph(subset, edge_index, relabel_nodes=True)
# (mx.array([[0, 1], [1, 2]]), None)
Source code in mlx_graphs/utils/subgraph.py
k_hop_subgraph
¶
k_hop_subgraph(
node_idx: Union[int, Sequence[int], array],
num_hops: int,
edge_index: array,
relabel_nodes: bool = False,
num_nodes: Optional[int] = None,
directed: bool = False,
) -> tuple[array, array, array, array]
Computes the num_hops-hop subgraph around the nodes node_idx.
Edges are followed backwards from the target nodes: the subgraph contains
every node whose messages reach node_idx within num_hops message
passing steps (i.e. the receptive field of node_idx for a GNN with
num_hops layers).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_idx
|
Union[int, Sequence[int], array]
|
The central node(s), as an int, a list or an array. |
required |
num_hops
|
int
|
Number of hops to follow. |
required |
edge_index
|
array
|
a [2, num_edges] array representing the source and target nodes of each edge |
required |
relabel_nodes
|
bool
|
If |
False
|
num_nodes
|
Optional[int]
|
Number of nodes in the graph. Inferred from |
None
|
directed
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
- `subset`
|
the sorted ids of the nodes of the subgraph; |
- `edge_index`
|
the edges of the subgraph; |
- `mapping`
|
the position of each node of |
- `edge_mask`
|
boolean mask of shape |
Example:
edge_index = mx.array([[0, 1, 2, 3], [1, 2, 3, 4]]) # path 0 -> ... -> 4
subset, sub_edge_index, mapping, edge_mask = k_hop_subgraph(3, 2, edge_index)
# subset: mx.array([1, 2, 3])
# sub_edge_index: mx.array([[1, 2], [2, 3]])
# mapping: mx.array([2])
# edge_mask: mx.array([False, True, True, False])
Source code in mlx_graphs/utils/subgraph.py
bipartite_subgraph
¶
bipartite_subgraph(
subset: tuple[NodeSubset, NodeSubset],
edge_index: array,
edge_features: Optional[array] = None,
relabel_nodes: bool = False,
size: Optional[tuple[int, int]] = None,
return_edge_mask: bool = False,
) -> Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
Returns the subgraph of a bipartite graph induced by a subset of the
source nodes and a subset of the destination nodes, i.e. the edges whose
source is in subset[0] and destination is in subset[1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
tuple[NodeSubset, NodeSubset]
|
A tuple |
required |
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
|
relabel_nodes
|
bool
|
If |
False
|
size
|
Optional[tuple[int, int]]
|
The number of source and destination nodes |
None
|
return_edge_mask
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
|
A tuple |
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
|
and their features ( |
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]
|
followed by the edge mask if |
Example:
edge_index = mx.array(
[[0, 5, 2, 3, 3, 4, 4, 3, 5, 5, 6], [0, 0, 3, 2, 0, 0, 2, 1, 2, 3, 1]]
)
subset = (mx.array([2, 3, 5]), mx.array([2, 3]))
bipartite_subgraph(subset, edge_index, relabel_nodes=True)
# (mx.array([[0, 1, 2, 2], [1, 0, 0, 1]]), None)
Source code in mlx_graphs/utils/subgraph.py
from mlx_graphs.utils import subgraph · mlx_graphs/utils/subgraph.py