29
29
# argument specifies the direction of the arrows. The "main"
30
30
# inputs/outputs are now specified via an orientation of 0, and there may
31
31
# 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)
33
34
# --Added the physical unit as a string argument to be used in the labels, so
34
35
# that the values of the flows can usually be applied automatically
35
36
# --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,
150
151
.. plot:: mpl_examples/api/sankey_demo_basics.py
151
152
"""
152
153
# Check the arguments.
153
- assert gap >= 0 , (
154
+ if gap < 0 :
155
+ raise ValueError (
154
156
"The gap is negative.\n This isn't allowed because it "
155
157
"would cause the paths to overlap." )
156
- assert radius <= gap , (
158
+ if radius > gap :
159
+ raise ValueError (
157
160
"The inner radius is greater than the path spacing.\n "
158
161
"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 (
160
164
"The angle is negative.\n This isn't allowed "
161
165
"because it would cause inputs to look like "
162
166
"outputs and vice versa." )
163
- assert tolerance >= 0 , (
167
+ if tolerance < 0 :
168
+ raise ValueError (
164
169
"The tolerance is negative.\n It must be a magnitude." )
165
170
166
171
# Create axes if necessary.
@@ -470,20 +475,23 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
470
475
rotation /= 90.0
471
476
if orientations is None :
472
477
orientations = [0 , 0 ]
473
- assert len (orientations ) == n , (
478
+ if len (orientations ) != n :
479
+ raise ValueError (
474
480
"orientations and flows must have the same length.\n "
475
481
"orientations has length %d, but flows has length %d."
476
482
% (len (orientations ), n ))
477
483
if labels != '' and getattr (labels , '__iter__' , False ):
478
484
# iterable() isn't used because it would give True if labels is a
479
485
# string
480
- assert len (labels ) == n , (
486
+ if len (labels ) != n :
487
+ raise ValueError (
481
488
"If labels is a list, then labels and flows must have the "
482
489
"same length.\n labels has length %d, but flows has length %d."
483
490
% (len (labels ), n ))
484
491
else :
485
492
labels = [labels ] * n
486
- assert trunklength >= 0 , (
493
+ if trunklength < 0 :
494
+ raise ValueError (
487
495
"trunklength is negative.\n This isn't allowed, because it would "
488
496
"cause poor layout." )
489
497
if np .absolute (np .sum (flows )) > self .tolerance :
@@ -504,28 +512,35 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
504
512
"cause poor layout.\n Consider changing the scale so"
505
513
" that the scaled sum is approximately 1.0." % gain , 'helpful' )
506
514
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 (
509
519
"At least one of the connection indices is negative." )
510
- assert prior < len (self .diagrams ), (
520
+ if prior >= len (self .diagrams ):
521
+ raise ValueError (
511
522
"The index of the prior diagram is %d, but there are "
512
523
"only %d other diagrams.\n The index is zero-based."
513
524
% (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 (
515
527
"The connection index to the source diagram is %d, but "
516
528
"that diagram has only %d flow
9E88
s.\n The index is zero-based."
517
529
% (connect [0 ], len (self .diagrams [prior ].flows )))
518
- assert connect [1 ] < n , (
530
+ if connect [1 ] >= n :
531
+ raise ValueError (
519
532
"The connection index to this diagram is %d, but this diagram"
520
533
"has only %d flows.\n The index is zero-based."
521
534
% (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 (
523
537
"The connection cannot be made. Check that the magnitude "
524
538
"of flow %d of diagram %d is greater than or equal to the "
525
539
"specified tolerance." % (connect [0 ], prior ))
526
540
flow_error = (self .diagrams [prior ].flows [connect [0 ]] +
527
541
flows [connect [1 ]])
528
- assert abs (flow_error ) < self .tolerance , (
542
+ if abs (flow_error ) > r self .tolerance :
543
+ raise ValueError (
529
544
"The scaled sum of the connected flows is %f, which is not "
530
545
"within the tolerance (%f)." % (flow_error , self .tolerance ))
531
546
@@ -556,7 +571,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
556
571
if is_input is not None :
557
572
angles [i ] = RIGHT
558
573
else :
559
- assert orient == - 1 , (
574
+ if orient != - 1 :
575
+ raise ValueError (
560
576
"The value of orientations[%d] is %d, "
561
577
"but it must be -1, 0, or 1." % (i , orient ))
562
578
if is_input :
@@ -566,7 +582,8 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
566
582
567
583
# Justify the lengths of the paths.
568
584
if iterable (pathlengths ):
569
- assert len (pathlengths ) == n , (
585
+ if len (pathlengths ) != n :
586
+ raise ValueError (
570
587
"If pathlengths is a list, then pathlengths and flows must "
571
588
"have the same length.\n pathlengths has length %d, but flows "
572
589
"has length %d." % (len (pathlengths ), n ))
0 commit comments