Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve barbs() error message#7407

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
NelleV merged 2 commits intomatplotlib:v2.xfromdopplershift:barbs-error
Nov 4, 2016

Conversation

dopplershift
Copy link
Contributor

So for the following code with badly sized (x,y with 3, u,v with 4) arrays:

x=np.arange(3)y=np.arange(3)u=np.ma.array(15.*np.ones((4,)),mask=[False,True,False,False])v=np.ma.array(15.*np.ones_like(u),mask=[False,True,False,False])plt.barbs(x,y,u,v)

The error would end up with something inscrutable (I lost over an hour figuring out what was wrong):

---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<SNIP>/Users/rmay/repos/matplotlib/lib/matplotlib/quiver.py in __init__(self, ax, *args, **kw)942self.set_transform(transforms.IdentityTransform())943 --> 944         self.set_UVC(u, v, c)945946def_find_tails(self,mag,rounding=True,half=5,full=10,flag=50):/Users/rmay/repos/matplotlib/lib/matplotlib/quiver.py in set_UVC(self, U, V, C)   1138         plot_barbs = self._make_barbs(u, v, flags, barbs, halves, empty,   1139                                       self._length, self._pivot, self.sizes,-> 1140                                       self.fill_empty, self.flip)   1141         self.set_verts(plot_barbs)   1142 /Users/rmay/repos/matplotlib/lib/matplotlib/quiver.py in _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length, pivot, sizes, fill_empty, flip)   1071    1072             # Add vertices for each flag-> 1073             for i in range(nflags[index]):   1074                 # The spacing that works for the barbs is a little to much for   1075                 # the flags, but this only occurs when we have more than 1TypeError:only integer arrays with one element can be converted to an index

It also only happens when masked arrays are involved--otherwise it silently succeeds, which seems poor as well. With this change now the error is easy to identify:

---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<SNIP>/Users/rmay/repos/matplotlib/lib/matplotlib/quiver.py in __init__(self, ax, *args, **kw)948self.set_transform(transforms.IdentityTransform())949 --> 950         self.set_UVC(u, v, c)951952def_find_tails(self,mag,rounding=True,half=5,full=10,flag=50):/Users/rmay/repos/matplotlib/lib/matplotlib/quiver.py in set_UVC(self, U, V, C)   1135             x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),   1136                                               self.u, self.v)-> 1137             check_array_shapes(x, y, u, v)   1138    1139         magnitude = np.hypot(u, v)/Users/rmay/repos/matplotlib/lib/matplotlib/quiver.py in check_array_shapes(*arrays)396     all_shapes=set(a.shapefor ain arrays)397iflen(all_shapes)!=1:--> 398         raise ValueError('The shapes of the passed in arrays do not match.')399400ValueError:The shapes of the passed in arrays do not match.

@dopplershiftdopplershift modified the milestones:2.0 (style change major release),2.0.1 (next bug fix release)Nov 4, 2016
Copy link
Member

@NelleVNelleV left a comment

Choose a reason for hiding this comment

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

The error message looks good, but I've got a couple of comments on the API.

@@ -392,6 +392,12 @@ def _parse_args(*args):
return X, Y, U, V, C


def check_array_shapes(*arrays):
Copy link
Member

Choose a reason for hiding this comment

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

Can you make this function private? I don't think it should be part of our public API.

I also think the name of the function is not very explicit on what it does. How aboutcheck_consistent_shape?

def check_array_shapes(*arrays):
all_shapes = set(a.shape for a in arrays)
if len(all_shapes) != 1:
raise ValueError('The shapes of the passed in arrays do not match.')
Copy link
Member

Choose a reason for hiding this comment

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

👍

With this change, when given masked arrays with incompatible sizes,barbs() will raise a ValueError with a message stating as much.Previously, it would error with a TypeError that did not make it clearat all the source of the problem.
@dopplershift
Copy link
ContributorAuthor

Changed the name of the helper to_check_consistent_shapes.

@NelleV
Copy link
Member

👍 Thanks for the patch!
I recently spend a couple of hours understanding why scatter wasn't happy because of a cryptic error message so this patch makes me very happy :)

@NelleVNelleV changed the titleImprove barbs() error message[MRG+1] Improve barbs() error messageNov 4, 2016
@dopplershift
Copy link
ContributorAuthor

Well considering Iwrotebarbs() and it still cost me time, I'd say we need to do better. 😬

def _check_consistent_shapes(*arrays):
all_shapes = set(a.shape for a in arrays)
if len(all_shapes) != 1:
raise ValueError('The shapes of the passed in arrays do not match.')
Copy link
Member

Choose a reason for hiding this comment

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

I think that line is too long (pep8 is complaining somewhere).

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Nope, even worse--it was the docstring typo I fixed. I rewrapped and pushed.

Copy link
Member

@phobsonphobson left a comment

Choose a reason for hiding this comment

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

I don't want to hold this up, but I would prefer if we leftnose out of the test

@@ -133,6 +134,20 @@ def test_barbs():
cmap='viridis')


@cleanup
@raises(ValueError)
Copy link
Member

Choose a reason for hiding this comment

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

Just a thought: we could avoid falling back tonose with pytest like this:

@cleanupdeftest_bad_masked_sizes():'Test error handling when given differing sized masked arrays'x=np.arange(3)y=np.arange(3)u=np.ma.array(15.*np.ones((4,)))v=np.ma.array(15.*np.ones_like(u))u[1]=np.ma.maskedv[1]=np.ma.maskedfig,ax=plt.subplots()withpytest.raises(ValueError):ax.barbs(x,y,u,v)

NelleV reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

If we're allowed to explicitly use pytest now, I will absolutely do that. I just tried to be consistent with what's already around.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I'm not seeing anywhere that we're explicitly using pytest yet...I sense this shouldn't be the first. 😞

Copy link
Member

Choose a reason for hiding this comment

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

oh, I thought it was official!

Copy link
Member

Choose a reason for hiding this comment

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

I personally don't care… I much prefer nose's API than pytests, so I have a tendency to use naturally pytest. I'd be interested in knowing whether we should write everything pytest-like.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Ah...on master.

Copy link
Member

Choose a reason for hiding this comment

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

Oops. Sorry for the noise. I missed the destination for this.

@NelleVNelleV merged commitdc13688 intomatplotlib:v2.xNov 4, 2016
@NelleV
Copy link
Member

Thanks@dopplershift !

@dopplershiftdopplershift deleted the barbs-error branchNovember 4, 2016 23:50
@QuLogicQuLogic changed the title[MRG+1] Improve barbs() error messageImprove barbs() error messageNov 5, 2016
@QuLogicQuLogic modified the milestones:2.0.1 (next bug fix release),2.0 (style change major release)Dec 7, 2016
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@NelleVNelleVNelleV approved these changes

@phobsonphobsonphobson left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
v2.0.0
Development

Successfully merging this pull request may close these issues.

4 participants
@dopplershift@NelleV@phobson@QuLogic

[8]ページ先頭

©2009-2025 Movatter.jp