Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f88335e

Browse files
dhomeierdstansby
andcommitted
Rectangle resize tests with multiple start/end corners
Co-authored-by: David Stansby <[email protected]>
1 parent f6ea853 commit f88335e

File tree

1 file changed

+65
-44
lines changed

1 file changed

+65
-44
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,20 @@ def test_rectangle_selector(ax, kwargs):
8282

8383
# purposely drag outside of axis for release
8484
do_event(tool, 'release', xdata=250, ydata=250, button=1)
85+
do_event(tool, 'onmove', xdata=250, ydata=250, button=1)
8586

8687
if kwargs.get('drawtype', None) not in ['line', 'none']:
8788
assert_allclose(tool.geometry,
88-
[[100., 100, 199, 199, 100],
89+
[[100, 100, 199, 199, 100],
8990
[100, 199, 199, 100, 100]],
9091
err_msg=tool.geometry)
9192

9293
onselect.assert_called_once()
9394
(epress, erelease), kwargs = onselect.call_args
9495
assert epress.xdata == 100
9596
assert epress.ydata == 100
96-
assert erelease.xdata == 199
97-
assert erelease.ydata == 199
97+
assert erelease.xdata == 200
98+
assert erelease.ydata == 200
9899
assert kwargs == {}
99100

100101

@@ -191,10 +192,16 @@ def test_rectangle_selector_set_props_handle_props(ax):
191192
assert artist.get_alpha() == 0.3
192193

193194

194-
def test_rectangle_resize(ax):
195+
# Should give same results if rectangle is created from any two
196+
# opposite corners
197+
@pytest.mark.parametrize('start, end', [[(0, 10), (100, 120)],
198+
[(100, 120), (0, 10)],
199+
[(0, 120), (100, 10)],
200+
[(100, 10), (0, 120)]])
201+
def test_rectangle_resize(ax, start, end):
195202
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
196203
# Create rectangle
197-
click_and_drag(tool, start=(0, 10), end=(100, 120))
204+
click_and_drag(tool, start=start, end=end)
198205
assert_allclose(tool.extents, (0.0, 100.0, 10.0, 120.0))
199206

200207
# resize NE handle
@@ -315,16 +322,16 @@ def test_rectangle_resize_center(ax, add_state):
315322
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
316323
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
317324
key=use_key)
318-
assert tool.extents == (xdata_new, extents[1] - xdiff,
319-
ydata_new, extents[3] - ydiff)
325+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
326+
ydata_new, extents[3] - ydiff))
320327

321328

322329
@pytest.mark.parametrize('add_state', [True, False])
323330
def test_rectangle_resize_square(ax, add_state):
324331
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
325332
# Create rectangle
326333
click_and_drag(tool, start=(70, 65), end=(120, 115))
327-
assert tool.extents == (70.0, 120.0, 65.0, 115.0)
334+
assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0))
328335

329336
if add_state:
330337
tool.add_state('square')
@@ -339,8 +346,8 @@ def test_rectangle_resize_square(ax, add_state):
339346
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
340347
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
341348
key=use_key)
342-
assert tool.extents == (extents[0], xdata_new,
343-
extents[2], extents[3] + xdiff)
349+
assert_allclose(tool.extents, (extents[0], xdata_new,
350+
extents[2], extents[3] + xdiff))
344351

345352
# resize E handle
346353
extents = tool.extents
@@ -389,11 +396,12 @@ def test_rectangle_resize_square(ax, add_state):
389396
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
390397
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
391398
key=use_key)
392-
assert_allclose(tool.extents, (extents[0] + ydiff, extents[1],
393-
ydata_new, extents[3]))
399+
assert_allclose(tool.extents, (xdata_new, extents[1],
400+
extents[2] + xdiff, extents[3]))
394401

395402

396403
def test_rectangle_resize_square_center(ax):
404+
ax.set_aspect(1)
397405
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True)
398406
# Create rectangle
399407
click_and_drag(tool, start=(70, 65), end=(120, 115))
@@ -459,6 +467,7 @@ def test_rectangle_resize_square_center(ax):
459467
@pytest.mark.parametrize('selector_class',
460468
[widgets.RectangleSelector, widgets.EllipseSelector])
461469
def test_rectangle_rotate(ax, selector_class):
470+
ax.set_aspect(1)
462471
tool = selector_class(ax, onselect=noop, interactive=True)
463472
# Draw rectangle
464473
click_and_drag(tool, start=(100, 100), end=(130, 140))
@@ -472,19 +481,19 @@ def test_rectangle_rotate(ax, selector_class):
472481
click_and_drag(tool, start=(130, 140), end=(120, 145))
473482
do_event(tool, 'on_key_press', key='r')
474483
assert len(tool._state) == 0
475-
# Extents shouldn't change (as shape of rectangle hasn't changed)
476-
assert tool.extents == (100, 130, 100, 140)
484+
# Extents change as the selector remains rigid in display coordinates
485+
assert_allclose(tool.extents, (110.10, 119.90, 95.49, 144.51), atol=0.01)
477486
assert_allclose(tool.rotation, 25.56, atol=0.01)
478487
tool.rotation = 45
479488
assert tool.rotation == 45
480489
# Corners should move
481490
assert_allclose(tool.corners,
482-
np.array([[118.53, 139.75, 111.46, 90.25],
483-
[95.25, 116.46, 144.75, 123.54]]), atol=0.01)
491+
np.array([[110.10, 131.31, 103.03, 81.81],
492+
[95.49, 116.70, 144.98, 123.77]]), atol=0.01)
484493

485494
# Scale using top-right corner
486495
click_and_drag(tool, start=(110, 145), end=(110, 160))
487-
assert_allclose(tool.extents, (100, 139.75, 100, 151.82), atol=0.01)
496+
assert_allclose(tool.extents, (110, 110, 145, 160), atol=0.01)
488497

489498
if selector_class == widgets.RectangleSelector:
490499
with pytest.raises(ValueError):
@@ -506,36 +515,38 @@ def test_rectangle_add_remove_set(ax):
506515

507516
@pytest.mark.parametrize('use_data_coordinates', [False, True])
508517
def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
509-
ax.set_aspect(0.8)
518+
ax = get_ax()
519+
ax.set_aspect(0.5)
520+
# Need to call a draw to update ax.transData
521+
plt.gcf().canvas.draw()
510522

511523
tool = widgets.RectangleSelector(ax, onselect=noop, interactive=True,
512524
use_data_coordinates=use_data_coordinates)
513-
# Create rectangle
514-
click_and_drag(tool, start=(70, 65), end=(120, 115))
515-
assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0))
516525
tool.add_state('square')
517526
tool.add_state('center')
527+
# Create rectangle, width 50 in data coordinates
528+
click_and_drag(tool, start=(70, 65), end=(120, 75))
518529

519530
if use_data_coordinates:
520-
# resize E handle
521-
extents = tool.extents
522-
xdata, ydata, width = extents[1], extents[3], extents[1] - extents[0]
523-
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
524-
xdata_new, ydata_new = xdata + xdiff, ydata
525-
ychange = width / 2 + xdiff
526-
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
527-
assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new,
528-
ycenter - ychange, ycenter + ychange])
531+
assert_allclose(tool.extents, (20, 120, 15, 115))
529532
else:
530-
# resize E handle
531-
extents = tool.extents
532-
xdata, ydata = extents[1], extents[3]
533-
xdiff = 10
534-
xdata_new, ydata_new = xdata + xdiff, ydata
535-
ychange = xdiff * 1 / tool._aspect_ratio_correction
536-
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
537-
assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new,
538-
46.25, 133.75])
533+
assert_allclose(tool.extents, (20, 120, -35, 165))
534+
535+
# resize E handle
536+
extents = tool.extents
537+
xdata, ydata = extents[1], extents[3]
538+
xdiff = 10
539+
xdata_new, ydata_new = xdata + xdiff, ydata
540+
if use_data_coordinates:
541+
# In data coordinates the difference should be equal in both directions
542+
ydiff = xdiff
543+
else:
544+
# In display coordinates, the change in data coordinates should be
545+
# different in each direction
546+
ydiff = xdiff / tool.ax._get_aspect_ratio()
547+
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
548+
assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new,
549+
extents[2] - ydiff, extents[3] + ydiff])
539550

