Skip to content

MaskToIndex

MaskToIndex(
    attrs: Optional[Union[str, list[str]]] = None, replace: bool = False
)

Bases: BaseTransform

Converts boolean mask attributes (named *_mask, e.g. train_mask) to index attributes (named *_index, e.g. train_index).

Parameters:

Name Type Description Default
attrs Optional[Union[str, list[str]]]

Name(s) of the mask attributes to convert. Default: None (all the attributes ending with _mask).

None
replace bool

If True, the mask attributes are removed. Default: False

False

Example:

graph = GraphData(
    edge_index=mx.array([[0, 1, 2], [1, 2, 3]]),
    train_mask=mx.array([True, False, True, False]),
)
graph = MaskToIndex()(graph)
graph.train_index
# mx.array([0, 2])

Methods:

Name Description
process

Adds the indices to data.

Source code in mlx_graphs/transforms/masks.py
def __init__(
    self,
    attrs: Optional[Union[str, list[str]]] = None,
    replace: bool = False,
):
    self.attrs = [attrs] if isinstance(attrs, str) else attrs
    self.replace = replace

process

process(data: GraphData) -> GraphData

Adds the indices to data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the index attributes.

Source code in mlx_graphs/transforms/masks.py
def process(self, data: GraphData) -> GraphData:
    """Adds the indices to ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the index attributes.
    """
    for attr in _attrs_with_suffix(self.attrs, data, "_mask"):
        if getattr(data, attr, None) is None:
            continue
        setattr(data, f"{attr[:-5]}_index", mask_to_index(getattr(data, attr)))
        if self.replace:
            delattr(data, attr)
    return data

from mlx_graphs.transforms import MaskToIndex · mlx_graphs/transforms/masks.py