mirror of
https://github.com/vale981/binfootprint
synced 2025-03-04 16:51:39 -05:00
make dumping numpy arrays more stable
This commit is contained in:
parent
f30a9691a5
commit
e12ecdc746
2 changed files with 19 additions and 7 deletions
|
@ -45,6 +45,8 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
scipy = None
|
scipy = None
|
||||||
|
|
||||||
|
import io
|
||||||
|
|
||||||
_spec_types = (bool, type(None))
|
_spec_types = (bool, type(None))
|
||||||
|
|
||||||
_SPEC = 0x00 # True, False, None
|
_SPEC = 0x00 # True, False, None
|
||||||
|
@ -357,17 +359,27 @@ def _load_list(b, classes):
|
||||||
|
|
||||||
def _dump_np_array(np_array):
|
def _dump_np_array(np_array):
|
||||||
b = init_BYTES([_NPARRAY])
|
b = init_BYTES([_NPARRAY])
|
||||||
nparray_bytes = np.ndarray.dumps(np_array)
|
memfile = io.BytesIO()
|
||||||
size = len(nparray_bytes)
|
np.savetxt(memfile, np_array.flatten(), fmt='%.18e',
|
||||||
b += struct.pack('>I', size)
|
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
|
b += nparray_bytes
|
||||||
return b
|
return b
|
||||||
|
|
||||||
|
|
||||||
def _load_np_array(b):
|
def _load_np_array(b):
|
||||||
assert comp_id(b[0], _NPARRAY)
|
assert comp_id(b[0], _NPARRAY)
|
||||||
size = struct.unpack('>I', b[1:5])[0]
|
memfile = io.BytesIO()
|
||||||
npa = np_load(b[5: size+5])
|
size = struct.unpack(">I", b[1:5])[0]
|
||||||
return npa, size+5
|
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):
|
def _dump_bfkey(ob):
|
||||||
b = init_BYTES([_BFKEY])
|
b = init_BYTES([_BFKEY])
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "binfootprint"
|
name = "binfootprint"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
description = "binary representation for simple data structures"
|
description = "binary representation for simple data structures"
|
||||||
authors = ["Richard Hartmann <richard.hartmann@tu-dresden.de>"]
|
authors = ["Richard Hartmann <richard.hartmann@tu-dresden.de>"]
|
||||||
license = "BSD (3 clause)"
|
license = "BSD (3 clause)"
|
||||||
|
|
Loading…
Add table
Reference in a new issue