Skip to content

Constant

Constant(value: float = 1.0, cat: bool = True)

Bases: BaseTransform

Adds a constant value to the node features of each node.

This is a common way to give features to the nodes of a graph that has none.

Parameters:

Name Type Description Default
value float

The value to add. Default: 1.0

1.0
cat bool

If True, the value is appended as a new column to the existing node features, otherwise it replaces them. Default: True

True

Example:

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

Methods:

Name Description
process

Adds the constant column to the node features of data.

Source code in mlx_graphs/transforms/features.py
def __init__(self, value: float = 1.0, cat: bool = True):
    self.value = value
    self.cat = cat

process

process(data: GraphData) -> GraphData

Adds the constant column 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/features.py
def process(self, data: GraphData) -> GraphData:
    """Adds the constant column to the node features of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the updated ``node_features``.
    """
    constant = mx.full((_num_nodes(data), 1), self.value, dtype=mx.float32)
    return _add_node_features(data, constant, self.cat)

from mlx_graphs.transforms import Constant · mlx_graphs/transforms/features.py