8000 Added get_shape as an alias for get_size + tests by saranti · Pull Request #25425 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Added get_shape as an alias for get_size + tests #25425

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 6 commits into from
Mar 17, 2023

Conversation

saranti
Copy link
Contributor
@saranti saranti commented Mar 10, 2023

PR Summary

Fixes #22494. Continuation of draft PR #22510. Added the get_shape commit and wrote tests for get_size method and the newly added get_shape method.

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

@tacaswell tacaswell mentioned this pull request Mar 10, 2023
4 tasks
im = AxesImage(ax)
z = np.arange(12, dtype=float).reshape((4, 3))
im.set_data(z)
assert im.get_shape() == (4, 3)
Copy link
Member

Choose a reason for hiding this comment

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

On one hand, this is testing two things so not technically a unit test. On the other hand all of our tests have a high aspect of integration testing to them (e.g. this test invokes plt.gca()!) so the trade off on one less slightly more complex test seems worth it to me, but fine either way.

Suggested change
assert im.get_shape() == (4, 3)
assert im.get_shape() == (4, 3)
assert im.get_size() == im.get_shape()

Copy link
Member
@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Left comments about testing, strongly prefer the exception gets tested but won't block merging over that.

I'm fine either way with merging the tests (but have enough of a preference to comment on it).

@tacaswell tacaswell added this to the v3.8.0 milestone Mar 10, 2023
"""
Return the shape of the image as tuple (numrows, numcols, channels).
"""
if self._A is None:
Copy link
Member

Choose a reason for hiding this comment

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

You could save having the RuntimeError repeated by making get_size call get_shape?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Left comments about testing, strongly prefer the exception gets tested but won't block merging over that.

I'm fine either way with merging the tests (but have enough of a preference to comment on it).

Thanks for the feedback, @jklymak @tacaswell. I decided to merge the tests and add the exception test.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see that you actually did this....

# generate dummy image to test get_shape method
ax = plt.gca()
im = AxesImage(ax)
with pytest.raises(RuntimeError):
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
with pytest.raises(RuntimeError):
with pytest.raises(RuntimeError, match="You must first set the image array"):

to make sure it is the "correct" RuntimeError.

@@ -287,10 +287,19 @@ def __getstate__(self):
def get_size(self):
"""Return the size of the image as tuple (numrows, numcols)."""
if self._A is None:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just do

return self.get_shape()[:2]

@oscargus
Copy link
Member

Thanks!

@oscargus oscargus merged commit a7ba07c into matplotlib:main Mar 17, 2023
@saranti saranti deleted the get_shape branch June 23, 2023 10:55
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.

[ENH]: Add get_shape as alias for get_size in AxesImage, or make that include depth too
4 participants
0