-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improved selection widget #3502
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
Merged
tacaswell
merged 18 commits into
matplotlib:master
from
blink1073:improved-selection-widget
Nov 13, 2014
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
61ab6df
Factor more behavior into the _SelectorWidget class.
blink1073 e43c788
Fix indentation
blink1073 ea732ef
Put onselect in __init__, fix syntax error, make sure lines are anima…
blink1073 900a6ea
Add tests for the selection widgets
blink1073 706b5b4
Remove private event handling methods to allow for subclassing old api
blink1073 ffd715a
Remove draw_rubberband and minor cleanup
blink1073 53b6056
Add to default_test_modules
blink1073 ddfcf59
Attempt to fix affine transform error
blink1073 76721c1
Fix affine transform error
blink1073 cf66812
Add test cleanup decorator
blink1073 babc377
Move cleanup to the individual checks
blink1073 cf58c0a
Fix failing tests
blink1073 f3261cf
Fix pep8 error
blink1073 c25efbd
Improve handling of out-of-bounds releases
blink1073 ce93c98
Revert changes to setupext.py
blink1073 51c70f0
Style change and use self.blit instead of blit.
blink1073 d711940
MNT : pep8 + pyflake changes
tacaswell 86f28fa
TST : ensure canvas drawn before testing widets
tacaswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
from __future__ import (absolute_import, division, print_function, | ||
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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a |
||
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GUI tests? madness!
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
matplotlib/lib/matplotlib/__init__.py
Line 1397 in c600130
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.