add copyright notice, short documentation, and remove tests

This commit is contained in:
Zoltán Vörös 2022-01-14 19:04:42 +01:00
parent 178af9a9fa
commit aa543d0c57
2 changed files with 39 additions and 13 deletions

View file

@ -1,3 +1,9 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Zoltán Vörös
import sys
use_ulab = False
@ -35,9 +41,23 @@ def ulab_descr_to_dtype(descriptor):
raise TypeError('')
else:
return np.float
else:
raise TypeError('descriptor could not be decoded')
def json_to_ndarray(json_string, b64=False):
"""
Turn a json string into an ndarray
The string must be the representation of a dictionary with the three keys
- dtype: a valid numpy dtype string (one of |u1, |i1, <u2, <i2, <f4, <f8, <c8, <c16, >u2, >i2, >f4, >f8, >c8, >c16)
- __numpy__: the hexified, or base64-encoded raw data array
- shape: the shape of the array (a list or tuple of integers)
Usage:
str = '{"dtype": "<f8", "__numpy__": "AAAAAAAAAAAAAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRAAAAAAAAAGEAAAAAAAAAcQAAAAAAAACBA\n", "shape": [3, 3]}'
json_to_ndarray(str, b64=True)
"""
obj = json.loads(json_string)
print(obj)
if not isinstance(obj, dict):
@ -63,8 +83,3 @@ def json_to_ndarray(json_string, b64=False):
ndarray.byteswap()
return ndarray
str = '{"dtype": "<f8", "__numpy__": "AAAAAAAAAAAAAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRAAAAAAAAAGEAAAAAAAAAcQAAAAAAAACBA\n", "shape": [3, 3]}'
print(json_to_ndarray(str, b64=True))

View file

@ -1,3 +1,9 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Zoltán Vörös
import sys
use_ulab = False
@ -39,6 +45,19 @@ def ulab_dtype_to_descr(dtype):
return desc
def ndarray_to_json(obj, b64=False):
"""
Turn an ndarray into a json string, using either base64 encoding or hexify
Returns a serialised dictionary with three keys:
- dtype: a valid numpy dtype string (one of |u1, |i1, <u2, <i2, <f4, <f8, <c8, <c16, >u2, >i2, >f4, >f8, >c8, >c16)
- __numpy__: the hexified, or base64-encoded raw data array
- shape: the shape of the array (a list or tuple of integers)
Usage:
ndarray = np.array([1, 2, 3], dtype=np.uint8)
ndarray_to_json(ndarray, b64=True)
"""
if not isinstance(obj, np.ndarray):
raise TypeError('input argument must be an ndarray')
@ -53,11 +72,3 @@ def ndarray_to_json(obj, b64=False):
data = b64encode(obj.tobytes())
return json.dumps({'__numpy__': data, 'dtype': dtype_desciptor, 'shape': obj.shape})
dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float)
for dtype in dtypes:
ndarray = np.array(range(9), dtype=dtype).reshape((3,3))
print(ndarray_to_json(ndarray))
print(ndarray_to_json(ndarray, b64=True))