Skip to content

Commit 6f763fa

Browse files
committed
Use assert_allclose in RectangleSelector tests
1 parent a5929d1 commit 6f763fa

1 file changed

Lines changed: 59 additions & 49 deletions

File tree

‎lib/matplotlib/tests/test_widgets.py‎

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import functools
2+
13
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
24
import matplotlib.colors as mcolors
35
import matplotlib.widgets as widgets
@@ -7,11 +9,17 @@
79
mock_event)
810

911
import numpy as np
10-
from numpy.testing import assert_allclose
1112

1213
import pytest
1314

1415

16+
# Set default tolerances for checking coordinates. These are needed due to
17+
# small innacuracies in floating point conversions between data/display
18+
# coordinates
19+
assert_allclose = functools.partial(
20+
np.testing.assert_allclose, atol=1e-12, rtol=1e-7)
21+
22+
1523
def check_rectangle(**kwargs):
1624
ax = get_ax()
1725

@@ -70,7 +78,7 @@ def onselect(epress, erelease):
7078
drag_from_anywhere=drag_from_anywhere)
7179
# Create rectangle
7280
click_and_drag(tool, start=(0, 10), end=(100, 120))
73-
assert tool.center == (50, 65)
81+
assert_allclose(tool.center, (50, 65))
7482
# Drag inside rectangle, but away from centre handle
7583
#
7684
# If drag_from_anywhere == True, this will move the rectangle by (10, 10),
@@ -79,11 +87,11 @@ def onselect(epress, erelease):
7987
# If drag_from_anywhere == False, this will create a new rectangle with
8088
# center (30, 20)
8189
click_and_drag(tool, start=(25, 15), end=(35, 25))
82-
assert tool.center == new_center
90+
assert_allclose(tool.center, new_center)
8391
# Check that in both cases, dragging outside the rectangle draws a new
8492
# rectangle
8593
click_and_drag(tool, start=(175, 185), end=(185, 195))
86-
assert tool.center == (180, 190)
94+
assert_allclose(tool.center, (180, 190))
8795

8896

8997
def test_rectangle_selector_set_props_handle_props():
@@ -121,35 +129,39 @@ def onselect(epress, erelease):
121129
tool = widgets.RectangleSelector(ax, onselect, interactive=True)
122130
# Create rectangle
123131
click_and_drag(tool, start=(0, 10), end=(100, 120))
124-
assert tool.extents == (0.0, 100.0, 10.0, 120.0)
132+
assert_allclose(tool.extents, (0.0, 100.0, 10.0, 120.0))
125133

126134
# resize NE handle
127135
extents = tool.extents
128136
xdata, ydata = extents[1], extents[3]
129137
xdata_new, ydata_new = xdata + 10, ydata + 5
130138
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
131-
assert tool.extents == (extents[0], xdata_new, extents[2], ydata_new)
139+
assert_allclose(
140+
tool.extents, (extents[0], xdata_new, extents[2], ydata_new))
132141

133142
# resize E handle
134143
extents = tool.extents
135144
xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2
136145
xdata_new, ydata_new = xdata + 10, ydata
137146
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
138-
assert tool.extents == (extents[0], xdata_new, extents[2], extents[3])
147+
np.testing.assert_allclose(
148+
tool.extents, (extents[0], xdata_new, extents[2], extents[3]))
139149

140150
# resize W handle
141151
extents = tool.extents
142152
xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2
143153
xdata_new, ydata_new = xdata + 15, ydata
144154
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
145-
assert tool.extents == (xdata_new, extents[1], extents[2], extents[3])
155+
assert_allclose(
156+
tool.extents, (xdata_new, extents[1], extents[2], extents[3]))
146157

147158
# resize SW handle
148159
extents = tool.extents
149160
xdata, ydata = extents[0], extents[2]
150161
xdata_new, ydata_new = xdata + 20, ydata + 25
151162
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
152-
assert tool.extents == (xdata_new, extents[1], ydata_new, extents[3])
163+
assert_allclose(
164+
tool.extents, (xdata_new, extents[1], ydata_new, extents[3]))
153165

154166

155167
def test_rectangle_add_state():
@@ -197,8 +209,8 @@ def onselect(epress, erelease):
197209
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
198210
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
199211
key=use_key)
200-
assert tool.extents == (extents[0] - xdiff, xdata_new,
201-
extents[2] - ydiff, ydata_new)
212+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
213+
extents[2] - ydiff, ydata_new))
202214

203215
# resize E handle
204216
extents = tool.extents
@@ -207,8 +219,8 @@ def onselect(epress, erelease):
207219
xdata_new, ydata_new = xdata + xdiff, ydata
208220
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
209221
key=use_key)
210-
assert tool.extents == (extents[0] - xdiff, xdata_new,
211-
extents[2], extents[3])
222+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
223+
extents[2], extents[3]))
212224

