Skip to content

Commit 5ab1352

Browse files
authored
Merge pull request #17289 from QuLogic/np119
Prepare for ragged array warnings in NumPy 1.19
2 parents 0165830 + 00deb04 commit 5ab1352

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3778,7 +3778,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
37783778
stats['med'] = med
37793779

37803780
if conf_intervals is not None:
3781-
if np.shape(conf_intervals)[0] != len(bxpstats):
3781+
if len(conf_intervals) != len(bxpstats):
37823782
raise ValueError(
37833783
"'conf_intervals' and 'x' have different lengths")
37843784
else:
@@ -6555,8 +6555,6 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
65556555
if histtype == 'barstacked' and not stacked:
65566556
stacked = True
65576557

6558-
# basic input validation
6559-
input_empty = np.size(x) == 0
65606558
# Massage 'x' for processing.
65616559
x = cbook._reshape_2D(x, 'x')
65626560
nx = len(x) # number of datasets
@@ -6581,9 +6579,13 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
65816579
if len(w) != nx:
65826580
raise ValueError('weights should have the same shape as x')
65836581

6582+
input_empty = True
65846583
for xi, wi in zip(x, w):
6585-
if wi is not None and len(wi) != len(xi):
6584+
len_xi = len(xi)
6585+
if wi is not None and len(wi) != len_xi:
65866586
raise ValueError('weights should have the same shape as x')
6587+
if len_xi:
6588+
input_empty = False
65876589

65886590
if color is None:
65896591
color = [self._get_lines.get_next_color() for i in range(nx)]

lib/matplotlib/cbook/__init__.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,12 @@ def _combine_masks(*args):
989989
else:
990990
if isinstance(x, np.ma.MaskedArray) and x.ndim > 1:
991991
raise ValueError("Masked arrays must be 1-D")
992-
x = np.asanyarray(x)
992+
try:
993+
x = np.asanyarray(x)
994+
except (np.VisibleDeprecationWarning, ValueError):
995+
# NumPy 1.19 raises a warning about ragged arrays, but we want
996+
# to accept basically anything here.
997+
x = np.asanyarray(x, dtype=object)
993998
if x.ndim == 1:
994999
x = safe_masked_invalid(x)
9951000
seqlist[i] = True
@@ -1316,24 +1321,48 @@ def _reshape_2D(X, name):
13161321
Use Fortran ordering to convert ndarrays and lists of iterables to lists of
13171322
1D arrays.
13181323
1319-
Lists of iterables are converted by applying `np.asarray` to each of their
1320-
elements. 1D ndarrays are returned in a singleton list containing them.
1321-
2D ndarrays are converted to the list of their *columns*.
1324+
Lists of iterables are converted by applying `np.asanyarray` to each of
1325+
their elements. 1D ndarrays are returned in a singleton list containing
1326+
them. 2D ndarrays are converted to the list of their *columns*.
13221327
13231328
*name* is used to generate the error message for invalid inputs.
13241329
"""
1325-
# Iterate over columns for ndarrays, over rows otherwise.
1326-
X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))
1330+
# Iterate over columns for ndarrays.
1331+
if isinstance(X, np.ndarray):
1332+
X = X.T
1333+
1334+
if len(X) == 0:
1335+
return [[]]
1336+
elif X.ndim == 1 and np.ndim(X[0]) == 0:
1337+
# 1D array of scalars: directly return it.
1338+
return [X]
1339+
elif X.ndim in [1, 2]:
1340+
# 2D array, or 1D array of iterables: flatten them first.
1341+
return [np.reshape(x, -1) for x in X]
1342+
else:
1343+
raise ValueError(f'{name} must have 2 or fewer dimensions')
1344+
1345+
# Iterate over list of iterables.
13271346
if len(X) == 0:
13281347
return [[]]
1329-
elif X.ndim == 1 and np.ndim(X[0]) == 0:
1348+
1349+
result = []
1350+
is_1d = True
1351+
for xi in X:
1352+
xi = np.asanyarray(xi)
1353+
nd = np.ndim(xi)
1354+
if nd > 1:
1355+
raise ValueError(f'{name} must have 2 or fewer dimensions')
1356+
elif nd == 1 and len(xi) != 1:
1357+
is_1d = False
1358+
result.append(xi.reshape(-1))
1359+
1360+
if is_1d:
13301361
# 1D array of scalars: directly return it.
1331-
return [X]
1332-
elif X.ndim in [1, 2]:
1333-
# 2D array, or 1D array of iterables: flatten them first.
1334-
return [np.reshape(x, -1) for x in X]
1362+
return [np.reshape(result, -1)]
13351363
else:
1336-
raise ValueError("{} must have 2 or fewer dimensions".format(name))
1364+
# 2D array, or 1D array of iterables: use flattened version.
1365+
return result
13371366

13381367

13391368
def violin_stats(X, method, points=100, quantiles=None):
@@ -1399,10 +1428,10 @@ def violin_stats(X, method, points=100, quantiles=None):
13991428
quantiles = _reshape_2D(quantiles, "quantiles")
14001429
# Else, mock quantiles if is none or empty
14011430
else:
1402-
quantiles = [[]] * np.shape(X)[0]
1431+
quantiles = [[]] * len(X)
14031432

14041433
# quantiles should has the same size as dataset
1405-
if np.shape(X)[:1] != np.shape(quantiles)[:1]:
1434+
if len(X) != len(quantiles):
14061435
raise ValueError("List of violinplot statistics and quantiles values"
14071436
" must have the same length")
14081437

@@ -1577,8 +1606,15 @@ def index_of(y):
15771606
try:
15781607
return y.index.values, y.values
15791608
except AttributeError:
1609+
pass
1610+
try:
15801611
y = _check_1d(y)
1612+
except (np.VisibleDeprecationWarning, ValueError):
1613+
# NumPy 1.19 will warn on ragged input, and we can't actually use it.
1614+
pass
1615+
else:
15811616
return np.arange(y.shape[0], dtype=float), y
1617+
raise ValueError('Input could not be cast to an at-least-1D NumPy array')
15821618

15831619

15841620
def safe_first_element(obj):

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ def include(gid, obj):
191191
elif obj.axes is None:
192192
return False
193193
if isinstance(obj, plt.Line2D):
194-
if np.array(obj.get_data()).shape == (2, 1):
194+
xdata, ydata = obj.get_data()
195+
if len(xdata) == len(ydata) == 1:
195196
return False
196197
elif not hasattr(obj, "axes") or obj.axes is None:
197198
return False

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def set_segments(self, segments):
257257
"""
258258
Set 3D segments.
259259
"""
260-
self._segments3d = np.asanyarray(segments)
260+
self._segments3d = segments
261261
LineCollection.set_segments(self, [])
262262

263263
def do_3d_projection(self, renderer):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy