Skip to content

Commit 644992f

Browse files
authored
Merge pull request #4695 from marthacryan/add-tests-b64
Add tests for b64 PR
2 parents f332922 + 63335d2 commit 644992f

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ commands:
8181
pytest plotly/tests/test_io
8282
no_output_timeout: 20m
8383
- run:
84-
name: Test dependencdies not imported
84+
name: Test dependencies not imported
8585
command: |
8686
cd packages/python/plotly
8787
. venv/bin/activate

packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from pytest import approx
55
from _plotly_utils.basevalidators import IntegerValidator
6+
from plotly.tests.b64 import b64
67
import numpy as np
78
import pandas as pd
89

@@ -111,6 +112,12 @@ def test_acceptance_aok_list(val, validator_aok):
111112
assert np.array_equal(validator_aok.validate_coerce(val), val)
112113

113114

115+
# Test base64 encoded arrays with array_ok=True
116+
@pytest.mark.parametrize("val", [b64(np.array([1, 0], dtype="int16")), b64([1, 0])])
117+
def test_acceptance_aok_base64(val, validator_aok):
118+
assert np.array_equal(validator_aok.validate_coerce(val), val)
119+
120+
114121
# ### Coerce ###
115122
# Coerced to general consistent numeric type
116123
@pytest.mark.parametrize(
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import time
2+
import numpy as np
3+
import plotly.graph_objs as go
4+
import plotly.io as pio
5+
import pytest
6+
7+
np.random.seed(1)
8+
pio.renderers.default = "png"
9+
10+
11+
def test_performance_b64_scatter3d():
12+
N = 50000
13+
14+
x = np.random.randn(N)
15+
y = np.random.randn(N).astype("float32")
16+
z = np.random.randint(size=N, low=0, high=256, dtype="uint8")
17+
c = np.random.randint(size=N, low=-10, high=10, dtype="int8")
18+
19+
# Test the performance with lists
20+
x_list = x.tolist()
21+
y_list = y.tolist()
22+
z_list = z.tolist()
23+
c_list = c.tolist()
24+
list_start = time.time()
25+
fig = go.Figure(
26+
data=[
27+
go.Scatter3d(
28+
x=x_list,
29+
y=y_list,
30+
z=z_list,
31+
marker=dict(color=c_list),
32+
mode="markers",
33+
opacity=0.2,
34+
)
35+
]
36+
)
37+
fig.show(engine="kaleido")
38+
list_time_elapsed = time.time() - list_start
39+
40+
# Test the performance with base64 arrays
41+
np_start = time.time()
42+
fig = go.Figure(
43+
data=[
44+
go.Scatter3d(
45+
x=x,
46+
y=y,
47+
z=z,
48+
marker=dict(color=c),
49+
mode="markers",
50+
opacity=0.2,
51+
)
52+
]
53+
)
54+
fig.show(engine="kaleido")
55+
56+
np_time_elapsed = time.time() - np_start
57+
58+
# np should be faster than lists
59+
assert (np_time_elapsed / list_time_elapsed) < 0.75
60+
61+
62+
FLOAT_TEST_CASES = [
63+
("float32", 100000, 0.95), # dtype # difference threshold
64+
("float64", 100000, 0.95),
65+
]
66+
67+
68+
@pytest.mark.parametrize("dtype, count, expected_size_difference", FLOAT_TEST_CASES)
69+
def test_performance_b64_float(dtype, count, expected_size_difference):
70+
np_arr_1 = np.random.random(count).astype(dtype)
71+
np_arr_2 = np.random.random(count).astype(dtype)
72+
list_1 = np_arr_1.tolist()
73+
list_2 = np_arr_2.tolist()
74+
75+
# Test the performance of the base64 arrays
76+
np_start = time.time()
77+
fig = go.Figure(data=[go.Scattergl(x=np_arr_1, y=np_arr_2)])
78+
fig.show(engine="kaleido")
79+
np_time_elapsed = time.time() - np_start
80+
81+
# Test the performance of the normal lists
82+
list_start = time.time()
83+
fig = go.Figure(data=[go.Scattergl(x=list_1, y=list_2)])
84+
fig.show(engine="kaleido")
85+
list_time_elapsed = time.time() - list_start
86+
87+
# np should be faster than lists
88+
assert (np_time_elapsed / list_time_elapsed) < expected_size_difference
89+
90+
91+
INT_SIZE_PERFORMANCE_TEST_CASES = [
92+
("uint8", 256, 10500, 30000),
93+
("uint32", 2**32, 10500, 100000),
94+
]
95+
96+
97+
@pytest.mark.parametrize(
98+
"dtype, max_value, count, expected_size_difference", INT_SIZE_PERFORMANCE_TEST_CASES
99+
)
100+
def test_size_performance_b64_int(dtype, max_value, count, expected_size_difference):
101+
np_arr_1 = (np.random.random(count) * max_value).astype(dtype)
102+
np_arr_2 = (np.random.random(count) * max_value).astype(dtype)
103+
104+
# Measure the size of figures with numpy arrays
105+
fig_np = go.Scatter(x=np_arr_1, y=np_arr_2)
106+
size_np = fig_np.to_json().__sizeof__()
107+
108+
# Measure the size of the figure with normal python lists
109+
fig_list = go.Scatter(x=np_arr_1.tolist(), y=np_arr_2.tolist())
110+
size_list = fig_list.to_json().__sizeof__()
111+
112+
# np should be smaller than lists
113+
assert size_list - size_np > expected_size_difference

0 commit comments

Comments
 (0)