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

Skip to content

Commit b2bb7be

Browse files
authored
Merge pull request #21695 from anntzer/check_shape
MNT: Use _api.check_shape more.
2 parents c2a5e22 + 7f49dd1 commit b2bb7be

File tree

5 files changed

+10
-23
lines changed

5 files changed

+10
-23
lines changed

lib/matplotlib/colors.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,7 @@ def _create_lookup_table(N, data, gamma=1.0):
504504
adata = np.array(data)
505505
except Exception as err:
506506
raise TypeError("data must be convertible to an array") from err
507-
shape = adata.shape
508-
if len(shape) != 2 or shape[1] != 3:
509-
raise ValueError("data must be nx3 format")
507+
_api.check_shape((None, 3), data=adata)
510508

511509
x = adata[:, 0]
512510
y0 = adata[:, 1]

lib/matplotlib/transforms.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,11 +1656,10 @@ def transform_angles(self, angles, pts, radians=False, pushoff=1e-5):
16561656
raise NotImplementedError('Only defined in 2D')
16571657
angles = np.asarray(angles)
16581658
pts = np.asarray(pts)
1659-
if angles.ndim != 1 or angles.shape[0] != pts.shape[0]:
1660-
raise ValueError("'angles' must be a column vector and have same "
1661-
"number of rows as 'pts'")
1662-
if pts.shape[1] != 2:
1663-
raise ValueError("'pts' must be array with 2 columns for x, y")
1659+
_api.check_shape((None, 2), pts=pts)
1660+
_api.check_shape((None,), angles=angles)
1661+
if len(angles) != len(pts):
1662+
raise ValueError("There must be as many 'angles' as 'pts'")
16641663
# Convert to radians if desired
16651664
if not radians:
16661665
angles = np.deg2rad(angles)

lib/matplotlib/tri/triinterpolate.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,8 +1406,7 @@ def _safe_inv22_vectorized(M):
14061406
14071407
*M* : array of (2, 2) matrices to inverse, shape (n, 2, 2)
14081408
"""
1409-
assert M.ndim == 3
1410-
assert M.shape[-2:] == (2, 2)
1409+
_api.check_shape((None, 2, 2), M=M)
14111410
M_inv = np.empty_like(M)
14121411
prod1 = M[:, 0, 0]*M[:, 1, 1]
14131412
delta = prod1 - M[:, 0, 1]*M[:, 1, 0]
@@ -1441,8 +1440,7 @@ def _pseudo_inv22sym_vectorized(M):
14411440
14421441
*M* : array of (2, 2) matrices to inverse, shape (n, 2, 2)
14431442
"""
1444-
assert M.ndim == 3
1445-
assert M.shape[-2:] == (2, 2)
1443+
_api.check_shape((None, 2, 2), M=M)
14461444
M_inv = np.empty_like(M)
14471445
prod1 = M[:, 0, 0]*M[:, 1, 1]
14481446
delta = prod1 - M[:, 0, 1]*M[:, 1, 0]

lib/matplotlib/widgets.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,8 @@ def set_val(self, val):
893893
----------
894894
val : tuple or array-like of float
895895
"""
896-
val = np.sort(np.asanyarray(val))
897-
if val.shape != (2,):
898-
raise ValueError(
899-
f"val must have shape (2,) but has shape {val.shape}"
900-
)
896+
val = np.sort(val)
897+
_api.check_shape((2,), val=val)
901898
val[0] = self._min_in_bounds(val[0])
902899
val[1] = self._max_in_bounds(val[1])
903900
xy = self.poly.xy

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,8 @@ def set_box_aspect(self, aspect, *, zoom=1):
343343
if aspect is None:
344344
aspect = np.asarray((4, 4, 3), dtype=float)
345345
else:
346-
orig_aspect = aspect
347346
aspect = np.asarray(aspect, dtype=float)
348-
if aspect.shape != (3,):
349-
raise ValueError(
350-
"You must pass a 3-tuple that can be cast to floats. "
351-
f"You passed {orig_aspect!r}"
352-
)
347+
_api.check_shape((3,), aspect=aspect)
353348
# default scale tuned to match the mpl32 appearance.
354349
aspect *= 1.8294640721620434 * zoom / np.linalg.norm(aspect)
355350

0 commit comments

Comments
 (0)