(obj)
| 27 | |
| 28 | |
| 29 | def b64_encode_numpy(obj): |
| 30 | # Convert 1D numpy arrays with numeric types to memoryviews with |
| 31 | # datatype and shape metadata. |
| 32 | if len(obj) == 0: |
| 33 | return obj.tolist() |
| 34 | |
| 35 | dtype = obj.dtype |
| 36 | if dtype.kind == "f": |
| 37 | return np_encode(obj) |
| 38 | elif dtype.kind == "b": |
| 39 | return np_encode(obj, np.uint8) |
| 40 | elif dtype.kind in ["u", "i"]: |
| 41 | # Try to see if we can downsize the array |
| 42 | max_value = np.amax(obj) |
| 43 | min_value = np.amin(obj) |
| 44 | signed = min_value < 0 |
| 45 | test_value = max(max_value, -min_value) |
| 46 | if signed: |
| 47 | if test_value < np.iinfo(np.int8): |
| 48 | return np_encode(obj, np.int8) |
| 49 | if test_value < np.iinfo(np.int16).max: |
| 50 | return np_encode(obj, np.int16) |
| 51 | if test_value < np.iinfo(np.int32).max: |
| 52 | return np_encode(obj, np.int32) |
| 53 | else: |
| 54 | if test_value < np.iinfo(np.uint8).max: |
| 55 | return np_encode(obj, np.uint8) |
| 56 | if test_value < np.iinfo(np.uint16).max: |
| 57 | return np_encode(obj, np.uint16) |
| 58 | if test_value < np.iinfo(np.uint32).max: |
| 59 | return np_encode(obj, np.uint32) |
| 60 | |
| 61 | # Convert all other numpy arrays to lists |
| 62 | return obj.tolist() |
| 63 | |
| 64 | |
| 65 | def np_encode(array, np_type=None): |
no test coverage detected