Skip to content

nexuml.evaluation.utils

nexuml.evaluation.utils

Utilities for evaluation algorithms.

ReservoirSampler

Online reservoir sampling (Vitter's Algorithm R).

Maintains a fixed-size random sample of items seen so far, suitable for streaming large datasets without storing everything.

add

add(items: Tensor) -> None

Add a batch of items (first dim = batch size).

get

get() -> torch.Tensor | None

Return sampled items as a tensor, or None if empty.

FeatureStore

Bases: ABC

Collect fit features for batch-fit distance estimators.

append abstractmethod

append(features: Tensor) -> None

Append a feature batch.

finalize abstractmethod

finalize() -> None

Finalize storage for reading.

as_array abstractmethod

as_array() -> torch.Tensor | np.ndarray | None

Return stored features as a CPU-compatible matrix.

cleanup abstractmethod

cleanup() -> None

Close handles and remove temporary files when not retained.

RAMFeatureStore

Bases: FeatureStore

In-memory feature storage with optional reservoir sampling.

MemmapFeatureStore

Bases: FeatureStore

Disk-backed feature storage that streams directly to a numpy memmap.

Features are written to disk during append() without buffering in RAM. The memmap is pre-allocated to (max_samples, D) when max_samples is provided; otherwise capacity starts at 1024 rows and doubles as needed. finalize() trims the file to the actual row count written.

create_feature_store

create_feature_store(
    backend: str = "ram",
    *,
    max_samples: int | None = None,
    storage_path: str | Path | None = None,
    retain_storage: bool = False,
) -> FeatureStore

Create a feature store backend.

Returns:

Type Description
FeatureStore

The requested FeatureStore instance.

Raises:

Type Description
ValueError

If backend is not "ram" or "memmap".