13
13
mock_event , noop )
14
14
15
15
import numpy as np
16
- from numpy .testing import assert_allclose
17
16
18
17
import pytest
19
18
@@ -23,6 +22,13 @@ def ax():
23
22
return get_ax ()
24
23
25
24
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
+
26
32
def test_save_blitted_widget_as_pdf ():
27
33
from matplotlib .widgets import CheckButtons , RadioButtons
28
34
from matplotlib .cbook import _get_running_interactive_framework
@@ -149,7 +155,7 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
149
155
drag_from_anywhere = drag_from_anywhere )
150
156
# Create rectangle
151
157
click_and_drag (tool , start = (0 , 10 ), end = (100 , 120 ))
152
- assert tool .center == (50 , 65 )
158
+ assert_allclose ( tool .center , (50 , 65 ) )
153
159
# Drag inside rectangle, but away from centre handle
154
160
#
155
161
# 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):
158
164
# If drag_from_anywhere == False, this will create a new rectangle with
159
165
# center (30, 20)
160
166
click_and_drag (tool , start = (25 , 15 ), end = (35 , 25 ))
161
- assert tool .center == new_center
167
+ assert_allclose ( tool .center , new_center )
162
168
# Check that in both cases, dragging outside the rectangle draws a new
163
169
# rectangle
164
170
click_and_drag (tool , start = (175 , 185 ), end = (185 , 195 ))
165
- assert tool .center == (180 , 190 )
171
+ assert_allclose ( tool .center , (180 , 190 ) )
166
172
167
173
168
174
def test_rectangle_selector_set_props_handle_props (ax ):
@@ -190,35 +196,39 @@ def test_rectangle_resize(ax):
190
196
tool = widgets .RectangleSelector (ax , interactive = True )
191
197
# Create rectangle
192
198
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 ) )
194
200
195
201
# resize NE handle
196
202
extents = tool .extents
197
203
xdata , ydata = extents [1 ], extents [3 ]
198
204
xdata_new , ydata_new = xdata + 10 , ydata + 5
199
205
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 ))
201
208
202
209
# resize E handle
203
210
extents = tool .extents
204
211
xdata , ydata = extents [1 ], extents [2 ] + (extents [3 ] - extents [2 ]) / 2
205
212
xdata_new , ydata_new = xdata + 10 , ydata
206
213
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 ]))
208
216
209
217
# resize W handle
210
218
extents = tool .extents
211
219
xdata , ydata = extents [0 ], extents [2 ] + (extents [3 ] - extents [2 ]) / 2
212
220
xdata_new , ydata_new = xdata + 15 , ydata
213
221
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 ]))
215
224
216
225
# resize SW handle
217
226
extents = tool .extents
218
227
xdata , ydata = extents [0 ], extents [2 ]
219
228
xdata_new , ydata_new = xdata + 20 , ydata + 25
220
229
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 ]))
222
232
223
233
224
234
def test_rectangle_add_state (ax ):
@@ -256,8 +266,8 @@ def test_rectangle_resize_center(ax, add_state):
256
266
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
257
267
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
258
268
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 ) )
261
271
262
272
# resize E handle
263
273
extents = tool .extents
@@ -266,8 +276,8 @@ def test_rectangle_resize_center(ax, add_state):
266
276
xdata_new , ydata_new = xdata + xdiff , ydata
267
277
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
268
278
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 ]) )
271
281
272
282
# resize E handle negative diff
273
283
extents = tool .extents
@@ -276,8 +286,8 @@ def test_rectangle_resize_center(ax, add_state):
276
286
xdata_new , ydata_new = xdata + xdiff , ydata
277
287
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
278
288
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 ]) )
281
291
282
292
# resize W handle
283
293
extents = tool .extents
@@ -286,8 +296,8 @@ def test_rectangle_resize_center(ax, add_state):
286
296
xdata_new , ydata_new = xdata + xdiff , ydata
287
297
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
288
298
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 ]) )
291
301
292
302
# resize W handle negative diff
293
303
extents = tool .extents
@@ -296,8 +306,8 @@ def test_rectangle_resize_center(ax, add_state):
296
306
xdata_new , ydata_new = xdata + xdiff , ydata
297
307
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
298
308
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 ]) )
301
311
302
312
# resize SW handle
303
313
extents = tool .extents
@@ -340,8 +350,8 @@ def test_rectangle_resize_square(ax, add_state):
340
350
xdata_new , ydata_new = xdata + xdiff , ydata
341
351
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
342
352
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 ) )
345
355
346
356
# resize E handle negative diff
347
357
extents = tool .extents
@@ -350,8 +360,8 @@ def test_rectangle_resize_square(ax, add_state):
350
360
xdata_new , ydata_new = xdata + xdiff , ydata
351
361
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
352
362
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 ) )
355
365
356
366
# resize W handle
357
367
extents = tool .extents
@@ -360,8 +370,8 @@ def test_rectangle_resize_square(ax, add_state):
360
370
xdata_new , ydata_new = xdata + xdiff , ydata
361
371
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
362
372
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 ) )
365
375
366
376
# resize W handle negative diff
367
377
extents = tool .extents
@@ -370,8 +380,8 @@ def test_rectangle_resize_square(ax, add_state):
370
380
xdata_new , ydata_new = xdata + xdiff , ydata
371
381
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
372
382
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 ) )
375
385
376
386
# resize SW handle
377
387
extents = tool .extents
@@ -380,8 +390,8 @@ def test_rectangle_resize_square(ax, add_state):
380
390
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
381
391
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
382
392
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 ]) )
385
395
386
396
387
397
def test_rectangle_resize_square_center (ax ):
@@ -453,7 +463,7 @@ def test_rectangle_rotate(ax, selector_class):
453
463
tool = selector_class (ax , interactive = True )
454
464
# Draw rectangle
455
465
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 ) )
457
467
assert len (tool ._state ) == 0
458
468
459
469
# Rotate anticlockwise using top-right corner
@@ -486,7 +496,7 @@ def test_rectangle_add_remove_set(ax):
486
496
tool = widgets .RectangleSelector (ax , interactive = True )
487
497
# Draw rectangle
488
498
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 ) )
490
500
assert len (tool ._state ) == 0
491
501
for state in ['rotate' , 'square' , 'center' ]:
492
502
tool .add_state (state )
@@ -503,15 +513,15 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
503
513
use_data_coordinates = use_data_coordinates )
504
514
# Create rectangle
505
515
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 ) )
507
517
tool .add_state ('square' )
508
518
tool .add_state ('center' )
509
519
510
520
if use_data_coordinates :
511
521
# resize E handle
512
522
extents = tool .extents
513
523
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
515
525
xdata_new , ydata_new = xdata + xdiff , ydata
516
526
ychange = width / 2 + xdiff
517
527
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
@@ -536,24 +546,22 @@ def test_ellipse(ax):
536
546
537
547
# drag the rectangle
538
548
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 ) )
540
550
541
551
# create from center
542
552
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 ) )
544
554
545
555
# create a square
546
556
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 ))
549
558
550
559
# create a square from center
551
560
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 ))
554
562
555
563
assert tool .geometry .shape == (2 , 73 )
556
- assert_allclose (tool .geometry [:, 0 ], [ 70. , 100 ] )
564
+ assert_allclose (tool .geometry [:, 0 ], ( 70 , 100 ) )
557
565
558
566
559
567
def test_rectangle_handles (ax ):
@@ -563,22 +571,22 @@ def test_rectangle_handles(ax):
563
571
tool .extents = (100 , 150 , 100 , 150 )
564
572
565
573
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 ) )
567
575
assert_allclose (tool .edge_centers ,
568
576
((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 ) )
570
578
571
579
# grab a corner and move it
572
580
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 ) )
574
582
575
583
# grab the center and move it
576
584
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 ) )
578
586
579
587
# create a new rectangle
580
588
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 ) )
582
590
583
591
# Check that marker_props worked.
584
592
assert mcolors .same_color (
@@ -597,7 +605,7 @@ def test_rectangle_selector_onselect(ax, interactive):
597
605
click_and_drag (tool , start = (100 , 110 ), end = (150 , 120 ))
598
606
599
607
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 ) )
601
609
602
610
onselect .reset_mock ()
603
611
click_and_drag (tool , start = (10 , 100 ), end = (10 , 100 ))
@@ -612,19 +620,19 @@ def test_rectangle_selector_ignore_outside(ax, ignore_event_outside):
612
620
ignore_event_outside = ignore_event_outside )
613
621
click_and_drag (tool , start = (100 , 110 ), end = (150 , 120 ))
614
622
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 ) )
616
624
617
625
onselect .reset_mock ()
618
626
# Trigger event outside of span
619
627
click_and_drag (tool , start = (150 , 150 ), end = (160 , 160 ))
620
628
if ignore_event_outside :
621
629
# event have been ignored and span haven't changed.
622
630
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 ) )
624
632
else :
625
633
# A new shape is created
626
634
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 ) )
628
636
629
637
630
638
@pytest .mark .parametrize ('orientation, onmove_callback, kwargs' , [
0 commit comments