-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Remove unnecessary np.{,as}array / astype calls. #24295
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
@@ -696,7 +696,7 @@ def test_pathcollection_legend_elements(): | |||
|
|||
h, l = sc.legend_elements(fmt="{x:g}") | |||
assert len(h) == 5 | |||
assert_array_equal(np.array(l).astype(float), np.arange(5)) | |||
assert_array_equal(l, ["0", "1", "2", "3", "4"]) |
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.
Is this a list
of str
? Can it be a regular assert
?
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.
Indeed; switched more places to use plain asserts.
@@ -727,7 +727,7 @@ def test_pathcollection_legend_elements(): | |||
|
|||
levels = [-1, 0, 55.4, 260] | |||
h6, lab6 = sc.legend_elements(num=levels, prop="sizes", fmt="{x:g}") | |||
assert_array_equal(np.array(lab6).astype(float), levels[2:]) | |||
assert_array_equal(np.asarray(lab6, float), levels[2:]) |
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.
How come this one became np.asarray
when the above one remained np.array
?
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.
Switched to a plain assert, as above.
lib/matplotlib/tests/test_sankey.py
Outdated
@@ -67,7 +67,7 @@ def test_sankey2(): | |||
s = Sankey(flows=[0.25, -0.25, 0.5, -0.5], labels=['Foo'], | |||
orientations=[-1], unit='Bar') | |||
sf = s.finish() | |||
assert np.all(np.equal(np.array((0.25, -0.25, 0.5, -0.5)), sf[0].flows)) | |||
assert np.array_equal(sf[0].flows, [0.25, -0.25, 0.5, -0.5]) |
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.
Better to use numpy.testing.assert_array_equal() because that gives better error messages. I wouldn’t bother to go through all tests and change it, but if you rewrite something, that’s the way to go.
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.
Good catch, fixed.
lib/matplotlib/backend_bases.py
Outdated
@@ -1464,7 +1464,7 @@ def on_pick(event): | |||
line = event.artist | |||
xdata, ydata = line.get_data() | |||
ind = event.ind | |||
print('on pick line:', np.array([xdata[ind], ydata[ind]]).T) | |||
print('on pick line:', xdata[ind], ydata[ind]) |
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 bet this array call was here to get reasonably truncated repr:
print('on pick line:', xdata[ind], ydata[ind]) | |
print(f'on pick line: {ind}: ({xdata[ind]:.3f}, {ydata[ind]:.3f})') |
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.
fair enough, fixed.
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.
modulo my comment about formatting in the docstring line.
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.
Modulo @tacaswell’s comment and the flake8 unused import.
Quite often numpy will call asarray for us, saving us the need to call asarray explicitly. When we do call asarray (or array) ourselves, a dtype can directly be passed in, rather than immediately calling astype immediately after. Passing the dtype makes it unnecessary for asarray to infer the dtype of the passed-in container, and can also save an extra array allocation if asarray first has to allocate an array of a type and astype immediately has to allocate an array of another type.
Done. |
Can self-merge on green. |
Quite often numpy will call asarray for us, saving us the need to call asarray explicitly.
When we do call asarray (or array) ourselves, a dtype can directly be passed in, rather than immediately calling astype immediately after. Passing the dtype makes it unnecessary for asarray to infer the dtype of the passed-in container, and can also save an extra array allocation if asarray first has to allocate an array of a type and astype immediately has to allocate an array of another type.
PR Summary
PR Checklist
Tests and Styling
pytest
passes).flake8-docstrings
and runflake8 --docstring-convention=all
).Documentation
Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst