Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cc7f94

Browse files
Speed up Collection.set_paths (matplotlib#29398)
* Faster Path creation * 3d calculation speedups _to_unmasked_float_array updates Linting and tweaks mean zsort small perf gain linting Fix tests Linting and tweaks mean zsort small perf gain linting Fix tests Update lib/matplotlib/collections.py Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com> remove Path __slots__ Code review suggestions * Remove test --------- Co-authored-by: Scott Shambaugh <scottshambaugh@users.noreply.github.com>
1 parent e1085ec commit 4cc7f94

File tree

6 files changed

+12
-20
lines changed

6 files changed

+12
-20
lines changed

‎lib/matplotlib/cbook.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,9 +1340,9 @@ def _to_unmasked_float_array(x):
13401340
values are converted to nans.
13411341
"""
13421342
if hasattr(x, 'mask'):
1343-
return np.ma.asarray(x, float).filled(np.nan)
1343+
return np.ma.asanyarray(x, float).filled(np.nan)
13441344
else:
1345-
return np.asarray(x, float)
1345+
return np.asanyarray(x, float)
13461346

13471347

13481348
def _check_1d(x):

‎lib/matplotlib/collections.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,15 +1242,15 @@ def set_verts(self, verts, closed=True):
12421242
return
12431243

12441244
# Fast path for arrays
1245-
if isinstance(verts, np.ndarray) and len(verts.shape) == 3:
1245+
if isinstance(verts, np.ndarray) and len(verts.shape) == 3andverts.size:
12461246
verts_pad = np.concatenate((verts, verts[:, :1]), axis=1)
1247-
# Creating the codes once is much faster than having Path do it
1248-
# separately each time by passing closed=True.
1249-
codes = np.empty(verts_pad.shape[1], dtype=mpath.Path.code_type)
1250-
codes[:] = mpath.Path.LINETO
1251-
codes[0] = mpath.Path.MOVETO
1252-
codes[-1] = mpath.Path.CLOSEPOLY
1253-
self._paths= [mpath.Path(xy, codes) for xy in verts_pad]
1247+
# It's faster to create the codes and internal flags once in a
1248+
# template path and reuse them.
1249+
template_path = mpath.Path(verts_pad[0], closed=True)
1250+
codes = template_path.codes
1251+
_make_path = mpath.Path._fast_from_codes_and_verts
1252+
self._paths = [_make_path(xy, codes, internals_from=template_path)
1253+
for xy in verts_pad]
12541254
return
12551255

12561256
self._paths = []

‎lib/matplotlib/path.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class Path:
7676
made up front in the constructor that will not change when the
7777
data changes.
7878
"""
79-
8079
code_type = np.uint8
8180

8281
# Path codes

‎lib/matplotlib/tests/test_transforms.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -987,12 +987,6 @@ def test_transformed_path():
987987
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)],
988988
atol=1e-15)
989989

990-
# Changing the path does not change the result (it's cached).
991-
path.points = [(0, 0)] * 4
992-
assert_allclose(trans_path.get_fully_transformed_path().vertices,
993-
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)],
994-
atol=1e-15)
995-
996990

997991
def test_transformed_patch_path():
998992
trans = mtransforms.Affine2D()

‎lib/mpl_toolkits/mplot3d/art3d.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ def _zalpha(colors, zs):
13651365
# Should really normalize against the viewing depth.
13661366
if len(colors) == 0 or len(zs) == 0:
13671367
return np.zeros((0, 4))
1368-
norm = Normalize(min(zs), max(zs))
1368+
norm = Normalize(np.min(zs), np.max(zs))
13691369
sats = 1 - norm(zs) * 0.7
13701370
rgba = np.broadcast_to(mcolors.to_rgba_array(colors), (len(zs), 4))
13711371
return np.column_stack([rgba[:, :3], rgba[:, 3] * sats])

‎lib/mpl_toolkits/mplot3d/proj3d.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ def _proj_transform_vectors(vecs, M):
164164

165165
def _proj_transform_vec_clip(vec, M, focal_length):
166166
vecw = np.dot(M, vec.data)
167-
w = vecw[3]
168-
txs, tys, tzs = vecw[0] / w, vecw[1] / w, vecw[2] / w
167+
txs, tys, tzs = vecw[0:3] / vecw[3]
169168
if np.isinf(focal_length): # don't clip orthographic projection
170169
tis = np.ones(txs.shape, dtype=bool)
171170
else:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /