-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add test for mutating input arrays #8990 #24511
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.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a while, please feel free to ping @matplotlib/developers
or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join us on gitter for real-time discussion.
For details on testing, writing docs, and our review process, please see the developer guide
We strive to be a welcoming and open project. Please follow our Code of Conduct.
pl.get_data() | ||
|
||
|
||
def test_1(): |
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 please give this test a more descriptive name? and if that test data is only used for this test, please put it in the function. If it's used for both, please put it in a fixture?
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.
Sure
assert str(pl.get_data_3d()) == result1 | ||
assert str(pl.get_data()) == result2 |
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.
why are these strings?
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.
That was the only way i was able to get a matching comparison on the tuple. Is there a better way to do this?
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 can do something like
np.all(p1.get_data() == result2)
where result2 is then an np-array.
You may have to do np.allclose
instead of np.all
as there may be floating-point effects, at least on result2
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.
Thanks Oscar. Using np.allclose
appears to return an assertion error on Ubuntu and Mac:
FAILED lib/mpl_toolkits/mplot3d/tests/test_axes3d.py::test_mutating_input_arrays_y_and_z - assert False is True
+ where False = <function allclose at 0x7f4c5b088ee0>((array([-0.07780068, -0.02919708, 0.02192821]), array([-0.04741606, -0.06321353, -0.07983062])), ((-0.07348146, -0.02919708, 0.01717111), (-0.04881992, -0.06321353, -0.07828444)))
+ where <function allclose at 0x7f4c5b088ee0> = np.allclose
+ and (array([-0.07780068, -0.02919708, 0.02192821]), array([-0.04741606, -0.06321353, -0.07983062])) = <bound method Line2D.get_data of <mpl_toolkits.mplot3d.art3d.Line3D object at 0x7f4c38963730>>()
+ where <bound method Line2D.get_data of <mpl_toolkits.mplot3d.art3d.Line3D object at 0x7f4c38963730>> = <mpl_toolkits.mplot3d.art3d.Line3D object at 0x7f4c38963730>.get_data
= 1 failed, 8896 passed, 207 skipped, 13 xfailed, 7 xpassed, 5 warnings in 706.06s (0:11:46) =
Error: Process completed with exit code 1.
I'm not really sure why this is happening.
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 values are slightly different:
(array([-0.07780068, -0.02919708, 0.02192821]), array([-0.04741606, -0.06321353, -0.07983062]))
vs
((-0.07348146, -0.02919708, 0.01717111), (-0.04881992, -0.06321353, -0.07828444))
I am wondering if setting limits etc will somehow make it different?
Please add the content of the tests as functions to https://github.com/matplotlib/matplotlib/blob/main/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py instead. |
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.
It may be easier to just do an image_comparison test. See
matplotlib/lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Lines 305 to 306 in c5ed8eb
@check_figures_equal(extensions=["png"]) | |
def test_plot_scalar(fig_test, fig_ref): |
for an example.
So do exactly the same operation on both fig_test and fig_ref (using different arrays for input data) and then mutate one of the input arrays.
This holds for both tests and will avoid the problem of figuring out the exact coordinates of the plotting results.
It can be good to make a comment in each test pointing to GH#8990
so that one understands where the test case comes from.
pl.get_data_3d() | ||
pl.get_data() |
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.
pl.get_data_3d() | |
pl.get_data() |
Not required
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.
Thanks for the help, the image comparison test worked fine on both of them. Now I just have that pr_clean test failing because the file I originally wrote the test on was added and deleted.
pl.get_data_3d() | ||
pl.get_data() | ||
|
||
array = tuple |
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 remove the array call below.
pl.get_data() | ||
|
||
array = tuple | ||
result1 = (array([1, 2, 3]), array([0., 0., 0.]), array([0., 0., 0.])) |
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.
result1 = (array([1, 2, 3]), array([0., 0., 0.]), array([0., 0., 0.])) | |
result1 = ((1, 2, 3),(0., 0., 0.), (0., 0., 0.)) |
allclose will check that the numerical values are close, so if using lists or tuples (or np.array) doesn't matter as long as the structure is correct. (Modulo that it may not be correct in my simplified version...)
Same on the next row.
assert np.allclose(pl.get_data_3d(), result1) is True | ||
assert np.allclose(pl.get_data(), result2) is True |
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.
assert np.allclose(pl.get_data_3d(), result1) is True | |
assert np.allclose(pl.get_data(), result2) is True | |
assert np.all(pl.get_data_3d() == result1) | |
assert np.allclose(pl.get_data(), result2) |
For result1
the values are exact enough to compare directly. is True
not required as assert is checking for True anyway.
ax2.plot(x, V2, zs=2, zdir='y') | ||
fig.draw_without_rendering() | ||
|
||
result_x = "[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3" \ |
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.
Please use numbers in tuples/lists here as well.
|
||
# test cases came from GH#8990 | ||
@check_figures_equal(extensions=["png"]) | ||
def test_mutating_input_arrays_sin_3d(fig_test, fig_ref): |
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.
What is this testing that the other test is not testing?
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.
It's testing that the second curve doesn't affect the first curve as reported here.
Edit: I just attempted a rebase but the added and deleted file hasn't been affected by it. It still won't pass the pr_clean test.
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.
In that example the important change is mutating the input not that we plotted it a second time (if you go back to 3.5.2 and remove the second plot I expect it to be broken).
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.
Ok. In that case, should we remove the second test entirely?
Thank you for your work on this @saranti ! Are you comfortable doing a rebase? This needs to be squashed down to one commit (we have a check that makes sure we do not add and then remove a file in a PR. This helps keep the repo size down, makes sure we do not accidentally commit a whole venv again, and provides a mild security check that no one sneaks a malicious file in!). |
You need to squash this to one commit (there are still two commits the first puts the test in their own file, the second moves them into an existing file). |
Thanks @saranti! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again. |
PR Summary
PR Checklist
Documentation and Tests
pytest
passes)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