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

Skip to content

Commit 62d667c

Browse files
dhomeierdstansby
andcommitted
Use assert_allclose in RectangleSelector tests
Co-authored-by: David Stansby <[email protected]>
1 parent 092e56a commit 62d667c

File tree

1 file changed

+57
-49
lines changed

1 file changed

+57
-49
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
mock_event, noop)
1414

1515
import numpy as np
16-
from numpy.testing import assert_allclose
1716

1817
import pytest
1918

@@ -23,6 +22,13 @@ def ax():
2322
return get_ax()
2423

2524

25+
# Set default tolerances for checking coordinates. These are needed due to
26+
# small innacuracies in floating point conversions between data/display
27+
# coordinates
28+
assert_allclose = functools.partial(
29+
np.testing.assert_allclose, atol=1e-12, rtol=1e-7)
30+
31+
2632
def test_save_blitted_widget_as_pdf():
2733
from matplotlib.widgets import CheckButtons, RadioButtons
2834
from matplotlib.cbook import _get_running_interactive_framework
@@ -149,7 +155,7 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
149155
drag_from_anywhere=drag_from_anywhere)
150156
# Create rectangle
151157
click_and_drag(tool, start=(0, 10), end=(100, 120))
152-
assert tool.center == (50, 65)
158+
assert_allclose(tool.center, (50, 65))
153159
# Drag inside rectangle, but away from centre handle
154160
#
155161
# If drag_from_anywhere == True, this will move the rectangle by (10, 10),
@@ -158,11 +164,11 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
158164
# If drag_from_anywhere == False, this will create a new rectangle with
159165
# center (30, 20)
160166
click_and_drag(tool, start=(25, 15), end=(35, 25))
161-
assert tool.center == new_center
167+
assert_allclose(tool.center, new_center)
162168
# Check that in both cases, dragging outside the rectangle draws a new
163169
# rectangle
164170
click_and_drag(tool, start=(175, 185), end=(185, 195))
165-
assert tool.center == (180, 190)
171+
assert_allclose(tool.center, (180, 190))
166172

167173

168174
def test_rectangle_selector_set_props_handle_props(ax):
@@ -190,35 +196,39 @@ def test_rectangle_resize(ax):
190196
tool = widgets.RectangleSelector(ax, interactive=True)
191197
# Create rectangle
192198
click_and_drag(tool, start=(0, 10), end=(100, 120))
193-
assert tool.extents == (0.0, 100.0, 10.0, 120.0)
199+
assert_allclose(tool.extents, (0.0, 100.0, 10.0, 120.0))
194200

195201
# resize NE handle
196202
extents = tool.extents
197203
xdata, ydata = extents[1], extents[3]
198204
xdata_new, ydata_new = xdata + 10, ydata + 5
199205
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
200-
assert tool.extents == (extents[0], xdata_new, extents[2], ydata_new)
206+
assert_allclose(
207+
tool.extents, (extents[0], xdata_new, extents[2], ydata_new))
201208

202209
# resize E handle
203210
extents = tool.extents
204211
xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2
205212
xdata_new, ydata_new = xdata + 10, ydata
206213
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
207-
assert tool.extents == (extents[0], xdata_new, extents[2], extents[3])
214+
np.testing.assert_allclose(
215+
tool.extents, (extents[0], xdata_new, extents[2], extents[3]))
208216

209217
# resize W handle
210218
extents = tool.extents
211219
xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2
212220
xdata_new, ydata_new = xdata + 15, ydata
213221
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
214-
assert tool.extents == (xdata_new, extents[1], extents[2], extents[3])
222+
assert_allclose(
223+
tool.extents, (xdata_new, extents[1], extents[2], extents[3]))
215224

216225
# resize SW handle
217226
extents = tool.extents
218227
xdata, ydata = extents[0], extents[2]
219228
xdata_new, ydata_new = xdata + 20, ydata + 25
220229
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
221-
assert tool.extents == (xdata_new, extents[1], ydata_new, extents[3])
230+
assert_allclose(
231+
tool.extents, (xdata_new, extents[1], ydata_new, extents[3]))
222232

223233

224234
def test_rectangle_add_state(ax):
@@ -256,8 +266,8 @@ def test_rectangle_resize_center(ax, add_state):
256266
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
257267
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
258268
key=use_key)
259-
assert tool.extents == (extents[0] - xdiff, xdata_new,
260-
extents[2] - ydiff, ydata_new)
269+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
270+
extents[2] - ydiff, ydata_new))
261271

262272
# resize E handle
263273
extents = tool.extents
@@ -266,8 +276,8 @@ def test_rectangle_resize_center(ax, add_state):
266276
xdata_new, ydata_new = xdata + xdiff, ydata
267277
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
268278
key=use_key)
269-
assert tool.extents == (extents[0] - xdiff, xdata_new,
270-
extents[2], extents[3])
279+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
280+
extents[2], extents[3]))
271281

