Skip to content

to_dense_batch

to_dense_batch(
    node_features: array,
    batch_indices: Optional[array] = None,
    fill_value: float = 0.0,
    max_num_nodes: Optional[int] = None,
) -> tuple[array, array]

Converts the node features of a batch of graphs to a dense array of shape [batch_size, max_num_nodes, num_features], padded with fill_value.

Parameters:

Name Type Description Default
node_features array

Array of shape [num_nodes, ...] with the features of the nodes of all the graphs.

required
batch_indices Optional[array]

Array of shape [num_nodes] with the graph of each node (e.g. GraphDataBatch.batch_indices). Default: None (all the nodes belong to a single graph).

None
fill_value float

Value of the padded entries. Default: 0.0

0.0
max_num_nodes Optional[int]

Number of nodes per graph in the output. Graphs with more nodes are truncated. Default: None (the size of the largest graph).

None

Returns:

Type Description
array

A tuple (dense, mask) with the dense features of shape

array

[batch_size, max_num_nodes, ...] and a boolean mask of shape

tuple[array, array]

[batch_size, max_num_nodes] which is True for real nodes and

tuple[array, array]

False for padding.

Example:

node_features = mx.array([[1.0], [2.0], [3.0]])
batch_indices = mx.array([0, 0, 1])

dense, mask = to_dense_batch(node_features, batch_indices)
# dense: mx.array([[[1.0], [2.0]], [[3.0], [0.0]]])
# mask: mx.array([[True, True], [True, False]])
Source code in mlx_graphs/utils/dense.py
def to_dense_batch(
    node_features: mx.array,
    batch_indices: Optional[mx.array] = None,
    fill_value: float = 0.0,
    max_num_nodes: Optional[int] = None,
) -> tuple[mx.array, mx.array]:
    """
    Converts the node features of a batch of graphs to a dense array of shape
    ``[batch_size, max_num_nodes, num_features]``, padded with ``fill_value``.

    Args:
        node_features: Array of shape ``[num_nodes, ...]`` with the features of
            the nodes of all the graphs.
        batch_indices: Array of shape ``[num_nodes]`` with the graph of each
            node (e.g. ``GraphDataBatch.batch_indices``). Default: ``None``
            (all the nodes belong to a single graph).
        fill_value: Value of the padded entries. Default: ``0.0``
        max_num_nodes: Number of nodes per graph in the output. Graphs with
            more nodes are truncated. Default: ``None`` (the size of the
            largest graph).

    Returns:
        A tuple ``(dense, mask)`` with the dense features of shape
        ``[batch_size, max_num_nodes, ...]`` and a boolean mask of shape
        ``[batch_size, max_num_nodes]`` which is ``True`` for real nodes and
        ``False`` for padding.

    Example:

    .. code-block:: python

        node_features = mx.array([[1.0], [2.0], [3.0]])
        batch_indices = mx.array([0, 0, 1])

        dense, mask = to_dense_batch(node_features, batch_indices)
        # dense: mx.array([[[1.0], [2.0]], [[3.0], [0.0]]])
        # mask: mx.array([[True, True], [True, False]])
    """
    num_nodes = node_features.shape[0]
    batch, local, batch_size, largest = _batch_layout(batch_indices, num_nodes)
    if max_num_nodes is None:
        max_num_nodes = largest

    feature_shape = node_features.shape[1:]
    # `mx.full` ignores an integer dtype for a float fill value.
    dense = mx.full(
        (batch_size * max_num_nodes, *feature_shape),
        fill_value,
        dtype=node_features.dtype,
    ).astype(node_features.dtype)
    mask = np.zeros(batch_size * max_num_nodes, dtype=bool)

    keep = np.nonzero(local < max_num_nodes)[0]
    if keep.size > 0:
        flat = batch[keep] * max_num_nodes + local[keep]
        if keep.size < num_nodes:
            node_features = node_features[mx.array(keep)]
        dense[mx.array(flat)] = node_features
        mask[flat] = True

    return (
        dense.reshape(batch_size, max_num_nodes, *feature_shape),
        mx.array(mask.reshape(batch_size, max_num_nodes)),
    )

from mlx_graphs.utils import to_dense_batch · mlx_graphs/utils/dense.py