Data
This module is used to initialise datasets (constructed using anemoi-datasets) and load data into the model. It performs validation checks, such as ensuring that the training dataset end date is before the start date of the validation dataset.
Each training, validation, and test dataset is a map-style dataset:
an index selects one sample, which __getitem__ reads from all data
readers. The datamodule pairs each dataset with a sampler that decides
which samples a rank loads and in which order, and with a dataloader
that records its position so that training can resume in the middle of
an epoch.
Dataset Architecture
The data module provides two types of dataset readers that wrap anemoi-datasets data:
Native Grid Dataset
The NativeGridDataset class is used for standard atmospheric data
on a native grid. It provides a simple interface for reading data samples
at specified time indices.
Trajectory Dataset
The TrajectoryDataset class extends NativeGridDataset to support
trajectory-based sampling, where data is organized into temporal
trajectories. This is useful for tracking atmospheric features over time
or for specialized training strategies that require trajectory awareness.
Trajectories are defined by:
Trajectory start: The reference datetime from which trajectories begin
Trajectory length: The number of time steps in each trajectory
Each sample in the dataset is associated with a trajectory ID, ensuring that samples are correctly grouped and that trajectory boundaries are respected during training.
Multi-Dataset
The MultiDataset class provides a higher-level wrapper that can
synchronize and combine multiple datasets (either NativeGridDataset
or TrajectoryDataset instances). This is the primary interface used
for training and supports:
Synchronizing samples across multiple datasets with different grids
Handling grid sharding for distributed training
Shuffling and the split of samples between groups of ranks are done by the sampler that the datamodule builds, not by the dataset.
Note
Users wishing to change the format of the batch input into the model
should sub-class MultiDataset and override the get_sample
method.
Data Loading Policy
The DataLoadingPolicy pairs the dataset construction above with the
batch sampler that orders, shards and batches its samples, so the two always
stay consistent with each other. It is selected via the
dataloader.loading_policy config entry. The default,
SynchronizedLoadingPolicy, builds one synchronized MultiDataset and
splits the samples of a distributed sampler into batches with torchdata’s
BatchSampler, which remembers its position so that training can resume
in the middle of an epoch.
API Reference
Dataset Readers
- class anemoi.training.data.data_reader.BaseAnemoiReader(dataset: str | dict | None = None, dataset_config: str | dict | None = None, start: datetime | int | None = None, end: datetime | int | None = None)
Bases:
objectAnemoi data reader for native grid datasets.
A sample is addressed by a 2-D anchor
(sequence, position). Relative time offsets are applied within a single sequence, so a sample can never span two sequences. Analysis datasets expose a single sequence (the whole time series); forecast datasets expose one sequence per initialisation (base date), with the forecast step as the position.- default_sampling: dict
Sampling config used by
compute_anchors().{"stride": 1}keeps every valid position;{"stride": None}uses stride = window size (non-overlapping).
- property missing_sequences: set[int]
Return sequences that are entirely missing and must not be sampled.
- compute_anchors(relative_indices: list[int] | ndarray, sampling: dict | None = None) ndarray
Return the valid
(sequence, position)anchors for a relative window.- Parameters:
relative_indices (list[int] | np.ndarray) – Relative offsets (in positions) requested around each anchor.
sampling (dict | None) – Sampling configuration with key
"stride".{"stride": None}uses stride = window size (non-overlapping);{"stride": 1}keeps every valid position;{"stride": 6}steps anchors by 6. Defaults todefault_sampling.
- Returns:
Array of shape
(n_anchors, 2)with(sequence, position)rows.- Return type:
np.ndarray
- property dates: ndarray
Return dataset dates.
- statistics_tendencies(timestep: int | str | timedelta | None = None) dict | None
Return dataset tendency statistics.
- property cutout_mask: ndarray
Return cutout mask.
- property boundary_mask: ndarray
Return boundary mask, defined as the complement of the cutout mask.
- class anemoi.training.data.data_reader.NativeGridDataset(dataset: str | dict | None = None, dataset_config: str | dict | None = None, start: datetime | int | None = None, end: datetime | int | None = None, sampling: dict | None = None)
Bases:
BaseAnemoiReaderNative grid (analysis) dataset.
A single sequence covering the whole time series; relative offsets index the time axis directly. This is the default analysis behaviour.
- class anemoi.training.data.data_reader.TrajectoryDataset(dataset: str | dict | None = None, dataset_config: str | dict | None = None, start: datetime | int | None = None, end: datetime | int | None = None, sampling: dict | None = None)
Bases:
BaseAnemoiReaderTrajectory dataset with an explicit lead-step axis.
Wraps a 5-D
trajectories-layout dataset opened throughanemoi.datasets.open_dataset()(on-disk shape(base_dates, variables, ensembles, steps, cells)). Each base date (forecast initialisation) is exposed as an independent sequence and the forecast step is the within-sequence position, so a training sample is always contained within a single forecast and never crosses initialisation boundaries.Step subsetting (
steps,step_start,step_end,step_frequency) and base-date subsetting (start/endon the valid-time envelope, orbase_start/base_end) are handled byopen_datasetvia the dataset configuration.- missing_positions(sequence: int = 0) set[int]
Forecast datasets do not track per-step missing values.
- anemoi.training.data.data_reader.create_dataset(dataset_config: dict, **_kwargs) BaseAnemoiReader
Factory function to create a data reader based on the dataset configuration.
Multi-Dataset API
- class anemoi.training.data.multidataset.MultiDataset(data_readers: dict[str, BaseAnemoiReader], relative_date_indices: dict[str, slice | int | list[int] | ndarray], fake_dataloading: bool = False)
Bases:
DatasetMulti-dataset wrapper that returns synchronized samples from multiple data readers.
Each index refers to one valid (sequence, position) anchor shared by all data readers. The dataloader’s sampler decides which indices a rank loads and in which order.
- set_relative_date_indices(relative_date_indices: dict[str, slice | int | list[int] | ndarray]) None
Set the time steps loaded for each sample, and the samples that provide them.
Call this before the dataloader is created: the number of samples changes with the time steps, and the sampler reads it when it is constructed.
- property supporting_arrays: dict[str, dict]
Return combined supporting arrays from all data readers.
Data Loading Policy API
- class anemoi.training.data.loading.DataLoadingPolicy
Bases:
ABCBuilds the dataset and batch sampler consumed by one dataloader stage.
A policy owns both the dataset layout (e.g. one synchronized dataset vs. several independently indexed ones) and the batch sampler that orders, shards and batches its samples, so the two always stay consistent with each other.
- abstractmethod build_dataset(data_readers: dict[str, BaseAnemoiReader], relative_date_indices: dict[str, slice | int | list[int] | ndarray], *, fake_dataloading: bool = False) Dataset
Build the dataset for one dataloader stage.
- Parameters:
data_readers (dict[str, BaseAnemoiReader]) – Dictionary mapping dataset names to their data readers.
relative_date_indices (dict[str, TimeIndices]) – Precomputed relative date indices for each data reader.
fake_dataloading (bool, optional) – Load one real sample and reuse it for subsequent accesses, by default False.
- Returns:
Map-style dataset that returns one sample for each index.
- Return type:
Dataset
- abstractmethod build_batch_sampler(dataset: Dataset, *, num_replicas: int, rank: int, shuffle: bool, seed: int, batch_size: int, drop_last: bool) BatchSampler
Build the batch sampler that orders, shards and batches the dataset’s samples.
The returned batch sampler must meet two conditions:
Its
samplerattribute has aset_epoch(epoch)method that selects the shuffle of an epoch. The datamodule calls it when it builds a dataloader, and Lightning calls it at the start of every epoch.Its batches depend only on the seed and the epoch. A resumed run restores the position saved by torchdata’s stateful
BatchSampler, or, if the batch sampler keeps no state, replays the batches of the epoch and skips the ones already trained on. Both give the same remaining batches only if the order is reproducible.
- Parameters:
dataset (Dataset) – Dataset built by
build_dataset()for the same stage.num_replicas (int) – Number of sample groups the dataset is split across.
rank (int) – Sample group this batch sampler produces batches for.
shuffle (bool) – Shuffle the samples.
seed (int) – Seed for the shuffle, shared by every rank of a sample group.
batch_size (int) – Number of samples per batch.
drop_last (bool) – Drop the remaining samples if they don’t divide evenly across sample groups.
- Returns:
Batch sampler that hands out lists of sample indices for this sample group.
- Return type:
BatchSampler
- class anemoi.training.data.loading.SynchronizedLoadingPolicy
Bases:
DataLoadingPolicyDefault policy: one synchronized dataset and a distributed sampler split into batches.
Every sample contains data from every reader, and the samplers of a sample group all hand out the same indices in the same (shuffled) order.
- build_dataset(data_readers: dict[str, BaseAnemoiReader], relative_date_indices: dict[str, slice | int | list[int] | ndarray], *, fake_dataloading: bool = False) MultiDataset
Build the dataset for one dataloader stage.
- Parameters:
data_readers (dict[str, BaseAnemoiReader]) – Dictionary mapping dataset names to their data readers.
relative_date_indices (dict[str, TimeIndices]) – Precomputed relative date indices for each data reader.
fake_dataloading (bool, optional) – Load one real sample and reuse it for subsequent accesses, by default False.
- Returns:
Map-style dataset that returns one sample for each index.
- Return type:
Dataset
- build_batch_sampler(dataset: Dataset, *, num_replicas: int, rank: int, shuffle: bool, seed: int, batch_size: int, drop_last: bool) BatchSampler
Build the batch sampler that orders, shards and batches the dataset’s samples.
The returned batch sampler must meet two conditions:
Its
samplerattribute has aset_epoch(epoch)method that selects the shuffle of an epoch. The datamodule calls it when it builds a dataloader, and Lightning calls it at the start of every epoch.Its batches depend only on the seed and the epoch. A resumed run restores the position saved by torchdata’s stateful
BatchSampler, or, if the batch sampler keeps no state, replays the batches of the epoch and skips the ones already trained on. Both give the same remaining batches only if the order is reproducible.
- Parameters:
dataset (Dataset) – Dataset built by
build_dataset()for the same stage.num_replicas (int) – Number of sample groups the dataset is split across.
rank (int) – Sample group this batch sampler produces batches for.
shuffle (bool) – Shuffle the samples.
seed (int) – Seed for the shuffle, shared by every rank of a sample group.
batch_size (int) – Number of samples per batch.
drop_last (bool) – Drop the remaining samples if they don’t divide evenly across sample groups.
- Returns:
Batch sampler that hands out lists of sample indices for this sample group.
- Return type:
BatchSampler