Data¶
tinax.data provides a read/write interface, deterministic process-aware training and evaluation pipelines, dataset splitting, random-access sources, and explicit multiprocessing worker lifetime.
Grain multiprocessing requires caller-parsed Abseil flags. Tinax validates this requirement but never mutates global flags.
Both the ArrayRecord and Parquet pairs return the same random-access grain.MapDataset, so they compose identically with split_by_column, split_random, training_batches, and evaluation_batches. read_array_record/write_array_record need the optional array-record package installed separately; pyarrow for the Parquet pair is a core dependency.
read_array_record
¶
Load one ArrayRecord file's JSON-encoded rows into a lazy, randomly-accessible dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to an existing ArrayRecord file. |
required |
Returns:
| Type | Description |
|---|---|
MapDataset
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
FileNotFoundError
|
If no file exists at |
write_array_record
¶
write_array_record(
path: str | PathLike[str],
records: Iterable[Mapping[str, Any]],
*,
group_size: int = 1,
) -> None
Serialize an iterable of JSON-encodable mapping rows into an ArrayRecord file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Destination file path. |
required |
records
|
Iterable[Mapping[str, Any]]
|
Iterable of JSON-encodable mappings, one per row. |
required |
group_size
|
int
|
Number of records grouped per compressed chunk. Must be a positive integer. |
1
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
ImportError
|
If |
read_parquet_record
¶
Load one Parquet file's rows into a lazy, randomly-accessible dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to an existing Parquet file. |
required |
Returns:
| Type | Description |
|---|---|
MapDataset
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
FileNotFoundError
|
If no file exists at |
write_parquet_record
¶
Serialize a sequence of mapping rows into a Parquet file.
Unlike write_array_record, records must be a finite, in-memory sequence:
Parquet's columnar layout requires the full column set up front and cannot stream
row by row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Destination file path. Overwritten if it already exists. |
required |
records
|
Sequence[Mapping[str, Any]]
|
Non-empty sequence of mapping rows sharing one column schema. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
split_by_column
¶
split_by_column(
dataset: MapDataset[dict[str, Any]],
column: str,
*,
values: tuple[str, str] = ("train", "test"),
drop_column: bool = True,
) -> tuple[
MapDataset[dict[str, Any]], MapDataset[dict[str, Any]]
]
Partition dataset by an exact two-value discriminator column.
Every row's column value must be one of values; a row holding anything
else would otherwise be silently dropped by a naive filter-based split. The returned
partitions are re-indexed sources with correct len and indexing, so they compose
with the length-based training_batches and evaluation_batches pipelines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MapDataset[dict[str, Any]]
|
Finite |
required |
column
|
str
|
Name of the column carrying the discriminator value. |
required |
values
|
tuple[str, str]
|
The (first, second) partition values. |
('train', 'test')
|
drop_column
|
bool
|
Whether to drop |
True
|
Returns:
| Type | Description |
|---|---|
tuple[MapDataset[dict[str, Any]], MapDataset[dict[str, Any]]]
|
The two partitions of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
split_random
¶
split_random(
dataset: MapDataset[dict[str, Any]],
train_ratio: float,
*,
seed: int,
drop_column: str | None = None,
) -> tuple[
MapDataset[dict[str, Any]], MapDataset[dict[str, Any]]
]
Shuffle dataset and cut it into two partitions at train_ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MapDataset[dict[str, Any]]
|
Finite |
required |
train_ratio
|
float
|
Fraction of records in the first partition; must be strictly between 0 and 1 so neither partition is degenerately empty. |
required |
seed
|
int
|
Integer seed for the shuffle. |
required |
drop_column
|
str | None
|
Optional column name to drop from both partitions afterward. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[MapDataset[dict[str, Any]], MapDataset[dict[str, Any]]]
|
The (first, second) partitions. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
InMemorySource
¶
Snapshot an outer iterable while preserving references to its possibly mutable records.
A deterministic random-access source supporting len() and integer indexing.
The outer iterable is copied into a tuple at construction, but individual records
are held by reference and are not deep copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
Iterable[T]
|
Iterable of records to snapshot into the source. |
required |
source_id
|
str
|
Stable, non-empty identifier used in source representations. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
source_id
property
¶
Return the caller-assigned stable identity used in source representations.
training_batches
¶
training_batches(
dataset: MapDataset[T],
*,
shard_options: ShardOptions,
seed: int,
shuffle: bool,
num_epochs: int | None,
batch_size: int,
batch_fn: BatchTransform[T, BatchT],
) -> MapDataset[BatchT]
Build deterministic full local batches with equal process step counts in every training epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MapDataset[T]
|
Finite |
required |
shard_options
|
ShardOptions
|
Per-process shard selection. |
required |
seed
|
int
|
Integer seed for deterministic ordering and shuffling. |
required |
shuffle
|
bool
|
Whether to shuffle records within each epoch. |
required |
num_epochs
|
int | None
|
Number of epochs to repeat, or |
required |
batch_size
|
int
|
Local (per-process) batch size. Must be positive. |
required |
batch_fn
|
BatchTransform[T, BatchT]
|
Callable collating a sequence of records into one batch. |
required |
Returns:
| Type | Description |
|---|---|
MapDataset[BatchT]
|
A |
MapDataset[BatchT]
|
repeated for |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
evaluation_batches
¶
evaluation_batches(
dataset: MapDataset[T],
*,
shard_options: ShardOptions,
batch_size: int,
batch_fn: BatchTransform[T, BatchT],
) -> MapDataset[BatchT]
Build one ordered finite pass that keeps remainder records and may have unequal process step counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
MapDataset[T]
|
Finite |
required |
shard_options
|
ShardOptions
|
Per-process shard selection. |
required |
batch_size
|
int
|
Local (per-process) batch size. Must be positive. |
required |
batch_fn
|
BatchTransform[T, BatchT]
|
Callable collating a sequence of records into one batch. |
required |
Returns:
| Type | Description |
|---|---|
MapDataset[BatchT]
|
A |
MapDataset[BatchT]
|
and without dropping the final partial batch. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
open_multiprocessing_iterator
¶
open_multiprocessing_iterator(
dataset: IterDataset[T],
*,
num_workers: int,
per_worker_buffer_size: int = 1,
worker_init_fn: Callable[[int, int], None]
| None = None,
sequential_slice: bool = False,
) -> Iterator[DatasetIterator[T]]
Open a Grain multiprocessing iterator and close every worker when the context exits.
A context manager yielding a dataset iterator backed by worker processes; on exit the iterator and all workers are closed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
IterDataset[T]
|
|
required |
num_workers
|
int
|
Number of worker processes. |
required |
per_worker_buffer_size
|
int
|
Prefetch buffer size per worker. Must be positive. |
1
|
worker_init_fn
|
Callable[[int, int], None] | None
|
Optional callable run in each worker as
|
None
|
sequential_slice
|
bool
|
Whether workers slice the dataset sequentially. |
False
|
Yields:
| Type | Description |
|---|---|
DatasetIterator[T]
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
RuntimeError
|
If |