540551

541552
def test_ellipse(ax):
@@ -1646,6 +1657,7 @@ def test_polygon_selector_verts_setter(fig_test, fig_ref, draw_bounding_box):
16461657
]
16471658
for (etype, event_args) in event_sequence:
16481659
do_event(tool_ref, etype, **event_args)
1660+
np.testing.assert_allclose(tool_ref.verts, verts)
16491661

16501662

16511663
def test_polygon_selector_box(ax):
@@ -1660,10 +1672,19 @@ def test_polygon_selector_box(ax):
16601672
*polygon_place_vertex(*verts[0]),
16611673
]
16621674

1675+
# Set smaller axes limits to reduce errors in converting from data to
1676+
# display coords. The canvas size is 640 x 640, so we need a tolerance of
1677+
# (data width / canvas width) = 50 / 640 ~ 0.08 when comparing points in
1678+
# data space
1679+
ax.set_xlim(-5, 45)
1680+
ax.set_ylim(-5, 45)
1681+
atol = 0.08
1682+
16631683
# Create selector
16641684
tool = widgets.PolygonSelector(ax, onselect=noop, draw_bounding_box=True)
16651685
for (etype, event_args) in event_sequence:
16661686
do_event(tool, etype, **event_args)
1687+
np.testing.assert_allclose(tool.verts, verts, atol=atol)
16671688

16681689
# In order to trigger the correct callbacks, trigger events on the canvas
16691690
# instead of the individual tools
@@ -1678,7 +1699,7 @@ def test_polygon_selector_box(ax):
16781699
MouseEvent(
16791700
"button_release_event", canvas, *t.transform((20, 20)), 1)._process()
16801701
np.testing.assert_allclose(
1681-
tool.verts, [(10, 0), (0, 10), (10, 20), (20, 10)])
1702+
tool.verts, [(10, 0), (0, 10), (10, 20), (20, 10)], atol=atol)
16821703

16831704
# Move using the center of the bounding box
16841705
MouseEvent(
@@ -1688,20 +1709,20 @@ def test_polygon_selector_box(ax):
16881709
MouseEvent(
16891710
"button_release_event", canvas, *t.transform((30, 30)), 1)._process()
16901711
np.testing.assert_allclose(
1691-
tool.verts, [(30, 20), (20, 30), (30, 40), (40, 30)])
1712+
tool.verts, [(30, 20), (20, 30), (30, 40), (40, 30)], atol=atol)
16921713

16931714
# Remove a point from the polygon and check that the box extents update
16941715
np.testing.assert_allclose(
1695-
tool._box.extents, (20.0, 40.0, 20.0, 40.0))
1716+
tool._box.extents, (20.0, 40.0, 20.0, 40.0), atol=atol)
16961717

16971718
MouseEvent(
16981719
"button_press_event", canvas, *t.transform((30, 20)), 3)._process()
16991720
MouseEvent(
17001721
"button_release_event", canvas, *t.transform((30, 20)), 3)._process()
17011722
np.testing.assert_allclose(
1702-
tool.verts, [(20, 30), (30, 40), (40, 30)])
1723+
tool.verts, [(20, 30), (30, 40), (40, 30)], atol=atol)
17031724
np.testing.assert_allclose(
1704-
tool._box.extents, (20.0, 40.0, 30.0, 40.0))
1725+
tool._box.extents, (20.0, 40.0, 30.0, 40.0), atol=atol)
17051726

17061727

17071728
def test_polygon_selector_clear_method(ax):

0 commit comments

Comments
 (0)