Skip to content

FeaturePropagation

FeaturePropagation(
    missing_mask: Union[array, ndarray], num_iterations: int = 40
)

Bases: BaseTransform

Fills in missing node features by propagating the known ones, with the feature propagation algorithm from "On the Unreasonable Effectiveness of Feature propagation in Learning on Graphs with Missing Node Features", as PyG's FeaturePropagation:

\[ \mathbf{X}^{(0)} &= (1 - \mathbf{M}) \cdot \mathbf{X} \mathbf{X}^{(\ell + 1)} &= \mathbf{X}^{(0)} + \mathbf{M} \cdot (\mathbf{D}^{-1/2} \mathbf{A} \mathbf{D}^{-1/2} \mathbf{X}^{(\ell)}) \]

where \(\mathbf{M}\) is the mask of the missing features and \(\mathbf{D}\) is the weighted in-degree. The edge weights are taken from edge_weights if it exists, otherwise from edge_features if it holds one scalar per edge (as PyG uses edge_attr), and are ones otherwise. No self-loops are added, and the weights of duplicate edges are summed (PyG instead collapses duplicate edges when the graph has no weights).

Parameters:

Name Type Description Default
missing_mask Union[array, ndarray]

Boolean mask of shape [num_nodes, num_node_features] that is True for the missing node features, e.g. mx.isnan(graph.node_features).

required
num_iterations int

Number of propagation steps. Default: 40

40

Example:

node_features = mx.array([[1.0], [float("nan")], [3.0]])
graph = GraphData(
    edge_index=mx.array([[0, 1, 1, 2], [1, 0, 2, 1]]),
    node_features=node_features,
)
graph = FeaturePropagation(mx.isnan(node_features))(graph)
graph.node_features
# mx.array([[1.0], [2.8284], [3.0]]), i.e. (1 + 3) / sqrt(2)

Methods:

Name Description
process

Fills in the missing node features of data.

Source code in mlx_graphs/transforms/propagation.py
def __init__(
    self, missing_mask: Union[mx.array, np.ndarray], num_iterations: int = 40
):
    self.missing_mask = mx.array(missing_mask).astype(mx.bool_)
    self.num_iterations = num_iterations

process

process(data: GraphData) -> GraphData

Fills in the missing node features of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the completed node_features.

Source code in mlx_graphs/transforms/propagation.py
def process(self, data: GraphData) -> GraphData:
    """Fills in the missing node features of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the completed ``node_features``.
    """
    features = data.node_features
    if features is None:
        raise ValueError("FeaturePropagation requires node features")
    if tuple(features.shape) != tuple(self.missing_mask.shape):
        raise ValueError(
            f"The missing mask has shape {tuple(self.missing_mask.shape)} but "
            f"the node features have shape {tuple(features.shape)}"
        )
    num_nodes = _num_nodes(data)
    src, dst = _host_edges(data)
    if getattr(data, "edge_weights", None) is not None:
        weights = _edge_weights(data)
    elif (
        data.edge_features is not None and data.edge_features.size == data.num_edges
    ):
        weights = np.asarray(data.edge_features).reshape(-1).astype(np.float64)
    else:
        weights = np.ones(data.num_edges, dtype=np.float64)
    weights = _sym_norm_weights(src, dst, weights, num_nodes)
    src_mx, dst_mx = mx.array(src), mx.array(dst)
    weights_mx = mx.array(weights.astype(np.float32))

    missing = self.missing_mask
    known = mx.where(missing, mx.zeros_like(features), features)
    out = known
    for _ in range(self.num_iterations):
        out = _propagate(out, src_mx, dst_mx, weights_mx, num_nodes)
        out = mx.where(missing, out, known)
    data.node_features = out.astype(features.dtype)
    return data

from mlx_graphs.transforms import FeaturePropagation · mlx_graphs/transforms/propagation.py