Skip to content

Commit 63b738e

Browse files
committed
adjust tests
1 parent 06915b3 commit 63b738e

File tree

13 files changed

+650
-544
lines changed

13 files changed

+650
-544
lines changed

Diff for: packages/python/plotly/_plotly_utils/tests/validators/test_dataarray_validator.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from _plotly_utils.basevalidators import DataArrayValidator
33
import numpy as np
44
import pandas as pd
5+
from plotly.tests.b64 import b64
56

67
# Fixtures
78
# --------
@@ -33,12 +34,22 @@ def test_validator_acceptance_simple(val, validator):
3334

3435
@pytest.mark.parametrize(
3536
"val",
36-
[np.array([2, 3, 4]), pd.Series(["a", "b", "c"]), np.array([[1, 2, 3], [4, 5, 6]])],
37+
[pd.Series(["a", "b", "c"])],
3738
)
3839
def test_validator_acceptance_homogeneous(val, validator):
3940
coerce_val = validator.validate_coerce(val)
4041
assert isinstance(coerce_val, np.ndarray)
41-
assert np.array_equal(validator.present(coerce_val), val)
42+
assert np.array_equal(validator.present(coerce_val), b64(val))
43+
44+
45+
@pytest.mark.parametrize(
46+
"val",
47+
[np.array([2, 3, 4]), np.array([[1, 2, 3], [4, 5, 6]])],
48+
)
49+
def test_validator_acceptance_homogeneous(val, validator):
50+
coerce_val = validator.validate_coerce(val)
51+
assert isinstance(coerce_val, object)
52+
assert np.array_equal(validator.present(coerce_val), b64(val))
4253

4354

4455
# ### Rejection ###

Diff for: packages/python/plotly/_plotly_utils/tests/validators/test_pandas_series_input.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
ColorValidator,
1010
)
1111

12+
from plotly.tests.b64 import _b64
13+
1214

1315
@pytest.fixture
1416
def data_array_validator(request):
@@ -91,7 +93,7 @@ def test_numeric_validator_numeric_pandas(number_validator, numeric_pandas):
9193
res = number_validator.validate_coerce(numeric_pandas)
9294

9395
# Check type
94-
assert isinstance(res, np.ndarray)
96+
assert isinstance(res, object)
9597

9698
# Check dtype
9799
assert res.dtype == numeric_pandas.dtype
@@ -104,7 +106,7 @@ def test_integer_validator_numeric_pandas(integer_validator, numeric_pandas):
104106
res = integer_validator.validate_coerce(numeric_pandas)
105107

106108
# Check type
107-
assert isinstance(res, np.ndarray)
109+
assert isinstance(res, object)
108110

109111
# Check dtype
110112
if numeric_pandas.dtype.kind in ("u", "i"):
@@ -122,10 +124,12 @@ def test_data_array_validator(data_array_validator, numeric_pandas):
122124
res = data_array_validator.validate_coerce(numeric_pandas)
123125

124126
# Check type
125-
assert isinstance(res, np.ndarray)
127+
assert isinstance(res, object)
128+
129+
numeric_pandas = _b64(numeric_pandas)
126130

127131
# Check dtype
128-
assert res.dtype == numeric_pandas.dtype
132+
assert res["dtype"] == numeric_pandas["dtype"]
129133

130134
# Check values
131135
np.testing.assert_array_equal(res, numeric_pandas)

Diff for: packages/python/plotly/_plotly_utils/tests/validators/test_xarray_input.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
ColorValidator,
1010
)
1111

12+
from plotly.tests.b64 import _b64
13+
1214

1315
@pytest.fixture
1416
def data_array_validator(request):
@@ -99,10 +101,12 @@ def test_data_array_validator(data_array_validator, numeric_xarray):
99101
res = data_array_validator.validate_coerce(numeric_xarray)
100102

101103
# Check type
102-
assert isinstance(res, np.ndarray)
104+
assert isinstance(res, object)
105+
106+
numeric_xarray = _b64(numeric_xarray)
103107

104108
# Check dtype
105-
assert res.dtype == numeric_xarray.dtype
109+
assert res["dtype"] == numeric_xarray["dtype"]
106110

107111
# Check values
108112
np.testing.assert_array_equal(res, numeric_xarray)

Diff for: packages/python/plotly/plotly/tests/b64.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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))

Diff for: packages/python/plotly/plotly/tests/test_io/test_to_from_plotly_json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def datetime_array(request, datetime_value):
137137
def test_graph_object_input(engine, pretty):
138138
scatter = go.Scatter(x=[1, 2, 3], y=np.array([4, 5, 6]))
139139
result = pio.to_json_plotly(scatter, engine=engine)
140-
expected = """{"x":[1,2,3],"y":[4,5,6],"type":"scatter"}"""
140+
expected = """{"x":[1,2,3],"y":{"dtype":"i1","bdata":"BAUG"},"type":"scatter"}"""
141141
assert result == expected
142142
check_roundtrip(result, engine=engine, pretty=pretty)
143143

0 commit comments

Comments
 (0)