Skip to content

Commit 43d0780

Browse files
committed
improve packing integers in b64 image test
1 parent dfa65db commit 43d0780

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

test/image/convert_b64.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
'float64': 'f8'
1313
}
1414

15+
int8bounds = numpy.iinfo(numpy.int8)
16+
int16bounds = numpy.iinfo(numpy.int16)
1517
int32bounds = numpy.iinfo(numpy.int32)
18+
uint8bounds = numpy.iinfo(numpy.uint8)
19+
uint16bounds = numpy.iinfo(numpy.uint16)
1620
uint32bounds = numpy.iinfo(numpy.uint32)
1721

1822
skipKeys = [
@@ -49,20 +53,31 @@ def arraysToB64(obj, newObj) :
4953
newObj[key] = val
5054
continue
5155

52-
# convert Big Ints until we could support them in plotly.js
56+
# convert default Big Ints until we could support them in plotly.js
5357
if str(arr.dtype) == 'int64' :
54-
if arr.max() > int32bounds.max or arr.min() < int32bounds.min :
58+
max = arr.max()
59+
min = arr.min()
60+
if max <= int8bounds.max and min >= int8bounds.min :
61+
arr = arr.astype(numpy.int8)
62+
elif max <= int16bounds.max and min >= int16bounds.min :
63+
arr = arr.astype(numpy.int16)
64+
elif max <= int32bounds.max and min >= int32bounds.min :
65+
arr = arr.astype(numpy.int32)
66+
else :
5567
newObj[key] = val
5668
continue
5769

58-
arr = arr.astype(numpy.int32)
5970
elif str(arr.dtype) == 'uint64' :
60-
if arr.max() > uint32bounds.max or arr.min() < uint32bounds.min :
71+
if max <= uint8bounds.max and min >= uint8bounds.min :
72+
arr = arr.astype(numpy.uint8)
73+
elif max <= uint16bounds.max and min >= uint16bounds.min :
74+
arr = arr.astype(numpy.uint16)
75+
elif max <= uint32bounds.max and min >= uint32bounds.min :
76+
arr = arr.astype(numpy.uint32)
77+
else :
6178
newObj[key] = val
6279
continue
6380

64-
arr = arr.astype(numpy.uint32)
65-
6681
if str(arr.dtype) in plotlyjsShortTypes :
6782
newObj[key] = {
6883
'dtype': plotlyjsShortTypes[str(arr.dtype)],
@@ -73,7 +88,6 @@ def arraysToB64(obj, newObj) :
7388
newObj[key]['shape'] = str(arr.shape)[1:-1]
7489

7590
#print(val)
76-
#print('____________________')
7791
#print(newObj[key])
7892
#print('____________________')
7993
else :

0 commit comments

Comments
 (0)