Casting - Cast your data structure to bytes¶
- manta_light.utils.dict_to_bytes(d: dict) bytes ¶
Convert a dictionary to bytes
- Parameters:
d (dict) – The dictionary to convert
- Returns:
The bytes representation of the dictionary
- Return type:
bytes
Examples
>>> from manta_light.utils import dict_to_bytes >>> dictionary = {"key1": ["a", "b"], "key2", 0.2} >>> dict_to_bytes(dictionary)
Note
You can also find dict_to_bytes
in manta.utils
.
- manta_light.utils.bytes_to_dict(b: bytes) dict ¶
Convert bytes to a dictionary
- Parameters:
b (bytes) – The bytes to convert
- Returns:
The dictionary representation of the bytes
- Return type:
dict
Examples
>>> from manta_light.utils import dict_to_bytes >>> dictionary = {"key1": ["a", "b"], "key2", 0.2} >>> dict_bytes = dict_to_bytes(dictionary) >>> bytes_to_dict(dict_bytes)
Note
You can also find bytes_to_dict
in manta.utils
.
- manta_light.utils.torchmodel_to_bytes(model: torch.nn.Module) bytes ¶
Transform a torch model into bytes
- Parameters:
model ("torch.nn.Module") – Torch model
- Returns:
Bytes from the torch model
- Return type:
bytes
Examples
>>> from torch.nn import Linear, ReLU, Sequential >>> from manta_light.utils import bytes_to_torchmodel, torchmodel_to_bytes >>> torch_model = Sequential( ... Linear(3, 2), ReLU(), Linear(2, 1), ReLU(), Linear(1, 1), ReLU() ... ) >>> torchmodel_to_bytes(torch_model)
- manta_light.utils.bytes_to_torchmodel(b: bytes) torch.nn.Module ¶
Transform bytes to torch model
- Parameters:
b (bytes) – Bytes from a torch model
- Returns:
Torch model
- Return type:
“torch.nn.Module”
Examples
>>> from torch.nn import Linear, ReLU, Sequential >>> from manta_light.utils import bytes_to_torchmodel, torchmodel_to_bytes >>> torch_model = Sequential( ... Linear(3, 2), ReLU(), Linear(2, 1), ReLU(), Linear(1, 1), ReLU() ... ) >>> model_bytes = torchmodel_to_bytes(torch_model) >>> bytes_to_torchmodel(model_bytes)
- manta_light.utils.numpy_to_bytes(data: ndarray | list | dict) bytes | dict ¶
Recursive function which converts a numpy array or iterator with numpy arrays to bytes
- Parameters:
data (np.array) – The numpy array to convert
- Returns:
The bytes representation of the numpy array
- Return type:
bytes
Examples
From
np.array
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> np_array = np.array([1, 2, 3]) >>> numpy_to_bytes(np_array)
From
Dict[str, np.array]
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> dict_np_array = {"key1": np.array([1, 2, 3]), "key2": np.array([4, 5, 6])} >>> numpy_to_bytes(dict_np_array)
From
Dict[str, list]
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> dict_list = {"key1": [1, 2, 3], "key2": [4, 5, 6]} >>> numpy_to_bytes(dict_list)
From
List[np.array]
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> list_np_array = [np.array([1, 2, 3]), np.array([4, 5, 6])] >>> numpy_to_bytes(list_np_array)
- manta_light.utils.bytes_to_numpy(b: bytes | dict) ndarray | dict ¶
Convert bytes to a numpy array
- Parameters:
b (bytes) – The bytes to convert
- Returns:
The numpy array representation of the bytes
- Return type:
np.array
Examples
From
np.array
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> np_array = np.array([1, 2, 3]) >>> np_bytes = numpy_to_bytes(np_array) >>> bytes_to_numpy(np_bytes)
From
Dict[str, np.array]
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> dict_np_array = {"key1": np.array([1, 2, 3]), "key2": np.array([4, 5, 6])} >>> np_bytes = numpy_to_bytes(dict_np_array) >>> bytes_to_numpy(np_bytes)
From
Dict[str, list]
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> dict_list = {"key1": [1, 2, 3], "key2": [4, 5, 6]} >>> np_bytes = numpy_to_bytes(dict_list) >>> bytes_to_numpy(np_bytes)
From
List[np.array]
>>> import numpy as np >>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes >>> list_np_array = [np.array([1, 2, 3]), np.array([4, 5, 6])] >>> np_bytes = numpy_to_bytes(list_np_array) >>> bytes_to_numpy(np_bytes)