laboneq.workflow.logbook.serializer
¶
Logbook serializer for artifacts.
See the functions below named serialize_ for the list of types that can be serialized.
LMFIT_IMPORTED = True
module-attribute
¶
SKLEARN_IMPORTED = True
module-attribute
¶
UNCERTAINTIES_IMPORTED = True
module-attribute
¶
OrjsonSerializer
¶
A helper class for using orjson.
It applies consistent settings when calling orjson.dumps.
COMPLEX_DTYPE_MAP = {np.dtype(np.complex128): np.dtype(np.float64), np.dtype(np.complex64): np.dtype(np.float32)}
class-attribute
instance-attribute
¶
SUPPORTED_TYPES = (type(None), int, float, complex, bool, str, dict, list, tuple, np.integer, np.ndarray)
class-attribute
instance-attribute
¶
default(obj)
¶
A default handler for objects orjson does not support directly.
dumps(obj)
¶
Call orjson.dumps with the appropriate settings.
Current options set are:
* OPT_SORT_KEYS
* OPT_SERIALIZE_NUMPY
The method self.default is passed to orjson.dump(..., default=...).
* self.default
serialize(obj, opener)
¶
Serialize the provided JSON-serializable object.
supported_object(obj)
¶
Return true if the whole object is supported.
supported_type(obj)
¶
Return true if the type of the object is supported.
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 |
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 |
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_dict(obj, opener)
¶
Serialize dicts.
serialize_laboneq_object(obj, opener)
¶
Serialize LabOne Q types.
serialize_list(obj, opener)
¶
Serialize lists.
Supports lists of quantum parameters and lists that can be
serialized by converting them to numpy arrays and using
.serialize_numpy_array.
serialize_lmfit_model_result(obj, opener)
¶
Serialize an lmfit ModelResult.
ModelResults are saved as JSON using their .summary method.
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_scikit_learn_base_estimator(obj, opener)
¶
Serialize scikit-learn estimators.
Estimators parameter values are saved as JSON.
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.
serialize_tuple(obj, opener)
¶
Serialize tuples.
Supports tuples of quantum parameters and values that can be serialized to JSON.
serialize_uncertainties_affince_scalar_func(obj, opener)
¶
Serialize uncertainties AffineScalarFunc.
AfficeScalarFunc objects are created when performing arithmetic on
uncertainties Variable objects.
The AffineScalarFunc has its value, std_dev and tag saved as JSON.
serialize_uncertainties_variable(obj, opener)
¶
Serialize uncertainties Variable.
The Variable has its value, std_dev and tag saved as JSON.