213225
# resize E handle negative diff
214226
extents = tool.extents
@@ -217,8 +229,8 @@ def onselect(epress, erelease):
217229
xdata_new, ydata_new = xdata + xdiff, ydata
218230
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
219231
key=use_key)
220-
assert tool.extents == (extents[0] - xdiff, xdata_new,
221-
extents[2], extents[3])
232+
assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new,
233+
extents[2], extents[3]))
222234

223235
# resize W handle
224236
extents = tool.extents
@@ -227,8 +239,8 @@ def onselect(epress, erelease):
227239
xdata_new, ydata_new = xdata + xdiff, ydata
228240
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
229241
key=use_key)
230-
assert tool.extents == (xdata_new, extents[1] - xdiff,
231-
extents[2], extents[3])
242+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
243+
extents[2], extents[3]))
232244

233245
# resize W handle negative diff
234246
extents = tool.extents
@@ -237,8 +249,8 @@ def onselect(epress, erelease):
237249
xdata_new, ydata_new = xdata + xdiff, ydata
238250
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
239251
key=use_key)
240-
assert tool.extents == (xdata_new, extents[1] - xdiff,
241-
extents[2], extents[3])
252+
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
253+
extents[2], extents[3]))
242254

243255
# resize SW handle
244256
extents = tool.extents
@@ -286,8 +298,8 @@ def onselect(epress, erelease):
286298
xdata_new, ydata_new = xdata + xdiff, ydata
287299
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
288300
key=use_key)
289-
assert tool.extents == (extents[0], xdata_new,
290-
extents[2], extents[3] + xdiff)
301+
assert_allclose(tool.extents, (extents[0], xdata_new,
302+
extents[2], extents[3] + xdiff))
291303

292304
# resize E handle negative diff
293305
extents = tool.extents
@@ -296,8 +308,8 @@ def onselect(epress, erelease):
296308
xdata_new, ydata_new = xdata + xdiff, ydata
297309
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
298310
key=use_key)
299-
assert tool.extents == (extents[0], xdata_new,
300-
extents[2], extents[3] + xdiff)
311+
assert_allclose(tool.extents, (extents[0], xdata_new,
312+
extents[2], extents[3] + xdiff))
301313

302314
# resize W handle
303315
extents = tool.extents
@@ -306,8 +318,8 @@ def onselect(epress, erelease):
306318
xdata_new, ydata_new = xdata + xdiff, ydata
307319
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
308320
key=use_key)
309-
assert tool.extents == (xdata_new, extents[1],
310-
extents[2], extents[3] - xdiff)
321+
assert_allclose(tool.extents, (xdata_new, extents[1],
322+
extents[2], extents[3] - xdiff))
311323

312324
# resize W handle negative diff
313325
extents = tool.extents
@@ -316,8 +328,8 @@ def onselect(epress, erelease):
316328
xdata_new, ydata_new = xdata + xdiff, ydata
317329
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
318330
key=use_key)
319-
assert tool.extents == (xdata_new, extents[1],
320-
extents[2], extents[3] - xdiff)
331+
assert_allclose(tool.extents, (xdata_new, extents[1],
332+
extents[2], extents[3] - xdiff))
321333

322334
# resize SW handle
323335
extents = tool.extents
@@ -326,8 +338,8 @@ def onselect(epress, erelease):
326338
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
327339
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
328340
key=use_key)
329-
assert tool.extents == (extents[0] + ydiff, extents[1],
330-
ydata_new, extents[3])
341+
assert_allclose(tool.extents, (extents[0] + ydiff, extents[1],
342+
ydata_new, extents[3]))
331343

332344

333345
def test_rectangle_resize_square_center():
@@ -409,7 +421,7 @@ def onselect(epress, erelease):
409421
tool = selector_class(ax, onselect=onselect, interactive=True)
410422
# Draw rectangle
411423
click_and_drag(tool, start=(100, 100), end=(130, 140))
412-
assert tool.extents == (100, 130, 100, 140)
424+
assert_allclose(tool.extents, (100, 130, 100, 140))
413425
assert len(tool._state) == 0
414426

415427
# Rotate anticlockwise using top-right corner
@@ -447,7 +459,7 @@ def onselect(epress, erelease):
447459
tool = widgets.RectangleSelector(ax, onselect=onselect, interactive=True)
448460
# Draw rectangle
449461
click_and_drag(tool, start=(100, 100), end=(130, 140))
450-
assert tool.extents == (100, 130, 100, 140)
462+
assert_allclose(tool.extents, (100, 130, 100, 140))
451463
assert len(tool._state) == 0
452464
for state in ['rotate', 'square', 'center']:
453465
tool.add_state(state)
@@ -468,15 +480,15 @@ def onselect(epress, erelease):
468480
use_data_coordinates=use_data_coordinates)
469481
# Create rectangle
470482
click_and_drag(tool, start=(70, 65), end=(120, 115))
471-
assert tool.extents == (70.0, 120.0, 65.0, 115.0)
483+
assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0))
472484
tool.add_state('square')
473485
tool.add_state('center')
474486

