12
12
mock_event , noop )
13
13
14
14
import numpy as np
15
- from numpy .testing import assert_allclose
16
15
17
16
import pytest
18
17
@@ -22,6 +21,13 @@ def ax():
22
21
return get_ax ()
23
22
24
23
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
+
25
31
def test_save_blitted_widget_as_pdf ():
26
32
from matplotlib .widgets import CheckButtons , RadioButtons
27
33
from matplotlib .cbook import _get_running_interactive_framework
@@ -138,7 +144,7 @@ def test_rectangle_drag(ax, drag_from_anywhere, new_center):
138
144
drag_from_anywhere = drag_from_anywhere )
139
145
# Create rectangle
140
146
click_and_drag (tool , start = (0 , 10 ), end = (100 , 120 ))
141
- assert tool .center == (50 , 65 )
147
+ assert_allclose ( tool .center , (50 , 65 ) )
142
148
# Drag inside rectangle, but away from centre handle
143
149
#
144
150
# 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):
147
153
# If drag_from_anywhere == False, this will create a new rectangle with
148
154
# center (30, 20)
149
155
click_and_drag (tool , start = (25 , 15 ), end = (35 , 25 ))
150
- assert tool .center == new_center
156
+ assert_allclose ( tool .center , new_center )
151
157
# Check that in both cases, dragging outside the rectangle draws a new
152
158
# rectangle
153
159
click_and_drag (tool , start = (175 , 185 ), end = (185 , 195 ))
154
- assert tool .center == (180 , 190 )
160
+ assert_allclose ( tool .center , (180 , 190 ) )
155
161
156
162
157
163
def test_rectangle_selector_set_props_handle_props (ax ):
@@ -179,35 +185,39 @@ def test_rectangle_resize(ax):
179
185
tool = widgets .RectangleSelector (ax , interactive = True )
180
186
# Create rectangle
181
187
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 ) )
183
189
184
190
# resize NE handle
185
191
extents = tool .extents
186
192
xdata , ydata = extents [1 ], extents [3 ]
187
193
xdata_new , ydata_new = xdata + 10 , ydata + 5
188
194
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 ))
190
197
191
198
# resize E handle
192
199
extents = tool .extents
193
200
xdata , ydata = extents [1 ], extents [2 ] + (extents [3 ] - extents [2 ]) / 2
194
201
xdata_new , ydata_new = xdata + 10 , ydata
195
202
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 ]))
197
205
198
206
# resize W handle
199
207
extents = tool .extents
200
208
xdata , ydata = extents [0 ], extents [2 ] + (extents [3 ] - extents [2 ]) / 2
201
209
xdata_new , ydata_new = xdata + 15 , ydata
202
210
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 ]))
204
213
205
214
# resize SW handle
206
215
extents = tool .extents
207
216
xdata , ydata = extents [0 ], extents [2 ]
208
217
xdata_new , ydata_new = xdata + 20 , ydata + 25
209
218
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 ]))
211
221
212
222
213
223
def test_rectangle_add_state (ax ):
@@ -245,8 +255,8 @@ def test_rectangle_resize_center(ax, add_state):
245
255
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
246
256
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
247
257
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 ) )
250
260
251
261
# resize E handle
252
262
extents = tool .extents
@@ -255,8 +265,8 @@ def test_rectangle_resize_center(ax, add_state):
255
265
xdata_new , ydata_new = xdata + xdiff , ydata
256
266
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
257
267
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 ]) )
260
270
261
271
# resize E handle negative diff
262
272
extents = tool .extents
@@ -265,8 +275,8 @@ def test_rectangle_resize_center(ax, add_state):
265
275
xdata_new , ydata_new = xdata + xdiff , ydata
266
276
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
267
277
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 ]) )
270
280
271
281
# resize W handle
272
282
extents = tool .extents
@@ -275,8 +285,8 @@ def test_rectangle_resize_center(ax, add_state):
275
285
xdata_new , ydata_new = xdata + xdiff , ydata
276
286
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
277
287
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 ]) )
280
290
281
291
# resize W handle negative diff
282
292
extents = tool .extents
@@ -285,8 +295,8 @@ def test_rectangle_resize_center(ax, add_state):
285
295
xdata_new , ydata_new = xdata + xdiff , ydata
286
296
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
287
297
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 ]) )
290
300
291
301
# resize SW handle
292
302
extents = tool .extents
@@ -329,8 +339,8 @@ def test_rectangle_resize_square(ax, add_state):
329
339
xdata_new , ydata_new = xdata + xdiff , ydata
330
340
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
331
341
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 ) )
334
344
335
345
# resize E handle negative diff
336
346
extents = tool .extents
@@ -339,8 +349,8 @@ def test_rectangle_resize_square(ax, add_state):
339
349
xdata_new , ydata_new = xdata + xdiff , ydata
340
350
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
341
351
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 ) )
344
354
345
355
# resize W handle
346
356
extents = tool .extents
@@ -349,8 +359,8 @@ def test_rectangle_resize_square(ax, add_state):
349
359
xdata_new , ydata_new = xdata + xdiff , ydata
350
360
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
351
361
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 ) )
354
364
355
365
# resize W handle negative diff
356
366
extents = tool .extents
@@ -359,8 +369,8 @@ def test_rectangle_resize_square(ax, add_state):
359
369
xdata_new , ydata_new = xdata + xdiff , ydata
360
370
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
361
371
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 ) )
364
374
365
375
# resize SW handle
366
376
extents = tool .extents
@@ -369,8 +379,8 @@ def test_rectangle_resize_square(ax, add_state):
369
379
xdata_new , ydata_new = xdata + xdiff , ydata + ydiff
370
380
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ),
371
381
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 ]) )
374
384
375
385
376
386
def test_rectangle_resize_square_center (ax ):
@@ -442,7 +452,7 @@ def test_rectangle_rotate(ax, selector_class):
442
452
tool = selector_class (ax , interactive = True )
443
453
# Draw rectangle
444
454
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 ) )
446
456
assert len (tool ._state ) == 0
447
457
448
458
# Rotate anticlockwise using top-right corner
@@ -475,7 +485,7 @@ def test_rectangle_add_remove_set(ax):
475
485
tool = widgets .RectangleSelector (ax , interactive = True )
476
486
# Draw rectangle
477
487
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 ) )
479
489
assert len (tool ._state ) == 0
480
490
for state in ['rotate' , 'square' , 'center' ]:
481
491
tool .add_state (state )
@@ -492,15 +502,15 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
492
502
use_data_coordinates = use_data_coordinates )
493
503
# Create rectangle
494
504
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 ) )
496
506
tool .add_state ('square' )
497
507
tool .add_state ('center' )
498
508
499
509
if use_data_coordinates :
500
510
# resize E handle
501
511
extents = tool .extents
502
512
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
504
514
xdata_new , ydata_new = xdata + xdiff , ydata
505
515
ychange = width / 2 + xdiff
506
516
click_and_drag (tool , start = (xdata , ydata ), end = (xdata_new , ydata_new ))
@@ -525,24 +535,22 @@ def test_ellipse(ax):
525
535
526
536
# drag the rectangle
527
537
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 ) )
529
539
530
540
# create from center
531
541
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 ) )
533
543
534
544
# create a square
535
545
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 ))
538
547
539
548
# create a square from center
540
549
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 ))
543
551
544
552
assert tool .geometry .shape == (2 , 73 )
545
- assert_allclose (tool .geometry [:, 0 ], [ 70. , 100 ] )
553
+ assert_allclose (tool .geometry [:, 0 ], ( 70 , 100 ) )
546
554
547
555
548
556
def test_rectangle_handles (ax ):
@@ -552,22 +560,22 @@ def test_rectangle_handles(ax):
552
560
tool .extents = (100 , 150 , 100 , 150 )
553
561
554
562
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 ) )
556
564
assert_allclose (tool .edge_centers ,
557
565
((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 ) )
559
567
560
568
# grab a corner and move it
561
569
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 ) )
563
571
564
572
# grab the center and move it
565
573
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 ) )
567
575
568
576
# create a new rectangle
569
577
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 ) )
571
579
572
580
# Check that marker_props worked.
573
581
assert mcolors .same_color (
@@ -586,7 +594,7 @@ def test_rectangle_selector_onselect(ax, interactive):
586
594
click_and_drag (tool , start = (100 , 110 ), end = (150 , 120 ))
587
595
588
596
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 ) )
590
598
591
599
onselect .reset_mock ()
592
600
click_and_drag (tool , start = (10 , 100 ), end = (10 , 100 ))
@@ -601,19 +609,19 @@ def test_rectangle_selector_ignore_outside(ax, ignore_event_outside):
601
609
ignore_event_outside = ignore_event_outside )
602
610
click_and_drag (tool , start = (100 , 110 ), end = (150 , 120 ))
603
611
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 ) )
605
613
606
614
onselect .reset_mock ()
607
615
# Trigger event outside of span
608
616
click_and_drag (tool , start = (150 , 150 ), end = (160 , 160 ))
609
617
if ignore_event_outside :
610
618
# event have been ignored and span haven't changed.
611
619
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 ) )
613
621
else :
614
622
# A new shape is created
615
623
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 ) )
617
625
618
626
619
627
@pytest .mark .parametrize ('orientation, onmove_callback, kwargs' , [
0 commit comments