7
7
import unittest .mock as mock
8
8
from plotly .express ._core import build_dataframe
9
9
from pandas .testing import assert_frame_equal
10
- from plotly .tests .b64 import b64 , _b64
11
10
import sys
12
11
import warnings
13
- import time
14
12
15
- np .random .seed (0 )
16
13
17
14
# Fixtures
18
15
# --------
@@ -31,9 +28,8 @@ def add_interchange_module_for_old_pandas():
31
28
32
29
def test_numpy ():
33
30
fig = px .scatter (x = [1 , 2 , 3 ], y = [2 , 3 , 4 ], color = [1 , 3 , 9 ])
34
-
35
- assert np .all (fig .data [0 ].x == b64 (np .array ([1 , 2 , 3 ])))
36
- assert np .all (fig .data [0 ].y == b64 (np .array ([2 , 3 , 4 ])))
31
+ assert np .all (fig .data [0 ].x == np .array ([1 , 2 , 3 ]))
32
+ assert np .all (fig .data [0 ].y == np .array ([2 , 3 , 4 ]))
37
33
assert np .all (fig .data [0 ].marker .color == np .array ([1 , 3 , 9 ]))
38
34
39
35
@@ -105,16 +101,16 @@ def test_several_dataframes():
105
101
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ]))
106
102
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
107
103
fig = px .scatter (x = df .y , y = df2 .y )
108
- assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
109
- assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
104
+ assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
105
+ assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
110
106
assert fig .data [0 ].hovertemplate == "x=%{x}<br>y=%{y}<extra></extra>"
111
107
112
108
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ]))
113
109
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
114
110
df3 = pd .DataFrame (dict (y = [0.1 , 0.2 ]))
115
111
fig = px .scatter (x = df .y , y = df2 .y , size = df3 .y )
116
- assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
117
- assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
112
+ assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
113
+ assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
118
114
assert (
119
115
fig .data [0 ].hovertemplate
120
116
== "x=%{x}<br>y=%{y}<br>size=%{marker.size}<extra></extra>"
@@ -124,8 +120,8 @@ def test_several_dataframes():
124
120
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
125
121
df3 = pd .DataFrame (dict (y = [0.1 , 0.2 ]))
126
122
fig = px .scatter (x = df .y , y = df2 .y , hover_data = [df3 .y ])
127
- assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
128
- assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
123
+ assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
124
+ assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
129
125
assert (
130
126
fig .data [0 ].hovertemplate
131
127
== "x=%{x}<br>y=%{y}<br>hover_data_0=%{customdata[0]}<extra></extra>"
@@ -135,57 +131,11 @@ def test_several_dataframes():
135
131
def test_name_heuristics ():
136
132
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ], z = [0.1 , 0.2 ]))
137
133
fig = px .scatter (df , x = df .y , y = df .x , size = df .y )
138
- assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
139
- assert np .all (fig .data [0 ].y == b64 ( np .array ([0 , 1 ]) ))
134
+ assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
135
+ assert np .all (fig .data [0 ].y == np .array ([0 , 1 ]))
140
136
assert fig .data [0 ].hovertemplate == "y=%{marker.size}<br>x=%{y}<extra></extra>"
141
137
142
138
143
- def test_performance_b64 ():
144
- np_arr_1 = np .random .random (1000000 )
145
- np_arr_2 = np .random .random (1000000 )
146
-
147
- # Test the performance of the base64 arrays
148
- np_arr_start = time .time ()
149
- fig = px .scatter (x = np_arr_1 , y = np_arr_2 , width = 800 , height = 800 )
150
- fig .update_layout (margin = dict (l = 0 , r = 0 , t = 0 , b = 0 ))
151
- np_arr_time_elapsed = time .time () - np_arr_start
152
-
153
- # Test the performance of the raw arrays
154
- list_start = time .time ()
155
- fig = px .scatter (x = np_arr_1 .tolist (), y = np_arr_2 .tolist (), width = 800 , height = 800 )
156
- fig .update_layout (margin = dict (l = 0 , r = 0 , t = 0 , b = 0 ))
157
- list_time_elapsed = time .time () - list_start
158
-
159
- # b64 should be faster than raw
160
- assert (np_arr_time_elapsed / list_time_elapsed ) < 0.7
161
-
162
-
163
- def test_size_performance_b64_uint8 ():
164
- np_arr_1 = np .random .randint (0 , high = 254 , size = 100000 , dtype = "uint8" )
165
- np_arr_2 = np .random .randint (0 , high = 254 , size = 100000 , dtype = "uint8" )
166
-
167
- # Compare the size of figures with b64 arrays and raw arrays
168
- fig_np_arr = px .scatter (x = np_arr_1 , y = np_arr_2 )
169
- size_np_arr = fig_np_arr .to_json ().__sizeof__ ()
170
- fig_list = px .scatter (x = np_arr_1 .tolist (), y = np_arr_2 .tolist ())
171
- size_list = fig_list .to_json ().__sizeof__ ()
172
-
173
- assert size_list - size_np_arr > 250000
174
-
175
-
176
- def test_size_performance_b64_float32 ():
177
- np_arr_1 = np .random .random (100000 ).astype ("float32" )
178
- np_arr_2 = np .random .random (100000 ).astype ("float32" )
179
-
180
- # Compare the size of figures with b64 arrays and raw arrays
181
- fig_np_arr = px .scatter (x = np_arr_1 , y = np_arr_2 )
182
- size_np_arr = fig_np_arr .to_json ().__sizeof__ ()
183
- fig_list = px .scatter (x = np_arr_1 .tolist (), y = np_arr_2 .tolist ())
184
- size_list = fig_list .to_json ().__sizeof__ ()
185
-
186
- assert size_list - size_np_arr > 250000
187
-
188
-
189
139
def test_repeated_name ():
190
140
iris = px .data .iris ()
191
141
fig = px .scatter (
@@ -455,27 +405,27 @@ def test_splom_case():
455
405
assert len (fig .data [0 ].dimensions ) == len (iris .columns )
456
406
dic = {"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [7 , 8 , 9 ]}
457
407
fig = px .scatter_matrix (dic )
458
- assert np .all (fig .data [0 ].dimensions [0 ].values == b64 ( np .array (dic ["a" ]) ))
408
+ assert np .all (fig .data [0 ].dimensions [0 ].values == np .array (dic ["a" ]))
459
409
ar = np .arange (9 ).reshape ((3 , 3 ))
460
410
fig = px .scatter_matrix (ar )
461
- assert np .all (fig .data [0 ].dimensions [0 ].values == b64 ( ar [:, 0 ]) )
411
+ assert np .all (fig .data [0 ].dimensions [0 ].values == ar [:, 0 ])
462
412
463
413
464
414
def test_int_col_names ():
465
415
# DataFrame with int column names
466
416
lengths = pd .DataFrame (np .random .random (100 ))
467
417
fig = px .histogram (lengths , x = 0 )
468
- assert np .all (b64 ( np .array (lengths ).flatten () ) == fig .data [0 ].x )
418
+ assert np .all (np .array (lengths ).flatten () == fig .data [0 ].x )
469
419
# Numpy array
470
420
ar = np .arange (100 ).reshape ((10 , 10 ))
471
421
fig = px .scatter (ar , x = 2 , y = 8 )
472
- assert np .all (fig .data [0 ].x == b64 ( ar [:, 2 ]) )
422
+ assert np .all (fig .data [0 ].x == ar [:, 2 ])
473
423
474
424
475
425
def test_data_frame_from_dict ():
476
426
fig = px .scatter ({"time" : [0 , 1 ], "money" : [1 , 2 ]}, x = "time" , y = "money" )
477
427
assert fig .data [0 ].hovertemplate == "time=%{x}<br>money=%{y}<extra></extra>"
478
- assert np .all (fig .data [0 ].x == _b64 ( [0 , 1 ]) )
428
+ assert np .all (fig .data [0 ].x == [0 , 1 ])
479
429
480
430
481
431
def test_arguments_not_modified ():
@@ -539,11 +489,13 @@ def test_identity_map():
539
489
540
490
def test_constants ():
541
491
fig = px .scatter (x = px .Constant (1 ), y = [1 , 2 ])
542
- assert fig .data [0 ].x == _b64 ([1 , 1 ])
492
+ assert fig .data [0 ].x [0 ] == 1
493
+ assert fig .data [0 ].x [1 ] == 1
543
494
assert "x=" in fig .data [0 ].hovertemplate
544
495
545
496
fig = px .scatter (x = px .Constant (1 , label = "time" ), y = [1 , 2 ])
546
- assert fig .data [0 ].x == _b64 ([1 , 1 ])
497
+ assert fig .data [0 ].x [0 ] == 1
498
+ assert fig .data [0 ].x [1 ] == 1
547
499
assert "x=" not in fig .data [0 ].hovertemplate
548
500
assert "time=" in fig .data [0 ].hovertemplate
549
501
@@ -567,12 +519,15 @@ def test_constants():
567
519
568
520
def test_ranges ():
569
521
fig = px .scatter (x = px .Range (), y = [1 , 2 ], hover_data = [px .Range ()])
570
- assert fig .data [0 ].x == _b64 ([0 , 1 ])
571
- assert fig .data [0 ].customdata == _b64 ([[0 ], [1 ]])
522
+ assert fig .data [0 ].x [0 ] == 0
523
+ assert fig .data [0 ].x [1 ] == 1
524
+ assert fig .data [0 ].customdata [0 ][0 ] == 0
525
+ assert fig .data [0 ].customdata [1 ][0 ] == 1
572
526
assert "x=" in fig .data [0 ].hovertemplate
573
527
574
528
fig = px .scatter (x = px .Range (label = "time" ), y = [1 , 2 ])
575
- assert fig .data [0 ].x == _b64 ([0 , 1 ])
529
+ assert fig .data [0 ].x [0 ] == 0
530
+ assert fig .data [0 ].x [1 ] == 1
576
531
assert "x=" not in fig .data [0 ].hovertemplate
577
532
assert "time=" in fig .data [0 ].hovertemplate
578
533
@@ -662,55 +617,55 @@ def test_x_or_y(fn):
662
617
categorical_df = pd .DataFrame (dict (col = categorical ), index = index )
663
618
664
619
fig = fn (x = numerical )
665
- assert fig .data [0 ].x == _b64 ( numerical )
666
- assert fig .data [0 ].y == _b64 ( range_4 )
620
+ assert list ( fig .data [0 ].x ) == numerical
621
+ assert list ( fig .data [0 ].y ) == range_4
667
622
assert fig .data [0 ].orientation == "h"
668
623
fig = fn (y = numerical )
669
- assert fig .data [0 ].x == _b64 ( range_4 )
670
- assert fig .data [0 ].y == _b64 ( numerical )
624
+ assert list ( fig .data [0 ].x ) == range_4
625
+ assert list ( fig .data [0 ].y ) == numerical
671
626
assert fig .data [0 ].orientation == "v"
672
627
fig = fn (numerical_df , x = "col" )
673
- assert fig .data [0 ].x == _b64 ( numerical )
674
- assert fig .data [0 ].y == _b64 ( index )
628
+ assert list ( fig .data [0 ].x ) == numerical
629
+ assert list ( fig .data [0 ].y ) == index
675
630
assert fig .data [0 ].orientation == "h"
676
631
fig = fn (numerical_df , y = "col" )
677
- assert fig .data [0 ].x == _b64 ( index )
678
- assert fig .data [0 ].y == _b64 ( numerical )
632
+ assert list ( fig .data [0 ].x ) == index
633
+ assert list ( fig .data [0 ].y ) == numerical
679
634
assert fig .data [0 ].orientation == "v"
680
635
681
636
if fn != px .bar :
682
637
fig = fn (x = categorical )
683
638
assert list (fig .data [0 ].x ) == categorical
684
- assert fig .data [0 ].y == _b64 ( range_4 )
639
+ assert list ( fig .data [0 ].y ) == range_4
685
640
assert fig .data [0 ].orientation == "h"
686
641
fig = fn (y = categorical )
687
- assert fig .data [0 ].x == _b64 ( range_4 )
642
+ assert list ( fig .data [0 ].x ) == range_4
688
643
assert list (fig .data [0 ].y ) == categorical
689
644
assert fig .data [0 ].orientation == "v"
690
645
fig = fn (categorical_df , x = "col" )
691
646
assert list (fig .data [0 ].x ) == categorical
692
- assert fig .data [0 ].y == _b64 ( index )
647
+ assert list ( fig .data [0 ].y ) == index
693
648
assert fig .data [0 ].orientation == "h"
694
649
fig = fn (categorical_df , y = "col" )
695
- assert fig .data [0 ].x == _b64 ( index )
650
+ assert list ( fig .data [0 ].x ) == index
696
651
assert list (fig .data [0 ].y ) == categorical
697
652
assert fig .data [0 ].orientation == "v"
698
653
699
654
else :
700
655
fig = fn (x = categorical )
701
656
assert list (fig .data [0 ].x ) == categorical
702
- assert fig .data [0 ].y == _b64 ( constant )
657
+ assert list ( fig .data [0 ].y ) == constant
703
658
assert fig .data [0 ].orientation == "v"
704
659
fig = fn (y = categorical )
705
- assert fig .data [0 ].x == _b64 ( constant )
660
+ assert list ( fig .data [0 ].x ) == constant
706
661
assert list (fig .data [0 ].y ) == categorical
707
662
assert fig .data [0 ].orientation == "h"
708
663
fig = fn (categorical_df , x = "col" )
709
664
assert list (fig .data [0 ].x ) == categorical
710
- assert fig .data [0 ].y == _b64 ( constant )
665
+ assert list ( fig .data [0 ].y ) == constant
711
666
assert fig .data [0 ].orientation == "v"
712
667
fig = fn (categorical_df , y = "col" )
713
- assert fig .data [0 ].x == _b64 ( constant )
668
+ assert list ( fig .data [0 ].x ) == constant
714
669
assert list (fig .data [0 ].y ) == categorical
715
670
assert fig .data [0 ].orientation == "h"
716
671
0 commit comments