Skip to content

Commit fa89889

Browse files
authored
Merge pull request #23561 from oscargus/tricleanups
Clean up code in tri
2 parents a84ecf1 + c739787 commit fa89889

File tree

4 files changed

+15
-25
lines changed

4 files changed

+15
-25
lines changed

lib/matplotlib/tri/triangulation.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,14 @@ def __init__(self, x, y, triangles=None, mask=None):
8080
f'range 0 <= i < {len(self.x)} but found value '
8181
f'{self.triangles.min()}')
8282

83-
if mask is not None:
84-
self.mask = np.asarray(mask, dtype=bool)
85-
if self.mask.shape != (self.triangles.shape[0],):
86-
raise ValueError('mask array must have same length as '
87-
'triangles array')
88-
8983
# Underlying C++ object is not created until first needed.
9084
self._cpp_triangulation = None
9185

9286
# Default TriFinder not created until needed.
9387
self._trifinder = None
9488

89+
self.set_mask(mask)
90+
9591
def calculate_plane_coefficients(self, z):
9692
"""
9793
Calculate plane equation coefficients for all unmasked triangles from

lib/matplotlib/tri/tricontour.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TriContourSet(ContourSet):
1919
def __init__(self, ax, *args, **kwargs):
2020
"""
2121
Draw triangular grid contour lines or filled regions,
22-
depending on whether keyword arg 'filled' is False
22+
depending on whether keyword arg *filled* is False
2323
(default) or True.
2424
2525
The first argument of the initializer must be an `~.axes.Axes`
@@ -51,10 +51,6 @@ def _process_args(self, *args, **kwargs):
5151
return kwargs
5252

5353
def _contour_args(self, args, kwargs):
54-
if self.filled:
55-
fn = 'contourf'
56-
else:
57-
fn = 'contour'
5854
tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args,
5955
**kwargs)
6056
z = np.ma.asarray(args[0])
@@ -76,7 +72,8 @@ def _contour_args(self, args, kwargs):
7672
self.zmax = float(z_check.max())
7773
self.zmin = float(z_check.min())
7874
if self.logscale and self.zmin <= 0:
79-
raise ValueError('Cannot %s log of negative values.' % fn)
75+
func = 'contourf' if self.filled else 'contour'
76+
raise ValueError(f'Cannot {func} log of negative values.')
8077
self._process_contour_level_args(args[1:])
8178
return (tri, z)
8279

lib/matplotlib/tri/triinterpolate.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,15 @@ def gradient(self, x, y):
272272
gradient.__doc__ = TriInterpolator._docstringgradient
273273

274274
def _interpolate_single_key(self, return_key, tri_index, x, y):
275+
_api.check_in_list(['z', 'dzdx', 'dzdy'], return_key=return_key)
275276
if return_key == 'z':
276277
return (self._plane_coefficients[tri_index, 0]*x +
277278
self._plane_coefficients[tri_index, 1]*y +
278279
self._plane_coefficients[tri_index, 2])
279280
elif return_key == 'dzdx':
280281
return self._plane_coefficients[tri_index, 0]
281-
elif return_key == 'dzdy':
282+
else: # 'dzdy'
282283
return self._plane_coefficients[tri_index, 1]
283-
else:
284-
raise ValueError("Invalid return_key: " + return_key)
285284

286285

287286
class CubicTriInterpolator(TriInterpolator):
@@ -413,6 +412,7 @@ def __init__(self, triangulation, z, kind='min_E', trifinder=None,
413412
# Computing eccentricities
414413
self._eccs = self._compute_tri_eccentricities(self._tris_pts)
415414
# Computing dof estimations for HCT triangle shape function
415+
_api.check_in_list(['user', 'geom', 'min_E'], kind=kind)
416416
self._dof = self._compute_dof(kind, dz=dz)
417417
# Loading HCT element
418418
self._ReferenceElement = _ReducedHCT_Element()
@@ -428,23 +428,22 @@ def gradient(self, x, y):
428428
gradient.__doc__ = TriInterpolator._docstringgradient
429429

430430
def _interpolate_single_key(self, return_key, tri_index, x, y):
431+
_api.check_in_list(['z', 'dzdx', 'dzdy'], return_key=return_key)
431432
tris_pts = self._tris_pts[tri_index]
432433
alpha = self._get_alpha_vec(x, y, tris_pts)
433434
ecc = self._eccs[tri_index]
434435
dof = np.expand_dims(self._dof[tri_index], axis=1)
435436
if return_key == 'z':
436437
return self._ReferenceElement.get_function_values(
437438
alpha, ecc, dof)
438-
elif return_key in ['dzdx', 'dzdy']:
439+
else: # 'dzdx', 'dzdy'
439440
J = self._get_jacobian(tris_pts)
440441
dzdx = self._ReferenceElement.get_function_derivatives(
441442
alpha, J, ecc, dof)
442443
if return_key == 'dzdx':
443444
return dzdx[:, 0, 0]
444445
else:
445446
return dzdx[:, 1, 0]
446-
else:
447-
raise ValueError("Invalid return_key: " + return_key)
448447

449448
def _compute_dof(self, kind, dz=None):
450449
"""
@@ -472,10 +471,8 @@ def _compute_dof(self, kind, dz=None):
472471
TE = _DOF_estimator_user(self, dz=dz)
473472
elif kind == 'geom':
474473
TE = _DOF_estimator_geom(self)
475-
elif kind == 'min_E':
474+
else: # 'min_E', checked in __init__
476475
TE = _DOF_estimator_min_E(self)
477-
else:
478-
_api.check_in_list(['user', 'geom', 'min_E'], kind=kind)
479476
return TE.compute_dof_from_df()
480477

481478
@staticmethod
@@ -1510,7 +1507,7 @@ def _roll_vectorized(M, roll_indices, axis):
15101507
for ir in range(r):
15111508
for ic in range(c):
15121509
M_roll[:, ir, ic] = M[vec_indices, (-roll_indices+ir) % r, ic]
1513-
elif axis == 1:
1510+
else: # 1
15141511
for ir in range(r):
15151512
for ic in range(c):
15161513
M_roll[:, ir, ic] = M[vec_indices, ir, (-roll_indices+ic) % c]
@@ -1562,15 +1559,15 @@ def _extract_submatrices(M, block_indices, block_size, axis):
15621559
r, c = M.shape
15631560
if axis == 0:
15641561
sh = [block_indices.shape[0], block_size, c]
1565-
elif axis == 1:
1562+
else: # 1
15661563
sh = [block_indices.shape[0], r, block_size]
15671564

15681565
dt = M.dtype
15691566
M_res = np.empty(sh, dtype=dt)
15701567
if axis == 0:
15711568
for ir in range(block_size):
15721569
M_res[:, ir, :] = M[(block_indices*block_size+ir), :]
1573-
elif axis == 1:
1570+
else: # 1
15741571
for ic in range(block_size):
15751572
M_res[:, :, ic] = M[:, (block_indices*block_size+ic)]
15761573

lib/matplotlib/tri/tripcolor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
123123
"are specified at the points, not at the faces.")
124124
collection = TriMesh(tri, alpha=alpha, array=point_colors,
125125
cmap=cmap, norm=norm, **kwargs)
126-
else:
126+
else: # 'flat'
127127
# Vertices of triangles.
128128
maskedTris = tri.get_masked_triangles()
129129
verts = np.stack((tri.x[maskedTris], tri.y[maskedTris]), axis=-1)

0 commit comments

Comments
 (0)