def test_selector_clear_method(selector): ax = get_ax() def onselect(*args): pass if selector == 'span': tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True, ignore_event_outside=True) else: tool = widgets.RectangleSelector(ax, onselect, interactive=True) click_and_drag(tool, start=(10, 10), end=(100, 120)) assert tool._selection_completed assert tool.visible if selector == 'span': assert tool.extents == (10, 100) tool.clear() assert not tool._selection_completed assert not tool.visible # Do another cycle of events to make sure we can click_and_drag(tool, start=(10, 10), end=(50, 120)) assert tool._selection_completed assert tool.visible if selector == 'span': assert tool.extents == (10, 50)
def test_span_selector_ignore_outside(ignore_event_outside): ax = get_ax() def onselect(vmin, vmax): ax._got_onselect = True def onmove(vmin, vmax): ax._got_on_move = True tool = widgets.SpanSelector(ax, onselect, 'horizontal', onmove_callback=onmove, ignore_event_outside=ignore_event_outside) click_and_drag(tool, start=(100, 100), end=(125, 125)) assert ax._got_onselect assert ax._got_on_move assert tool.extents == (100, 125) # Reset ax._got_onselect = False ax._got_on_move = False # Trigger event outside of span click_and_drag(tool, start=(150, 150), end=(160, 160)) if ignore_event_outside: # event have been ignored and span haven't changed. assert not ax._got_onselect assert not ax._got_on_move assert tool.extents == (100, 125) else: # A new shape is created assert ax._got_onselect assert ax._got_on_move assert tool.extents == (150, 160)
def test_span_selector_bound(direction): fig, ax = plt.subplots(1, 1) ax.plot([10, 20], [10, 30]) ax.figure.canvas.draw() x_bound = ax.get_xbound() y_bound = ax.get_ybound() tool = widgets.SpanSelector(ax, print, direction, interactive=True) assert ax.get_xbound() == x_bound assert ax.get_ybound() == y_bound bound = x_bound if direction == 'horizontal' else y_bound assert tool._edge_handles.positions == list(bound) press_data = [10.5, 11.5] move_data = [11, 13] # Updating selector is done in onmove release_data = move_data click_and_drag(tool, start=press_data, end=move_data) assert ax.get_xbound() == x_bound assert ax.get_ybound() == y_bound index = 0 if direction == 'horizontal' else 1 handle_positions = [press_data[index], release_data[index]] assert tool._edge_handles.positions == handle_positions
def test_ellipse(): """For ellipse, test out the key modifiers""" ax = get_ax() def onselect(epress, erelease): pass tool = widgets.EllipseSelector(ax, onselect=onselect, grab_range=10, interactive=True) tool.extents = (100, 150, 100, 150) # drag the rectangle click_and_drag(tool, start=(125, 125), end=(145, 145)) assert tool.extents == (120, 170, 120, 170) # create from center click_and_drag(tool, start=(100, 100), end=(125, 125), key='control') assert tool.extents == (75, 125, 75, 125) # create a square click_and_drag(tool, start=(10, 10), end=(35, 30), key='shift') extents = [int(e) for e in tool.extents] assert extents == [10, 35, 10, 35] # create a square from center click_and_drag(tool, start=(100, 100), end=(125, 130), key='ctrl+shift') extents = [int(e) for e in tool.extents] assert extents == [70, 130, 70, 130] assert tool.geometry.shape == (2, 73) assert_allclose(tool.geometry[:, 0], [70., 100])
def test_selector_clear(selector): ax = get_ax() def onselect(*args): pass kwargs = dict(ax=ax, onselect=onselect, interactive=True) if selector == 'span': Selector = widgets.SpanSelector kwargs['direction'] = 'horizontal' else: Selector = widgets.RectangleSelector tool = Selector(**kwargs) click_and_drag(tool, start=(10, 10), end=(100, 120)) # press-release event outside the selector to clear the selector click_and_drag(tool, start=(130, 130), end=(130, 130)) assert not tool._selection_completed ax = get_ax() kwargs['ignore_event_outside'] = True tool = Selector(**kwargs) assert tool.ignore_event_outside click_and_drag(tool, start=(10, 10), end=(100, 120)) # press-release event outside the selector ignored click_and_drag(tool, start=(130, 130), end=(130, 130)) assert tool._selection_completed do_event(tool, 'on_key_press', key='escape') assert not tool._selection_completed
def test_rectange_add_remove_set(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect=onselect, interactive=True) # Draw rectangle click_and_drag(tool, start=(100, 100), end=(130, 140)) assert tool.extents == (100, 130, 100, 140) assert len(tool._state) == 0 for state in ['rotate', 'square', 'center']: tool.add_state(state) assert len(tool._state) == 1 tool.remove_state(state) assert len(tool._state) == 0
def test_rectangle_add_state(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True) # Create rectangle click_and_drag(tool, start=(70, 65), end=(125, 130)) with pytest.raises(ValueError): tool.add_state('unsupported_state') with pytest.raises(ValueError): tool.add_state('clear') tool.add_state('move') tool.add_state('square') tool.add_state('center')
def test_span_selector_onselect(interactive): # check when press and release events take place at the same position ax = get_ax() def onselect(vmin, vmax): ax._got_onselect = True tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=interactive) # move outside of axis click_and_drag(tool, start=(100, 100), end=(150, 100)) assert tool.ax._got_onselect assert tool.extents == (100, 150) # Reset tool.ax._got_onselect tool.ax._got_onselect = False click_and_drag(tool, start=(10, 100), end=(10, 100)) assert tool.ax._got_onselect
def test_rectangle_resize_square_center_aspect(use_data_coordinates): ax = get_ax() ax.set_aspect(0.8) def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True, use_data_coordinates=use_data_coordinates) # Create rectangle click_and_drag(tool, start=(70, 65), end=(120, 115)) assert tool.extents == (70.0, 120.0, 65.0, 115.0) tool.add_state('square') tool.add_state('center') if use_data_coordinates: # resize E handle extents = tool.extents xdata, ydata, width = extents[1], extents[3], extents[1] - extents[0] xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2 xdata_new, ydata_new = xdata + xdiff, ydata ychange = width / 2 + xdiff click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new, ycenter - ychange, ycenter + ychange]) else: # resize E handle extents = tool.extents xdata, ydata = extents[1], extents[3] xdiff = 10 xdata_new, ydata_new = xdata + xdiff, ydata ychange = xdiff * 1 / tool._aspect_ratio_correction click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new, 46.25, 133.75])
def test_span_selector_drag(drag_from_anywhere): ax = get_ax() def onselect(*args): pass # Create span tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True, drag_from_anywhere=drag_from_anywhere) click_and_drag(tool, start=(10, 10), end=(100, 120)) assert tool.extents == (10, 100) # Drag inside span # # If drag_from_anywhere == True, this will move the span by 10, # giving new value extents = 20, 110 # # If drag_from_anywhere == False, this will create a new span with # value extents = 25, 35 click_and_drag(tool, start=(25, 15), end=(35, 25)) if drag_from_anywhere: assert tool.extents == (20, 110) else: assert tool.extents == (25, 35) # Check that in both cases, dragging outside the span draws a new span click_and_drag(tool, start=(175, 185), end=(185, 195)) assert tool.extents == (175, 185)
def test_rectangle_resize(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True) # Create rectangle click_and_drag(tool, start=(0, 10), end=(100, 120)) assert tool.extents == (0.0, 100.0, 10.0, 120.0) # resize NE handle extents = tool.extents xdata, ydata = extents[1], extents[3] xdata_new, ydata_new = xdata + 10, ydata + 5 click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert tool.extents == (extents[0], xdata_new, extents[2], ydata_new) # resize E handle extents = tool.extents xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2 xdata_new, ydata_new = xdata + 10, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert tool.extents == (extents[0], xdata_new, extents[2], extents[3]) # resize W handle extents = tool.extents xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2 xdata_new, ydata_new = xdata + 15, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert tool.extents == (xdata_new, extents[1], extents[2], extents[3]) # resize SW handle extents = tool.extents xdata, ydata = extents[0], extents[2] xdata_new, ydata_new = xdata + 20, ydata + 25 click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert tool.extents == (xdata_new, extents[1], ydata_new, extents[3])
def test_rectangle_selector_ignore_outside(ignore_event_outside): ax = get_ax() def onselect(vmin, vmax): ax._got_onselect = True tool = widgets.RectangleSelector(ax, onselect, ignore_event_outside=ignore_event_outside) click_and_drag(tool, start=(100, 110), end=(150, 120)) assert tool.ax._got_onselect assert tool.extents == (100.0, 150.0, 110.0, 120.0) # Reset ax._got_onselect = False # Trigger event outside of span click_and_drag(tool, start=(150, 150), end=(160, 160)) if ignore_event_outside: # event have been ignored and span haven't changed. assert not ax._got_onselect assert tool.extents == (100.0, 150.0, 110.0, 120.0) else: # A new shape is created assert ax._got_onselect assert tool.extents == (150.0, 160.0, 150.0, 160.0)
def test_rectangle_selector_set_props_handle_props(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True, props=dict(facecolor='b', alpha=0.2), handle_props=dict(alpha=0.5)) # Create rectangle click_and_drag(tool, start=(0, 10), end=(100, 120)) artist = tool._selection_artist assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2) tool.set_props(facecolor='r', alpha=0.3) assert artist.get_facecolor() == mcolors.to_rgba('r', alpha=0.3) for artist in tool._handles_artists: assert artist.get_markeredgecolor() == 'black' assert artist.get_alpha() == 0.5 tool.set_handle_props(markeredgecolor='r', alpha=0.3) for artist in tool._handles_artists: assert artist.get_markeredgecolor() == 'r' assert artist.get_alpha() == 0.3
def test_rectangle_rotate(selector_class): ax = get_ax() def onselect(epress, erelease): pass tool = selector_class(ax, onselect=onselect, interactive=True) # Draw rectangle click_and_drag(tool, start=(100, 100), end=(130, 140)) assert tool.extents == (100, 130, 100, 140) assert len(tool._state) == 0 # Rotate anticlockwise using top-right corner do_event(tool, 'on_key_press', key='r') assert tool._state == set(['rotate']) assert len(tool._state) == 1 click_and_drag(tool, start=(130, 140), end=(120, 145)) do_event(tool, 'on_key_press', key='r') assert len(tool._state) == 0 # Extents shouldn't change (as shape of rectangle hasn't changed) assert tool.extents == (100, 130, 100, 140) assert_allclose(tool.rotation, 25.56, atol=0.01) tool.rotation = 45 assert tool.rotation == 45 # Corners should move assert_allclose(tool.corners, np.array([[118.53, 139.75, 111.46, 90.25], [95.25, 116.46, 144.75, 123.54]]), atol=0.01) # Scale using top-right corner click_and_drag(tool, start=(110, 145), end=(110, 160)) assert_allclose(tool.extents, (100, 139.75, 100, 151.82), atol=0.01) if selector_class == widgets.RectangleSelector: with pytest.raises(ValueError): tool._selection_artist.rotation_point = 'unvalid_value'
def test_rectangle_handles(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect=onselect, grab_range=10, interactive=True, handle_props={'markerfacecolor': 'r', 'markeredgecolor': 'b'}) tool.extents = (100, 150, 100, 150) assert_allclose(tool.corners, ((100, 150, 150, 100), (100, 100, 150, 150))) assert tool.extents == (100, 150, 100, 150) assert_allclose(tool.edge_centers, ((100, 125.0, 150, 125.0), (125.0, 100, 125.0, 150))) assert tool.extents == (100, 150, 100, 150) # grab a corner and move it click_and_drag(tool, start=(100, 100), end=(120, 120)) assert tool.extents == (120, 150, 120, 150) # grab the center and move it click_and_drag(tool, start=(132, 132), end=(120, 120)) assert tool.extents == (108, 138, 108, 138) # create a new rectangle click_and_drag(tool, start=(10, 10), end=(100, 100)) assert tool.extents == (10, 100, 10, 100) # Check that marker_props worked. assert mcolors.same_color( tool._corner_handles.artists[0].get_markerfacecolor(), 'r') assert mcolors.same_color( tool._corner_handles.artists[0].get_markeredgecolor(), 'b')
def test_rectangle_drag(drag_from_anywhere, new_center): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True, drag_from_anywhere=drag_from_anywhere) # Create rectangle click_and_drag(tool, start=(0, 10), end=(100, 120)) assert tool.center == (50, 65) # Drag inside rectangle, but away from centre handle # # If drag_from_anywhere == True, this will move the rectangle by (10, 10), # giving it a new center of (60, 75) # # If drag_from_anywhere == False, this will create a new rectangle with # center (30, 20) click_and_drag(tool, start=(25, 15), end=(35, 25)) assert tool.center == new_center # Check that in both cases, dragging outside the rectangle draws a new # rectangle click_and_drag(tool, start=(175, 185), end=(185, 195)) assert tool.center == (180, 190)
def test_rectangle_resize_square(add_state): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True) # Create rectangle click_and_drag(tool, start=(70, 65), end=(120, 115)) assert tool.extents == (70.0, 120.0, 65.0, 115.0) if add_state: tool.add_state('square') use_key = None else: use_key = 'shift' # resize NE handle extents = tool.extents xdata, ydata = extents[1], extents[3] xdiff, ydiff = 10, 5 xdata_new, ydata_new = xdata + xdiff, ydata + ydiff click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), key=use_key) assert tool.extents == (extents[0], xdata_new, extents[2], extents[3] + xdiff) # resize E handle extents = tool.extents xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2 xdiff = 10 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), key=use_key) assert tool.extents == (extents[0], xdata_new, extents[2], extents[3] + xdiff) # resize E handle negative diff extents = tool.extents xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2 xdiff = -20 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), key=use_key) assert tool.extents == (extents[0], xdata_new, extents[2], extents[3] + xdiff) # resize W handle extents = tool.extents xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2 xdiff = 15 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), key=use_key) assert tool.extents == (xdata_new, extents[1], extents[2], extents[3] - xdiff) # resize W handle negative diff extents = tool.extents xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2 xdiff = -25 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), key=use_key) assert tool.extents == (xdata_new, extents[1], extents[2], extents[3] - xdiff) # resize SW handle extents = tool.extents xdata, ydata = extents[0], extents[2] xdiff, ydiff = 20, 25 xdata_new, ydata_new = xdata + xdiff, ydata + ydiff click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new), key=use_key) assert tool.extents == (extents[0] + ydiff, extents[1], ydata_new, extents[3])
def test_rectangle_resize_square_center(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect, interactive=True) # Create rectangle click_and_drag(tool, start=(70, 65), end=(120, 115)) tool.add_state('square') tool.add_state('center') assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0)) # resize NE handle extents = tool.extents xdata, ydata = extents[1], extents[3] xdiff, ydiff = 10, 5 xdata_new, ydata_new = xdata + xdiff, ydata + ydiff click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new, extents[2] - xdiff, extents[3] + xdiff)) # resize E handle extents = tool.extents xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2 xdiff = 10 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new, extents[2] - xdiff, extents[3] + xdiff)) # resize E handle negative diff extents = tool.extents xdata, ydata = extents[1], extents[2] + (extents[3] - extents[2]) / 2 xdiff = -20 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, (extents[0] - xdiff, xdata_new, extents[2] - xdiff, extents[3] + xdiff)) # resize W handle extents = tool.extents xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2 xdiff = 5 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff, extents[2] + xdiff, extents[3] - xdiff)) # resize W handle negative diff extents = tool.extents xdata, ydata = extents[0], extents[2] + (extents[3] - extents[2]) / 2 xdiff = -25 xdata_new, ydata_new = xdata + xdiff, ydata click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff, extents[2] + xdiff, extents[3] - xdiff)) # resize SW handle extents = tool.extents xdata, ydata = extents[0], extents[2] xdiff, ydiff = 20, 25 xdata_new, ydata_new = xdata + xdiff, ydata + ydiff click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new)) assert_allclose(tool.extents, (extents[0] + ydiff, extents[1] - ydiff, ydata_new, extents[3] - ydiff))