Skip to content

Commit 87ac1ea

Browse files
committed
fix(graphics): preserve unspecified dimensions in expand_dims
Signed-off-by: 李永祺 <doribelove@gmail.com>
1 parent f6a572c commit 87ac1ea

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎spatialmath/base/graphics.py‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,10 @@ def _axes_dimensions(ax: plt.Axes) -> int:
15131513
# handle the case of Animate objects pretending to be Axes
15141514
classname = ax.__class__.__name__
15151515
base_classes = ax.__class__.__bases__
1516-
if classname in ("Axes3DSubplot", "Animate") or any(base_class.__name__ in ("Axes3DSubplot", "Animate") for base_class in base_classes):
1516+
if classname in ("Axes3DSubplot", "Animate") or any(
1517+
base_class.__name__ in ("Axes3DSubplot", "Animate")
1518+
for base_class in base_classes
1519+
):
15171520
return 3
15181521
elif classname in ("AxesSubplot", "Animate2"):
15191522
return 2
@@ -1736,20 +1739,24 @@ def plotvol3(
17361739
ax._plotvol = True
17371740
return ax
17381741

1739-
def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
1742+
def expand_dims(dim: ArrayLike | None = None, nd: int = 2) -> NDArray | None:
17401743
"""
17411744
Expand compact axis dimensions
17421745
1743-
:param dim: dimensions, defaults to None
1746+
:param dim: dimensions, or None if unspecified, defaults to None
17441747
:type dim: scalar, array_like(2), array_like(4), array_like(6), optional
1745-
:param nd: number of axes dimensions, defaults to 2
1748+
:param nd: number of axes dimensions (2 or 3), defaults to 2
17461749
:type nd: int, optional
17471750
:raises ValueError: bad arguments
1748-
:return: 2d or 3d dimensions vector
1749-
:rtype: ndarray(4) or ndarray(6)
1751+
:return: 2d or 3d dimensions vector, or None if unspecified
1752+
:rtype: ndarray(4), ndarray(6), or None
17501753
17511754
Compute bounding dimensions for plots from shorthand notation.
17521755
1756+
``None`` is returned unchanged so the caller can choose whether to
1757+
autoscale or obtain dimensions from another source. No default extent
1758+
is inferred.
1759+
17531760
If ``nd==2``, [xmin, xmax, ymin, ymax]:
17541761
* A -> [-A, A, -A, A]
17551762
* [A,B] -> [A, B, A, B]
@@ -1760,6 +1767,11 @@ def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
17601767
* [A,B] -> [A, B, A, B, A, B]
17611768
* [A,B,C,D,E,F] -> [A, B, C, D, E, F]
17621769
"""
1770+
if nd not in (2, 3):
1771+
raise ValueError("nd is 2 or 3")
1772+
if dim is None:
1773+
return None
1774+
17631775
dim = smb.getvector(dim)
17641776

17651777
if nd == 2:
@@ -1780,8 +1792,6 @@ def expand_dims(dim: ArrayLike = None, nd: int = 2) -> NDArray:
17801792
return dim
17811793
else:
17821794
raise ValueError("bad dimension specified")
1783-
else:
1784-
raise ValueError("nd is 2 or 3")
17851795

17861796
def isnotebook() -> bool:
17871797
"""

‎tests/base/test_graphics.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@ class TestGraphics(unittest.TestCase):
1313
def teardown_method(self, method):
1414
plt.close("all")
1515

16+
def test_expand_dims_unspecified(self):
17+
self.assertIsNone(expand_dims())
18+
for nd in (2, 3):
19+
with self.subTest(nd=nd):
20+
self.assertIsNone(expand_dims(nd=nd))
21+
self.assertIsNone(expand_dims(None, nd=nd))
22+
23+
def test_expand_dims_specified(self):
24+
for nd in (2, 3):
25+
with self.subTest(nd=nd):
26+
# Zero is an explicit extent, not an unspecified one.
27+
np.testing.assert_array_equal(expand_dims(0, nd), [0, 0] * nd)
28+
np.testing.assert_array_equal(expand_dims(3, nd), [-3, 3] * nd)
29+
np.testing.assert_array_equal(expand_dims([-2, 5], nd), [-2, 5] * nd)
30+
limits = np.arange(2 * nd)
31+
np.testing.assert_array_equal(expand_dims(limits, nd), limits)
32+
with self.assertRaises(ValueError):
33+
expand_dims([1, 2, 3], nd)
34+
35+
def test_expand_dims_invalid_nd(self):
36+
for nd in (1, 4):
37+
for dim in (None, 1):
38+
with self.subTest(nd=nd, dim=dim):
39+
with self.assertRaisesRegex(ValueError, "nd is 2 or 3"):
40+
expand_dims(dim, nd)
41+
1642
@pytest.mark.skipif(
1743
sys.platform.startswith("darwin") and sys.version_info < (3, 11),
1844
reason="tkinter bug with mac",

0 commit comments

Comments
 (0)