272282
# resize E handle negative diff
273283
extents = tool.extents
@@ -276,8 +286,8 @@ def test_rectangle_resize_center(ax, add_state):
276286
xdata_new, ydata_new = xdata + xdiff, ydata
277287
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
278288
key=use_key)
279-
assert tool.extents == (extents[0] - xdiff, xdata_new,
280-
extents[2], extents[3])
289+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
290+
extents[2], extents[3]))
281291

282292
# resize W handle
283293
extents = tool.extents
@@ -286,8 +296,8 @@ def test_rectangle_resize_center(ax, add_state):
286296
xdata_new, ydata_new = xdata + xdiff, ydata
287297
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
288298
key=use_key)
289-
assert tool.extents == (xdata_new, extents[1] - xdiff,
290-
extents[2], extents[3])
299+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
300+
extents[2], extents[3]))
291301

292302
# resize W handle negative diff
293303
extents = tool.extents
@@ -296,8 +306,8 @@ def test_rectangle_resize_center(ax, add_state):
296306
xdata_new, ydata_new = xdata + xdiff, ydata
297307
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
298308
key=use_key)
299-
assert tool.extents == (xdata_new, extents[1] - xdiff,
300-
extents[2], extents[3])
309+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
310+
extents[2], extents[3]))
301311

302312
# resize SW handle
303313
extents = tool.extents
@@ -340,8 +350,8 @@ def test_rectangle_resize_square(ax, add_state):
340350
xdata_new, ydata_new = xdata + xdiff, ydata
341351
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
342352
key=use_key)
343-
assert tool.extents == (extents[0], xdata_new,
344-
extents[2], extents[3] + xdiff)
353+
assert_allclose(tool.extents, (extents[0], xdata_new,
354+
extents[2], extents[3] + xdiff))
345355

346356
# resize E handle negative diff
347357
extents = tool.extents
@@ -350,8 +360,8 @@ def test_rectangle_resize_square(ax, add_state):
350360
xdata_new, ydata_new = xdata + xdiff, ydata
351361
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
352362
key=use_key)
353-
assert tool.extents == (extents[0], xdata_new,
354-
extents[2], extents[3] + xdiff)
363+
assert_allclose(tool.extents, (extents[0], xdata_new,
364+
extents[2], extents[3] + xdiff))
355365

356366
# resize W handle
357367
extents = tool.extents
@@ -360,8 +370,8 @@ def test_rectangle_resize_square(ax, add_state):
360370
xdata_new, ydata_new = xdata + xdiff, ydata
361371
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
362372
key=use_key)
363-
assert tool.extents == (xdata_new, extents[1],
364-
extents[2], extents[3] - xdiff)
373+
assert_allclose(tool.extents, (xdata_new, extents[1],
374+
extents[2], extents[3] - xdiff))
365375

366376
# resize W handle negative diff
367377
extents = tool.extents
@@ -370,8 +380,8 @@ def test_rectangle_resize_square(ax, add_state):
370380
xdata_new, ydata_new = xdata + xdiff, ydata
371381
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
372382
key=use_key)
373-
assert tool.extents == (xdata_new, extents[1],
374-
extents[2], extents[3] - xdiff)
383+
assert_allclose(tool.extents, (xdata_new, extents[1],
384+
extents[2], extents[3] - xdiff))
375385

376386
# resize SW handle
377387
extents = tool.extents
@@ -380,8 +390,8 @@ def test_rectangle_resize_square(ax, add_state):
380390
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
381391
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
382392
key=use_key)
383-
assert tool.extents == (extents[0] + ydiff, extents[1],
384-
ydata_new, extents[3])
393+
assert_allclose(tool.extents, (extents[0] + ydiff, extents[1],
394+
ydata_new, extents[3]))
385395

386396

387397
def test_rectangle_resize_square_center(ax):
@@ -453,7 +463,7 @@ def test_rectangle_rotate(ax, selector_class):
453463
tool = selector_class(ax, interactive=True)
454464
# Draw rectangle
455465
click_and_drag(tool, start=(100, 100), end=(130, 140))
456-
assert tool.extents == (100, 130, 100, 140)
466+
assert_allclose(tool.extents, (100, 130, 100, 140))
457467
assert len(tool._state) == 0
458468

459469
# Rotate anticlockwise using top-right corner
@@ -486,7 +496,7 @@ def test_rectangle_add_remove_set(ax):
486496
tool = widgets.RectangleSelector(ax, interactive=True)
487497
# Draw rectangle
488498
click_and_drag(tool, start=(100, 100), end=(130, 140))
489-
assert tool.extents == (100, 130, 100, 140)
499+
assert_allclose(tool.extents, (100, 130, 100, 140))
490500
assert len(tool._state) == 0
491501
for state in ['rotate', 'square', 'center']:
492502
tool.add_state(state)
@@ -503,15 +513,15 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
503513
use_data_coordinates=use_data_coordinates)
504514
# Create rectangle
505515
click_and_drag(tool, start=(70, 65), end=(120, 115))
506-
assert tool.extents == (70.0, 120.0, 65.0, 115.0)
516+
assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0))
507517
tool.add_state('square')
508518
tool.add_state('center')
509519

