snnlab
API referencesnnlab.sim

snnlab.sim.datasets

Complete declared API of the datasets module, with signatures, data fields, validation and source.

Back to sim reference

Dataset loaders — MNIST (static images) and SHD (spiking audio events).

Pulled out of cli.py. The single canonical entry point is load_dataset(name, ...). Image-mode notebooks (snapshot rendering) use _load_dataset_image() for a single sample.

MNIST returns dense (N, 784) pixel rows in [0, 1] that the train/infer paths Poisson-encode per batch. SHD is event data — each utterance is a list of (spike_time_seconds, unit) pairs over 700 channels — so it returns an OBJECT array of per-sample event tuples instead of a dense block; the caller bins each sample to spikes lazily (see ShdBinnedDataset in train.py). Densifying the whole set at the model's native dt would be tens of GB, hence the lazy contract.

The signatures, defaults, fields, docstrings and implementation excerpts below are generated from the Python source. Annotations are shown as declared; unannotated means the source supplies no type annotation. These pages document callable surfaces, including legacy support utilities, without promising backend support for every declaration.

SymbolKind
load_datasetfunction

load_dataset

View source

def load_dataset(name, max_samples=None, split=False, dt_ms=None, t_ms=None, evaluation_split='validation', evaluation_only=False)

Source docstring:

Load full dataset as (X, y) numpy arrays in [0, 1] / int64.

Args:
    name: "mnist" (dense pixel rows) or "shd" (object array of events)
    max_samples: optional cap on the official training partition; the
           official MNIST test partition is never subsampled through this
           argument
    split: if True, return (X_train, X_eval, y_train, y_eval)
    evaluation_split: for MNIST, select either the deterministic validation
           split used during training or the untouched official test split.
           SHD retains its official train/test contract.
    dt_ms, t_ms: currently unused; kept for call-site compatibility
    evaluation_only: skip the unused MNIST training partition; requires
           split=True and evaluation_split='test'. Training outputs are None.

Single canonical loader used by train, infer, and image paths so
"first digit-0 sample" means the same physical sample everywhere.
ParameterAnnotationDefaultMeaning
nameunannotatedrequiredName used to identify the authored or rendered object.
max_samplesunannotatedNoneDefined by the source contract and implementation below.
splitunannotatedFalseDefined by the source contract and implementation below.
dt_msunannotatedNoneSimulation timestep in milliseconds.
t_msunannotatedNonePhysical duration in milliseconds.
evaluation_splitunannotated'validation'Defined by the source contract and implementation below.
evaluation_onlyunannotatedFalseDefined by the source contract and implementation below.

Return expressions (branch-dependent; names refer to the linked implementation):

(None, X_test, None, y_test)
(X_tr, X_te, y_tr, y_te)
(np.concatenate([X_tr, X_te]), np.concatenate([y_tr, y_te]))
(X, X_test, y, y_test)
(X_train, X_validation, y_train, y_validation)
(X, y)

Explicit exceptions in this implementation; called helpers may raise additional errors:

Explicit exception expression
ValueError("evaluation_only requires split=True and evaluation_split='test'")
ValueError(f"MNIST evaluation_split must be 'validation' or 'test', got {evaluation_split!r}")
ValueError(f'Unknown dataset: {name}')
Implementation
def load_dataset(
    name,
    max_samples=None,
    split=False,
    dt_ms=None,
    t_ms=None,
    evaluation_split="validation",
    evaluation_only=False,
):
    """Load full dataset as (X, y) numpy arrays in [0, 1] / int64.

    Args:
        name: "mnist" (dense pixel rows) or "shd" (object array of events)
        max_samples: optional cap on the official training partition; the
               official MNIST test partition is never subsampled through this
               argument
        split: if True, return (X_train, X_eval, y_train, y_eval)
        evaluation_split: for MNIST, select either the deterministic validation
               split used during training or the untouched official test split.
               SHD retains its official train/test contract.
        dt_ms, t_ms: currently unused; kept for call-site compatibility
        evaluation_only: skip the unused MNIST training partition; requires
               split=True and evaluation_split='test'. Training outputs are None.

    Single canonical loader used by train, infer, and image paths so
    "first digit-0 sample" means the same physical sample everywhere.
    """
    if evaluation_only and (not split or evaluation_split != "test"):
        raise ValueError("evaluation_only requires split=True and evaluation_split='test'")
    if name == "mnist":
        from torchvision import datasets, transforms

        mnist_test = datasets.MNIST(
            root="/tmp/mnist",
            train=False,
            download=True,
            transform=transforms.ToTensor(),
        )
        X_test = mnist_test.data.numpy().reshape(-1, 784).astype(np.float32) / 255.0
        y_test = mnist_test.targets.numpy().astype(np.int64)
        if evaluation_only:
            return None, X_test, None, y_test
        mnist_train = datasets.MNIST(
            root="/tmp/mnist",
            train=True,
            download=True,
            transform=transforms.ToTensor(),
        )
        X = mnist_train.data.numpy().reshape(-1, 784).astype(np.float32) / 255.0
        y = mnist_train.targets.numpy().astype(np.int64)
    elif name == "shd":
        # Event data. Keep SHD's OFFICIAL train/test split — it holds out two
        # speakers unseen in training, and every published SHD number is reported
        # on that test set, so resplitting would break
        # comparability. Returns object arrays of per-sample (units, times); the
        # caller bins them to spikes lazily at the run's dt.
        X_tr, y_tr = _read_shd_split("train")
        X_te, y_te = _read_shd_split("test")
        X_tr, y_tr = _subsample(X_tr, y_tr, max_samples)
        X_te, y_te = _subsample(X_te, y_te, max_samples)
        if split:
            return X_tr, X_te, y_tr, y_te
        return np.concatenate([X_tr, X_te]), np.concatenate([y_tr, y_te])
    else:
        raise ValueError(f"Unknown dataset: {name}")

    if max_samples is not None and max_samples < len(X):
        idx = np.random.RandomState(MNIST_SPLIT_SEED).choice(
            len(X), max_samples, replace=False
        )
        X, y = X[idx], y[idx]

    if split:
        if evaluation_split == "test":
            return X, X_test, y, y_test
        from sklearn.model_selection import train_test_split

        validation_size = max(
            int(np.ceil(len(y) * MNIST_VALIDATION_FRACTION)),
            len(np.unique(y)),
        )
        X_train, X_validation, y_train, y_validation = train_test_split(
            X,
            y,
            test_size=validation_size,
            random_state=MNIST_SPLIT_SEED,
            stratify=y,
        )
        if evaluation_split == "validation":
            return X_train, X_validation, y_train, y_validation
        raise ValueError(
            "MNIST evaluation_split must be 'validation' or 'test', "
            f"got {evaluation_split!r}"
        )
    return X, y

Constants and type aliases

Initial source expressions are shown, not evaluated runtime values. Legacy configuration may mutate module defaults.

NameAnnotationInitial expressionSource
SHD_N_INunannotated700Source
SHD_N_CLASSESunannotated20Source
DATASET_N_HIDDEN_DEFAULTSunannotated{'mnist': 1024, 'shd': 256}Source
MNIST_SPLIT_SEEDunannotated42Source
MNIST_VALIDATION_FRACTIONunannotated0.1Source

On this page