|
| 1 | +import base64 |
1 | 2 | import decimal |
2 | 3 | import json as _json |
3 | 4 | import sys |
4 | 5 | import re |
5 | 6 | from functools import reduce |
6 | 7 |
|
7 | 8 | from _plotly_utils.optional_imports import get_module |
8 | | -from _plotly_utils.basevalidators import ImageUriValidator |
| 9 | +from _plotly_utils.basevalidators import ( |
| 10 | + ImageUriValidator, |
| 11 | + copy_to_readonly_numpy_array, |
| 12 | + is_homogeneous_array, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +int8min = -128 |
| 17 | +int8max = 127 |
| 18 | +int16min = -32768 |
| 19 | +int16max = 32767 |
| 20 | +int32min = -2147483648 |
| 21 | +int32max = 2147483647 |
| 22 | + |
| 23 | +uint8max = 255 |
| 24 | +uint16max = 65535 |
| 25 | +uint32max = 4294967295 |
| 26 | + |
| 27 | +plotlyjsShortTypes = { |
| 28 | + "int8": "i1", |
| 29 | + "uint8": "u1", |
| 30 | + "int16": "i2", |
| 31 | + "uint16": "u2", |
| 32 | + "int32": "i4", |
| 33 | + "uint32": "u4", |
| 34 | + "float32": "f4", |
| 35 | + "float64": "f8", |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +def to_typed_array_spec(v): |
| 40 | + """ |
| 41 | + Convert numpy array to plotly.js typed array spec |
| 42 | + If not possible return the original value |
| 43 | + """ |
| 44 | + v = copy_to_readonly_numpy_array(v) |
| 45 | + |
| 46 | + np = get_module("numpy", should_load=False) |
| 47 | + if not np or not isinstance(v, np.ndarray): |
| 48 | + return v |
| 49 | + |
| 50 | + dtype = str(v.dtype) |
| 51 | + |
| 52 | + # convert default Big Ints until we could support them in plotly.js |
| 53 | + if dtype == "int64": |
| 54 | + max = v.max() |
| 55 | + min = v.min() |
| 56 | + if max <= int8max and min >= int8min: |
| 57 | + v = v.astype("int8") |
| 58 | + elif max <= int16max and min >= int16min: |
| 59 | + v = v.astype("int16") |
| 60 | + elif max <= int32max and min >= int32min: |
| 61 | + v = v.astype("int32") |
| 62 | + else: |
| 63 | + return v |
| 64 | + |
| 65 | + elif dtype == "uint64": |
| 66 | + max = v.max() |
| 67 | + min = v.min() |
| 68 | + if max <= uint8max and min >= 0: |
| 69 | + v = v.astype("uint8") |
| 70 | + elif max <= uint16max and min >= 0: |
| 71 | + v = v.astype("uint16") |
| 72 | + elif max <= uint32max and min >= 0: |
| 73 | + v = v.astype("uint32") |
| 74 | + else: |
| 75 | + return v |
| 76 | + |
| 77 | + dtype = str(v.dtype) |
| 78 | + |
| 79 | + if dtype in plotlyjsShortTypes: |
| 80 | + arrObj = { |
| 81 | + "dtype": plotlyjsShortTypes[dtype], |
| 82 | + "bdata": base64.b64encode(v).decode("ascii"), |
| 83 | + } |
| 84 | + |
| 85 | + if v.ndim > 1: |
| 86 | + arrObj["shape"] = str(v.shape)[1:-1] |
| 87 | + |
| 88 | + return arrObj |
| 89 | + |
| 90 | + return v |
| 91 | + |
| 92 | + |
| 93 | +def is_skipped_key(key): |
| 94 | + """ |
| 95 | + Return whether the key is skipped for conversion to the typed array spec |
| 96 | + """ |
| 97 | + skipped_keys = ["geojson", "layer", "layers", "range"] |
| 98 | + return any(skipped_key == key for skipped_key in skipped_keys) |
| 99 | + |
| 100 | + |
| 101 | +def convert_to_base64(obj): |
| 102 | + if isinstance(obj, dict): |
| 103 | + for key, value in obj.items(): |
| 104 | + if is_skipped_key(key): |
| 105 | + continue |
| 106 | + elif is_homogeneous_array(value): |
| 107 | + obj[key] = to_typed_array_spec(value) |
| 108 | + else: |
| 109 | + convert_to_base64(value) |
| 110 | + elif isinstance(obj, list) or isinstance(obj, tuple): |
| 111 | + for value in obj: |
| 112 | + convert_to_base64(value) |
9 | 113 |
|
10 | 114 |
|
11 | 115 | def cumsum(x): |
|
0 commit comments