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_polygon_selector_set_props_handle_props(): ax = get_ax() ax._selections_count = 0 def onselect(vertices): ax._selections_count += 1 ax._current_result = vertices tool = widgets.PolygonSelector(ax, onselect, props=dict(color='b', alpha=0.2), handle_props=dict(alpha=0.5)) event_sequence = (polygon_place_vertex(50, 50) + polygon_place_vertex(150, 50) + polygon_place_vertex(50, 150) + polygon_place_vertex(50, 50)) for (etype, event_args) in event_sequence: do_event(tool, etype, **event_args) artist = tool._selection_artist assert artist.get_color() == 'b' assert artist.get_alpha() == 0.2 tool.set_props(color='r', alpha=0.3) assert artist.get_color() == 'r' assert artist.get_alpha() == 0.3 for artist in tool._handles_artists: assert artist.get_color() == 'b' assert artist.get_alpha() == 0.5 tool.set_handle_props(color='r', alpha=0.3) for artist in tool._handles_artists: assert artist.get_color() == 'r' assert artist.get_alpha() == 0.3
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) do_event(tool, 'press', xdata=100, ydata=100, button=1) # move outside of axis do_event(tool, 'onmove', xdata=150, ydata=100, button=1) do_event(tool, 'release', xdata=150, ydata=100, button=1) assert tool.ax._got_onselect assert tool.extents == (100, 150) # Reset tool.ax._got_onselect tool.ax._got_onselect = False do_event(tool, 'press', xdata=10, ydata=100, button=1) do_event(tool, 'release', xdata=10, ydata=100, button=1) assert tool.ax._got_onselect
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 do_event(tool, 'press', xdata=press_data[0], ydata=press_data[1], button=1) do_event(tool, 'onmove', xdata=move_data[0], ydata=move_data[1], button=1) 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 check_polygon_selector(event_sequence, expected_result, selections_count): """ Helper function to test Polygon Selector. Parameters ---------- event_sequence : list of tuples (etype, dict()) A sequence of events to perform. The sequence is a list of tuples where the first element of the tuple is an etype (e.g., 'onmove', 'press', etc.), and the second element of the tuple is a dictionary of the arguments for the event (e.g., xdata=5, key='shift', etc.). expected_result : list of vertices (xdata, ydata) The list of vertices that are expected to result from the event sequence. selections_count : int Wait for the tool to call its `onselect` function `selections_count` times, before comparing the result to the `expected_result` """ ax = get_ax() ax._selections_count = 0 def onselect(vertices): ax._selections_count += 1 ax._current_result = vertices tool = widgets.PolygonSelector(ax, onselect) for (etype, event_args) in event_sequence: do_event(tool, etype, **event_args) assert ax._selections_count == selections_count assert ax._current_result == expected_result
def _resize_rectangle(tool, xdata, ydata, xdata_new, ydata_new, use_key=None): do_event(tool, 'press', xdata=xdata, ydata=ydata, button=1) if use_key is not None: do_event(tool, 'on_key_press', key=use_key) do_event(tool, 'onmove', xdata=xdata_new, ydata=ydata_new, button=1) if use_key is not None: do_event(tool, 'on_key_release', key=use_key) do_event(tool, 'release', xdata=xdata_new, ydata=ydata_new, button=1) return tool
def test_TextBox(): from unittest.mock import Mock submit_event = Mock() text_change_event = Mock() ax = get_ax() tool = widgets.TextBox(ax, '') tool.on_submit(submit_event) tool.on_text_change(text_change_event) assert tool.text == '' do_event(tool, '_click') tool.set_val('x**2') assert tool.text == 'x**2' assert text_change_event.call_count == 1 tool.begin_typing(tool.text) tool.stop_typing() assert submit_event.call_count == 2 do_event(tool, '_click') do_event(tool, '_keypress', key='+') do_event(tool, '_keypress', key='5') assert text_change_event.call_count == 3
def test_TextBox(toolbar): # Avoid "toolmanager is provisional" warning. dict.__setitem__(plt.rcParams, "toolbar", toolbar) from unittest.mock import Mock submit_event = Mock() text_change_event = Mock() ax = get_ax() tool = widgets.TextBox(ax, '') tool.on_submit(submit_event) tool.on_text_change(text_change_event) assert tool.text == '' do_event(tool, '_click') tool.set_val('x**2') assert tool.text == 'x**2' assert text_change_event.call_count == 1 tool.begin_typing(tool.text) tool.stop_typing() assert submit_event.call_count == 2 do_event(tool, '_click') do_event(tool, '_keypress', key='+') do_event(tool, '_keypress', key='5') assert text_change_event.call_count == 3
def test_polygon_selector_verts_setter(fig_test, fig_ref): verts = [(0.1, 0.4), (0.5, 0.9), (0.3, 0.2)] ax_test = fig_test.add_subplot() def onselect(vertices): pass tool_test = widgets.PolygonSelector(ax_test, onselect) tool_test.verts = verts assert tool_test.verts == verts ax_ref = fig_ref.add_subplot() tool_ref = widgets.PolygonSelector(ax_ref, onselect) event_sequence = (polygon_place_vertex(*verts[0]) + polygon_place_vertex(*verts[1]) + polygon_place_vertex(*verts[2]) + polygon_place_vertex(*verts[0])) for (etype, event_args) in event_sequence: do_event(tool_ref, etype, **event_args)
def check_span(*args, **kwargs): ax = get_ax() def onselect(vmin, vmax): ax._got_onselect = True assert vmin == 100 assert vmax == 150 def onmove(vmin, vmax): assert vmin == 100 assert vmax == 125 ax._got_on_move = True if 'onmove_callback' in kwargs: kwargs['onmove_callback'] = onmove tool = widgets.SpanSelector(ax, onselect, *args, **kwargs) do_event(tool, 'press', xdata=100, ydata=100, button=1) do_event(tool, 'onmove', xdata=125, ydata=125, button=1) do_event(tool, 'release', xdata=150, ydata=150, button=1) assert ax._got_onselect if 'onmove_callback' in kwargs: assert ax._got_on_move
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 do_event(tool, 'press', xdata=0, ydata=10, button=1) do_event(tool, 'onmove', xdata=100, ydata=120, button=1) do_event(tool, 'release', xdata=100, ydata=120, button=1) 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_polygon_selector_redraw(): verts = [(50, 50), (150, 50), (50, 150)] event_sequence = ( polygon_place_vertex(*verts[0]) + polygon_place_vertex(*verts[1]) + polygon_place_vertex(*verts[2]) + polygon_place_vertex(*verts[0]) + # Polygon completed, now remove first two verts polygon_remove_vertex(*verts[1]) + polygon_remove_vertex(*verts[2]) + # At this point the tool should be reset so we can add # more vertices polygon_place_vertex(*verts[1])) ax = get_ax() def onselect(vertices): pass tool = widgets.PolygonSelector(ax, onselect) for (etype, event_args) in event_sequence: do_event(tool, etype, **event_args) # After removing two verts, only one remains, and the # selector should be automatically resete assert tool.verts == verts[0:2]
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 check_lasso_selector(**kwargs): ax = get_ax() def onselect(verts): ax._got_onselect = True assert verts == [(100, 100), (125, 125), (150, 150)] tool = widgets.LassoSelector(ax, onselect, **kwargs) do_event(tool, 'press', xdata=100, ydata=100, button=1) do_event(tool, 'onmove', xdata=125, ydata=125, button=1) do_event(tool, 'release', xdata=150, ydata=150, button=1) assert ax._got_onselect
def check_rectangle(**kwargs): ax = get_ax() def onselect(epress, erelease): ax._got_onselect = True assert epress.xdata == 100 assert epress.ydata == 100 assert erelease.xdata == 199 assert erelease.ydata == 199 tool = widgets.RectangleSelector(ax, onselect, **kwargs) do_event(tool, 'press', xdata=100, ydata=100, button=1) do_event(tool, 'onmove', xdata=199, ydata=199, button=1) # purposely drag outside of axis for release do_event(tool, 'release', xdata=250, ydata=250, button=1) if kwargs.get('drawtype', None) not in ['line', 'none']: assert_allclose( tool.geometry, [[100., 100, 199, 199, 100], [100, 199, 199, 100, 100]], err_msg=tool.geometry) assert ax._got_onselect
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) do_event(tool, 'press', xdata=10, ydata=10, button=1) do_event(tool, 'onmove', xdata=100, ydata=120, button=1) do_event(tool, 'release', xdata=100, ydata=120, button=1) 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 do_event(tool, 'press', xdata=25, ydata=15, button=1) do_event(tool, 'onmove', xdata=35, ydata=25, button=1) do_event(tool, 'release', xdata=35, ydata=25, button=1) 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 do_event(tool, 'press', xdata=175, ydata=185, button=1) do_event(tool, 'onmove', xdata=185, ydata=195, button=1) do_event(tool, 'release', xdata=185, ydata=195, button=1) assert tool.extents == (175, 185)
def test_rectangle_handles(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect=onselect, maxdist=10, interactive=True, marker_props={ 'markerfacecolor': 'r', 'markeredgecolor': 'b' }) tool.extents = (100, 150, 100, 150) assert tool.corners == ((100, 150, 150, 100), (100, 100, 150, 150)) assert tool.extents == (100, 150, 100, 150) assert 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 do_event(tool, 'press', xdata=100, ydata=100) do_event(tool, 'onmove', xdata=120, ydata=120) do_event(tool, 'release', xdata=120, ydata=120) assert tool.extents == (120, 150, 120, 150) # grab the center and move it do_event(tool, 'press', xdata=132, ydata=132) do_event(tool, 'onmove', xdata=120, ydata=120) do_event(tool, 'release', xdata=120, ydata=120) assert tool.extents == (108, 138, 108, 138) # create a new rectangle do_event(tool, 'press', xdata=10, ydata=10) do_event(tool, 'onmove', xdata=100, ydata=100) do_event(tool, 'release', xdata=100, ydata=100) assert tool.extents == (10, 100, 10, 100) # Check that marker_props worked. assert mcolors.same_color( tool._corner_handles.artist.get_markerfacecolor(), 'r') assert mcolors.same_color( tool._corner_handles.artist.get_markeredgecolor(), 'b')
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) do_event(tool, 'press', xdata=10, ydata=10, button=1) do_event(tool, 'onmove', xdata=100, ydata=120, button=1) do_event(tool, 'release', xdata=100, ydata=120, button=1) 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 do_event(tool, 'press', xdata=10, ydata=10, button=1) do_event(tool, 'onmove', xdata=50, ydata=120, button=1) do_event(tool, 'release', xdata=50, ydata=120, button=1) assert tool._selection_completed assert tool.visible if selector == 'span': assert tool.extents == (10, 50)
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 do_event(tool, 'press', xdata=0, ydata=10, button=1) do_event(tool, 'onmove', xdata=100, ydata=120, button=1) do_event(tool, 'release', xdata=100, ydata=120, button=1) 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) do_event(tool, 'press', xdata=25, ydata=15, button=1) do_event(tool, 'onmove', xdata=35, ydata=25, button=1) do_event(tool, 'release', xdata=35, ydata=25, button=1) assert tool.center == new_center # Check that in both cases, dragging outside the rectangle draws a new # rectangle do_event(tool, 'press', xdata=175, ydata=185, button=1) do_event(tool, 'onmove', xdata=185, ydata=195, button=1) do_event(tool, 'release', xdata=185, ydata=195, button=1) assert tool.center == (180, 190)
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) do_event(tool, 'press', xdata=100, ydata=100, button=1) do_event(tool, 'onmove', xdata=125, ydata=125, button=1) do_event(tool, 'release', xdata=125, ydata=125, button=1) 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 do_event(tool, 'press', xdata=150, ydata=150, button=1) do_event(tool, 'onmove', xdata=160, ydata=160, button=1) do_event(tool, 'release', xdata=160, ydata=160, button=1) 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_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) do_event(tool, 'press', xdata=100, ydata=110, button=1) do_event(tool, 'onmove', xdata=150, ydata=120, button=1) do_event(tool, 'release', xdata=150, ydata=120, button=1) 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 do_event(tool, 'press', xdata=150, ydata=150, button=1) do_event(tool, 'onmove', xdata=160, ydata=160, button=1) do_event(tool, 'release', xdata=160, ydata=160, button=1) 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_handles(): ax = get_ax() def onselect(epress, erelease): pass tool = widgets.RectangleSelector(ax, onselect=onselect, maxdist=10, interactive=True) tool.extents = (100, 150, 100, 150) assert tool.corners == ((100, 150, 150, 100), (100, 100, 150, 150)) assert tool.extents == (100, 150, 100, 150) assert 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 do_event(tool, 'press', xdata=100, ydata=100) do_event(tool, 'onmove', xdata=120, ydata=120) do_event(tool, 'release', xdata=120, ydata=120) assert tool.extents == (120, 150, 120, 150) # grab the center and move it do_event(tool, 'press', xdata=132, ydata=132) do_event(tool, 'onmove', xdata=120, ydata=120) do_event(tool, 'release', xdata=120, ydata=120) assert tool.extents == (108, 138, 108, 138) # create a new rectangle do_event(tool, 'press', xdata=10, ydata=10) do_event(tool, 'onmove', xdata=100, ydata=100) do_event(tool, 'release', xdata=100, ydata=100) assert tool.extents == (10, 100, 10, 100)
def test_ellipse(): """For ellipse, test out the key modifiers""" ax = get_ax() def onselect(epress, erelease): pass tool = widgets.EllipseSelector(ax, onselect=onselect, maxdist=10, interactive=True) tool.extents = (100, 150, 100, 150) # drag the rectangle do_event(tool, 'press', xdata=10, ydata=10, button=1, key=' ') do_event(tool, 'onmove', xdata=30, ydata=30, button=1) do_event(tool, 'release', xdata=30, ydata=30, button=1) assert tool.extents == (120, 170, 120, 170) # create from center do_event(tool, 'on_key_press', xdata=100, ydata=100, button=1, key='control') do_event(tool, 'press', xdata=100, ydata=100, button=1) do_event(tool, 'onmove', xdata=125, ydata=125, button=1) do_event(tool, 'release', xdata=125, ydata=125, button=1) do_event(tool, 'on_key_release', xdata=100, ydata=100, button=1, key='control') assert tool.extents == (75, 125, 75, 125) # create a square do_event(tool, 'on_key_press', xdata=10, ydata=10, button=1, key='shift') do_event(tool, 'press', xdata=10, ydata=10, button=1) do_event(tool, 'onmove', xdata=35, ydata=30, button=1) do_event(tool, 'release', xdata=35, ydata=30, button=1) do_event(tool, 'on_key_release', xdata=10, ydata=10, button=1, key='shift') extents = [int(e) for e in tool.extents] assert extents == [10, 35, 10, 34] # create a square from center do_event(tool, 'on_key_press', xdata=100, ydata=100, button=1, key='ctrl+shift') do_event(tool, 'press', xdata=100, ydata=100, button=1) do_event(tool, 'onmove', xdata=125, ydata=130, button=1) do_event(tool, 'release', xdata=125, ydata=130, button=1) do_event(tool, 'on_key_release', xdata=100, ydata=100, button=1, key='ctrl+shift') extents = [int(e) for e in tool.extents] assert extents == [70, 129, 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) do_event(tool, 'press', xdata=10, ydata=10, button=1) do_event(tool, 'onmove', xdata=100, ydata=120, button=1) do_event(tool, 'release', xdata=100, ydata=120, button=1) # press-release event outside the selector to clear the selector do_event(tool, 'press', xdata=130, ydata=130, button=1) do_event(tool, 'release', xdata=130, ydata=130, button=1) assert not tool._selection_completed ax = get_ax() kwargs['ignore_event_outside'] = True tool = Selector(**kwargs) assert tool.ignore_event_outside do_event(tool, 'press', xdata=10, ydata=10, button=1) do_event(tool, 'onmove', xdata=100, ydata=120, button=1) do_event(tool, 'release', xdata=100, ydata=120, button=1) # press-release event outside the selector ignored do_event(tool, 'press', xdata=130, ydata=130, button=1) do_event(tool, 'release', xdata=130, ydata=130, button=1) assert tool._selection_completed do_event(tool, 'on_key_press', key='escape') assert not tool._selection_completed