qewton.data.datasets.array_data package¶
Module contents¶
Submodules¶
qewton.data.datasets.array_data.base module¶
Base implementation for array-like datasets. Handles backend tensors, e.g. np.ndarray or torch.Tensor, that support slicing and have a .shape property.
- class qewton.data.datasets.array_data.base.ArrayLikeDataSet(data: Any, data_configs: DataConfiguration | list[DataConfiguration], backend: type[ComputingBackend] | None = None, data_paths: list[str] | None = None, save_data: bool = False)[source]¶
Bases:
DataSetA dataset of dense, array-like data, e.g. np.ndarray or torch.Tensor.
Holds one or more data items, e.g. inputs and targets, each described by its own DataConfiguration. All items belong to one backend, which is either given or inferred from the data. The data can also be loaded from files via from_file, optionally only once the dataset is set up.
- Parameters:
data (Any | list[Any]) – The data item(s). Several items are passed as a list, together with a list of data_configs. An item may be None if it is loaded from data_paths in setup().
data_configs (DataConfiguration | list[DataConfiguration]) – One configuration per data item, mapping its dimensions to semantic axes.
backend (type[Backend], optional) – The backend of the data. If None, it is inferred from the first data item. Defaults to None.
data_paths (list[str], optional) – One file per data item, from which setup() loads the data if it is not loaded yet. Set by from_file. Defaults to None.
save_data (bool, optional) – If True, the data will be saved when the dataset is serialized. Defaults to False.
- Raises:
TypeError – If a data item is not a tensor of the backend.
ValueError – If no backend is given and none can be inferred from the data, or if a variable name appears in several data configs.
- classmethod from_file(path: str | list[str], data_configs: DataConfiguration | list[DataConfiguration], backend: type[ComputingBackend], load_on_setup: bool = False)[source]¶
Creates a dataset from files, read with backend.load_data.
- Parameters:
path (str | list[str]) – The file(s) to load, one per data item.
data_configs (DataConfiguration | list[DataConfiguration]) – One configuration per file, mapping its dimensions to semantic axes.
backend (type[Backend]) – The backend that reads the files and handles the data.
load_on_setup (bool, optional) – If True, the files are only read when the dataset is set up. Defaults to False.
- Returns:
- The dataset, keeping the paths so the data can
be reloaded after deserialization.
- Return type:
- update_configs()[source]¶
Validates and updates the size of dimensions in the data configurations based on the actual shape of the provided data objects.
- property data_configs: list[DataConfiguration]¶
Returns the data configurations for the data stored in this dataset.
- get_batch(idcs) list[Any][source]¶
Retrieve a batch of data given a list of indices.
- Parameters:
idcs (list[int]) – Indices of the samples to retrieve.
- Returns:
- A list where each element corresponds to a data object
(e.g., input tensors, targets).
- Return type:
list[Any]
- property data_set_loaded: bool¶
Check if the dataset is fully loaded into memory.
- Returns:
True if all data objects are loaded, False otherwise.
- Return type:
bool
- get_continuous_batch(start_idx, end_idx) list[Any][source]¶
Retrieve a continuous slice of data.
- Parameters:
start_idx (int) – Starting index.
end_idx (int) – Ending index.
- Returns:
Sliced data objects.
- Return type:
list[Any]
- load_complete_data(variable=None, data_item=None)[source]¶
Fully loads data into memory.
- Parameters:
variable (str, optional) – If provided, returns only the data for this specific variable.
data_item (int, optional) – If provided, returns the i-th data object.
- Returns:
The requested data.
- Return type:
Any
- load(serializer: Deserializer, data_config: dict) None[source]¶
Loads the object’s state from the serialized data. It sets the object’s attributes based on the serialized data.
- Parameters:
serializer (Deserializer) – The deserializer instance that manages the loading process.
data_config (dict) – A dictionary containing the serialized data for the object.
qewton.data.datasets.array_data.grid module¶
- class qewton.data.datasets.array_data.grid.GridDataSet(data, feature_variables: Variable | list[Variable], geometry_variable: Variable = None, point_grid=None, point_filter=None)[source]¶
Bases:
ArrayLikeDataSetA dataset of one or more array-like data items sharing the same grid geometry, each with shape [batch, *grid, feature].
- Parameters:
data (Any | list[Any]) – One or more array-like data items, each of shape (batch_size, *grid_shape, feature_dim). All items must share the same grid_shape. An item’s batch_size may be smaller than the others’, as long as it equals 1.
feature_variables (Variable | list[Variable]) – One feature Variable per data item, in the same order as data.
geometry_variable (Variable, optional) – The variable connected to the grid geometry. Defaults to an auto-named Variable(“grid”, …) matching the grid’s dimensionality.
point_grid (TensorType, optional) – Explicit grid point coordinates, of shape grid_shape + (len(grid_shape),). If given, a GridGeometry is built from these points instead of an IndexGridGeometry. Defaults to None.
point_filter (TensorType, optional) – Whether each grid point belongs to the domain, of shape grid_shape + (1,), e.g. for a rectilinear grid in a domain that is no box; only with point_grid. Defaults to None (every point does).
- Raises:
AssertionError – If the data items do not share the same grid shape or a compatible batch size, if the number of data items does not match the number of feature variables, or if point_grid’s shape does not match the data’s grid shape.
qewton.data.datasets.array_data.hdf5 module¶
- class qewton.data.datasets.array_data.hdf5.HDF5DataSet(path: str, keys: str | list[str], data_configs: ~qewton.config.data_configurations.base.DataConfiguration | list[~qewton.config.data_configurations.base.DataConfiguration], backend: type[~qewton.backends.base.Backend], dtype: ~typing.Any = <class 'qewton.config.dtypes.Float32'>, load_on_setup: bool = False, save_data: bool = False)[source]¶
Bases:
ArrayLikeDataSetA dataset read from an HDF5 file into memory.
Each key names a dataset inside the file, which becomes one data item, e.g. inputs and targets. The data is read completely into tensors of the given backend, so it behaves like any other ArrayLikeDataSet.
Only the path and the keys are kept, never an open file. The dataset can therefore be saved, pickled and copied, e.g. by the Tuner. After loading a saved dataset, setup() reads the file again.
- Parameters:
path (str) – The HDF5 file.
keys (str | list[str]) – The datasets inside the file to read, one per data item. Datasets inside groups are given as “group/dataset”.
data_configs (DataConfiguration | list[DataConfiguration]) – One configuration per key, mapping its dimensions to semantic axes.
backend (type[Backend]) – The backend whose tensors hold the data.
dtype (optional) – The data type the data is cast to. None keeps the type stored in the file. Defaults to Float32.
load_on_setup (bool, optional) – If True, the file is only read when the dataset is set up. Defaults to False.
save_data (bool, optional) – If True, the data will be saved when the dataset is serialized. Defaults to False.
- Raises:
ImportError – If h5py is not installed.
KeyError – If a key is not found in the file.
TypeError – If a key names a group instead of a dataset.
- classmethod from_file(path: str, keys: str | list[str], data_configs: ~qewton.config.data_configurations.base.DataConfiguration | list[~qewton.config.data_configurations.base.DataConfiguration], backend: type[~qewton.backends.base.Backend], dtype: ~typing.Any = <class 'qewton.config.dtypes.Float32'>, load_on_setup: bool = False)[source]¶
Same as the constructor, for symmetry with ArrayLikeDataSet.from_file.
- property metadata: dict¶
The HDF5 attributes of each key, read from the file.