Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
Nov 28, 2022
Merged

Conversation

saranti
Copy link
Contributor

@saranti saranti commented Nov 20, 2022

PR Summary

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

Copy link

@github-actions github-actions bot left a 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():
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 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

Comment on lines 30 to 31
assert str(pl.get_data_3d()) == result1
assert str(pl.get_data()) == result2
Copy link
Member

Choose a reason for hiding this comment

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

why are these strings?

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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?

@oscargus
Copy link
Member

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.

Copy link
Member

@oscargus oscargus left a 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

@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.

Comment on lines 2145 to 2146
pl.get_data_3d()
pl.get_data()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pl.get_data_3d()
pl.get_data()

Not required

Copy link
Contributor Author

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
Copy link
Member

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.]))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Comment on lines 2153 to 2154
assert np.allclose(pl.get_data_3d(), result1) is True
assert np.allclose(pl.get_data(), result2) is True
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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" \
Copy link
Member

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):
Copy link
Member

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?

Copy link
Contributor Author

@saranti saranti Nov 23, 2022

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.

Copy link
Member

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).

Copy link
Contributor Author

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?

@tacaswell
Copy link
Member

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!).

@tacaswell
Copy link
Member

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).

@QuLogic QuLogic merged commit f630c28 into matplotlib:main Nov 28, 2022
@QuLogic
Copy link
Member

QuLogic commented Nov 28, 2022

Thanks @saranti! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

@saranti saranti deleted the test branch November 29, 2022 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Surprising behaviour of mutating input arrays to Axes.plot vs Axes3D.plot
5 participants