HDF5Encoder¶
Bases: BaseEncoder
Attributes¶
| Name | Type | Value | Doc |
|---|---|---|---|
ENCODE_ID |
int |
0 |
Internal counter to ensure unique encoding ids. This is set back to 0 if reset_encode_id=True in encode(). Internally, this is only happens in dump(). Nothing bad happens if it is not reset, but the cache ids will not start at 0 as is "standard". |
assert_decode¶
Inherited class method from BaseEncoder.assert_decode().
decode_cached_obj
staticmethod
¶
decode_cached_obj(encoded, decode_cache=None)
Decode a cached object reference from HDF5 format.
This method handles the deserialization of object references that were cached during encoding to avoid duplicate serialization of identical objects. It supports both reference-type caching (where multiple references point to the same object) and copy-type caching (where objects with identical content are stored once and copied).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded cached object reference. Should have 'encode_type' attribute set to 'Serializable' and 'cache_type' attribute indicating the type of cache reference. |
required |
decode_cache
|
dict
|
Dictionary mapping cache IDs to decoded objects. Used to resolve object references and handle circular references. |
None
|
Returns:
| Type | Description |
|---|---|
Serializable | DeferredRef
|
The decoded object. If the referenced object is not yet available in the cache, returns a DeferredRef placeholder that will be resolved later. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid cached object reference. |
DecodableVersionError
|
If the serialization version is not supported. |
RuntimeError
|
If object references cannot be resolved due to missing source objects. |
decode_class
staticmethod
¶
decode_class(encoded) -> type
Decode a class/type from HDF5 format.
Deserializes a class reference that was serialized using encode_class. This allows for proper reconstruction of class types during deserialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded class information. Should have a 'class' subgroup with 'module' and 'class' attributes. |
required |
Returns:
| Type | Description |
|---|---|
type
|
The decoded class/type object. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid class reference. |
DecodableVersionError
|
If the serialization version is not supported. |
ImportError
|
If the class cannot be imported from the specified module. |
decode_dict
staticmethod
¶
decode_dict(encoded, decode_cache=None)
Decode a dictionary from HDF5 format.
Deserializes dictionary objects that were serialized using encode_dict. Preserves the original dictionary structure and insertion order by separately serializing keys and values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded dictionary. Should have a 'dict' subgroup with 'keys' and 'values' subgroups. |
required |
decode_cache
|
dict
|
Dictionary mapping cache IDs to decoded objects for reference resolution. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The decoded dictionary object. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid dictionary. |
DecodableVersionError
|
If the serialization version is not supported. |
decode_function
staticmethod
¶
decode_function(encoded)
Decode a callable function from HDF5 format.
Deserializes function objects that were serialized using encode_function. Reconstructs the function by evaluating its source code in an appropriate environment with necessary imports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded function. Should have a 'function' subgroup with 'source' dataset containing the function's source code. |
required |
Returns:
| Type | Description |
|---|---|
callable
|
The decoded function object. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid function. |
DecodableVersionError
|
If the serialization version is not supported. |
decode_iterable
staticmethod
¶
decode_iterable(encoded, decode_cache=None)
Decode an iterable (list, tuple, set) from HDF5 format.
Deserializes iterable objects that were serialized using encode_iterable. Supports both the original format (individual groups for each element) and the optimized format (HDF5 datasets for homogeneous native types).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded iterable. Should have an 'iterable' subgroup with appropriate structure. |
required |
decode_cache
|
dict
|
Dictionary mapping cache IDs to decoded objects for reference resolution. |
None
|
Returns:
| Type | Description |
|---|---|
list | tuple | set
|
The decoded iterable object of the appropriate type. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid iterable. |
DecodableVersionError
|
If the serialization version is not supported. |
decode_primitive
staticmethod
¶
decode_primitive(encoded)
Decode a primitive value from HDF5 format.
Deserializes primitive Python types that were serialized using encode_primitive. Handles type reconstruction and special cases like None values and bytes encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded primitive value. Should have 'encode_type' attribute set to 'primitive'. |
required |
Returns:
| Type | Description |
|---|---|
EncodablePrimitives
|
The decoded primitive value (int, float, bool, complex, None, str, or bytes). |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid primitive. |
DecodableVersionError
|
If the serialization version is not supported. |
ValueError
|
If the decoded primitive type is unexpected. |
decode_root_group
staticmethod
¶
decode_root_group(encoded: Encoded, decode_cache: DecodeCache = None) -> Encodable
Decode the root HDF5 group containing a serialized object.
This same "unwrap a single anonymous subgroup" shape is also what
every individual _SERIALIZE_ATTRS wrapper group looks like
(Serializable.encode never puts its own attributes directly on
the h5_group it's handed), so this method is actually invoked
recursively throughout a file's whole tree, not only once at the
true file root -- only the true root ever carries a "version"
attribute, which is how it's told apart below.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Encoded
|
An HDF5 group containing a serialized object, or a wrapper around one. Should contain exactly one subgroup, with no attributes of its own except possibly a single "version" attribute (present only on the file's true root group). |
required |
decode_cache
|
DecodeCache
|
Cache used to track decoded objects and resolve references during deserialization. |
None
|
Returns:
| Type | Description |
|---|---|
Encodable
|
The decoded object contained in the HDF5 file. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded structure is not a valid HDF5 serialization format. |
decode_uncached_obj
staticmethod
¶
decode_uncached_obj(encoded, decode_cache=None)
Decode a Serializable object from HDF5 format.
Deserializes a Serializable object that was not previously cached. This method handles the core deserialization logic for Serializable objects, including attribute reconstruction and circular reference handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded
|
Group
|
The HDF5 group containing the encoded Serializable object. Should have 'encode_type' attribute set to 'Serializable' and contain subgroups for each serialized attribute. |
required |
decode_cache
|
dict
|
Dictionary mapping cache IDs to decoded objects. Used to handle circular references and object caching during deserialization. |
None
|
Returns:
| Type | Description |
|---|---|
Serializable
|
The decoded Serializable object of the appropriate class. |
Raises:
| Type | Description |
|---|---|
IncorrectDecodableTypeError
|
If the encoded object is not a valid Serializable object. |
DecodableVersionError
|
If the serialization version is not supported. |
ImportError
|
If the class cannot be imported from the specified module. |
encode_array
staticmethod
¶
encode_array(to_encode, h5_group=None)
Encode NumPy arrays and SciPy sparse matrices to HDF5 format.
Serializes array data with support for both dense and sparse matrices. Uses optimized storage strategies including compression and chunking for large arrays, and handles complex numbers by separating real and imaginary parts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_encode
|
EncodableArrays
|
The array to encode. Can be a NumPy array (NDArray) or SciPy sparse matrix (SPSArray). |
required |
h5_group
|
Group
|
The HDF5 group to write the array data to. |
None
|
Returns:
| Type | Description |
|---|---|
Group
|
The HDF5 group containing the encoded array data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the array type is not supported. |
encode_cached_obj
staticmethod
¶
encode_cached_obj(cache_id, h5_group=None, cache_type='reference', reference_cache_id=None, source_cache_id=None)
Encode a cached object reference in HDF5 format.
This method creates a reference to an object that has already been serialized, avoiding duplicate storage of identical objects. Used for implementing object caching during serialization to improve efficiency and handle circular references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_id
|
int
|
The cache ID for this reference. |
required |
h5_group
|
Group
|
The HDF5 group to write the cached object reference to. |
None
|
cache_type
|
str
|
Type of cache reference, either 'reference' (multiple references to same object) or 'copy' (copy of an existing object). Default is 'reference'. |
'reference'
|
reference_cache_id
|
int
|
For copy-type caching, the cache ID of the reference object. |
None
|
source_cache_id
|
int
|
For copy-type caching, the cache ID to assign to the copied object. |
None
|
Returns:
| Type | Description |
|---|---|
Group
|
The HDF5 group containing the encoded cached object reference. |
encode_dict
staticmethod
¶
encode_dict(to_encode, encode_cache=None, ignore_no_serialize_flags=False, h5_group=None)
Encode a dictionary into a serializable format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_encode
|
dict
|
The dictionary to be encoded. |
required |
encode_cache
|
EncodeCache
|
Cache used to track encoded objects and avoid duplication. |
None
|
ignore_no_serialize_flags
|
bool
|
If True, ignores any no-serialize flags on objects. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
Encoded
|
The encoded representation of the dictionary. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This is an abstract method that must be implemented by subclasses. |
encode_function
staticmethod
¶
encode_function(to_encode, h5_group=None)
Serialize a callable function.
encode_iterable
staticmethod
¶
encode_iterable(to_encode, encode_cache=None, ignore_no_serialize_flags=False, h5_group=None)
Encode an iterable into a serializable format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_encode
|
EncodableIterables
|
The iterable to be encoded. |
required |
encode_cache
|
EncodeCache
|
Cache used to track encoded objects and avoid duplication. |
None
|
ignore_no_serialize_flags
|
bool
|
If True, ignores any no-serialize flags on objects. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
Encoded
|
The encoded representation of the iterable. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This is an abstract method that must be implemented by subclasses. |
encode_primitive
staticmethod
¶
encode_primitive(to_encode, h5_group=None)
Encode a primitive value in HDF5 format.
Serializes primitive Python types (int, float, bool, complex, None, str, bytes)
to HDF5 format. Handles type preservation and special cases like None values.
Numpy scalars keep their dtype (the attribute stores it), and complex
values are tagged with cast_to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_encode
|
EncodablePrimitives
|
The primitive value to encode. Can be int, float, bool, complex, None, str, or bytes. |
required |
h5_group
|
Group
|
The HDF5 group to write the primitive value to. |
None
|
Returns:
| Type | Description |
|---|---|
Group
|
The HDF5 group containing the encoded primitive value. |
encode_uncached_obj
staticmethod
¶
encode_uncached_obj(to_encode, encode_cache=None, ignore_no_serialize_flags=False, h5_group=None)
Encode an object that has not been previously cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_encode
|
Serializable
|
The object to be encoded. |
required |
encode_cache
|
EncodeCache
|
Cache used to track encoded objects and avoid duplication. |
None
|
ignore_no_serialize_flags
|
bool
|
If True, ignores any no-serialize flags on objects. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
Encoded
|
The encoded representation of the object. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This is an abstract method that must be implemented by subclasses. |