Skip to content

mlvbench.cache

Caching utilities for reusable files.

mlvbench.cache provides utilities for caching files and reusing them across experiments. The following example illustrates how the cache is typically used:

from pathlib import Path

from mlvbench.cache import (
    get_cached_resource,
    create_cached_resource,
    finalize_cached_resource,
)

def my_data(seed: int = 2) -> Path:
    # A cached resource is identified by a name, version and the parameters that where
    # used to generate the resource:
    name = "my_data"
    version = "1"
    parameters = { "seed": seed }

    # If the resource with the given parameters already exists, get_cached_resource will
    # return its path.
    path = get_cached_resource(name, version, parameters)
    if path is not None:
        return path

    # If the resource doesn't exist yet, create_cached_resource will create a temporary
    # directory where we can prepare the data.
    path = create_cached_resource(name, version, parameters)

    # Prepare the data at the given path
    ...

    # Once all files have been generated, finalize_cached_resource will move the data to
    # its final location. This method takes care of race conditions: If a different
    # process has generated the same resource in the meantime, the newly created data
    # will be discarded and the cached data is used.
    path = finalize_cached_resource(path)

    return path

The location of the cache defaults to "~/.cache/mlvbench/cache" and can be configured via the MLVBENCH_CACHE_PATH environment variable.

PrecomputedDataset

Bases: Dataset

Precomputed dataset.

__getitem__(index)

Return the sample at the given index.

Returns:

Type Description
dict[str, ndarray]

A dictionary mapping each feature name to its value for this sample. Each value has the shape and dtype of the corresponding cached feature with the leading sample dimension removed.

__init__(path, features=None, mmap=True)

Initialize the dataset.

Parameters:

Name Type Description Default
path Path

Path to the precomputed dataset directory.

required
features list[str] | dict[str, str] | None

Optional selection of the features to load. If None (default), all stored features are loaded under their stored names. If a list, only the given stored features are loaded. If a dict, it maps the feature names in the returned samples to the names of the stored buffers, e.g. {"image": "image_reversed"}. The same buffer may be used for multiple features, but is loaded only once.

None
mmap bool

If True (default), buffers are memory-mapped from disk. If False, the entire arrays are loaded into RAM at construction time.

True

__len__()

Return the number of samples in the dataset.

create_cached_resource(name, version, parameters)

Create a new cache entry and return a temporary data path.

This will create a new cached resource at a temporary location. Call finalize_cached_resource to move it to its final location and make the data available for future runs.

Parameters:

Name Type Description Default
name str

Name of the cached resource.

required
version str

Version string of the resource.

required
parameters dict[str, Any]

Parameter dict that identifies this particular resource.

required

Returns:

Type Description
Path

Path to the directory where resource data should be written.

finalize_cached_resource(path)

Atomically finalize a cached resource by moving it to its final path.

If the target already exists (e.g., another worker finalized first), this emits a warning and deletes the newly created data.

Parameters:

Name Type Description Default
path Path

The data path returned by create_cached_resource.

required

Returns:

Type Description
Path

The updated data path.

Raises:

Type Description
ValueError

If path is not located within the cache directory.

get_cached_resource(name, version, parameters)

Return the data path of a complete cache entry, or None if not ready.

Parameters:

Name Type Description Default
name str

Name of the cached resource.

required
version str

Version string of the resource.

required
parameters dict[str, Any]

Parameter dict that identifies this particular resource.

required

Returns:

Type Description
Path | None

Path to the data/ subdirectory if the entry is complete, else None.

precompute_dataset(dataset, output_path, progress_bar=True)

Precompute the dataset and store it as np.memmap.

Parameters:

Name Type Description Default
dataset Dataset

The dataset to precompute.

required
output_path Path

The path to store the precomputed dataset.

required
progress_bar bool

Whether to show a progress bar.

True

Returns:

Type Description
PrecomputedDataset

A PrecomputedDataset object.