-
Notifications
You must be signed in to change notification settings - Fork 235
Add testing.check_figures_equal to avoid storing baseline images #555
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
Changes from 4 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 hidden or 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 hidden or 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,37 @@ | ||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from matplotlib.testing.compare import compare_images | ||
|
|
||
| from ..exceptions import GMTImageComparisonFailure, GMTInvalidInput | ||
|
|
||
|
|
||
| def check_figures_equal(fig_ref, fig_test, fig_prefix=None, tol=0.0): | ||
| result_dir = "result_images" | ||
|
|
||
| if not fig_prefix: | ||
| try: | ||
| fig_prefix = sys._getframe(1).f_code.co_name | ||
| except ValueError: | ||
| raise GMTInvalidInput("fig_prefix is required.") | ||
|
|
||
| os.makedirs(result_dir, exist_ok=True) | ||
|
|
||
| ref_image_path = os.path.join(result_dir, fig_prefix + "-expected.png") | ||
| test_image_path = os.path.join(result_dir, fig_prefix + ".png") | ||
|
|
||
| fig_ref.savefig(ref_image_path) | ||
| fig_test.savefig(test_image_path) | ||
|
|
||
| err = compare_images(ref_image_path, test_image_path, tol, in_decorator=True) | ||
|
|
||
| if err is None: # Images are the same | ||
| os.remove(ref_image_path) | ||
| os.remove(test_image_path) | ||
| else: | ||
| for key in ["actual", "expected"]: | ||
| err[key] = os.path.relpath(err[key]) | ||
| raise GMTImageComparisonFailure( | ||
| "images not close (RMS %(rms).3f):\n\t%(actual)s\n\t%(expected)s " % err | ||
| ) | ||
This file contains hidden or 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,9 @@ | ||
| from matplotlib.testing.decorators import check_figures_equal | ||
|
|
||
| import pygmt | ||
|
|
||
|
|
||
| @check_figures_equal(extensions=["png"]) | ||
| def test_plot(fig_test, fig_ref): | ||
| fig_test.subplots().plot([1, 3, 5]) | ||
| fig_ref.subplots().plot([0, 1, 2], [1, 3, 5]) |
This file contains hidden or 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 hidden or 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,28 @@ | ||
| """ | ||
| Test the testing functions for PyGMT | ||
| """ | ||
| import pytest | ||
|
|
||
| from .. import Figure | ||
| from ..exceptions import GMTImageComparisonFailure | ||
| from ..helpers.testing import check_figures_equal | ||
|
|
||
|
|
||
| def test_check_figures_equal(): | ||
| fig_ref = Figure() | ||
| fig_ref.basemap(projection="X10c", region=[0, 10, 0, 10], frame=True) | ||
|
|
||
| fig_test = Figure() | ||
| fig_test.basemap(projection="X10c", region=[0, 10, 0, 10], frame=True) | ||
| check_figures_equal(fig_ref, fig_test) | ||
|
|
||
|
|
||
| def test_check_figures_unequal(): | ||
| fig_ref = Figure() | ||
| fig_ref.basemap(projection="X10c", region=[0, 10, 0, 10], frame=True) | ||
|
|
||
| fig_test = Figure() | ||
| fig_test.basemap(projection="X10c", region=[0, 15, 0, 15], frame=True) | ||
|
|
||
| with pytest.raises(GMTImageComparisonFailure): | ||
| check_figures_equal(fig_ref, fig_test) |
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.
Uh oh!
There was an error while loading. Please reload this page.