-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
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): |
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.
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 about check_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.') |
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.
👍
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 clear at all the source of the problem.
9ad1c9a
to
11c9aeb
Compare
Changed the name of the helper to |
👍 Thanks for the patch! |
Well considering I wrote |
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.') |
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.
I think that line is too long (pep8 is complaining somewhere).
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.
Nope, even worse--it was the docstring typo I fixed. I rewrapped and pushed.
11c9aeb
to
65d818d
Compare
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.
I don't want to hold this up, but I would prefer if we left nose
out of the test
@@ -133,6 +134,20 @@ def test_barbs(): | |||
cmap='viridis') | |||
|
|||
|
|||
@cleanup | |||
@raises(ValueError) |
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.
Just a thought: we could avoid falling back to nose
with pytest like this:
@cleanup
def test_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.masked
v[1] = np.ma.masked
fig, ax = plt.subplots()
with pytest.raises(ValueError):
ax.barbs(x, y, u, v)
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.
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.
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.
I'm not seeing anywhere that we're explicitly using pytest yet...I sense this shouldn't be the first. 😞
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.
oh, I thought it was official!
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.
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.
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.
Ah...on master.
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.
Oops. Sorry for the noise. I missed the destination for this.
Thanks @dopplershift ! |
So for the following code with badly sized (x,y with 3, u,v with 4) arrays:
The error would end up with something inscrutable (I lost over an hour figuring out what was wrong):
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: