-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
TST: Use placeholders for text in layout tests #29872
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
@@ -641,7 +644,7 @@ def test_compressed1(): | |||
fig.draw_without_rendering() | |||
|
|||
pos = axs[0, 0].get_position() | |||
np.testing.assert_allclose(pos.x0, 0.2344, atol=1e-3) | |||
np.testing.assert_allclose(pos.x0, 0.2381, atol=1e-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.
Note, I found test_compressed1
to be a bit flaky. Out of 200 repeats, 12 failed on this line or one of the two below where I changed the tolerance. I'm not entirely sure why, as the other tests don't have an issue. If you prefer, I can change the tolerance on all of these lines for consistency.
81dfd4b
to
de67704
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.
Looks good. I would never have known to use monkeypatch!
lib/matplotlib/testing/conftest.py
Outdated
if self.get_text() == '': | ||
return | ||
bbox = self.get_window_extent() | ||
Rectangle(bbox.p0, bbox.width, bbox.height, color='grey').draw(renderer) |
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.
Rectangle(bbox.p0, bbox.width, bbox.height, color='grey').draw(renderer) | |
Rectangle(bbox.p0, bbox.width, bbox.height, facecolor='grey').draw(renderer) |
color
would also set the edgecolor, which is transparent by default ('none'). The edgecolor induces a line to be drawn around the region and since lines have antialiasing, the edges can become blury. We don't want that. See #29874 (comment), where the same effect shows up in a different context.
de67704
to
35442a2
Compare
I pushed a couple minor tweaks:
|
lib/matplotlib/testing/conftest.py
Outdated
return | ||
bbox = self.get_window_extent() | ||
rect = Rectangle(bbox.p0, bbox.width, bbox.height, | ||
facecolor=(self.get_color(), 0.5), edgecolor='none') |
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 a bit torn on using transparency here. On the one hand, that correctly catches overlay (but that should be rare). On the other hand, alpha blending is an additional component determining the output image, can that lead to more unstable reference images in the future?
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. I think it should only make a difference if there is overlap and the z-order changes, but that's not important enough to check here.
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.
Test images looks a bit redacted now 😆, but I think this is better.
35442a2
to
60c293f
Compare
I made one more change to 2 tests that I found in #29816 that are about tight layout, namely that calling I fixed this by setting the layout on the Figure itself, so it would be enabled on save and correctly ignore text. Also, I changed the table test to use placeholders. |
37e5518
to
064c10e
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.
This should also be mentioned in
https://matplotlib.org/devdocs/devel/testing.html#writing-an-image-comparison-test
d46840e
to
7c466f9
Compare
While updating that doc section, I was also reminded that we should prefer |
I would do the mpl20 change in one go. I checked the diffs: The smaller figure sizes are ok. AFAICS no layout information is lost. The only one I'm not sure about is tight_layout_offsetboxes2.svg The offset boxes (red/green) and the labels of the right column overlap. But I guess that's just how tightlayout works. It does not account for the actual label sizes. |
Tight_layout should account for the actual reported label sizes. |
So is this an issue due the rectangle not representing the actual text width but rather an approximation? |
I'm not sure - I dont' see an equivalent test for constrained layout - but maybe it is a draw twice sort of error, where the offset boxes get moved when the axes gets resized, and therefore the margin made for the axes is too small for th new position. We run constrained layout twice to get around that; usually the position is "closer" after the first time and the new margin is big enough. |
Hm @QuLogic maybe the workaround here could be to increase the figure size to not be bothered with this topic right now? 🤯 Not great, but digging into whether this is a tightlayout issue or a box approximation issue also does not feel like a good use of time. And I don't like the present image with the overlap. |
But these are not labels, they are Based on the comment in the test, it appears the intention is to ensure that the boxes are included in the Axes bounding box, but not necessarily have any effect on the ticks labels. Then the real test is the difference between 1 and 2, where the invisible So I think that a) we can drop this test down to just .png since it's really just checking if a specific artist is included, b) maybe we should remove the tick labels entirely to avoid confusing ourselves, and c) maybe this doesn't need to be an image test at all? |
Oh I see. Yes the boxes can overlap the labels in their own axes. They should not overlap labels in other axes. |
Here's my suggestion for removing the image from the test: b6dff32 It creates 3 figures:
Then the test is that in figure 2, each If that makes some sense, then I'll rebase this PR on top of that, so we can drop the figures before changing them here. |
If we pass `remove_text` to `image_comparison`, we don't expect tests to change from FreeType. But if `tight_layout` is called *before* the end of the test function, the layout will happen with the text that was supposed to be removed.
Co-authored-by: Tim Hoffmann <[email protected]>
I am 👍 on moving to the version of that test that @QuLogic has proposed and moving all of these to mpl20 style in this PR. I think that this is the next PR in the sequence to move to freetype 2.13 |
7c466f9
to
3d0fd3c
Compare
…872-on-v3.10.x Backport PR #29872 on branch v3.10.x (TST: Use placeholders for text in layout tests)
PR summary
This is an alternative to #29833 which patches the
Text
object to draw a rectangle and not depend on any properties of the actual font. The calculation for the placeholders is somewhat arbitrary, but actually ends up rather close to the size of the original text, so most images don't change a whole lot.I tested this by changing
matplotlib.testing.set_font_settings_for_testing
from DejaVu Sans to Noto Sans; this would have failed most of the tests before, but none failed now.PR checklist