Skip to content

laboneq.workflow.logbook.serializer

Logbook serializer for artifacts.

See the functions below named serialize_ for the list of types that can be serialized.

SerializationNotSupportedError

Bases: RuntimeError

SerializeOpener

Bases: ABC

An protocol allowing serializers to open files and access options.

Serializers need to write out potentially multiple files without knowing precisely where the files will end up.

Simultaneously the caller of serialize (e.g. the logbook) needs to keep a record of which files were created.

This class allows all of this to happen by abstracting away the file creation interface.

name() abstractmethod

Return a name for the object being serialized.

open(ext, *, encoding=None, suffix=None, description=None, binary=False) abstractmethod

Return an open file handle.

Parameters:

Name Type Description Default
ext str

The file extension to use (without a starting period).

required
encoding str | None

The encoding to use for text files.

None
suffix str | None

A suffix to add to the name of the file before the extension. This allows serializers that save multiple files to distinguish the files saved in a human-readable fashion.

None
description str | None

A description of the file contents. For example, a serializer saving a figure might save a .png file with the description "The plotted figure." and a .json file with the description "Metadata for the plot.".

None
binary bool

If true, files are opened for writing in binary mode. If false, the default, files are opened in text mode.

False

options() abstractmethod

Return the serialization options.

serialize(obj, opener)

Serialize an object.

Parameters:

Name Type Description Default
obj object

The object to serialize.

required
opener SerializeOpener

A SerializeOpener for retrieving options and opening files to write objects to.

required

serialize_bytes(obj, opener)

Serialize a Python bytes object.

Bytes objects are saved as a binary file with extension .dat.

No options are supported.

serialize_laboneq_object(obj, opener)

Serialize LabOne Q types.

serialize_list(obj, opener)

Serialize lists.

serialize_matplotlib_figure(obj, opener)

Serialize a matplotlib Figure.

Matplotlib figures are saved with matplotlib.figure.Figure.savefig.

The format to save in is passed in the format option which defaults to png.

The remaining options are passed as the pil_kwargs argument to .savefig.

serialize_numpy_array(obj, opener)

Serialize a NumPy ndarray.

NumPy arrays are saved with numpy.save and the extension .npy.

Any options are passed directly as keyword arguments to .save.

serialize_pil_image(obj, opener)

Serialize a PIL image.

PIL images are saved with PIL.Image.save.

The format to save in is passed in the format option which defaults to png. The format jpg is automatically converted to jpeg for PIL.

The remaining options are passed directly to PIL.Image.save as keyword arguments.

serialize_str(obj, opener)

Serialize a Python str object.

String objects are saved as a text file with extension .txt and UTF-8 encoding.

No options are supported.