File tree Expand file tree Collapse file tree 1 file changed +82
-0
lines changed
packages/python/plotly/plotly/tests Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ import numpy as np
2+ import base64
3+
4+ plotlyjsShortTypes = {
5+ "int8" : "i1" ,
6+ "uint8" : "u1" ,
7+ "int16" : "i2" ,
8+ "uint16" : "u2" ,
9+ "int32" : "i4" ,
10+ "uint32" : "u4" ,
11+ "float32" : "f4" ,
12+ "float64" : "f8" ,
13+ }
14+
15+ int8min = - 128
16+ int8max = 127
17+ int16min = - 32768
18+ int16max = 32767
19+ int32min = - 2147483648
20+ int32max = 2147483647
21+
22+ uint8max = 255
23+ uint16max = 65535
24+ uint32max = 4294967295
25+
26+
27+ def b64 (v ):
28+ """
29+ Convert numpy array to plotly.js typed array sepc
30+ If not possible return the original value
31+ """
32+
33+ if not isinstance (v , np .ndarray ):
34+ return v
35+
36+ dtype = str (v .dtype )
37+
38+ # convert default Big Ints until we could support them in plotly.js
39+ if dtype == "int64" :
40+ max = v .max ()
41+ min = v .min ()
42+ if max <= int8max and min >= int8min :
43+ v = v .astype ("int8" )
44+ elif max <= int16max and min >= int16min :
45+ v = v .astype ("int16" )
46+ elif max <= int32max and min >= int32min :
47+ v = v .astype ("int32" )
48+ else :
49+ return v
50+
51+ elif dtype == "uint64" :
52+ max = v .max ()
53+ min = v .min ()
54+ if max <= uint8max and min >= 0 :
55+ v = v .astype ("uint8" )
56+ elif max <= uint16max and min >= 0 :
57+ v = v .astype ("uint16" )
58+ elif max <= uint32max and min >= 0 :
59+ v = v .astype ("uint32" )
60+ else :
61+ return v
62+
63+ dtype = str (v .dtype )
64+
65+ if dtype in plotlyjsShortTypes :
66+ arrObj = {
67+ "dtype" : plotlyjsShortTypes [dtype ],
68+ "bdata" : base64 .b64encode (v ).decode ("ascii" ),
69+ }
70+
71+ if v .ndim > 1 :
72+ arrObj ["shape" ] = str (v .shape )[1 :- 1 ]
73+
74+ print (arrObj )
75+
76+ return arrObj
77+
78+ return v
79+
80+
81+ def _b64 (v ):
82+ return b64 (np .array (v ))
You can’t perform that action at this time.
0 commit comments