make dumping numpy arrays more stable

This commit is contained in:
Valentin Boettcher 2022-07-24 20:51:04 +02:00
parent f30a9691a5
commit e12ecdc746
2 changed files with 19 additions and 7 deletions

View file

@ -45,6 +45,8 @@ try:
except ImportError:
scipy = None
import io
_spec_types = (bool, type(None))
_SPEC = 0x00 # True, False, None
@ -357,17 +359,27 @@ def _load_list(b, classes):
def _dump_np_array(np_array):
b = init_BYTES([_NPARRAY])
nparray_bytes = np.ndarray.dumps(np_array)
size = len(nparray_bytes)
b += struct.pack('>I', size)
memfile = io.BytesIO()
np.savetxt(memfile, np_array.flatten(), fmt='%.18e',
delimiter=' ', newline='\n',
header='', footer='', comments='# ', encoding="latin1")
nparray_bytes = dump((str(np_array.dtype), np_array.shape, memfile.getvalue()))
size = len(nparray_bytes)
b += struct.pack(">I", size)
b += nparray_bytes
return b
def _load_np_array(b):
assert comp_id(b[0], _NPARRAY)
size = struct.unpack('>I', b[1:5])[0]
npa = np_load(b[5: size+5])
return npa, size+5
memfile = io.BytesIO()
size = struct.unpack(">I", b[1:5])[0]
dtype_str, shape, txt = load(b[5 : size + 5])
memfile.write(txt)
memfile.seek(0)
npa = np.loadtxt(memfile, dtype=np.dtype(dtype_str),
encoding="latin1", comments='# ').reshape(shape)
return npa, size + 5
def _dump_bfkey(ob):
b = init_BYTES([_BFKEY])

View file

@ -1,6 +1,6 @@
[tool.poetry]
name = "binfootprint"
version = "0.1.0"
version = "0.2.0"
description = "binary representation for simple data structures"
authors = ["Richard Hartmann <richard.hartmann@tu-dresden.de>"]
license = "BSD (3 clause)"