API Reference

h5pydantic

Modelling

For the most part, h5pydantic attempts to leave modelling to pydantic, there are, of course, areas where the interaction is not seamless.

Optional

It is possible to type a field (which could be a Group, a Dataset, an attribute, or a list) as typing.Optional. pydantic requires that that this be done by assigning None to the field. I strongly suggest using typing.Optional as well, for readability.

At this point, only typing.Optional Groups and Attributes are supported. An Optional list[] does not make much sense, just leave the list empty. Optional Datasets should be supported in future.

Lists

h5pydantic will take a list[] type to mean a zero counted group. An empty list is the only supported default value.

Instances

class h5pydantic.H5Group

A pydantic.BaseModel representing a h5py.Group.

This should be extended to model all your groups.

close()

Close the underlying HDF5 file.

dump(filename, partial=False)

Dump the H5Group object tree into a file.

Parameters:
  • filename (Path) – Path to dump the HDF5Group object tree to.

  • partial – If False, will raise an error if any H5Dataset is not written to.

Returns: None

dumper(filename, partial=False)

Context manager to dump the H5Group object tree into a file.

Inside the context manager, Datasets can be assigned to, which will write that data to the file.

At the cleanup stage, if partial is False, this context manager will ensure all Datasets have been written to.

Parameters:
  • filename (Path) – Path to dump the HDF5Group object tree to.

  • partial – If False, will raise an error if any H5Dataset is not written to.

Returns: None

classmethod load(filename)

Load a file into a tree of H5Group models.

Can be used as context manager, which allows dataset access in the body of the context manager, and will close() the filename at the end of the context block.

Parameters:

filename (Path) – Path of HDF5 to load.

Returns:

The parsed H5Group model.

Return type:

Self

class h5pydantic.H5Dataset(data_=None)

A pydantic.BaseModel representing a h5py.Dataset.

Parameters:

data_ (ndarray | None) –

__getitem__(key)

Allow array like access to the underlying h5py Dataset.

__setitem__(index, value)

Allow aray like assignment to the underlying h5py Datset.

Types

class h5pydantic.types.H5Int32

Signed Integers, using 32 bits.

numpy

alias of int32

class h5pydantic.types.H5Int64

Signed Integers, using 64 bits.

numpy

alias of int64

class h5pydantic.types.H5Type

All subclasses must be able to save all their possible values to HDF5 without error.