Skip to content
heliaEDGE
Reference
HELIA

grain_data

Grain pipelines over a DataSource.

Machine-readable model

  • to_grainfunctionRead source through Grain: shuffle, repeat, transform, batch and prefetch.
function

to_grain

Python

Read source through Grain: shuffle, repeat, transform, batch and prefetch.

helia_edge/data/grain_data.py:24

to_grain(
source: DataSource[Any],
*,
seed: int | None = None,
shuffle: bool = False,
transform: Callable[[Any, np.random.Generator], Any] | None = None,
batch_size: int | None = None,
drop_remainder: bool = False,
num_epochs: int | None = 1,
workers: int = 0,
worker_buffer_size: int = 1,
) -> grain.IterDataset

Read source through Grain: shuffle, repeat, transform, batch and prefetch.

The order is source, shuffle (a new permutation each epoch), repeat, transform, batch, so batches run across epoch boundaries. Record order and transform randomness depend only on seed, not on workers. Every iter() replays the same elements, so a Keras fit that iterates once per epoch sees the same epoch each time: for Keras training, pass num_epochs=None and set steps_per_epoch.

Parameters of to_grain
NameTypeDefaultDescription
sourceDataSource[Any]RequiredRecords addressed by index.
seedint | NoneNoneSeed for the shuffle and for the per-record generator passed to ``transform``, from 0 to 2**32 - 1. Required when ``shuffle`` is set or ``transform`` is given.
shuffleboolFalseShuffle the records of every epoch (each of the ``num_epochs`` passes).
transformCallable[[Any, np.random.Generator], Any] | NoneNone``transform(record, rng) -> record``, run per record in the Grain workers; ``rng`` is a NumPy generator derived from ``seed`` and the record's position.
batch_sizeint | NoneNoneStack this many records into one batch of NumPy arrays; None keeps single records.
drop_remainderboolFalseDrop the last batch if it is smaller than ``batch_size``.
num_epochsint | None1Number of passes over ``source``; None repeats indefinitely.
workersint0Grain worker processes; 0 reads in this process.
worker_buffer_sizeint1Elements each worker prepares ahead.
Returns of to_grain
TypeDescription
grain.IterDatasetgrain.IterDataset: Re-iterable; each ``iter()`` starts from the first element.
Errors raised by to_grain
TypeDescription
ValueErrorIf ``seed`` is missing when needed or out of range, or a count is not a positive integer (``workers`` may be 0).
ImportErrorIf Grain is not installed (``helia-edge[grain]``).