Skip to content

FeatureStore

Bases: ABC

Abstract key-value store of arrays, following PyG's FeatureStore.

Subclasses implement _put_tensor(), _get_tensor(), _remove_tensor(), _get_tensor_size() and get_all_tensor_attrs(). Arrays are passed and returned as mx.array (np.ndarray is also accepted when putting).

Attributes can be given as a TensorAttr, as positional arguments (group_name, attr_name, index) or as keywords, and the store can be indexed like a dict:

store.put_tensor(x, attr_name="x")
store["x"] = x  # same, homogeneous graphs (group_name=None)
rows = store.get_tensor(attr_name="x", index=mx.array([0, 5]))

Methods:

Name Description
get_all_tensor_attrs

The attributes (without index) of all the stored arrays.

put_tensor

Adds an array (or overwrites rows of an existing one when an

get_tensor

Returns the rows index (all rows if None) of an array.

multi_get_tensor

get_tensor() for several attributes.

remove_tensor

Removes an array. Returns whether it existed.

update_tensor

Replaces an existing array (or its rows index).

get_tensor_size

Shape of the full array, None if it does not exist.

get_all_tensor_attrs abstractmethod

get_all_tensor_attrs() -> list[TensorAttr]

The attributes (without index) of all the stored arrays.

Source code in mlx_graphs/data/feature_store.py
@abstractmethod
def get_all_tensor_attrs(self) -> list[TensorAttr]:
    """The attributes (without index) of all the stored arrays."""

put_tensor

put_tensor(tensor: Union[array, ndarray], *args, **kwargs) -> bool

Adds an array (or overwrites rows of an existing one when an index is given). Returns whether it succeeded.

Source code in mlx_graphs/data/feature_store.py
def put_tensor(self, tensor: Union[mx.array, np.ndarray], *args, **kwargs) -> bool:
    """Adds an array (or overwrites rows of an existing one when an
    ``index`` is given). Returns whether it succeeded."""
    return self._put_tensor(tensor, TensorAttr.cast(*args, **kwargs))

get_tensor

get_tensor(*args, **kwargs) -> array

Returns the rows index (all rows if None) of an array.

Raises:

Type Description
KeyError

If the array does not exist.

Source code in mlx_graphs/data/feature_store.py
def get_tensor(self, *args, **kwargs) -> mx.array:
    """Returns the rows ``index`` (all rows if ``None``) of an array.

    Raises:
        KeyError: If the array does not exist.
    """
    attr = TensorAttr.cast(*args, **kwargs)
    out = self._get_tensor(attr)
    if out is None:
        raise KeyError(f"'{attr.key}' is not in the feature store")
    return mx.array(out)

multi_get_tensor

multi_get_tensor(attrs: list[TensorAttr]) -> list[array]

get_tensor() for several attributes.

Source code in mlx_graphs/data/feature_store.py
def multi_get_tensor(self, attrs: list[TensorAttr]) -> list[mx.array]:
    """:meth:`get_tensor` for several attributes."""
    return [self.get_tensor(attr) for attr in attrs]

remove_tensor

remove_tensor(*args, **kwargs) -> bool

Removes an array. Returns whether it existed.

Source code in mlx_graphs/data/feature_store.py
def remove_tensor(self, *args, **kwargs) -> bool:
    """Removes an array. Returns whether it existed."""
    return self._remove_tensor(TensorAttr.cast(*args, **kwargs))

update_tensor

update_tensor(tensor: Union[array, ndarray], *args, **kwargs) -> bool

Replaces an existing array (or its rows index).

Source code in mlx_graphs/data/feature_store.py
def update_tensor(
    self, tensor: Union[mx.array, np.ndarray], *args, **kwargs
) -> bool:
    """Replaces an existing array (or its rows ``index``)."""
    attr = TensorAttr.cast(*args, **kwargs)
    if self._get_tensor_size(attr) is None:
        raise KeyError(f"'{attr.key}' is not in the feature store")
    if attr.index is None:
        self._remove_tensor(attr)
    return self._put_tensor(tensor, attr)

get_tensor_size

get_tensor_size(*args, **kwargs) -> Optional[tuple[int, ...]]

Shape of the full array, None if it does not exist.

Source code in mlx_graphs/data/feature_store.py
def get_tensor_size(self, *args, **kwargs) -> Optional[tuple[int, ...]]:
    """Shape of the full array, ``None`` if it does not exist."""
    return self._get_tensor_size(TensorAttr.cast(*args, **kwargs))

from mlx_graphs.data import FeatureStore · mlx_graphs/data/feature_store.py