Coverage for install/scipp/serialization.py: 86%
22 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-17 01:51 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-17 01:51 +0000
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3# @author Simon Heybrock
5from typing import Any
7from .core import DataArray, DataGroup, Dataset, Variable
10def serialize(
11 var: Variable | DataArray | Dataset | DataGroup[Any],
12) -> tuple[dict[str, Any], list[bytes]]:
13 """Serialize Scipp object."""
14 from io import BytesIO
16 from .io.hdf5 import save_hdf5
18 header: dict[str, Any] = {}
19 buf = BytesIO()
20 save_hdf5(var, buf)
21 frames = [buf.getvalue()]
22 return header, frames
25def deserialize(
26 header: dict[str, Any], frames: list[bytes]
27) -> Variable | DataArray | Dataset | DataGroup[Any]:
28 """Deserialize Scipp object."""
29 from io import BytesIO
31 from .io.hdf5 import load_hdf5
33 return load_hdf5(BytesIO(frames[0]))
36try:
37 from distributed.protocol import register_serialization
39 register_serialization(Variable, serialize, deserialize)
40 register_serialization(DataArray, serialize, deserialize)
41 register_serialization(Dataset, serialize, deserialize)
42except ImportError:
43 pass
44__all__ = ['serialize', 'deserialize']