8000 assert removed from files s* · matplotlib/matplotlib@9e4b911 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e4b911

Browse files
committed
assert removed from files s*
1 parent eaca138 commit 9e4b911

File tree

4 files changed

+76
-37
lines changed

4 files changed

+76
-37
lines changed

lib/matplotlib/sankey.py

-17Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
# argument specifies the direction of the arrows. The "main"
3030
# inputs/outputs are now specified via an orientation of 0, and there may
3131
# be several of each.
32-
# --Added assertions to catch common calling errors
32+
# --Changed assertions to ValueError to catch common calling errors (by
33+
# Francesco Montesano, franz.bergesung@gmail.com)
3334
# --Added the physical unit as a string argument to be used in the labels, so
3435
# that the values of the flows can usually be applied automatically
3536
# --Added an argument for a minimum magnitude below which flows are not shown
@@ -150,17 +151,21 @@ def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25,
150151
.. plot:: mpl_examples/api/sankey_demo_basics.py
151152
"""
152153
# Check the arguments.
153-
assert gap >= 0, (
154+
if gap <0:
155+
raise ValueError(
154156
"The gap is negative.\nThis isn't allowed because it "
155157
"would cause the paths to overlap.")
156-
assert radius <= gap, (
158+
if radius > gap:
159+
raise ValueError(
157160
"The inner radius is greater than the path spacing.\n"
158161
"This isn't allowed because it would cause the paths to overlap.")
159-
assert head_angle >= 0, (
162+
if head_angle < 0:
163+
raise ValueError(
160164
"The angle is negative.\nThis isn't allowed "
161165
"because it would cause inputs to look like "
162166
"outputs and vice versa.")
163-
assert tolerance >= 0, (
167+
if tolerance < 0:
168+
raise ValueError(
164169
"The tolerance is negative.\nIt must be a magnitude.")
165170

166171
# Create axes if necessary.
@@ -470,20 +475,23 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
470475
rotation /= 90.0
471476
if orientations is None:
472477
orientations = [0, 0]
473-
assert len(orientations) == n, (
478+
if len(orientations) != n:
479+
raise ValueError(
474480
"orientations and flows must have the same length.\n"
475481
"orientations has length %d, but flows has length %d."
476482
% (len(orientations), n))
477483
if labels != '' and getattr(labels, '__iter__', False):
478484
# iterable() isn't used because it would give True if labels is a
479485
# string
480-
assert len(labels) == n, (
486+
if len(labels) != n:
487+
raise ValueError(
481488
"If labels is a list, then labels and flows must have the "
482489
"same length.\nlabels has length %d, but flows has length %d."
483490
% (len(labels), n))
484491
else:
485492
labels = [labels] * n
486-
assert trunklength >= 0, (
493+
if trunklength < 0:
494+
raise ValueError(
487495
"trunklength is negative.\nThis isn't allowed, because it would "
488496
"cause poor layout.")
489497
if np.absolute(np.sum(flows)) > self.tolerance:
@@ -504,28 +512,35 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
504512
"cause poor layout.\nConsider changing the scale so"
505513
" that the scaled sum is approximately 1.0." % gain, 'helpful')
506514
if prior is not None:
507-
assert prior >= 0, "The index of the prior diagram is negative."
508-
assert min(connect) >= 0, (
515+
if prior < 0:
516+
raise ValueError("The index of the prior diagram is negative.")
517+
if min(connect) < 0:
518+
raise ValueError(
509519
"At least one of the connection indices is negative.")
510-
assert prior < len(self.diagrams), (
520+
if prior >= len(self.diagrams):
521+
raise ValueError(
511522
"The index of the prior diagram is %d, but there are "
512523
"only %d other diagrams.\nThe index is zero-based."
513524
% (prior, len(self.diagrams)))
514-
assert connect[0] < len(self.diagrams[prior].flows), (
525+
if connect[0] >= len(self.diagrams[prior].flows):
526+
raise ValueError(
515527
"The connection index to the source diagram is %d, but "
516528
"that diagram has only %d flow 9E88 s.\nThe index is zero-based."
517529
% (connect[0], len(self.diagrams[prior].flows)))
518-
assert connect[1] < n, (
530+
if connect[1] >= n:
531+
raise ValueError(
519532
"The connection index to this diagram is %d, but this diagram"
520533
"has only %d flows.\n The index is zero-based."
521534
% (connect[1], n))
522-
assert self.diagrams[prior].angles[connect[0]] is not None, (
535+
if self.diagrams[prior].angles[connect[0]] is not None:
536+
raise ValueError(
523537
"The connection cannot be made. Check that the magnitude "
524538
"of flow %d of diagram %d is greater than or equal to the "
525539
"specified tolerance." % (connect[0], prior))
526540
flow_error = (self.diagrams[prior].flows[connect[0]] +
527541
flows[connect[1]])
528-
assert abs(flow_error) < self.tolerance, (
542+
if abs(flow_error) >r self.tolerance:
543+
raise ValueError(
529544
"The scaled sum of the connected flows is %f, which is not "
530545
"within the tolerance (%f)." % (flow_error, self.tolerance))
531546

@@ -556,7 +571,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
556571
if is_input is not None:
557572
angles[i] = RIGHT
558573
else:
559-
assert orient == -1, (
574+
if orient != -1:
575+
raise ValueError(
560576
"The value of orientations[%d] is %d, "
561577
"but it must be -1, 0, or 1." % (i, orient))
562578
if is_input:
@@ -566,7 +582,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
566582

567583
# Justify the lengths of the paths.
568584
if iterable(pathlengths):
569-
assert len(pathlengths) == n, (
585+
if len(pathlengths) != n:
586+
raise ValueError(
570587
"If pathlengths is a list, then pathlengths and flows must "
571588
"have the same length.\npathlengths has length %d, but flows "
572589
"has length %d." % (len(pathlengths), n))

lib/matplotlib/scale.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,12 @@ def __init__(self, axis, **kwargs):
440440
subs = kwargs.pop('subsy', None)
441441
linscale = kwargs.pop('linscaley', 1.0)
442442

443-
assert base > 1.0
444-
assert linthresh > 0.0
445-
assert linscale > 0.0
443+
if base <= 1.0:
444+
raise ValueError("'basex/basey' be larger than 1")
445+
if linthresh <= 0.0:
446+
raise ValueError("'linthreshx/linthreshy' must be positive")
447+
if linscale <= 0.0:
448+
raise ValueError("'linscalex/linthreshy' must be positive")
446449

447450
self._transform = self.SymmetricalLogTransform(base,
448451
linthresh,

lib/matplotlib/spines.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def __init__(self, axes, spine_type, path, **kwargs):
7171
# non-rectangular axes is currently implemented, and this lets
7272
# t 10000 hem pass through the spines machinery without errors.)
7373
self._position = None
74-
assert isinstance(path, matplotlib.path.Path)
74+
if not isinstance(path, matplotlib.path.Path):
75+
msg = "'path' must be an instance of 'matplotlib.path.Path'"
76+
raise ValueError(msg)
7577
self._path = path
7678

7779
# To support drawing both linear and circular spines, this
@@ -176,7 +178,8 @@ def is_frame_like(self):
176178
position = ('axes', 0.5)
177179
elif position == 'zero':
178180
position = ('data', 0)
179-
assert len(position) == 2, "position should be 2-tuple"
181+
if len(position) != 2:
182+
raise ValueError("position should be 2-tuple")
180183
position_type, amount = position
181184
if position_type == 'outward' and amount == 0:
182185
return True
@@ -372,8 +375,11 @@ def set_position(self, position):
372375
# special positions
373376
pass
374377
else:
375-
assert len(position) == 2, "position should be 'center' or 2-tuple"
376-
assert position[0] in ['outward', 'axes', 'data']
378+
if len(position) != 2:
379+
raise ValueError("position should be 'center' or 2-tuple")
380+
if position[0] not in ['outward', 'axes', 'data']:
381+
msg = "position[0] should be in ['outward', 'axes', 'data']"
382+
raise ValueError(msg)
377383
self._position = position
378384
self._calc_offset_transform()
379385

lib/matplotlib/streamplot.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,19 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
9090

9191
use_multicolor_lines = isinstance(color, np.ndarray)
9292
if use_multicolor_lines:
93-
assert color.shape == grid.shape
93+
if color.shape != grid.shape:
94+
msg = "If 'color' is given, must have the shape of 'Grid(x,y)'"
95+
raise ValueError(msg)
9496
line_colors = []
9597
color = np.ma.masked_invalid(color)
9698
else:
9799
line_kw['color'] = color
98100
arrow_kw['color'] = color
99101

100102
if isinstance(linewidth, np.ndarray):
101-
assert linewidth.shape == grid.shape
103+
if linewidth.shape != grid.shape:
104+
msg = "If 'linewidth' is given, must have the shape of 'Grid(x,y)'"
105+
raise ValueError(msg)
102106
line_kw['linewidth'] = []
103107
else:
104108
line_kw['linewidth'] = linewidth
@@ -108,8 +112,9 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
108112
arrow_kw['zorder'] = zorder
109113

110114
## Sanity checks.
111-
assert u.shape == grid.shape
112-
assert v.shape == grid.shape
115+
if (u.shape != grid.shape) and (v.shape != grid.shape):
116+
msg = "'u' and 'v' must be of shape 'Grid(x,y)'"
117+
raise ValueError(msg)
113118

114119
u = np.ma.masked_invalid(u)
115120
v = np.ma.masked_invalid(v)
@@ -256,19 +261,25 @@ class Grid(object):
256261
"""Grid of data."""
257262
def __init__(self, x, y):
258263

259-
if len(x.shape) == 2:
260-
x_row = x[0]
261-
assert np.allclose(x_row, x)
264+
if x.ndim == 1:
265+
pass
266+
elif x.ndim == 2:
267+
x_row = x[0, :]
268+
if not np.allclose(x_row, x):
269+
raise ValueError("The rows of 'x' must be equal")
262270
x = x_row
263271
else:
264-
assert len(x.shape) == 1
272+
raise ValueError("'x' can have at maximum 2 dimensions")
265273

266-
if len(y.shape) == 2:
274+
if y.ndim == 1:
275+
pass
276+
elif y.ndim == 2:
267277
y_col = y[:, 0]
268-
assert np.allclose(y_col, y.T)
278+
if not np.allclose(y_col, y.T):
279+
raise ValueError("The columns of 'y' must be equal")
269280
y = y_col
270281
else:
271-
assert len(y.shape) == 1
282+
raise ValueError("'y' can have at maximum 2 dimensions")
272283

273284
self.nx = len(x)
274285
self.ny = len(y)
@@ -304,10 +315,12 @@ class StreamMask(object):
304315

305316
def __init__(self, density):
306317
if np.isscalar(density):
307-
assert density > 0
318+
if density <= 0:
319+
raise ValueError("If a scalar, 'density' must be positive")
308320
self.nx = self.ny = int(30 * density)
309321
else:
310-
assert len(density) == 2
322+
if len(density) != 2:
323+
raise ValueError("'density' can have at maximum 2 dimensions")
311324
self.nx = int(30 * density[0])
312325
self.ny = int(30 * density[1])
313326
self._mask = np.zeros((self.ny, self.nx))

0 commit comments

Comments
 (0)
0