From eff63fb81311ac1a3e9297710718d648e473f016 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 13 Sep 2023 12:07:43 -0500 Subject: [PATCH] MNT: Numpy 2.0 removals from ndarray class xref numpy/numpy#24682 Only a handful of lines, and _mostly_ in tests/relatively unused utility functions like rgb_to_hsv, but still something that should be addressed. --- lib/matplotlib/cbook.py | 3 ++- lib/matplotlib/colors.py | 5 +++-- lib/matplotlib/tests/test_axes.py | 6 +++--- lib/matplotlib/tests/test_colors.py | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index bea97102006b..80ec1612688b 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -734,7 +734,8 @@ def safe_masked_invalid(x, copy=False): if not x.dtype.isnative: # If we have already made a copy, do the byteswap in place, else make a # copy with the byte order swapped. - x = x.byteswap(inplace=copy).newbyteorder('N') # Swap to native order. + # Swap to native order. + x = x.byteswap(inplace=copy).view(x.dtype.newbyteorder('N')) try: xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False) except TypeError: diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index d0d20806666a..904b6ecfa04b 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -726,7 +726,8 @@ def __call__(self, X, alpha=None, bytes=False): xa = np.array(X, copy=True) if not xa.dtype.isnative: - xa = xa.byteswap().newbyteorder() # Native byteorder is faster. + # Native byteorder is faster. + xa = xa.byteswap().view(xa.dtype.newbyteorder()) if xa.dtype.kind == "f": xa *= self.N # xa == 1 (== N after multiplication) is not out of range. @@ -2161,7 +2162,7 @@ def rgb_to_hsv(arr): out = np.zeros_like(arr) arr_max = arr.max(-1) ipos = arr_max > 0 - delta = arr.ptp(-1) + delta = np.ptp(arr, -1) s = np.zeros_like(delta) s[ipos] = delta[ipos] / arr_max[ipos] ipos = delta > 0 diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3b5f9019ead2..ab9dab03e543 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1333,7 +1333,7 @@ def test_pcolormesh(): Qz = np.sin(Y) + np.sin(X) Qx = (Qx + 1.1) Z = np.hypot(X, Y) / 5 - Z = (Z - Z.min()) / Z.ptp() + Z = (Z - Z.min()) / np.ptp(Z) # The color array can include masked values: Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z) @@ -1354,7 +1354,7 @@ def test_pcolormesh_small(): Qz = np.sin(Y) + np.sin(X) Qx = (Qx + 1.1) Z = np.hypot(X, Y) / 5 - Z = (Z - Z.min()) / Z.ptp() + Z = (Z - Z.min()) / np.ptp(Z) Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z) Zm2 = ma.masked_where(Qz < -0.5 * np.max(Qz), Z) @@ -1384,7 +1384,7 @@ def test_pcolormesh_alpha(): Qx = X Qy = Y + np.sin(X) Z = np.hypot(X, Y) / 5 - Z = (Z - Z.min()) / Z.ptp() + Z = (Z - Z.min()) / np.ptp(Z) vir = mpl.colormaps["viridis"].resampled(16) # make another colormap with varying alpha colors = vir(np.arange(16)) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index e8fc9baa1479..1c77f995fb53 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -216,7 +216,7 @@ def test_colormap_endian(): a = [-0.5, 0, 0.5, 1, 1.5, np.nan] for dt in ["f2", "f4", "f8"]: anative = np.ma.masked_invalid(np.array(a, dtype=dt)) - aforeign = anative.byteswap().newbyteorder() + aforeign = anative.byteswap().view(anative.dtype.newbyteorder()) assert_array_equal(cmap(anative), cmap(aforeign)) @@ -1126,7 +1126,7 @@ def alternative_hillshade(azimuth, elev, z): intensity = np.tensordot(normals, illum, axes=(2, 0)) intensity -= intensity.min() - intensity /= intensity.ptp() + intensity /= np.ptp(intensity) return intensity y, x = np.mgrid[5:0:-1, :5]