Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tacaswell committed Dec 31, 2024
1 parent 62a96ff commit 7708f0c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
17 changes: 9 additions & 8 deletions lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ def _recache(self):
# set to 'none'. The marker function will override this for unfilled
# markers.
self._filled = self._fillstyle != 'none'
self._marker_function()
func = getattr(self, self._marker_function)
func()

def __bool__(self):
return bool(len(self._path.vertices))
Expand Down Expand Up @@ -308,23 +309,23 @@ def _set_marker(self, marker):
`matplotlib.markers`.
"""
if isinstance(marker, str) and cbook.is_math_text(marker):
self._marker_function = self._set_mathtext_path
self._marker_function = '_set_mathtext_path'
elif isinstance(marker, (int, str)) and marker in self.markers:
self._marker_function = getattr(self, '_set_' + self.markers[marker])
self._marker_function = f'_set_{self.markers[marker]}'
elif (isinstance(marker, np.ndarray) and marker.ndim == 2 and
marker.shape[1] == 2):
self._marker_function = self._set_vertices
self._marker_function = '_set_vertices'
elif isinstance(marker, Path):
self._marker_function = self._set_path_marker
self._marker_function = '_set_path_marker'
elif (isinstance(marker, Sized) and len(marker) in (2, 3) and
marker[1] in (0, 1, 2)):
self._marker_function = self._set_tuple_marker
self._marker_function = '_set_tuple_marker'
elif isinstance(marker, MarkerStyle):
self.__dict__ = copy.deepcopy(marker.__dict__)
self.__dict__.update(copy.deepcopy(marker.__dict__))
else:
try:
Path(marker)
self._marker_function = self._set_vertices
self._marker_function = '_set_vertices'
except ValueError as err:
raise ValueError(
f'Unrecognized marker style {marker!r}') from err
Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def _load_image(path):
return np.asarray(img)


def compare_images(expected, actual, tol, in_decorator=False):
def compare_images(expected, actual, tol, in_decorator=False, *, deadband=0):
"""
Compare two "image" files checking differences within a tolerance.
Expand Down Expand Up @@ -469,11 +469,16 @@ def compare_images(expected, actual, tol, in_decorator=False):
if np.array_equal(expected_image, actual_image):
return None

rms, abs_diff = _image.calculate_rms_and_diff(expected_image, actual_image)
rms, abs_diff, max_difference = _image.calculate_rms_and_diff(
expected_image, actual_image
)

if rms <= tol:
return None

if max_difference <= deadband:
return None

Image.fromarray(abs_diff).save(diff_image, format="png")

results = dict(rms=rms, expected=str(expected),
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ exclude = [
"lib/matplotlib/tests/",
# tinypages is used for testing the sphinx ext,
# stubtest will import and run, opening a figure if not excluded
".*/tinypages"
".*/tinypages",
"lib/matplotlib/pylab.py"
]
files = [
"lib/matplotlib",
Expand Down
5 changes: 3 additions & 2 deletions src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ calculate_rms_and_diff(py::array_t<unsigned char> expected_image,
py::array_t<unsigned char> diff_image(diff_dims);
auto diff = diff_image.mutable_unchecked<3>();

auto max_difference = 0;
double total = 0.0;
for (auto i = 0; i < height; i++) {
for (auto j = 0; j < width; j++) {
Expand All @@ -257,7 +258,7 @@ calculate_rms_and_diff(py::array_t<unsigned char> expected_image,
static_cast<double>(actual(i, j, k));

total += pixel_diff*pixel_diff;

max_difference = max_difference < abs(pixel_diff) ? abs(pixel_diff) : max_difference;
if (k != 3) { // Hard-code a fully solid alpha channel by omitting it.
diff(i, j, k) = static_cast<unsigned char>(std::clamp(
abs(pixel_diff) * 10, // Expand differences in luminance domain.
Expand All @@ -268,7 +269,7 @@ calculate_rms_and_diff(py::array_t<unsigned char> expected_image,
}
total = total / (width * height * depth);

return py::make_tuple(sqrt(total), diff_image);
return py::make_tuple(sqrt(total), diff_image, max_difference);
}


Expand Down

0 comments on commit 7708f0c

Please sign in to comment.