Skip to content

TargetIndegree

TargetIndegree(
    norm: bool = True, max_value: Optional[float] = None, cat: bool = True
)

Bases: BaseTransform

Saves the globally normalized in-degree of the target node of each edge in its edge features, as PyG's TargetIndegree:

\[ \mathbf{u}(i,j) = \frac{\deg(j)}{\max_{v \in \mathcal{V}} \deg(v)} \]

Parameters:

Name Type Description Default
norm bool

Whether to normalize the degrees. Default: True

True
max_value Optional[float]

The value to divide the degrees by if norm is True. Defaults to the maximum in-degree of the graph.

None
cat bool

If True, the degrees are appended as a new column to the existing edge features, otherwise they replace them. Default: True

True

Example:

graph = GraphData(edge_index=mx.array([[0, 1, 2], [1, 2, 1]]))
graph = TargetIndegree()(graph)
graph.edge_features
# mx.array([[1.0], [0.5], [1.0]])

Methods:

Name Description
process

Adds the target in-degrees to the edge features of data.

Source code in mlx_graphs/transforms/degree.py
def __init__(
    self, norm: bool = True, max_value: Optional[float] = None, cat: bool = True
):
    self.norm = norm
    self.max_value = max_value
    self.cat = cat

process

process(data: GraphData) -> GraphData

Adds the target in-degrees to the edge features of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the updated edge_features.

Source code in mlx_graphs/transforms/degree.py
def process(self, data: GraphData) -> GraphData:
    """Adds the target in-degrees to the edge features of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the updated ``edge_features``.
    """
    col = _host_edges(data)[1]
    degree = np.bincount(col, minlength=_num_nodes(data)).astype(np.float64)
    if self.norm:
        scale = degree.max(initial=0) if self.max_value is None else self.max_value
        with np.errstate(divide="ignore", invalid="ignore"):
            degree = degree / scale
    pseudo = mx.array(degree[col].astype(np.float32).reshape(-1, 1))

    existing = data.edge_features
    if existing is not None and self.cat:
        if existing.ndim == 1:
            existing = existing.reshape(-1, 1)
        pseudo = mx.concatenate([existing, pseudo.astype(existing.dtype)], axis=-1)
    data.edge_features = pseudo
    return data

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