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

Skip to content

Commit 728a749

Browse files
dhomeierdstansby
andcommitted
Use assert_allclose in RectangleSelector tests
Co-authored-by: David Stansby <[email protected]>
1 parent 8063e15 commit 728a749

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
@@ -12,7 +12,6 @@
1212
mock_event, noop)
1313

1414
import numpy as np
15-
from numpy.testing import assert_allclose
1615

1716
import pytest
1817

@@ -22,6 +21,13 @@ def ax():
2221
return get_ax()
2322

2423

24+
# Set default tolerances for checking coordinates. These are needed due to
25+
# small innacuracies in floating point conversions between data/display
26+
# coordinates
27+
assert_allclose = functools.partial(
28+
np.testing.assert_allclose, atol=1e-12, rtol=1e-7)
29+
30+
2531
def test_save_blitted_widget_as_pdf():
2632
from matplotlib.widgets import CheckButtons, RadioButtons
2733
from matplotlib.cbook import _get_running_interactive_framework
@@ -138,7 +144,7 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
138144
drag_from_anywhere=drag_from_anywhere)
139145
# Create rectangle
140146
click_and_drag(tool, start=(0, 10), end=(100, 120))
141-
assert tool.center == (50, 65)
147+
assert_allclose(tool.center, (50, 65))
142148
# Drag inside rectangle, but away from centre handle
143149
#
144150
# If drag_from_anywhere == True, this will move the rectangle by (10, 10),
@@ -147,11 +153,11 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
147153
# If drag_from_anywhere == False, this will create a new rectangle with
148154
# center (30, 20)
149155
click_and_drag(tool, start=(25, 15), end=(35, 25))
150-
assert tool.center == new_center
156+
assert_allclose(tool.center, new_center)
151157
# Check that in both cases, dragging outside the rectangle draws a new
152158
# rectangle
153159
click_and_drag(tool, start=(175, 185), end=(185, 195))
154-
assert tool.center == (180, 190)
160+
assert_allclose(tool.center, (180, 190))
155161

156162

157163
def test_rectangle_selector_set_props_handle_props(ax):
@@ -179,35 +185,39 @@ def test_rectangle_resize(ax):
179185
tool = widgets.RectangleSelector(ax, interactive=True)
180186
# Create rectangle
181187
click_and_drag(tool, start=(0, 10), end=(100, 120))
182-
assert tool.extents == (0.0, 100.0, 10.0, 120.0)
188+
assert_allclose(tool.extents, (0.0, 100.0, 10.0, 120.0))
183189

184190
# resize NE handle
185191
extents = tool.extents
186192
xdata, ydata = extents[1], extents[3]
187193
xdata_new, ydata_new = xdata + 10, ydata + 5
188194
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
189-
assert tool.extents == (extents[0], xdata_new, extents[2], ydata_new)
195+
assert_allclose(
196+
tool.extents, (extents[0], xdata_new, extents[2], ydata_new))
190197

191198
# resize E handle
192199
extents = tool.extents
193200
xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2
194201
xdata_new, ydata_new = xdata + 10, ydata
195202
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
196-
assert tool.extents == (extents[0], xdata_new, extents[2], extents[3])
203+
np.testing.assert_allclose(
204+
tool.extents, (extents[0], xdata_new, extents[2], extents[3]))
197205

198206
# resize W handle
199207
extents = tool.extents
200208
xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2
201209
xdata_new, ydata_new = xdata + 15, ydata
202210
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
203-
assert tool.extents == (xdata_new, extents[1], extents[2], extents[3])
211+
assert_allclose(
212+
tool.extents, (xdata_new, extents[1], extents[2], extents[3]))
204213

205214
# resize SW handle
206215
extents = tool.extents
207216
xdata, ydata = extents[0], extents[2]
208217
xdata_new, ydata_new = xdata + 20, ydata + 25
209218
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
210-
assert tool.extents == (xdata_new, extents[1], ydata_new, extents[3])
219+
assert_allclose(
220+
tool.extents, (xdata_new, extents[1], ydata_new, extents[3]))
211221

212222

213223
def test_rectangle_add_state(ax):
@@ -245,8 +255,8 @@ def test_rectangle_resize_center(ax, add_state):
245255
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
246256
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
247257
key=use_key)
248-
assert tool.extents == (extents[0] - xdiff, xdata_new,
249-
extents[2] - ydiff, ydata_new)
258+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
259+
extents[2] - ydiff, ydata_new))
250260

251261
# resize E handle
252262
extents = tool.extents
@@ -255,8 +265,8 @@ def test_rectangle_resize_center(ax, add_state):
255265
xdata_new, ydata_new = xdata + xdiff, ydata
256266
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
257267
key=use_key)
258-
assert tool.extents == (extents[0] - xdiff, xdata_new,
259-
extents[2], extents[3])
268+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
269+
extents[2], extents[3]))
260270

