h5pydantic.model

Module Contents

Classes

_H5Base

An implementation detail, to share the _load and _dump APIs.

H5DatasetConfig

All of the dataset configuration options.

H5Dataset

A pydantic.BaseModel representing a h5py.Dataset.

H5Group

A pydantic.BaseModel representing a h5py.Group.

Attributes

_H5Container

h5pydantic.model._H5Container
class h5pydantic.model._H5Base

Bases: pydantic.BaseModel

An implementation detail, to share the _load and _dump APIs.

classmethod __init_subclass__(**kwargs)
abstract _dump_container(h5file, prefix)

Dump the group/dataset container to the h5file.

Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

Return type:

_H5Container

_dump_children(container, h5file, prefix)
Parameters:
  • container (_H5Container) –

  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

_dump(h5file, prefix)
Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

classmethod _load_intrinsic(h5file, prefix)
Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

Return type:

dict[str, Any]

classmethod _load_children(h5file, prefix)
Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

classmethod _load(h5file, prefix)
Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

Return type:

typing_extensions.Self

class h5pydantic.model.H5DatasetConfig

Bases: pydantic.BaseModel

All of the dataset configuration options.

shape: tuple[pydantic.StrictInt, Ellipsis]
dtype: typing_extensions.Type[str] | typing_extensions.Type[float] | typing_extensions.Type[h5pydantic.types.H5Type] | typing_extensions.Type[enum.Enum]
class h5pydantic.model.H5Dataset(data_=None, **kwargs)

Bases: _H5Base

A pydantic.BaseModel representing a h5py.Dataset.

Parameters:
  • data – allows the value of the DataSet to be initialised to a numpy.Array. If this keyword is used, the data cannot be subsequently modified using the array assignment syntax.

  • data_ (Optional[numpy.ndarray]) –

class Config
arbitrary_types_allowed = True
_h5config: H5DatasetConfig
_data: numpy.ndarray | None
_dset: h5py.Dataset | None
_modified: bool
classmethod __init_subclass__(**kwargs)
_dump_container(h5file, prefix)

Dump the group/dataset container to the h5file.

Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

Return type:

typing_extensions.Self

classmethod _load_intrinsic(h5file, prefix)
Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

Return type:

dict

__getitem__(key)

Allow array like access to the underlying h5py Dataset.

__setitem__(index, value)

Allow aray like assignment to the underlying h5py Datset.

class h5pydantic.model.H5Group

Bases: _H5Base

A pydantic.BaseModel representing a h5py.Group.

This should be extended to model all your groups.

class Config
validate_assignment = True
_h5file: h5py.File
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 (pathlib.Path) – Path of HDF5 to load.

Returns:

The parsed H5Group model.

Return type:

typing_extensions.Self

close()

Close the underlying HDF5 file.

__enter__()
__exit__(exc_type, exc_value, traceback)
_dump_container(h5file, prefix)

Dump the group/dataset container to the h5file.

Parameters:
  • h5file (h5py.File) –

  • prefix (pathlib.PurePosixPath) –

Return type:

h5py.Group

_datasets(parent)
Parameters:

parent (str) –

_check_all_modified()
dump(filename, partial=False)

Dump the H5Group object tree into a file.

Parameters:
  • filename (pathlib.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 (pathlib.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