475487
if use_data_coordinates:
476488
# resize E handle
477489
extents = tool.extents
478490
xdata, ydata, width = extents[1], extents[3], extents[1] - extents[0]
479-
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
491+
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
480492
xdata_new, ydata_new = xdata + xdiff, ydata
481493
ychange = width / 2 + xdiff
482494
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
@@ -507,24 +519,22 @@ def onselect(epress, erelease):
507519

508520
# drag the rectangle
509521
click_and_drag(tool, start=(125, 125), end=(145, 145))
510-
assert tool.extents == (120, 170, 120, 170)
522+
assert_allclose(tool.extents, (120, 170, 120, 170))
511523

512524
# create from center
513525
click_and_drag(tool, start=(100, 100), end=(125, 125), key='control')
514-
assert tool.extents == (75, 125, 75, 125)
526+
assert_allclose(tool.extents, (75, 125, 75, 125))
515527

516528
# create a square
517529
click_and_drag(tool, start=(10, 10), end=(35, 30), key='shift')
518-
extents = [int(e) for e in tool.extents]
519-
assert extents == [10, 35, 10, 35]
530+
assert_allclose(tool.extents, (10, 35, 10, 35))
520531

521532
# create a square from center
522533
click_and_drag(tool, start=(100, 100), end=(125, 130), key='ctrl+shift')
523-
extents = [int(e) for e in tool.extents]
524-
assert extents == [70, 130, 70, 130]
534+
assert_allclose(tool.extents, (70, 130, 70, 130))
525535

526536
assert tool.geometry.shape == (2, 73)
527-
assert_allclose(tool.geometry[:, 0], [70., 100])
537+
assert_allclose(tool.geometry[:, 0], (70, 100))
528538

529539

530540
def test_rectangle_handles():
@@ -541,22 +551,22 @@ def onselect(epress, erelease):
541551
tool.extents = (100, 150, 100, 150)
542552

543553
assert_allclose(tool.corners, ((100, 150, 150, 100), (100, 100, 150, 150)))
544-
assert tool.extents == (100, 150, 100, 150)
554+
assert_allclose(tool.extents, (100, 150, 100, 150))
545555
assert_allclose(tool.edge_centers,
546556
((100, 125.0, 150, 125.0), (125.0, 100, 125.0, 150)))
547-
assert tool.extents == (100, 150, 100, 150)
557+
assert_allclose(tool.extents, (100, 150, 100, 150))
548558

549559
# grab a corner and move it
550560
click_and_drag(tool, start=(100, 100), end=(120, 120))
551-
assert tool.extents == (120, 150, 120, 150)
561+
assert_allclose(tool.extents, (120, 150, 120, 150))
552562

553563
# grab the center and move it
554564
click_and_drag(tool, start=(132, 132), end=(120, 120))
555-
assert tool.extents == (108, 138, 108, 138)
565+
assert_allclose(tool.extents, (108, 138, 108, 138))
556566

557567
# create a new rectangle
558568
click_and_drag(tool, start=(10, 10), end=(100, 100))
559-
assert tool.extents == (10, 100, 10, 100)
569+
assert_allclose(tool.extents, (10, 100, 10, 100))
560570

561571
# Check that marker_props worked.
562572
assert mcolors.same_color(
@@ -578,7 +588,7 @@ def onselect(vmin, vmax):
578588
click_and_drag(tool, start=(100, 110), end=(150, 120))
579589

580590
assert tool.ax._got_onselect
581-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
591+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
582592

583593
# Reset tool.ax._got_onselect
584594
tool.ax._got_onselect = False
@@ -597,7 +607,7 @@ def onselect(vmin, vmax):
597607
ignore_event_outside=ignore_event_outside)
598608
click_and_drag(tool, start=(100, 110), end=(150, 120))
599609
assert tool.ax._got_onselect
600-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
610+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
601611

602612
# Reset
603613
ax._got_onselect = False
@@ -606,11 +616,11 @@ def onselect(vmin, vmax):
606616
if ignore_event_outside:
607617
# event have been ignored and span haven't changed.
608618
assert not ax._got_onselect
609-
assert tool.extents == (100.0, 150.0, 110.0, 120.0)
619+
assert_allclose(tool.extents, (100.0, 150.0, 110.0, 120.0))
610620
else:
611621
# A new shape is created
612622
assert ax._got_onselect
613-
assert tool.extents == (150.0, 160.0, 150.0, 160.0)
623+
assert_allclose(tool.extents, (150.0, 160.0, 150.0, 160.0))
614624

615625

616626
def check_span(*args, **kwargs):

0 commit comments

Comments
 (0)