261271
# resize E handle negative diff
262272
extents = tool.extents
@@ -265,8 +275,8 @@ def test_rectangle_resize_center(ax, add_state):
265275
xdata_new, ydata_new = xdata + xdiff, ydata
266276
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
267277
key=use_key)
268-
assert tool.extents == (extents[0] - xdiff, xdata_new,
269-
extents[2], extents[3])
278+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
279+
extents[2], extents[3]))
270280

271281
# resize W handle
272282
extents = tool.extents
@@ -275,8 +285,8 @@ def test_rectangle_resize_center(ax, add_state):
275285
xdata_new, ydata_new = xdata + xdiff, ydata
276286
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
277287
key=use_key)
278-
assert tool.extents == (xdata_new, extents[1] - xdiff,
279-
extents[2], extents[3])
288+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
289+
extents[2], extents[3]))
280290

281291
# resize W handle negative diff
282292
extents = tool.extents
@@ -285,8 +295,8 @@ def test_rectangle_resize_center(ax, add_state):
285295
xdata_new, ydata_new = xdata + xdiff, ydata
286296
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
287297
key=use_key)
288-
assert tool.extents == (xdata_new, extents[1] - xdiff,
289-
extents[2], extents[3])
298+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
299+
extents[2], extents[3]))
290300

291301
# resize SW handle
292302
extents = tool.extents
@@ -329,8 +339,8 @@ def test_rectangle_resize_square(ax, add_state):
329339
xdata_new, ydata_new = xdata + xdiff, ydata
330340
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
331341
key=use_key)
332-
assert tool.extents == (extents[0], xdata_new,
333-
extents[2], extents[3] + xdiff)
342+
assert_allclose(tool.extents, (extents[0], xdata_new,
343+
extents[2], extents[3] + xdiff))
334344

335345
# resize E handle negative diff
336346
extents = tool.extents
@@ -339,8 +349,8 @@ def test_rectangle_resize_square(ax, add_state):
339349
xdata_new, ydata_new = xdata + xdiff, ydata
340350
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
341351
key=use_key)
342-
assert tool.extents == (extents[0], xdata_new,
343-
extents[2], extents[3] + xdiff)
352+
assert_allclose(tool.extents, (extents[0], xdata_new,
353+
extents[2], extents[3] + xdiff))
344354

345355
# resize W handle
346356
extents = tool.extents
@@ -349,8 +359,8 @@ def test_rectangle_resize_square(ax, add_state):
349359
xdata_new, ydata_new = xdata + xdiff, ydata
350360
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
351361
key=use_key)
352-
assert tool.extents == (xdata_new, extents[1],
353-
extents[2], extents[3] - xdiff)
362+
assert_allclose(tool.extents, (xdata_new, extents[1],
363+
extents[2], extents[3] - xdiff))
354364

355365
# resize W handle negative diff
356366
extents = tool.extents
@@ -359,8 +369,8 @@ def test_rectangle_resize_square(ax, add_state):
359369
xdata_new, ydata_new = xdata + xdiff, ydata
360370
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
361371
key=use_key)
362-
assert tool.extents == (xdata_new, extents[1],
363-
extents[2], extents[3] - xdiff)
372+
assert_allclose(tool.extents, (xdata_new, extents[1],
373+
extents[2], extents[3] - xdiff))
364374

365375
# resize SW handle
366376
extents = tool.extents
@@ -369,8 +379,8 @@ def test_rectangle_resize_square(ax, add_state):
369379
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
370380
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
371381
key=use_key)
372-
assert tool.extents == (extents[0] + ydiff, extents[1],
373-
ydata_new, extents[3])
382+
assert_allclose(tool.extents, (extents[0] + ydiff, extents[1],
383+
ydata_new, extents[3]))
374384

375385

376386
def test_rectangle_resize_square_center(ax):
@@ -442,7 +452,7 @@ def test_rectangle_rotate(ax, selector_class):
442452
tool = selector_class(ax, interactive=True)
443453
# Draw rectangle
444454
click_and_drag(tool, start=(100, 100), end=(130, 140))
445-
assert tool.extents == (100, 130, 100, 140)
455+
assert_allclose(tool.extents, (100, 130, 100, 140))
446456
assert len(tool._state) == 0
447457

448458
# Rotate anticlockwise using top-right corner
@@ -475,7 +485,7 @@ def test_rectangle_add_remove_set(ax):
475485
tool = widgets.RectangleSelector(ax, interactive=True)
476486
# Draw rectangle
477487
click_and_drag(tool, start=(100, 100), end=(130, 140))
478-
assert tool.extents == (100, 130, 100, 140)
488+
assert_allclose(tool.extents, (100, 130, 100, 140))
479489
assert len(tool._state) == 0
480490
for state in ['rotate', 'square', 'center']:
481491
tool.add_state(state)
@@ -492,15 +502,15 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
492502
use_data_coordinates=use_data_coordinates)
493503
# Create rectangle
494504
click_and_drag(tool, start=(70, 65), end=(120, 115))
495-
assert tool.extents == (70.0, 120.0, 65.0, 115.0)
505+
assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0))
496506
tool.add_state('square')
497507
tool.add_state('center')
498508

