X Tutup
Skip to content

New Feature - PolygonSelector Widget#8403

Merged
phobson merged 9 commits intomatplotlib:masterfrom
bduick:widgets-polygonselector
Apr 13, 2017
Merged

New Feature - PolygonSelector Widget#8403
phobson merged 9 commits intomatplotlib:masterfrom
bduick:widgets-polygonselector

Conversation

@bduick
Copy link
Copy Markdown
Contributor

@bduick bduick commented Mar 30, 2017

Hello matplotlib community! Please checkout the PolygonSelector widget. The demo is pretty straight forward, and basically a copy of the LassoSelector demo. I'm open to any feedback, or suggestions for improvement. Thanks for your consideration.

demo-screen-captures

Copy link
Copy Markdown
Member

@phobson phobson left a comment

Choose a reason for hiding this comment

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

This looks really cool! I haven't taken a deep look yet, but here are some comments that should get the CI builds to pass.


# Move first vertex before completing the polygon.
expected_result = [(75, 50), (150, 50), (50, 150)]
event_sequence = [*polygon_place_vertex(50, 50),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unfortunately, we have to support python 2.7 and 3.4, so this won't work :/

ax = get_ax()

ax._onselect_cnt = 0
def onselect(vertices):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PEP8 wants a blank like here

@bduick
Copy link
Copy Markdown
Contributor Author

bduick commented Mar 30, 2017

Thanks phobson. Glad you like it.

@dstansby dstansby added this to the 2.1 (next point release) milestone Mar 30, 2017
When a polygon is completed or modified after completion,
the `onselect` function is called and passed a list of the vertices as
``(xdata, ydata)`` tuples.
useblit : bool, optional
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interactive is not documented.

The markers for the vertices of the polygon are drawn with
`markerprops`. The default is ``dict(marker='o', markersize=7, mec='k',
mfc='k', alpha=0.5)``.
vertex_select_radius : int
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be optional

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

and would this be better as a float?

@tacaswell
Copy link
Copy Markdown
Member

This looks useful 👍

attn @blink1073 I have completely lost track of where you are at on other selector work.

tacaswell
tacaswell previously approved these changes Apr 2, 2017
@tacaswell tacaswell dismissed their stale review April 2, 2017 04:15

should be modulo doc changes

Copy link
Copy Markdown
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Modulo minor documentation changes.

@bduick
Copy link
Copy Markdown
Contributor Author

bduick commented Apr 2, 2017

@tacaswell i can add support for non-interactive. i think i will implement it so that the only difference with interactive mode is that the polygon is automatically cleared after the selection is made. alternatively, does anyone think that i should also inhibit the move_vertex and move_all functions that are currently allowed before the polygon has been completed? in other words, the way it currently works is that if you haven't closed the polygon yet, you can move all the vertices or move a single vertex by holding shift or ctrl. should i disable this in non-interactive to be more consistent with the other widgets?

@tacaswell
Copy link
Copy Markdown
Member

On a second reading, just drop interactive as a kwarg.

@blink1073
Copy link
Copy Markdown
Member

This is great! I had tried and given up on a similar tool in another PR.

@bduick
Copy link
Copy Markdown
Contributor Author

bduick commented Apr 3, 2017

My bad on the interactive kwarg. Didn't even realize until after your latest comment that I had accidentally left it in the signature.

@bduick
Copy link
Copy Markdown
Contributor Author

bduick commented Apr 3, 2017

@blink1073, glad you like the tool. i found your PR after thomas tagged you. the PR makes a lot of good points. i did find that i had to put in some special logic to work around logic in _SelectorWidget that wasn't applicable for the PolygonSelector.

@tacaswell
Copy link
Copy Markdown
Member

@bduick Ah, sorry my comments were cryptic. Could you add to the docstring that 'vertex_select_radius' is optional?

I think that is the only thing standing between this and merging :)

@bduick
Copy link
Copy Markdown
Contributor Author

bduick commented Apr 4, 2017

I should've understood. Anyways, fixed and committed!

"""Select indices from a matplotlib collection using `PolygonSelector`.

Selected indices are saved in the `ind` attribute. This tool highlights
selected points by fading them out (i.e., reducing their alpha values).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't it fade out the points that are not selected?

if len(self.fc) == 0:
raise ValueError('Collection must have a facecolor')
elif len(self.fc) == 1:
self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isn't this np.repeat(self.fc, self.Npts)?

Copy link
Copy Markdown
Member

@QuLogic QuLogic Apr 4, 2017

Choose a reason for hiding this comment

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

Actually, no, I just tried it out and it's not, but it is equal to np.tile(x, (self.Npts, 1)) if I've understood it correctly.


def onselect(self, verts):
path = Path(verts)
self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does path.contains_points(self.xys) work here?

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 or vertices (xdata, ydata)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

list of?

assert slider.val == slider.valmax


def check_polygon_selector(event_sequence, expected_result, selections_cnt):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't see any reason to abbreviate count.

``(xdata, ydata)`` tuples.
useblit : bool, optional
lineprops : dict, optional
The line for the sides of the polygon is drawn with `lineprops`. The
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"... drawn with the properties given by lineprops."

(Also, references do not work to arguments.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can you give me some more info re: "references do not work to arguments"? I'm not sure what you mean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@QuLogic, is there anything else I need to do to address this comment?

The line for the sides of the polygon is drawn with `lineprops`. The
default is ``dict(color='k', linestyle='-', linewidth=2, alpha=0.5)``.
markerprops : dict, optional
The markers for the vertices of the polygon are drawn with
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"... drawn with the properties given by ..."

"""Button press event handler"""
# Check for selection of a tool handle.
if ((self._polygon_completed or 'move_vertex' in self.state)
and len(self._xs) > 0):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be indented one more time.

# Add back the pending vertex if leaving the 'move_vertex' or
# 'move_all' mode (by checking the released key)
if (not self._polygon_completed
and (event.key == self.state_modifier_keys.get('move_vertex')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be indented once more.

# if the polygon is completed or the user is locked on to the start
# vertex.
if (self._polygon_completed
or (len(self._xs) > 3
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be indented once more.

@bduick
Copy link
Copy Markdown
Contributor Author

bduick commented Apr 5, 2017

@QuLogic, thanks for the thorough review. Your 3 comments on the polygon_selector_demo.py also apply to lasso_selector_demo.py. Can I commit the fixes for the lasso demo in this PR, or should I create a new one?

@tacaswell
Copy link
Copy Markdown
Member

@bduick which ever is easier for you.

@tacaswell
Copy link
Copy Markdown
Member

failures look to be due to changing DNS for matplotlib.org and it taking time for those changes to propogate

@jrmlhermitte
Copy link
Copy Markdown
Contributor

very cool!

@tacaswell
Copy link
Copy Markdown
Member

attn @phobson

@phobson phobson merged commit 239276e into matplotlib:master Apr 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

X Tutup