510520
if use_data_coordinates:
511521
# resize E handle
512522
extents = tool.extents
513523
xdata, ydata, width = extents[1], extents[3], extents[1] - extents[0]
514-
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
524+
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
515525
xdata_new, ydata_new = xdata + xdiff, ydata
516526
ychange = width / 2 + xdiff
517527
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
@@ -536,24 +546,22 @@ def test_ellipse(ax):
536546

537547
# drag the rectangle
538548
click_and_drag(tool, start=(125, 125), end=(145, 145))
539-
assert tool.extents == (120, 170, 120, 170)
549+
assert_allclose(tool.extents, (120, 170, 120, 170))
540550

541551
# create from center
542552
click_and_drag(tool, start=(100, 100), end=(125, 125), key='control')
543-
assert tool.extents == (75, 125, 75, 125)
553+
assert_allclose(tool.extents, (75, 125, 75, 125))
544554

545555
# create a square
546556
click_and_drag(tool, start=(10, 10), end=(35, 30), key='shift')
547-
extents = [int(e) for e in tool.extents]
548-
assert extents == [10, 35, 10, 35]
557+
assert_allclose(tool.extents, (10, 35, 10, 35))
549558

550559
# create a square from center
551560
click_and_drag(tool, start=(100, 100), end=(125, 130), key='ctrl+shift')
552-
extents = [int(e) for e in tool.extents]
553-
assert extents == [70, 130, 70, 130]
561+
assert_allclose(tool.extents, (70, 130, 70, 130))
554562

555563
assert tool.geometry.shape == (2, 73)
556-
assert_allclose(tool.geometry[:, 0], [70., 100])
564+
assert_allclose(tool.geometry[:, 0], (70, 100))
557565

558566

559567
def test_rectangle_handles(ax):
@@ -563,22 +571,22 @@ def test_rectangle_handles(ax):
563571
tool.extents = (100, 150, 100, 150)
564572

565573
assert_allclose(tool.corners, ((100, 150, 150, 100), (100, 100, 150, 150)))
566-
assert tool.extents == (100, 150, 100, 150)
574+
assert_allclose(tool.extents, (100, 150, 100, 150))
567575
assert_allclose(tool.edge_centers,
568576
((100, 125.0, 150, 125.0), (125.0, 100, 125.0, 150)))
569-
assert tool.extents == (100, 150, 100, 150)
577+
assert_allclose(tool.extents, (100, 150, 100, 150))
570578

571579
# grab a corner and move it
572580
click_and_drag(tool, start=(100, 100), end=(120, 120))
573-
assert tool.extents == (120, 150, 120, 150)
581+
assert_allclose(tool.extents, (120, 150, 120, 150))
574582

575583
# grab the center and move it
576584
click_and_drag(tool, start=(132, 132), end=(120, 120))
577-
assert tool.extents == (108, 138, 108, 138)
585+
assert_allclose(tool.extents, (108, 138, 108, 138))
578586

579587
# create a new rectangle
580588
click_and_drag(tool, start=(10, 10), end=(100, 100))
581-
assert tool.extents == (10, 100, 10, 100)
589+
assert_allclose(tool.extents, (10, 100, 10, 100))
582590

583591
# Check that marker_props worked.
584592
assert mcolors.same_color(
@@ -597,7 +605,7 @@ def test_rectangle_selector_onselect(ax, interactive):
597605
click_and_drag(tool, start=(100, 110), end=(150, 120))
598606

599607
onselect.assert_called_once()
600-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
608+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
601609

602610
onselect.reset_mock()
603611
click_and_drag(tool, start=(10, 100), end=(10, 100))
@@ -612,19 +620,19 @@ def test_rectangle_selector_ignore_outside(ax, ignore_event_outside):
612620
ignore_event_outside=ignore_event_outside)
613621
click_and_drag(tool, start=(100, 110), end=(150, 120))
614622
onselect.assert_called_once()
615-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
623+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
616624

617625
onselect.reset_mock()
618626
# Trigger event outside of span
619627
click_and_drag(tool, start=(150, 150), end=(160, 160))
620628
if ignore_event_outside:
621629
# event have been ignored and span haven't changed.
622630
onselect.assert_not_called()
623-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
631+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
624632
else:
625633
# A new shape is created
626634
onselect.assert_called_once()
627-
assert tool.extents == (150.0, 160.0, 150.0, 160.0)
635+
assert_allclose(tool.extents, (150.0, 160.0, 150.0, 160.0))
628636

629637

630638
@pytest.mark.parametrize('orientation, onmove_callback, kwargs', [

0 commit comments

Comments
 (0)