499509
if use_data_coordinates:
500510
# resize E handle
501511
extents = tool.extents
502512
xdata, ydata, width = extents[1], extents[3], extents[1] - extents[0]
503-
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
513+
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
504514
xdata_new, ydata_new = xdata + xdiff, ydata
505515
ychange = width / 2 + xdiff
506516
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
@@ -525,24 +535,22 @@ def test_ellipse(ax):
525535

526536
# drag the rectangle
527537
click_and_drag(tool, start=(125, 125), end=(145, 145))
528-
assert tool.extents == (120, 170, 120, 170)
538+
assert_allclose(tool.extents, (120, 170, 120, 170))
529539

530540
# create from center
531541
click_and_drag(tool, start=(100, 100), end=(125, 125), key='control')
532-
assert tool.extents == (75, 125, 75, 125)
542+
assert_allclose(tool.extents, (75, 125, 75, 125))
533543

534544
# create a square
535545
click_and_drag(tool, start=(10, 10), end=(35, 30), key='shift')
536-
extents = [int(e) for e in tool.extents]
537-
assert extents == [10, 35, 10, 35]
546+
assert_allclose(tool.extents, (10, 35, 10, 35))
538547

539548
# create a square from center
540549
click_and_drag(tool, start=(100, 100), end=(125, 130), key='ctrl+shift')
541-
extents = [int(e) for e in tool.extents]
542-
assert extents == [70, 130, 70, 130]
550+
assert_allclose(tool.extents, (70, 130, 70, 130))
543551

544552
assert tool.geometry.shape == (2, 73)
545-
assert_allclose(tool.geometry[:, 0], [70., 100])
553+
assert_allclose(tool.geometry[:, 0], (70, 100))
546554

547555

548556
def test_rectangle_handles(ax):
@@ -552,22 +560,22 @@ def test_rectangle_handles(ax):
552560
tool.extents = (100, 150, 100, 150)
553561

554562
assert_allclose(tool.corners, ((100, 150, 150, 100), (100, 100, 150, 150)))
555-
assert tool.extents == (100, 150, 100, 150)
563+
assert_allclose(tool.extents, (100, 150, 100, 150))
556564
assert_allclose(tool.edge_centers,
557565
((100, 125.0, 150, 125.0), (125.0, 100, 125.0, 150)))
558-
assert tool.extents == (100, 150, 100, 150)
566+
assert_allclose(tool.extents, (100, 150, 100, 150))
559567

560568
# grab a corner and move it
561569
click_and_drag(tool, start=(100, 100), end=(120, 120))
562-
assert tool.extents == (120, 150, 120, 150)
570+
assert_allclose(tool.extents, (120, 150, 120, 150))
563571

564572
# grab the center and move it
565573
click_and_drag(tool, start=(132, 132), end=(120, 120))
566-
assert tool.extents == (108, 138, 108, 138)
574+
assert_allclose(tool.extents, (108, 138, 108, 138))
567575

568576
# create a new rectangle
569577
click_and_drag(tool, start=(10, 10), end=(100, 100))
570-
assert tool.extents == (10, 100, 10, 100)
578+
assert_allclose(tool.extents, (10, 100, 10, 100))
571579

572580
# Check that marker_props worked.
573581
assert mcolors.same_color(
@@ -586,7 +594,7 @@ def test_rectangle_selector_onselect(ax, interactive):
586594
click_and_drag(tool, start=(100, 110), end=(150, 120))
587595

588596
onselect.assert_called_once()
589-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
597+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
590598

591599
onselect.reset_mock()
592600
click_and_drag(tool, start=(10, 100), end=(10, 100))
@@ -601,19 +609,19 @@ def test_rectangle_selector_ignore_outside(ax, ignore_event_outside):
601609
ignore_event_outside=ignore_event_outside)
602610
click_and_drag(tool, start=(100, 110), end=(150, 120))
603611
onselect.assert_called_once()
604-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
612+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
605613

606614
onselect.reset_mock()
607615
# Trigger event outside of span
608616
click_and_drag(tool, start=(150, 150), end=(160, 160))
609617
if ignore_event_outside:
610618
# event have been ignored and span haven't changed.
611619
onselect.assert_not_called()
612-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
620+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
613621
else:
614622
# A new shape is created
615623
onselect.assert_called_once()
616-
assert tool.extents == (150.0, 160.0, 150.0, 160.0)
624+
assert_allclose(tool.extents, (150.0, 160.0, 150.0, 160.0))
617625

618626

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

0 commit comments

Comments
 (0)