Hdf5File
Hdf5File is a File subclass that points at an
HDF5 file and provides methods for
inspecting its groups and datasets and reading dataset data.
Install the optional dependency with pip install 'datachain[hdf5]'.
An HDF5 file is a single byte stream, so Hdf5File rows are created by
read_storage with
type="hdf5". Data is read through the regular streaming file handle, so a
single dataset - or a single slice of one - is fetched without pulling the whole
file:
import datachain as dc
chain = dc.read_storage("s3://bucket-name/trajectories/", type="hdf5")
for (file,) in chain.limit(1).to_iter("file"):
print(file.get_info())
Paths follow the HDF5 convention and are absolute within the file (e.g.
/robot/joint_positions); the reader also accepts them without the leading
slash. A File obtained some other way can be converted with
file.as_hdf5_file().
The models are not re-exported from the top-level datachain namespace, so that
import datachain never loads h5py. Import them directly when annotating a
UDF or building a model by hand:
There are additional models for working with HDF5 files:
Hdf5Info- summary metadata for a file (attributes, dataset paths, group paths).Hdf5Dataset- a single dataset within a file; exposesshape,chunks,dtype, andattrs, and reads data viaread()orselect().Hdf5Selection- a lazy, bounded region inside a dataset (e.g. one image frame) that can travel through a chain as a column and is materialized on demand viaread()or rendered to image bytes viaread_bytes().
Only the generic HDF5 group/dataset model is handled here. Conventions layered on top of HDF5 - NetCDF4 dimensions and coordinates, LeRobot episode layouts - are not interpreted, though such files still load as ordinary HDF5.
Hdf5File
Bases: File
A data model for handling HDF5 files.
This model inherits from the File model and provides additional
functionality for inspecting an HDF5 file's groups and datasets and reading
dataset data.
Paths follow the HDF5 convention and are absolute within the file
(e.g. /robot/joint_positions); the reader also accepts them without the
leading slash.
Source code in datachain/lib/file.py
get_dataset
get_dataset(path: str) -> Hdf5Dataset
Return a single dataset by its path within the file.
Source code in datachain/lib/hdf5.py
get_datasets
get_datasets(group: str = '/') -> Iterator[Hdf5Dataset]
Yield every dataset under group (recursively).
Source code in datachain/lib/hdf5.py
get_info
get_info() -> Hdf5Info
Return summary metadata for the file.
Source code in datachain/lib/hdf5.py
Hdf5Dataset
Bases: DataModel
A single dataset within an :class:Hdf5File.
shape is the HDF5 shape as a list, so a scalar dataset has an empty
shape while a zero-length one-dimensional dataset has [0].
read
Read dataset data, optionally restricted to a NumPy-style selection.
select
select(
index: int | list[int],
media: Literal["image", "audio", "video"] | None = None,
) -> Hdf5Selection
Return a lazy :class:Hdf5Selection pointing at an item in this dataset.
index addresses the leading axes (e.g. i or [i] for one
frame of an (N, H, W, C) dataset). The region is read on demand via
:meth:Hdf5Selection.read, so the item can travel through a DataChain
as a column without materializing its bytes.
Source code in datachain/lib/hdf5.py
Hdf5Selection
Bases: DataModel
A lazy, bounded region inside an :class:Hdf5Dataset.
Points at a single item (or block) inside a dataset without reading it,
analogous to how :class:~datachain.lib.file.File points at a byte stream.
index addresses the leading axes; :meth:read materializes the region.
read
read() -> Any
read_bytes
Render the selected region to encoded media bytes.
Only media="image" is supported for now: the region is read and
encoded with Pillow (e.g. PNG), so callers such as Studio can stream a
preview without materializing the image into the row.