Skip to content
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
5 changes: 2 additions & 3 deletions src/machinevisiontoolbox/ImageBlobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1915,9 +1915,8 @@ def plot_labelbox(
fmt, dict(color="yellow", linewidth=2), kwargs, artist=Patch
)
for blob in self:
plot_labelbox(
text=label.format(blob.id), fmt=fmt, lrbt=blob.bbox, **options
)
# the format string is already in options, so is not passed again
plot_labelbox(text=label.format(blob.id), lrbt=blob.bbox, **options)

def plot_centroid(
self,
Expand Down
7 changes: 6 additions & 1 deletion src/machinevisiontoolbox/base/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import spatialmath.base as smb
from matplotlib import cbook, colors
from matplotlib.artist import Artist
from matplotlib.patches import Rectangle
from matplotlib.patches import Patch, Rectangle

from machinevisiontoolbox.base import name2color

Expand Down Expand Up @@ -1188,6 +1188,11 @@ def function(*fmt, ..., **kwargs):
"""

res = parse_fmt(fmt)
if artist is not None and issubclass(artist, Patch) and "marker" in res:
shown = fmt[0] if isinstance(fmt, (tuple, list)) else fmt
raise ValueError(
f"format string {shown!r} has a marker, which a {artist.__name__} cannot draw"
)

if artist is not None:
defaults = cbook.normalize_kwargs(defaults or {}, artist)
Expand Down
17 changes: 17 additions & 0 deletions tests/base/test_mpl_styling.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,20 @@ def test_dashes_keep_the_default_color():
"color": "red",
"linestyle": "--",
}


def test_a_marker_in_the_format_string_is_refused_for_a_patch():
from matplotlib.patches import Patch

with pytest.raises(ValueError, match="cannot draw"):
mpl_styling("r.", {}, {}, artist=Patch)
with pytest.raises(ValueError, match="'o'"):
mpl_styling(("o",), {}, {}, artist=Patch) # as *args arrives
# a line style, a color and a whole color name are fine
assert mpl_styling("r--", {}, {}, artist=Patch) == {
"color": "r",
"linestyle": "--",
}
# and a marker is fine for a line, or when no artist is given
assert mpl_styling("r.", {}, {}, artist=Line2D)["marker"] == "."
assert mpl_styling("r.", {}, {})["marker"] == "."
42 changes: 33 additions & 9 deletions tests/test_image_blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import matplotlib

matplotlib.use("Agg") # non-interactive backend; must precede pyplot import
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
import numpy.testing as nt
Expand Down Expand Up @@ -700,26 +701,49 @@ def _patches(ax):
return len(ax.patches)

# ------------------------------------------------------------------ #
# plot_box — SKIPPED; positional fmt arg is broken, upstream PR pending
# TODO: remove skips once the PR is merged into the release
# plot_box and plot_labelbox (one Rectangle patch per blob)
# ------------------------------------------------------------------ #

@unittest.skip(
"plot_box(): passing a positional fmt argument is broken in the current "
"release. An upstream PR has been submitted but not yet merged. "
"Revisit and remove this skip once the fix is distributed."
)
def test_plot_box_ratio(self):
self._test_plot_ratio("plot_box", self._patches)

@unittest.skip("plot_box(): see test_plot_box_ratio for details")
def test_plot_box_fmt(self):
self._test_plot_adds("plot_box", self._patches, "r--")

@unittest.skip("plot_box(): see test_plot_box_ratio for details")
def test_plot_box_kwargs(self):
self._test_plot_adds("plot_box", self._patches, color="yellow")

def _box_patch(self, method, *args, **kwargs):
fig, ax = self._fresh_ax()
getattr(self.blobs1, method)(*args, **kwargs)
patch = ax.patches[0]
plt.close(fig)
return patch

def test_box_format_strings(self):
for method in ("plot_box", "plot_labelbox"):
for fmt, color, linestyle in [
("r--", (1, 0, 0), "--"),
("g", (0, 0.5, 0), "solid"),
("C1:", mcolors.to_rgb("C1"), ":"),
("orange", mcolors.to_rgb("orange"), "solid"),
("--", mcolors.to_rgb("yellow"), "--"), # the default color
]:
with self.subTest(method=method, fmt=fmt):
patch = self._box_patch(method, fmt)
self.assertEqual(tuple(patch.get_edgecolor()[:3]), tuple(color))
self.assertEqual(patch.get_linestyle(), linestyle)

def test_box_with_a_marker_in_the_format_string_says_so(self):
for method in ("plot_box", "plot_labelbox"):
for fmt in ("o", "r."):
with self.subTest(method=method, fmt=fmt):
with self.assertRaisesRegex(ValueError, "cannot draw"):
self._box_patch(method, fmt)

def test_plot_labelbox_ratio(self):
self._test_plot_ratio("plot_labelbox", self._patches)

# ------------------------------------------------------------------ #
# plot_centroid (default: marker=["bx","bo"] → 2 Line2D per blob)
# ------------------------------------------------------------------ #
Expand Down
Loading