dantro.data_loaders.load_hdf5 module

Implements loading of Hdf5 files into the dantro data tree

class dantro.data_loaders.load_hdf5.Hdf5LoaderMixin[source]

Bases: object

Supplies functionality to load hdf5 files into the data manager.

It resolves the hdf5 groups into corresponding data groups and the datasets into NumpyDataContainers.

If enable_mapping is set, the class variables _HDF5_DSET_MAP and _HDF5_GROUP_MAP are used to map from a string to a container type. The class variable _HDF5_MAP_FROM_ATTR determines the default value of the attribute to read and use as input string for the mapping.


the default class to use for datasets. This should be a dantro BaseDataContainer -derived class. Note that certain data groups can overwrite the default class for underlying members.




if mapping is enabled, the equivalent dantro types for HDF5 groups are determined from this mapping.


Dict[str, type]


if mapping is enabled, the equivalent dantro types for HDF5 datasets are determined from this mapping.


Dict[str, type]


the name of the HDF5 dataset or group attribute to read in order to determine the type mapping. For example, this could be "content". This is the fallback value if no map_from_attr argument is given to dantro.data_loaders.load_hdf5.Hdf5LoaderMixin._load_hdf5()




if true (default), will attempt to decode HDF5 attributes that are stored as byte arrays into regular Python strings; this can make attribute handling much easier.




alias of dantro.containers.numeric.NumpyDataContainer

_load_hdf5(*args, **kwargs)

Loads the specified hdf5 file into DataGroup- and DataContainer-like objects; this completely recreates the hierarchic structure of the hdf5 file. The data can be loaded into memory completely, or be loaded as a proxy object.

The h5py File and Group objects will be converted to the specified DataGroup-derived objects; the Dataset objects to the specified DataContainer-derived object.

All HDF5 group or dataset attributes are carried over and are accessible under the attrs attribute of the respective dantro objects in the tree.

  • filepath (str) – The path to the HDF5 file that is to be loaded

  • TargetCls (type) – The group type this is loaded into

  • load_as_proxy (bool, optional) – if True, the leaf datasets are loaded as dantro.proxy.hdf5.Hdf5DataProxy objects. That way, the data is only loaded into memory when their .data property is accessed the first time, either directly or indirectly.

  • proxy_kwargs (dict, optional) – When loading as proxy, these parameters are unpacked in the __init__ call. For available argument see Hdf5DataProxy.

  • lower_case_keys (bool, optional) – whether to use only lower-case versions of the paths encountered in the HDF5 file.

  • enable_mapping (bool, optional) – If true, will use the class variables _HDF5_GROUP_MAP and _HDF5_DSET_MAP to map groups or datasets to a custom container class during loading. Which attribute to read is determined by the map_from_attr argument (see there).

  • map_from_attr (str, optional) – From which attribute to read the key that is used in the mapping. If nothing is given, the class variable _HDF5_MAP_FROM_ATTR is used.

  • direct_insertion (bool, optional) – If True, some non-crucial checks are skipped during insertion and elements are inserted (more or less) directly into the data tree, thus speeding up the data loading process. This option should only be enabled if data is loaded into a yet unpopulated part of the data tree, otherwise existing elements might be overwritten silently. This option only applies to data groups, not to containers.

  • progress_params (dict, optional) –

    parameters for the progress indicator. Possible keys:

    level (int):

    how verbose to print progress info; possible values are: 0: None, 1: on file level, 2: on dataset level. Note that this option and the progress_indicator of the DataManager are independent from each other.


    format string for progress report, receives the following keys:

    • progress_info (total progress indicator),

    • fname (basename of current hdf5 file),

    • fpath (full path of current hdf5 file),

    • name (current dataset name),

    • path (current path within the hdf5 file)


The populated root-level group, corresponding to

the base group of the file

Return type



ValueError – If enable_mapping, but no map attribute can be determined from the given argument or the class variable _HDF5_MAP_FROM_ATTR

_load_hdf5_proxy(*args, **kwargs)

This is a shorthand for _load_hdf5() with the load_as_proxy flag set.

_load_hdf5_as_dask(*args, **kwargs)

This is a shorthand for _load_hdf5() with the load_as_proxy flag set and resolve_as_dask passed as additional arguments to the proxy via proxy_kwargs.

_recursively_load_hdf5(src: Union[h5py._hl.group.Group, h5py._hl.files.File], *, target: dantro.base.BaseDataGroup, lower_case_keys: bool, direct_insertion: bool, **kwargs)[source]

Recursively loads the data from a source object (an h5.File or a h5.Group) into the target dantro group.

  • src (Union[h5.Group, h5.File]) – The HDF5 source object from which to load the data. This object it iterated over.

  • target (BaseDataGroup) – The target group to populate with the data from src.

  • lower_case_keys (bool) – Whether to make keys lower-case

  • direct_insertion (bool) – Whether to use direct insertion mode on the target group (and all groups below)

  • **kwargs – Passed on to the group and container loader methods, _container_from_h5dataset() and _group_from_h5group().


NotImplementedError – When encountering objects other than groups or datasets in the HDF5 file

_group_from_h5group(h5grp: h5py._hl.group.Group, target: dantro.base.BaseDataGroup, *, name: str, map_attr: str, GroupMap: dict, **_) → dantro.base.BaseDataGroup[source]

Adds a new group from a h5.Group

The group types may be mapped to different dantro types; this is controlled by the extracted HDF5 attribute with the name specified in the _HDF5_MAP_FROM_ATTR class attribute.

  • h5grp (h5.Group) – The HDF5 group to create a dantro group for in the target group.

  • target (BaseDataGroup) – The group in which to create a new group that represents h5grp

  • name (str) – the name of the new group

  • GroupMap (dict) – Map of names to BaseDataGroup-derived types; always needed, but may be empty

  • map_attr (str) – The HDF5 attribute to inspect in order to determine the name of the mapping

  • **_ – ignored

_container_from_h5dataset(h5dset: h5py._hl.dataset.Dataset, target: dantro.base.BaseDataGroup, *, name: str, load_as_proxy: bool, proxy_kwargs: dict, DsetCls: type, map_attr: str, DsetMap: dict, plvl: int, pfstr: str, **_) → dantro.base.BaseDataContainer[source]

Adds a new data container from a h5.Dataset

The group types may be mapped to different dantro types; this is controlled by the extracted HDF5 attribute with the name specified in the _HDF5_MAP_FROM_ATTR class attribute.

  • h5dset (h5.Dataset) – The source dataset to load into target as a dantro data container.

  • target (BaseDataGroup) – The target group where the h5dset will be represented in as a new dantro data container.

  • name (str) – the name of the new container

  • load_as_proxy (bool) – Whether to load as Hdf5DataProxy

  • proxy_kwargs (dict) – Upon proxy initialization, unpacked into dantro.proxy.hdf5.Hdf5DataProxy.__init__()

  • DsetCls (BaseDataContainer) – The type that is used to create the dataset-equivalents in target. If mapping is enabled, this serves as the fallback type.

  • map_attr (str) – The HDF5 attribute to inspect in order to determine the name of the mapping

  • DsetMap (dict) – Map of names to BaseDataContainer-derived types; always needed, but may be empty

  • plvl (int) – the verbosity of the progress indicator

  • pfstr (str) – a format string for the progress indicator

_decode_attr_val(attr_val) → str[source]

Wrapper around decode_bytestrings

_evaluate_type_mapping(key: str, *, attrs: dict, tmap: Dict[str, type], fallback: type) → type[source]

Given an attributes dict or group attributes, evaluates which type a target container should use.