msgpack-numpy-opentensor

0.5.0 · active · verified Thu Apr 16

msgpack-numpy-opentensor provides efficient serialization and deserialization routines for NumPy array and scalar data types using the MessagePack binary format. It is functionally derived from the `msgpack-numpy` library, offering compatibility with NumPy data structures. The current PyPI version is `0.5.0`. While its related GitHub repository shows more recent development, PyPI releases are infrequent, with the latest over a year old.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to serialize a NumPy array into MessagePack binary format and then deserialize it back into a NumPy array using the `msgpack-numpy-opentensor` library. It shows both packing (`packb`) and unpacking (`unpackb`) using the provided encoder and decoder functions, highlighting that deserialized arrays are typically read-only.

import numpy as np
import msgpack
import msgpack_numpy_opentensor as m

# Create a NumPy array
x = np.random.rand(5, 5)

# Pack the NumPy array using msgpack-numpy-opentensor's encoder
# Optionally, you can call m.patch() to monkey-patch msgpack globally
# m.patch()
packed_x = msgpack.packb(x, default=m.encode)

# Unpack the bytes back into a NumPy array using the decoder
unpacked_x = msgpack.unpackb(packed_x, object_hook=m.decode, raw=False)

print("Original array:\n", x)
print("Unpacked array:\n", unpacked_x)
print("Arrays are equal:", np.array_equal(x, unpacked_x))
print("Unpacked array is read-only:", not unpacked_x.flags['WRITEABLE'])

view raw JSON →