From e12ecdc74677e3d21b3114850bb1f7899683d70a Mon Sep 17 00:00:00 2001 From: Valentin Boettcher Date: Sun, 24 Jul 2022 20:51:04 +0200 Subject: [PATCH] make dumping numpy arrays more stable --- binfootprint/binfootprint.py | 24 ++++++++++++++++++------ pyproject.toml | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/binfootprint/binfootprint.py b/binfootprint/binfootprint.py index eaa78fe..8856d37 100644 --- a/binfootprint/binfootprint.py +++ b/binfootprint/binfootprint.py @@ -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]) diff --git a/pyproject.toml b/pyproject.toml index 0e54b6b..c7ba21c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "BSD (3 clause)"