nemos.batching.LazyArrayDataLoader#

class nemos.batching.LazyArrayDataLoader(*arrays, batch_size, shuffle='chunk_order', seed=None)[source]#

Bases: _BaseArrayDataLoader

DataLoader for lazy/out-of-core arrays (e.g. dask, zarr, HDF5).

Unlike ArrayDataLoader, this loader does not eagerly convert arrays to JAX arrays. Instead, it reads slices from the source arrays and converts each batch to JAX on the fly. This keeps memory usage proportional to batch size rather than dataset size.

The default shuffle strategy is "chunk_order" (approximate): chunk order is randomized each pass, but samples within the same chunk always stay together in the same batch and keep their original order. Use "chunk_and_samples" to also permute samples within each batch. Both require only contiguous reads. Passing shuffle="full" shuffles the whole dataset like ArrayDataLoader. This requires the arrays to support fancy indexing and may be slower than reading contiguous segments. Indices within each batch are sorted to support HDF5 arrays via h5py.

This loader is re-iterable: each call to __iter__() returns a fresh iterator.

Examples

>>> import numpy as np
>>> from nemos.batching import LazyArrayDataLoader
>>> X = np.ones((100, 5))
>>> y = np.ones((100,))
>>> loader = LazyArrayDataLoader(X, y, batch_size=32, shuffle="chunk_order")
>>> for X_batch, y_batch in loader:
...     pass  # Train on batch

Attributes

batch_size

Number of samples in each batch.

n_samples

Total number of samples in the dataset.

Parameters:
  • arrays (ArrayLike)

  • batch_size (int)

  • shuffle (str)

  • seed (int | None)

__init__(*arrays, batch_size, shuffle='chunk_order', seed=None)[source]#

Initialize a lazy array data loader.

Parameters:
  • *arrays (ArrayLike) – Input and output arrays (any number). Each must support .shape and sequential slicing (arr[start:end]). The "full" strategy additionally requires support for fancy indexing.

  • batch_size (int) – Number of samples per batch.

  • shuffle (str) – Shuffle strategy: one of "none", "chunk_order", "chunk_and_samples", or "full". Default is "chunk_order".

  • seed (int | None) – Random seed for shuffling. Default is None.

Methods

__init__(*arrays, batch_size[, shuffle, seed])

Initialize a lazy array data loader.

sample_batch()

Return the first batch containing at least one valid sample.

__iter__()#

Return a fresh iterator over batches, dispatching on the shuffle strategy.

Return type:

Iterator[tuple[Array, ...]]

property batch_size: int#

Number of samples in each batch.

property n_samples: int#

Total number of samples in the dataset.

sample_batch()#

Return the first batch containing at least one valid sample.

Scans contiguous batches in sequential order (ignoring shuffle) and returns the first one with a sample that is finite (no NaN/Inf) across every array. This skips leading all-invalid regions, such as the NaN warmup of convolutional-basis features, while loading only as much data as needed.

Validity uses the same get_valid_multitree notion as validate_inputs and _preprocess_inputs, so an accepted batch is exactly one that passes validation and stays non-empty after NaN-dropping.

Return type:

tuple[Array, ...]

Returns:

The first batch with a valid sample. If no batch has one (the whole dataset is invalid), the first batch is returned so that downstream validation raises the canonical “all samples invalid” error.