-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add testing.check_figures_equal to avoid storing baseline images
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
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,35 @@ | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from matplotlib.testing.compare import compare_images | ||
from ..exceptions import GMTImageComparisonFailure | ||
|
||
|
||
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 VauleError: | ||
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 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,7 @@ | ||
import pygmt | ||
from matplotlib.testing.decorators import check_figures_equal | ||
|
||
@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 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,27 @@ | ||
""" | ||
Test the testing functions for PyGMT | ||
""" | ||
from .. import Figure | ||
from ..helpers.testing import check_figures_equal | ||
from ..exceptions import GMTImageComparisonFailure | ||
import pytest | ||
|
||
|
||
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) |