Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved selection widget #3502

Merged
merged 18 commits into from
Nov 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,7 @@ def tk_window_focus():
'matplotlib.tests.test_transforms',
'matplotlib.tests.test_triangulation',
'mpl_toolkits.tests.test_mplot3d',
'matplotlib.tests.test_widgets',
]


Expand Down
170 changes: 170 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from __future__ import (absolute_import, division, print_function,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GUI tests? madness!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I had already written these tests for scikit-image, but yes, it was a pain...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to white-list this file to be run in

default_test_modules = [

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

unicode_literals)

try:
# mock in python 3.3+
from unittest import mock
except ImportError:
import mock

import matplotlib.widgets as widgets
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import cleanup


def get_event(ax, button=1, xdata=0, ydata=0, key=None, step=1):
"""
*name*
the event name

*canvas*
the FigureCanvas instance generating the event

*guiEvent*
the GUI event that triggered the matplotlib event

*x*
x position - pixels from left of canvas

*y*
y position - pixels from bottom of canvas

*inaxes*
the :class:`~matplotlib.axes.Axes` instance if mouse is over axes

*xdata*
x coord of mouse in data coords

*ydata*
y coord of mouse in data coords

*button*
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
for scroll events)

*key*
the key depressed when the mouse event triggered (see
:class:`KeyEvent`)

*step*
number of scroll steps (positive for 'up', negative for 'down')
"""
event = mock.Mock()
event.button = button
event.x, event.y = ax.transData.transform([(xdata, ydata),
(xdata, ydata)])[00]
event.xdata, event.ydata = xdata, ydata
event.inaxes = ax
event.canvas = ax.figure.canvas
event.key = key
event.step = step
event.guiEvent = None
event.name = 'Custom'
return event


@cleanup
def check_rectangle(**kwargs):
fig, ax = plt.subplots(1, 1)
ax.plot([0, 200], [0, 200])
ax.figure.canvas.draw()

def onselect(epress, erelease):
ax._got_onselect = True
assert epress.xdata == 100
assert epress.ydata == 100
assert erelease.xdata == 200
assert erelease.ydata == 200

tool = widgets.RectangleSelector(ax, onselect, **kwargs)
event = get_event(ax, xdata=100, ydata=100, button=1)
tool.press(event)

event = get_event(ax, xdata=125, ydata=125, button=1)
tool.onmove(event)

# purposely drag outside of axis for release
event = get_event(ax, xdata=250, ydata=250, button=1)
tool.release(event)

assert ax._got_onselect


def test_rectangle_selector():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a @cleanup (from matplotlib.testing.decorators) to this. I suspect that the other failing tests are due the artists form this test leaking out.

check_rectangle()
check_rectangle(drawtype='line', useblit=False)
check_rectangle(useblit=True, button=1)
check_rectangle(drawtype='none', minspanx=10, minspany=10)
check_rectangle(minspanx=10, minspany=10, spancoords='pixels')
check_rectangle(rectprops=dict(fill=True))


@cleanup
def check_span(*args, **kwargs):
fig, ax = plt.subplots(1, 1)
ax.plot([0, 200], [0, 200])
ax.figure.canvas.draw()

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)
event = get_event(ax, xdata=100, ydata=100, button=1)
tool.press(event)

event = get_event(ax, xdata=125, ydata=125, button=1)
tool.onmove(event)

event = get_event(ax, xdata=150, ydata=150, button=1)
tool.release(event)

assert ax._got_onselect

if 'onmove_callback' in kwargs:
assert ax._got_on_move


def test_span_selector():
check_span('horizontal', minspan=10, useblit=True)
check_span('vertical', onmove_callback=True, button=1)
check_span('horizontal', rectprops=dict(fill=True))


@cleanup
def check_lasso_selector(**kwargs):
fig, ax = plt.subplots(1, 1)
ax = plt.gca()
ax.plot([0, 200], [0, 200])
ax.figure.canvas.draw()

def onselect(verts):
ax._got_onselect = True
assert verts == [(100, 100), (125, 125), (150, 150)]

tool = widgets.LassoSelector(ax, onselect, **kwargs)
event = get_event(ax, xdata=100, ydata=100, button=1)
tool.press(event)

event = get_event(ax, xdata=125, ydata=125, button=1)
tool.onmove(event)

event = get_event(ax, xdata=150, ydata=150, button=1)
tool.release(event)

assert ax._got_onselect


def test_lasso_selector():
check_lasso_selector()
check_lasso_selector(useblit=False, lineprops=dict(color='red'))
check_lasso_selector(useblit=True, button=1)
Loading