Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Simplify/pathlibify image_comparison. #14338

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 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def convert(filename, cache):
hash of the exact contents of the input file. There is no limit on the
size of the cache, so it may need to be manually cleared periodically.
"""
base, extension = filename.rsplit('.', 1)
base, extension = os.fspath(filename).rsplit('.', 1)
if extension not in converter:
import pytest
pytest.skip(f"Don't know how to convert {extension} files to png")
Expand Down Expand Up @@ -369,18 +369,17 @@ def compare_images(expected, actual, tol, in_decorator=False):
"""
from matplotlib import _png

actual = os.fspath(actual)
if not os.path.exists(actual):
raise Exception("Output image %s does not exist." % actual)

if os.stat(actual).st_size == 0:
raise Exception("Output image file %s is empty." % actual)

# Convert the image to png
extension = expected.split('.')[-1]

expected = os.fspath(expected)
if not os.path.exists(expected):
raise IOError('Baseline image %r does not exist.' % expected)

extension = expected.split('.')[-1]
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
Expand Down
59 changes: 23 additions & 36 deletions lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ def _raise_on_image_difference(expected, actual, tol):
__tracebackhide__ = True

err = compare_images(expected, actual, tol, in_decorator=True)

if not os.path.exists(expected):
raise ImageComparisonFailure('image does not exist: %s' % expected)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the same check is already done in compare_images, just above.


if err:
for key in ["actual", "expected"]:
err[key] = os.path.relpath(err[key])
Expand Down Expand Up @@ -172,38 +168,33 @@ class _ImageComparisonBase:
This class provides *just* the comparison-related functionality and avoids
any code that would be specific to any testing framework.
"""
def __init__(self, tol, remove_text, savefig_kwargs):
self.func = self.baseline_dir = self.result_dir = None
self.tol = tol
self.remove_text = remove_text
self.savefig_kwargs = savefig_kwargs

def delayed_init(self, func):
assert self.func is None, "it looks like same decorator used twice"
def __init__(self, func, tol, remove_text, savefig_kwargs):
self.func = func
self.baseline_dir, self.result_dir = _image_directories(func)
self.tol = tol
self.remove_text = remove_text
self.savefig_kwargs = savefig_kwargs

def copy_baseline(self, baseline, extension):
baseline_path = os.path.join(self.baseline_dir, baseline)
orig_expected_fname = baseline_path + '.' + extension
if extension == 'eps' and not os.path.exists(orig_expected_fname):
orig_expected_fname = baseline_path + '.pdf'
baseline_path = self.baseline_dir / baseline
orig_expected_path = baseline_path.with_suffix(f'.{extension}')
if extension == 'eps' and not orig_expected_path.exists():
orig_expected_path = orig_expected_path.with_suffix('.pdf')
expected_fname = make_test_filename(
os.path.join(self.result_dir,
os.path.basename(orig_expected_fname)),
'expected')
self.result_dir / orig_expected_path.name, 'expected')
try:
# os.symlink errors if the target already exists.
with contextlib.suppress(OSError):
os.remove(expected_fname)
try:
os.symlink(orig_expected_fname, expected_fname)
os.symlink(orig_expected_path, expected_fname)
except OSError: # On Windows, symlink *may* be unavailable.
shutil.copyfile(orig_expected_fname, expected_fname)
shutil.copyfile(orig_expected_path, expected_fname)
except OSError:
raise ImageComparisonFailure(
f"Missing baseline image {expected_fname} because the "
f"following file cannot be accessed: {orig_expected_fname}")
f"following file cannot be accessed: {orig_expected_path}")
return expected_fname

def compare(self, idx, baseline, extension):
Expand All @@ -214,17 +205,16 @@ def compare(self, idx, baseline, extension):
if self.remove_text:
remove_ticks_and_titles(fig)

actual_fname = (
os.path.join(self.result_dir, baseline) + '.' + extension)
actual_path = (self.result_dir / baseline).with_suffix(f'.{extension}')
kwargs = self.savefig_kwargs.copy()
if extension == 'pdf':
kwargs.setdefault('metadata',
{'Creator': None, 'Producer': None,
'CreationDate': None})
fig.savefig(actual_fname, **kwargs)
fig.savefig(actual_path, **kwargs)

expected_fname = self.copy_baseline(baseline, extension)
_raise_on_image_difference(expected_fname, actual_fname, self.tol)
expected_path = self.copy_baseline(baseline, extension)
_raise_on_image_difference(expected_path, actual_path, self.tol)


def _pytest_image_comparison(baseline_images, extensions, tol,
Expand Down Expand Up @@ -254,9 +244,8 @@ def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
__tracebackhide__ = True
img = _ImageComparisonBase(tol=tol, remove_text=remove_text,
img = _ImageComparisonBase(func, tol=tol, remove_text=remove_text,
savefig_kwargs=savefig_kwargs)
img.delayed_init(func)
matplotlib.testing.set_font_settings_for_testing()
func(*args, **kwargs)

Expand Down Expand Up @@ -395,7 +384,7 @@ def test_plot(fig_test, fig_ref):
def decorator(func):
import pytest

_, result_dir = map(Path, _image_directories(func))
_, result_dir = _image_directories(func)

if len(inspect.signature(func).parameters) == 2:
# Free-standing function.
Expand All @@ -404,9 +393,8 @@ def wrapper(ext):
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(fig_test, fig_ref)
test_image_path = str(
result_dir / (func.__name__ + "." + ext))
ref_image_path = str(
test_image_path = result_dir / (func.__name__ + "." + ext)
ref_image_path = (
result_dir / (func.__name__ + "-expected." + ext))
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
Expand All @@ -420,9 +408,8 @@ def wrapper(self, ext):
fig_test = plt.figure("test")
fig_ref = plt.figure("reference")
func(self, fig_test, fig_ref)
test_image_path = str(
result_dir / (func.__name__ + "." + ext))
ref_image_path = str(
test_image_path = result_dir / (func.__name__ + "." + ext)
ref_image_path = (
result_dir / (func.__name__ + "-expected." + ext))
fig_test.savefig(test_image_path)
fig_ref.savefig(ref_image_path)
Expand All @@ -447,7 +434,7 @@ def _image_directories(func):
baseline_dir = module_path.parent / "baseline_images" / module_path.stem
result_dir = Path().resolve() / "result_images" / module_path.stem
result_dir.mkdir(parents=True, exist_ok=True)
return str(baseline_dir), str(result_dir)
return baseline_dir, result_dir


@cbook.deprecated("3.1", alternative="pytest.mark.backend")
Expand Down