pysdic.core.serialization.NpzSerializerMixin#

Note

This class is internal: it is not exported in the pysdic namespace. Its methods are available on all the classes that inherit from it.

Serialization to and from NPZ files (without pickle).

class NpzSerializerMixin[source]#

Bases: SerializerMixin

Mixin class to provide NPZ serialization and deserialization capabilities. Classes that inherit from this mixin can be easily converted to and from NPZ format.

The file is written and read without pickle (allow_pickle=False): only numeric, boolean and string data are supported.

  • numpy.ndarray, list, tuple, set -> array (read back as a numpy.ndarray).

  • int, float, bool, str, bytes and numpy scalars -> 0-d array (read back as Python scalars).

  • None -> listed in a reserved entry (read back as None).

Warning

The dictionary returned by the to_dict method must have only one level of nesting, as NPZ format does not support nested structures.

Warning

Files written by previous versions of this mixin stored scalars as pickled object arrays and cannot be read with allow_pickle=False.

classmethod from_npz_file(file_path, **kwargs)[source]#

Deserialize an object from an NPZ file.

Parameters:

file_path (Any) – The path to the NPZ file to be read (str, Path, …).

Returns:

An instance of the class, populated with the data read from the NPZ file.

Return type:

Any

to_npz_file(file_path, *, make_dirs=False, **kwargs)[source]#

Serialize the object to an NPZ file.

Note

The file is written at exactly file_path: no .npz extension is appended (unlike numpy.savez called with a path).

Parameters:
  • file_path (Any) – The path to the file where the NPZ representation of the class will be saved (str, Path, …).

  • make_dirs (bool, optional (default: False)) – If True, the method will create any missing directories in the file path before writing the NPZ.

Return type:

None