Skip to content

subgraph

Functions:

Name Description
subgraph

Returns the subgraph induced by the nodes in subset, i.e. the edges of

k_hop_subgraph

Computes the num_hops-hop subgraph around the nodes node_idx.

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 [num_nodes].

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 [num_edges, ...]. Default: None

None
relabel_nodes bool

If True, the returned edge_index uses new node ids in [0, len(subset)): the position of each node in subset (its rank among the selected nodes for a boolean mask). Default: False

False
num_nodes Optional[int]

Number of nodes in the graph. Inferred from edge_index (or from the length of a boolean subset) if not given.

None
return_edge_mask bool

If True, also returns the boolean mask of shape [num_edges] of the edges that were kept. Default: False

False

Returns:

Type Description
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

A tuple (edge_index, edge_features) with the edges of the subgraph

Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

and their features (None if edge_features was not given),

Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

followed by the edge mask if return_edge_mask is True.

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
def subgraph(
    subset: NodeSubset,
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    relabel_nodes: bool = False,
    num_nodes: Optional[int] = None,
    return_edge_mask: bool = False,
) -> Union[
    tuple[mx.array, Optional[mx.array]],
    tuple[mx.array, Optional[mx.array], mx.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``.

    Args:
        subset: The nodes to keep, either as an array (or list) of node
            indices or as a boolean mask of shape ``[num_nodes]``.
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        edge_features: Optional features of each edge, of shape
            ``[num_edges, ...]``. Default: ``None``
        relabel_nodes: If ``True``, the returned ``edge_index`` uses new node
            ids in ``[0, len(subset))``: the position of each node in
            ``subset`` (its rank among the selected nodes for a boolean mask).
            Default: ``False``
        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            (or from the length of a boolean ``subset``) if not given.
        return_edge_mask: If ``True``, also returns the boolean mask of shape
            ``[num_edges]`` of the edges that were kept. Default: ``False``

    Returns:
        A tuple ``(edge_index, edge_features)`` with the edges of the subgraph
        and their features (``None`` if ``edge_features`` was not given),
        followed by the edge mask if ``return_edge_mask`` is ``True``.

    Example:

    .. code-block:: python

        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)
    """
    sub_edge_index, sub_edge_features, edge_mask = _subgraph(
        edge_index, subset, edge_features, relabel_nodes, num_nodes
    )
    if return_edge_mask:
        return sub_edge_index, sub_edge_features, mx.array(edge_mask)
    return sub_edge_index, sub_edge_features

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 True, the returned edge_index uses new node ids in [0, len(subset)), following the order of the returned subset. Default: False

False
num_nodes Optional[int]

Number of nodes in the graph. Inferred from edge_index if not given.

None
directed bool

If True, only the edges that were followed to reach the nodes are kept. If False (default), all the edges between the nodes of the subgraph are kept.

False

Returns:

Type Description
array

A tuple (subset, edge_index, mapping, edge_mask) with

- `subset`

the sorted ids of the nodes of the subgraph;

- `edge_index`

the edges of the subgraph;

- `mapping`

the position of each node of node_idx in subset (its new id when relabel_nodes=True);

- `edge_mask`

boolean mask of shape [num_edges] of the edges that were kept.

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
def k_hop_subgraph(
    node_idx: Union[int, Sequence[int], mx.array],
    num_hops: int,
    edge_index: mx.array,
    relabel_nodes: bool = False,
    num_nodes: Optional[int] = None,
    directed: bool = False,
) -> tuple[mx.array, mx.array, mx.array, mx.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).

    Args:
        node_idx: The central node(s), as an int, a list or an array.
        num_hops: Number of hops to follow.
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        relabel_nodes: If ``True``, the returned ``edge_index`` uses new node
            ids in ``[0, len(subset))``, following the order of the returned
            ``subset``. Default: ``False``
        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            if not given.
        directed: If ``True``, only the edges that were followed to reach the
            nodes are kept. If ``False`` (default), all the edges between the
            nodes of the subgraph are kept.

    Returns:
        A tuple ``(subset, edge_index, mapping, edge_mask)`` with

        - ``subset``: the sorted ids of the nodes of the subgraph;
        - ``edge_index``: the edges of the subgraph;
        - ``mapping``: the position of each node of ``node_idx`` in ``subset``
            (its new id when ``relabel_nodes=True``);
        - ``edge_mask``: boolean mask of shape ``[num_edges]`` of the edges
            that were kept.

    Example:

    .. code-block:: python

        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])
    """
    return _k_hop_subgraph(
        edge_index, node_idx, num_hops, relabel_nodes, num_nodes, directed
    )

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 (src_subset, dst_subset) of the source and destination nodes to keep, each given as an array (or list) of node indices or as a boolean mask.

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 [num_edges, ...]. Default: None

None
relabel_nodes bool

If True, the source (destination) nodes of the returned edge_index are relabeled to their position in src_subset (dst_subset), or their rank among the selected nodes for a boolean mask. Default: False

False
size Optional[tuple[int, int]]

The number of source and destination nodes (num_src, num_dst). Inferred from edge_index if not given.

None
return_edge_mask bool

If True, also returns the boolean mask of shape [num_edges] of the edges that were kept. Default: False

False

Returns:

Type Description
Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

A tuple (edge_index, edge_features) with the edges of the subgraph

Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

and their features (None if edge_features was not given),

Union[tuple[array, Optional[array]], tuple[array, Optional[array], array]]

followed by the edge mask if return_edge_mask is True.

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
def bipartite_subgraph(
    subset: tuple[NodeSubset, NodeSubset],
    edge_index: mx.array,
    edge_features: Optional[mx.array] = None,
    relabel_nodes: bool = False,
    size: Optional[tuple[int, int]] = None,
    return_edge_mask: bool = False,
) -> Union[
    tuple[mx.array, Optional[mx.array]],
    tuple[mx.array, Optional[mx.array], mx.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]``.

    Args:
        subset: A tuple ``(src_subset, dst_subset)`` of the source and
            destination nodes to keep, each given as an array (or list) of
            node indices or as a boolean mask.
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        edge_features: Optional features of each edge, of shape
            ``[num_edges, ...]``. Default: ``None``
        relabel_nodes: If ``True``, the source (destination) nodes of the
            returned ``edge_index`` are relabeled to their position in
            ``src_subset`` (``dst_subset``), or their rank among the selected
            nodes for a boolean mask. Default: ``False``
        size: The number of source and destination nodes ``(num_src,
            num_dst)``. Inferred from ``edge_index`` if not given.
        return_edge_mask: If ``True``, also returns the boolean mask of shape
            ``[num_edges]`` of the edges that were kept. Default: ``False``

    Returns:
        A tuple ``(edge_index, edge_features)`` with the edges of the subgraph
        and their features (``None`` if ``edge_features`` was not given),
        followed by the edge mask if ``return_edge_mask`` is ``True``.

    Example:

    .. code-block:: python

        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)
    """
    sub_edge_index, sub_edge_features, edge_mask = _bipartite_subgraph(
        edge_index, subset, edge_features, relabel_nodes, size
    )
    if return_edge_mask:
        return sub_edge_index, sub_edge_features, mx.array(edge_mask)
    return sub_edge_index, sub_edge_features

from mlx_graphs.utils import subgraph · mlx_graphs/utils/subgraph.py