-
Notifications
You must be signed in to change notification settings - Fork 224
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
Add testing.check_figures_equal to avoid storing baseline images #555
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b78614
Add testing.check_figures_equal to avoid storing baseline images
seisman 2bb82f4
Merge branch 'master' into testing
seisman 847814b
Merge branch 'master' into testing
weiji14 27e03ed
Black lint, fix typos and run isort
weiji14 d2ad3f5
Turn check_figures_equal into a decorator function
weiji14 3e0d3fb
Ensure pytest fixtures can be used with check_figures_equal decorator
weiji14 04b3f41
Reorder parameters and add docstring note on code origin from matplotlib
weiji14 cfe3a24
Move check_figures_equal out of decorators.py and back into testing.py
weiji14 1155df0
Add notes on using check_figures_equal to MAINTENANCE.md
weiji14 e6ad74d
Extra checks to ensure image files exist or not
weiji14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
""" | ||
Helper functions for testing. | ||
""" | ||
|
||
import inspect | ||
import os | ||
|
||
from matplotlib.testing.compare import compare_images | ||
|
||
from ..exceptions import GMTImageComparisonFailure | ||
from ..figure import Figure | ||
|
||
|
||
def check_figures_equal(*, tol=0.0, result_dir="result_images"): | ||
""" | ||
Decorator for test cases that generate and compare two figures. | ||
|
||
The decorated function must take two arguments, *fig_ref* and *fig_test*, | ||
and draw the reference and test images on them. After the function | ||
returns, the figures are saved and compared. | ||
|
||
This decorator is practically identical to matplotlib's check_figures_equal | ||
function, but adapted for PyGMT figures. See also the original code at | ||
https://matplotlib.org/3.3.1/api/testing_api.html# | ||
matplotlib.testing.decorators.check_figures_equal | ||
|
||
Parameters | ||
---------- | ||
tol : float | ||
The RMS threshold above which the test is considered failed. | ||
result_dir : str | ||
The directory where the figures will be stored. | ||
|
||
Examples | ||
-------- | ||
|
||
>>> import pytest | ||
>>> import shutil | ||
|
||
>>> @check_figures_equal(result_dir="tmp_result_images") | ||
... def test_check_figures_equal(fig_ref, fig_test): | ||
... fig_ref.basemap(projection="X5c", region=[0, 5, 0, 5], frame=True) | ||
... fig_test.basemap(projection="X5c", region=[0, 5, 0, 5], frame="af") | ||
>>> test_check_figures_equal() | ||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> @check_figures_equal(result_dir="tmp_result_images") | ||
... def test_check_figures_unequal(fig_ref, fig_test): | ||
... fig_ref.basemap(projection="X5c", region=[0, 5, 0, 5], frame=True) | ||
... fig_test.basemap(projection="X5c", region=[0, 3, 0, 3], frame=True) | ||
>>> with pytest.raises(GMTImageComparisonFailure): | ||
... test_check_figures_unequal() | ||
|
||
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass | ||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
def decorator(func): | ||
|
||
os.makedirs(result_dir, exist_ok=True) | ||
old_sig = inspect.signature(func) | ||
|
||
def wrapper(*args, **kwargs): | ||
try: | ||
fig_ref = Figure() | ||
fig_test = Figure() | ||
func(*args, fig_ref=fig_ref, fig_test=fig_test, **kwargs) | ||
ref_image_path = os.path.join( | ||
result_dir, func.__name__ + "-expected.png" | ||
) | ||
test_image_path = os.path.join(result_dir, func.__name__ + ".png") | ||
fig_ref.savefig(ref_image_path) | ||
fig_test.savefig(test_image_path) | ||
|
||
# Code below is adapted for PyGMT, and is originally based on | ||
# matplotlib.testing.decorators._raise_on_image_difference | ||
err = compare_images( | ||
expected=ref_image_path, | ||
actual=test_image_path, | ||
tol=tol, | ||
in_decorator=True, | ||
) | ||
if err is None: # Images are the same | ||
os.remove(ref_image_path) | ||
os.remove(test_image_path) | ||
else: # Images are not the same | ||
for key in ["actual", "expected", "diff"]: | ||
err[key] = os.path.relpath(err[key]) | ||
raise GMTImageComparisonFailure( | ||
"images not close (RMS %(rms).3f):\n\t%(actual)s\n\t%(expected)s " | ||
% err | ||
) | ||
finally: | ||
del fig_ref | ||
del fig_test | ||
|
||
parameters = [ | ||
param | ||
for param in old_sig.parameters.values() | ||
if param.name not in {"fig_test", "fig_ref"} | ||
] | ||
new_sig = old_sig.replace(parameters=parameters) | ||
wrapper.__signature__ = new_sig | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,12 +2,13 @@ | |||||||||||||||||||||||||||||||||||||||||||||
Test Figure.grdimage | ||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||
import numpy as np | ||||||||||||||||||||||||||||||||||||||||||||||
import xarray as xr | ||||||||||||||||||||||||||||||||||||||||||||||
import pytest | ||||||||||||||||||||||||||||||||||||||||||||||
import xarray as xr | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
from .. import Figure | ||||||||||||||||||||||||||||||||||||||||||||||
from ..exceptions import GMTInvalidInput | ||||||||||||||||||||||||||||||||||||||||||||||
from ..datasets import load_earth_relief | ||||||||||||||||||||||||||||||||||||||||||||||
from ..exceptions import GMTInvalidInput | ||||||||||||||||||||||||||||||||||||||||||||||
from ..helpers.testing import check_figures_equal | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
@pytest.fixture(scope="module", name="grid") | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -93,3 +94,12 @@ def test_grdimage_over_dateline(xrgrid): | |||||||||||||||||||||||||||||||||||||||||||||
xrgrid.gmt.gtype = 1 # geographic coordinate system | ||||||||||||||||||||||||||||||||||||||||||||||
fig.grdimage(grid=xrgrid, region="g", projection="A0/0/1c", V="i") | ||||||||||||||||||||||||||||||||||||||||||||||
return fig | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
@check_figures_equal() | ||||||||||||||||||||||||||||||||||||||||||||||
def test_grdimage_central_longitude(grid, fig_ref, fig_test): | ||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||
Test that plotting a grid centred at different longitudes/meridians work. | ||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||
fig_ref.grdimage("@earth_relief_01d_g", projection="W120/15c", cmap="geo") | ||||||||||||||||||||||||||||||||||||||||||||||
fig_test.grdimage(grid, projection="W120/15c", cmap="geo") | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+99
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I'll update this test in #560 later 😄. Problem with using this fancy |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
One more thing about the result_dir is that, the
check_figures_equal
decorator generates images inresult_images
directory, whilepytest.mark.mpl_image_compare
generates images in directories likeresults/tmpjtnnwqt4
.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.
Yes, which was partly why I opened up the issue at matplotlib/pytest-mpl#94, to get all of that
pytest-mpl
goodness (e.g. not having a hardcodedresult_dir
). I'll try to make a Pull Request topytest-mpl
for that, so we can just use a proper@pytest.mark.mpl_check_equal
decorator in the future (will open a new issue after this one is merged). For now though, since we don't have many tests usingcheck_figures_equal
yet, we can probably just leave it like so.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.
Yes, looks good to me.