1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
"""
This is a developer utility to help analyze and triage image
comparison failures.
It allows the failures to be quickly compared against the expected
results, and the new results to be either accepted (by copying the new
results to the source tree) or rejected (by copying the original
expected result to the source tree).
To start:
If you ran the tests from the top-level of a source checkout, simply run:
python tools/triage_tests.py
Otherwise, you can manually select the location of `result_images`
on the commandline.
Keys:
left/right: Move between test, expected and diff images
up/down: Move between tests
A: Accept test. Copy the test result to the source tree.
R: Reject test. Copy the expected result to the source tree.
"""
import os
from pathlib import Path
import shutil
import sys
from matplotlib.backends.qt_compat import QtCore, QtGui, QtWidgets
from matplotlib.backends.qt_compat import _exec
# matplotlib stores the baseline images under two separate subtrees,
# but these are all flattened in the result_images directory. In
# order to find the source, we need to search for a match in one of
# these two places.
BASELINE_IMAGES = [
Path('lib/matplotlib/tests/baseline_images'),
*Path('lib/mpl_toolkits').glob('*/tests/baseline_images'),
]
# Non-png image extensions
exts = ['pdf', 'svg', 'eps']
class Thumbnail(QtWidgets.QFrame):
"""
Represents one of the three thumbnails at the top of the window.
"""
def __init__(self, parent, index, name):
super().__init__()
self.parent = parent
self.index = index
layout = QtWidgets.QVBoxLayout()
label = QtWidgets.QLabel(name)
label.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter |
QtCore.Qt.AlignmentFlag.AlignVCenter)
layout.addWidget(label, 0)
self.image = QtWidgets.QLabel()
self.image.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter |
QtCore.Qt.AlignmentFlag.AlignVCenter)
self.image.setMinimumSize(800 // 3, 600 // 3)
layout.addWidget(self.image)
self.setLayout(layout)
def mousePressEvent(self, event):
self.parent.set_large_image(self.index)
class EventFilter(QtCore.QObject):
# A hack keypresses can be handled globally and aren't swallowed
# by the individual widgets
def __init__(self, window):
super().__init__()
self.window = window
def eventFilter(self, receiver, event):
if event.type() == QtCore.QEvent.Type.KeyPress:
self.window.keyPressEvent(event)
return True
else:
return super().eventFilter(receiver, event)
class Dialog(QtWidgets.QDialog):
"""
The main dialog window.
"""
def __init__(self, entries):
super().__init__()
self.entries = entries
self.current_entry = -1
self.current_thumbnail = -1
event_filter = EventFilter(self)
self.installEventFilter(event_filter)
# The list of files on the left-hand side.
self.filelist = QtWidgets.QListWidget()
self.filelist.setMinimumWidth(400)
for entry in entries:
self.filelist.addItem(entry.display)
self.filelist.currentRowChanged.connect(self.set_entry)
thumbnails_box = QtWidgets.QWidget()
thumbnails_layout = QtWidgets.QVBoxLayout()
self.thumbnails = []
for i, name in enumerate(('test', 'expected', 'diff')):
thumbnail = Thumbnail(self, i, name)
thumbnails_layout.addWidget(thumbnail)
self.thumbnails.append(thumbnail)
thumbnails_box.setLayout(thumbnails_layout)
images_layout = QtWidgets.QVBoxLayout()
images_box = QtWidgets.QWidget()
self.image_display = QtWidgets.QLabel()
self.image_display.setAlignment(
QtCore.Qt.AlignmentFlag.AlignHCenter |
QtCore.Qt.AlignmentFlag.AlignVCenter)
self.image_display.setMinimumSize(800, 600)
images_layout.addWidget(self.image_display, 6)
images_box.setLayout(images_layout)
buttons_box = QtWidgets.QWidget()
buttons_layout = QtWidgets.QHBoxLayout()
accept_button = QtWidgets.QPushButton("Accept (A)")
accept_button.clicked.connect(self.accept_test)
buttons_layout.addWidget(accept_button)
reject_button = QtWidgets.QPushButton("Reject (R)")
reject_button.clicked.connect(self.reject_test)
buttons_layout.addWidget(reject_button)
buttons_box.setLayout(buttons_layout)
images_layout.addWidget(buttons_box)
main_layout = QtWidgets.QHBoxLayout()
main_layout.addWidget(self.filelist, 1)
main_layout.addWidget(thumbnails_box, 1)
main_layout.addWidget(images_box, 3)
self.setLayout(main_layout)
self.setWindowTitle("matplotlib test triager")
self.set_entry(0)
def set_entry(self, index):
if self.current_entry == index:
return
self.current_entry = index
entry = self.entries[index]
self.pixmaps = []
for fname, thumbnail in zip(entry.thumbnails, self.thumbnails):
pixmap = QtGui.QPixmap(os.fspath(fname))
scaled_pixmap = pixmap.scaled(
thumbnail.size(),
QtCore.Qt.AspectRatioMode.KeepAspectRatio,
QtCore.Qt.TransformationMode.SmoothTransformation)
thumbnail.image.setPixmap(scaled_pixmap)
self.pixmaps.append(scaled_pixmap)
self.set_large_image(0)
self.filelist.setCurrentRow(self.current_entry)
def set_large_image(self, index):
self.thumbnails[self.current_thumbnail].setFrameShape(
QtWidgets.QFrame.Shape.NoFrame)
self.current_thumbnail = index
pixmap = QtGui.QPixmap(os.fspath(
self.entries[self.current_entry]
.thumbnails[self.current_thumbnail]))
self.image_display.setPixmap(pixmap)
self.thumbnails[self.current_thumbnail].setFrameShape(
QtWidgets.QFrame.Shape.Box)
def accept_test(self):
entry = self.entries[self.current_entry]
if entry.status == 'autogen':
print('Cannot accept autogenerated test cases.')
return
entry.accept()
self.filelist.currentItem().setText(
self.entries[self.current_entry].display)
# Auto-move to the next entry
self.set_entry(min((self.current_entry + 1), len(self.entries) - 1))
def reject_test(self):
entry = self.entries[self.current_entry]
if entry.status == 'autogen':
print('Cannot reject autogenerated test cases.')
return
entry.reject()
self.filelist.currentItem().setText(
self.entries[self.current_entry].display)
# Auto-move to the next entry
self.set_entry(min((self.current_entry + 1), len(self.entries) - 1))
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key.Key_Left:
self.set_large_image((self.current_thumbnail - 1) % 3)
elif e.key() == QtCore.Qt.Key.Key_Right:
self.set_large_image((self.current_thumbnail + 1) % 3)
elif e.key() == QtCore.Qt.Key.Key_Up:
self.set_entry(max(self.current_entry - 1, 0))
elif e.key() == QtCore.Qt.Key.Key_Down:
self.set_entry(min(self.current_entry + 1, len(self.entries) - 1))
elif e.key() == QtCore.Qt.Key.Key_A:
self.accept_test()
elif e.key() == QtCore.Qt.Key.Key_R:
self.reject_test()
else:
super().keyPressEvent(e)
class Entry:
"""
A model for a single image comparison test.
"""
def __init__(self, path, root, source):
self.source = source
self.root = root
self.dir = path.parent
self.diff = path.name
self.reldir = self.dir.relative_to(self.root)
basename = self.diff[:-len('-failed-diff.png')]
for ext in exts:
if basename.endswith(f'_{ext}'):
display_extension = f'_{ext}'
extension = ext
basename = basename[:-len(display_extension)]
break
else:
display_extension = ''
extension = 'png'
self.basename = basename
self.extension = extension
self.generated = f'{basename}.{extension}'
self.expected = f'{basename}-expected.{extension}'
self.expected_display = f'{basename}-expected{display_extension}.png'
self.generated_display = f'{basename}{display_extension}.png'
self.name = self.reldir / self.basename
self.destdir = self.get_dest_dir(self.reldir)
self.thumbnails = [
self.generated_display,
self.expected_display,
self.diff
]
self.thumbnails = [self.dir / x for x in self.thumbnails]
if not Path(self.destdir, self.generated).exists():
# This case arises from a check_figures_equal test.
self.status = 'autogen'
elif ((self.dir / self.generated).read_bytes()
== (self.destdir / self.generated).read_bytes()):
self.status = 'accept'
else:
self.status = 'unknown'
def get_dest_dir(self, reldir):
"""
Find the source tree directory corresponding to the given
result_images subdirectory.
"""
for baseline_dir in BASELINE_IMAGES:
path = self.source / baseline_dir / reldir
if path.is_dir():
return path
raise ValueError(f"Can't find baseline dir for {reldir}")
@property
def display(self):
"""
Get the display string for this entry. This is the text that
appears in the list widget.
"""
status_map = {
'unknown': '\N{BALLOT BOX}',
'accept': '\N{BALLOT BOX WITH CHECK}',
'reject': '\N{BALLOT BOX WITH X}',
'autogen': '\N{WHITE SQUARE CONTAINING BLACK SMALL SQUARE}',
}
box = status_map[self.status]
return f'{box} {self.name} [{self.extension}]'
def accept(self):
"""
Accept this test by copying the generated result to the source tree.
"""
copy_file(self.dir / self.generated, self.destdir / self.generated)
self.status = 'accept'
def reject(self):
"""
Reject this test by copying the expected result to the source tree.
"""
expected = self.dir / self.expected
if not expected.is_symlink():
copy_file(expected, self.destdir / self.generated)
self.status = 'reject'
def copy_file(a, b):
"""Copy file from *a* to *b*."""
print(f'copying: {a} to {b}')
shutil.copyfile(a, b)
def find_failing_tests(result_images, source):
"""
Find all of the failing tests by looking for files with
`-failed-diff` at the end of the basename.
"""
return [Entry(path, result_images, source)
for path in sorted(Path(result_images).glob("**/*-failed-diff.*"))]
def launch(result_images, source):
"""
Launch the GUI.
"""
entries = find_failing_tests(result_images, source)
if len(entries) == 0:
print("No failed tests")
sys.exit(0)
app = QtWidgets.QApplication(sys.argv)
dialog = Dialog(entries)
dialog.show()
filter = EventFilter(dialog)
app.installEventFilter(filter)
sys.exit(_exec(app))
if __name__ == '__main__':
import argparse
source_dir = Path(__file__).parent.parent
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""
Triage image comparison test failures.
If no arguments are provided, it assumes you ran the tests at the
top-level of a source checkout as `pytest .`.
Keys:
left/right: Move between test, expected and diff images
up/down: Move between tests
A: Accept test. Copy the test result to the source tree.
R: Reject test. Copy the expected result to the source tree.
""")
parser.add_argument("result_images", type=Path, nargs='?',
default=source_dir / 'result_images',
help="The location of the result_images directory")
parser.add_argument("source", type=Path, nargs='?', default=source_dir,
help="The location of the matplotlib source tree")
args = parser.parse_args()
launch(args.result_images, args.source)
|