From d3cd57a20cfe1f36b7182d8cbbb7f4dfbb5ac2af Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 8 Feb 2019 00:05:18 +0100 Subject: [PATCH] Replace np.compress by boolean indexing. It's shorter, better known, and works directly on 2D inputs without fiddling with ravel()ing (for the changes for pcolor). --- lib/matplotlib/axes/_axes.py | 22 ++++++++++------------ lib/matplotlib/cbook/__init__.py | 8 ++++---- lib/matplotlib/ticker.py | 4 ++-- lib/matplotlib/tri/triangulation.py | 2 +- lib/matplotlib/tri/tripcolor.py | 2 +- lib/matplotlib/tri/tritools.py | 2 +- 6 files changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 1b7cee64bbec..40457994025d 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5814,23 +5814,21 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None, # don't plot if C or any of the surrounding vertices are masked. mask = ma.getmaskarray(C) + xymask - compress = np.compress - - ravelmask = (mask == 0).ravel() - X1 = compress(ravelmask, ma.filled(X[:-1, :-1]).ravel()) - Y1 = compress(ravelmask, ma.filled(Y[:-1, :-1]).ravel()) - X2 = compress(ravelmask, ma.filled(X[1:, :-1]).ravel()) - Y2 = compress(ravelmask, ma.filled(Y[1:, :-1]).ravel()) - X3 = compress(ravelmask, ma.filled(X[1:, 1:]).ravel()) - Y3 = compress(ravelmask, ma.filled(Y[1:, 1:]).ravel()) - X4 = compress(ravelmask, ma.filled(X[:-1, 1:]).ravel()) - Y4 = compress(ravelmask, ma.filled(Y[:-1, 1:]).ravel()) + unmask = ~mask + X1 = ma.filled(X[:-1, :-1])[unmask] + Y1 = ma.filled(Y[:-1, :-1])[unmask] + X2 = ma.filled(X[1:, :-1])[unmask] + Y2 = ma.filled(Y[1:, :-1])[unmask] + X3 = ma.filled(X[1:, 1:])[unmask] + Y3 = ma.filled(Y[1:, 1:])[unmask] + X4 = ma.filled(X[:-1, 1:])[unmask] + Y4 = ma.filled(Y[:-1, 1:])[unmask] npoly = len(X1) xy = np.stack([X1, Y1, X2, Y2, X3, Y3, X4, Y4, X1, Y1], axis=-1) verts = xy.reshape((npoly, 5, 2)) - C = compress(ravelmask, ma.filled(C[0:Ny - 1, 0:Nx - 1]).ravel()) + C = ma.filled(C[:Ny - 1, :Nx - 1])[unmask] linewidths = (0.25,) if 'linewidth' in kwargs: diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 40a513be0ba1..5608d5c16e76 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -1311,14 +1311,14 @@ def _compute_conf_interval(data, med, iqr, bootstrap): hival = np.percentile(x, whis[1]) # get high extreme - wiskhi = np.compress(x <= hival, x) + wiskhi = x[x <= hival] if len(wiskhi) == 0 or np.max(wiskhi) < q3: stats['whishi'] = q3 else: stats['whishi'] = np.max(wiskhi) # get low extreme - wisklo = np.compress(x >= loval, x) + wisklo = x[x >= loval] if len(wisklo) == 0 or np.min(wisklo) > q1: stats['whislo'] = q1 else: @@ -1326,8 +1326,8 @@ def _compute_conf_interval(data, med, iqr, bootstrap): # compute a single array of outliers stats['fliers'] = np.hstack([ - np.compress(x < stats['whislo'], x), - np.compress(x > stats['whishi'], x) + x[x < stats['whislo']], + x[x > stats['whishi']], ]) # add in the remaining stats diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index f952e604a12a..af82e798378b 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -2641,9 +2641,9 @@ def __call__(self): mod = np.abs((locs - t0) % majorstep) cond1 = mod > minorstep / 10.0 cond2 = ~np.isclose(mod, majorstep, atol=0) - locs = locs.compress(cond1 & cond2) + locs = locs[cond1 & cond2] - return self.raise_if_exceeds(np.array(locs)) + return self.raise_if_exceeds(locs) def tick_values(self, vmin, vmax): raise NotImplementedError('Cannot get tick locations for a ' diff --git a/lib/matplotlib/tri/triangulation.py b/lib/matplotlib/tri/triangulation.py index 3374e2dfe86d..07f124013595 100644 --- a/lib/matplotlib/tri/triangulation.py +++ b/lib/matplotlib/tri/triangulation.py @@ -117,7 +117,7 @@ def get_masked_triangles(self): Return an array of triangles that are not masked. """ if self.mask is not None: - return self.triangles.compress(1 - self.mask, axis=0) + return self.triangles[~self.mask] else: return self.triangles diff --git a/lib/matplotlib/tri/tripcolor.py b/lib/matplotlib/tri/tripcolor.py index f72ba8c520fc..71f5d2de6dc9 100644 --- a/lib/matplotlib/tri/tripcolor.py +++ b/lib/matplotlib/tri/tripcolor.py @@ -112,7 +112,7 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None, C = C[maskedTris].mean(axis=1) elif tri.mask is not None: # Remove color values of masked triangles. - C = C.compress(1-tri.mask) + C = C[~tri.mask] collection = PolyCollection(verts, **kwargs) diff --git a/lib/matplotlib/tri/tritools.py b/lib/matplotlib/tri/tritools.py index d97bbd56734a..9d205f225c9d 100644 --- a/lib/matplotlib/tri/tritools.py +++ b/lib/matplotlib/tri/tritools.py @@ -293,7 +293,7 @@ def _total_to_compress_renum(mask, n=None): n = np.size(mask) if mask is not None: renum = np.full(n, -1, dtype=np.int32) # Default num is -1 - valid = np.arange(n, dtype=np.int32).compress(~mask, axis=0) + valid = np.arange(n, dtype=np.int32)[~mask] renum[valid] = np.arange(np.size(valid, 0), dtype=np.int32) return renum else: