-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_axis_span_shapes.py
476 lines (453 loc) · 13.7 KB
/
test_axis_span_shapes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from plotly.basedatatypes import _indexing_combinations
import plotly.express as px
import pytest
from .common import _cmp_partial_dict, _check_figure_layout_objects
@pytest.fixture
def subplot_fig_fixture():
fig = px.scatter(
px.data.tips(), x="total_bill", y="tip", facet_row="smoker", facet_col="sex"
)
# explicitly set domains so that we know what they will be
# these could be anything but we make them plausible
for ax, dom in zip(
[("x", ""), ("x", "2"), ("x", "3"), ("x", "4")],
[(0, 0.4), (0.5, 0.9), (0, 0.4), (0.5, 0.9)],
):
axname = ax[0] + "axis" + ax[1]
fig["layout"][axname]["domain"] = dom
for ax, dom in zip(
[("y", ""), ("y", "2"), ("y", "3"), ("y", "4")],
[(0, 0.4), (0, 0.4), (0.5, 0.9), (0.5, 0.9)],
):
axname = ax[0] + "axis" + ax[1]
fig["layout"][axname]["domain"] = dom
return fig
@pytest.fixture
def subplot_empty_traces_fig_fixture():
fig = px.scatter(
px.data.tips(), x="total_bill", y="tip", facet_row="day", facet_col="time"
)
# explicitly set domains so that we know what they will be
# these could be anything but we make them plausible
for ax, dom in zip(
[("x", "")] + [("x", str(n)) for n in range(2, 9)],
[((n % 2) * 0.5, (n % 2) * 0.5 + 0.4) for n in range(8)],
):
axname = ax[0] + "axis" + ax[1]
fig["layout"][axname]["domain"] = dom
for ax, dom in zip(
[("y", "")] + [("y", str(n)) for n in range(2, 9)],
[((n // 2) * 0.25, (n // 2) * 0.25 + 0.2) for n in range(8)],
):
axname = ax[0] + "axis" + ax[1]
fig["layout"][axname]["domain"] = dom
return fig
@pytest.fixture
def non_subplot_fig_fixture():
fig = go.Figure(go.Scatter(x=[1, 2, 3], y=[4, 3, 2]))
return fig
# Fixture is here for testing custom-sized subplots
@pytest.fixture
def custom_sized_subplots():
fig = make_subplots(
rows=5,
cols=2,
specs=[
[{}, {"rowspan": 2}],
[{}, None],
[{"rowspan": 2, "colspan": 2}, None],
[None, None],
[{}, {}],
],
print_grid=True,
)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2)
return fig
# stuff to test:
# add_vline, hline etc. add the intended shape
# - then WLOG maybe we can just test 1 of them, e.g., add_vline?
# test that the addressing works correctly? this is already tested for in add_shape...
# make sure all the methods work for subplots and single plot
# test edge-cases of _make_paper_spanning_shape: bad direction, bad shape (e.g., a path)
@pytest.mark.parametrize(
"test_input,expected",
# test_input: (function,kwargs)
# expected: list of dictionaries with key:value pairs we expect in the added shapes
[
(
(go.Figure.add_vline, dict(x=20, row=1, col=1)),
[
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x",
"y0": 0,
"y1": 1,
"yref": "y domain",
}
],
),
(
(go.Figure.add_vline, dict(x=20, row=2, col=2)),
[
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x4",
"y0": 0,
"y1": 1,
"yref": "y4 domain",
}
],
),
(
(go.Figure.add_hline, dict(y=6, row=1, col=1)),
[
{
"type": "line",
"x0": 0,
"x1": 1,
"xref": "x domain",
"y0": 6,
"y1": 6,
"yref": "y",
}
],
),
(
(go.Figure.add_hline, dict(y=6, row=2, col=2)),
[
{
"type": "line",
"x0": 0,
"x1": 1,
"xref": "x4 domain",
"y0": 6,
"y1": 6,
"yref": "y4",
}
],
),
(
(go.Figure.add_vrect, dict(x0=20, x1=30, row=1, col=1)),
[
{
"type": "rect",
"x0": 20,
"x1": 30,
"xref": "x",
"y0": 0,
"y1": 1,
"yref": "y domain",
}
],
),
(
(go.Figure.add_vrect, dict(x0=20, x1=30, row=2, col=2)),
[
{
"type": "rect",
"x0": 20,
"x1": 30,
"xref": "x4",
"y0": 0,
"y1": 1,
"yref": "y4 domain",
}
],
),
(
(go.Figure.add_hrect, dict(y0=6, y1=8, row=1, col=1)),
[
{
"type": "rect",
"x0": 0,
"x1": 1,
"xref": "x domain",
"y0": 6,
"y1": 8,
"yref": "y",
}
],
),
(
(go.Figure.add_hrect, dict(y0=6, y1=8, row=2, col=2)),
[
{
"type": "rect",
"x0": 0,
"x1": 1,
"xref": "x4 domain",
"y0": 6,
"y1": 8,
"yref": "y4",
}
],
),
(
(go.Figure.add_vline, dict(x=20, row=2, col="all")),
[
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x3",
"y0": 0,
"y1": 1,
"yref": "y3 domain",
},
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x4",
"y0": 0,
"y1": 1,
"yref": "y4 domain",
},
],
),
(
(go.Figure.add_vline, dict(x=20, row="all", col=2)),
[
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x2",
"y0": 0,
"y1": 1,
"yref": "y2 domain",
},
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x4",
"y0": 0,
"y1": 1,
"yref": "y4 domain",
},
],
),
],
)
def test_add_span_shape(test_input, expected, subplot_fig_fixture):
_check_figure_layout_objects(test_input, expected, subplot_fig_fixture)
@pytest.mark.parametrize(
"test_input,expected",
# test_input: (function,kwargs)
# expected: list of dictionaries with key:value pairs we expect in the added shapes
[
(
(go.Figure.add_vline, dict(x=20, row=[3, 4], col="all")),
[
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x5",
"y0": 0,
"y1": 1,
"yref": "y5 domain",
},
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x7",
"y0": 0,
"y1": 1,
"yref": "y7 domain",
},
],
),
(
(
go.Figure.add_vline,
dict(x=20, row="all", col=2, exclude_empty_subplots=False),
),
[
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x2",
"y0": 0,
"y1": 1,
"yref": "y2 domain",
},
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x4",
"y0": 0,
"y1": 1,
"yref": "y4 domain",
},
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x6",
"y0": 0,
"y1": 1,
"yref": "y6 domain",
},
{
"type": "line",
"x0": 20,
"x1": 20,
"xref": "x8",
"y0": 0,
"y1": 1,
"yref": "y8 domain",
},
],
),
],
)
def test_add_span_shape_no_empty_plot(
test_input, expected, subplot_empty_traces_fig_fixture
):
_check_figure_layout_objects(test_input, expected, subplot_empty_traces_fig_fixture)
@pytest.mark.parametrize(
"test_input,expected",
# test_input: (function,kwargs)
# expected: list of dictionaries with key:value pairs we expect in the added shapes
[
(
(go.Figure.add_hline, dict(y=6)),
[
{
"type": "line",
"x0": 0,
"x1": 1,
"xref": "x domain",
"y0": 6,
"y1": 6,
"yref": "y",
}
],
),
(
(go.Figure.add_vline, dict(x=6)),
[
{
"type": "line",
"y0": 0,
"y1": 1,
"xref": "x",
"x0": 6,
"x1": 6,
"yref": "y domain",
}
],
),
],
)
def test_non_subplot_add_span_shape(test_input, expected, non_subplot_fig_fixture):
_check_figure_layout_objects(test_input, expected, non_subplot_fig_fixture)
@pytest.mark.parametrize(
"test_input",
[
(go.Figure.add_hline, dict(y=10, row=4, col=5)),
# valid row, invalid column
(go.Figure.add_hline, dict(y=10, row=1, col=5)),
],
)
def test_invalid_subplot_address(test_input, subplot_fig_fixture):
f, kwargs = test_input
with pytest.raises(IndexError):
f(subplot_fig_fixture, **kwargs)
def _check_figure_shapes_custom_sized(test_input, expected, fig):
# look up domains in fig
corrects = []
for d, ax in expected:
dom = [0, 1]
if ax[: len("xaxis")] == "xaxis":
d["x0"], d["x1"] = dom
elif ax[: len("yaxis")] == "yaxis":
d["y0"], d["y1"] = dom
else:
raise ValueError("bad axis")
corrects.append(d)
f, kwargs = test_input
f(fig, **kwargs)
if len(fig.layout.shapes) == 0:
assert False
if len(fig.layout.shapes) != len(corrects):
assert False
ret = True
for s, d in zip(fig.layout.shapes, corrects):
ret &= _cmp_partial_dict(s, d)
assert ret
@pytest.mark.parametrize(
"test_input,expected",
# test_input: (function,kwargs)
# expected: list of dictionaries with key:value pairs we expect in the added shapes
[
(
(go.Figure.add_vline, dict(x=1.5, row="all", col=2)),
[
(
{
"type": "line",
"x0": 1.5,
"x1": 1.5,
"xref": "x2",
"yref": "y2 domain",
},
"yaxis2",
),
(
{
"type": "line",
"x0": 1.5,
"x1": 1.5,
"xref": "x6",
"yref": "y6 domain",
},
"yaxis6",
),
],
),
(
(go.Figure.add_hline, dict(y=1.5, row=5, col="all")),
[
(
{
"type": "line",
"yref": "y5",
"y0": 1.5,
"y1": 1.5,
"xref": "x5 domain",
},
"xaxis5",
),
(
{
"type": "line",
"yref": "y6",
"y0": 1.5,
"y1": 1.5,
"xref": "x6 domain",
},
"xaxis6",
),
],
),
],
)
def test_custom_sized_subplots(test_input, expected, custom_sized_subplots):
_check_figure_shapes_custom_sized(test_input, expected, custom_sized_subplots)