Skip to content

LocalDegreeProfile

Bases: BaseTransform

Appends the Local Degree Profile (LDP) from "A Simple yet Effective Baseline for Non-attribute Graph Classification" to the node features, as PyG's LocalDegreeProfile:

\[ \mathbf{x}_i = \mathbf{x}_i \, \Vert \, (\deg(i), \min(DN(i)), \max(DN(i)), \textrm{mean}(DN(i)), \textrm{std}(DN(i))) \]

where \(\deg(i)\) is the out-degree of node \(i\) and \(DN(i) = \{ \deg(j) \mid (i, j) \in \mathcal{E} \}\) holds the degrees of its neighbors. The statistics of a node without neighbors are zeros. As in PyG, the standard deviation is the population one, and values below \(\sqrt{10^{-5}}\) are set to zero.

Example:

graph = GraphData(edge_index=mx.array([[0, 1, 1, 2], [1, 0, 2, 1]]))
graph = LocalDegreeProfile()(graph)
graph.node_features
# mx.array([[1, 2, 2, 2, 0], [2, 1, 1, 1, 0], [1, 2, 2, 2, 0]])

Methods:

Name Description
process

Appends the local degree profile to the node features of data.

process

process(data: GraphData) -> GraphData

Appends the local degree profile to the node features of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the updated node_features.

Source code in mlx_graphs/transforms/degree.py
def process(self, data: GraphData) -> GraphData:
    """Appends the local degree profile to the node features of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the updated ``node_features``.
    """
    num_nodes = _num_nodes(data)
    row, col = _host_edges(data)
    degree = np.bincount(row, minlength=num_nodes).astype(np.float64)
    neighbor_degree = degree[col]

    count = np.maximum(degree, 1.0)
    minimum = np.full(num_nodes, np.inf)
    maximum = np.full(num_nodes, -np.inf)
    np.minimum.at(minimum, row, neighbor_degree)
    np.maximum.at(maximum, row, neighbor_degree)
    minimum[degree == 0] = 0.0
    maximum[degree == 0] = 0.0
    mean = np.bincount(row, neighbor_degree, minlength=num_nodes) / count
    mean_sq = np.bincount(row, neighbor_degree**2, minlength=num_nodes) / count
    std = np.sqrt(np.maximum(mean_sq - mean**2, 1e-5))
    std[std <= np.sqrt(1e-5)] = 0.0

    profile = np.stack([degree, minimum, maximum, mean, std], axis=-1)
    return _add_node_features(data, mx.array(profile.astype(np.float32)), True)

from mlx_graphs.transforms import LocalDegreeProfile · mlx_graphs/transforms/degree.py