-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_sankey.py
More file actions
105 lines (85 loc) · 3.79 KB
/
test_sankey.py
File metadata and controls
105 lines (85 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pytest
from numpy.testing import assert_allclose, assert_array_equal
from matplotlib.sankey import Sankey
from matplotlib.testing.decorators import check_figures_equal
def test_sankey():
# let's just create a sankey instance and check the code runs
sankey = Sankey()
sankey.add()
def test_label():
s = Sankey(flows=[0.25], labels=['First'], orientations=[-1])
assert s.diagrams[0].texts[0].get_text() == 'First\n0.25'
def test_format_using_callable():
# test using callable by slightly incrementing above label example
def show_three_decimal_places(value):
return f'{value:.3f}'
s = Sankey(flows=[0.25], labels=['First'], orientations=[-1],
format=show_three_decimal_places)
assert s.diagrams[0].texts[0].get_text() == 'First\n0.250'
@pytest.mark.parametrize('kwargs, msg', (
({'gap': -1}, "'gap' is negative"),
({'gap': 1, 'radius': 2}, "'radius' is greater than 'gap'"),
({'head_angle': -1}, "'head_angle' is negative"),
({'tolerance': -1}, "'tolerance' is negative"),
({'flows': [1, -1], 'orientations': [-1, 0, 1]},
r"The shapes of 'flows' \(2,\) and 'orientations'"),
({'flows': [1, -1], 'labels': ['a', 'b', 'c']},
r"The shapes of 'flows' \(2,\) and 'labels'"),
))
def test_sankey_errors(kwargs, msg):
with pytest.raises(ValueError, match=msg):
Sankey(**kwargs)
@pytest.mark.parametrize('kwargs, msg', (
({'trunklength': -1}, "'trunklength' is negative"),
({'flows': [0.2, 0.3], 'prior': 0}, "The scaled sum of the connected"),
({'prior': -1}, "The index of the prior diagram is negative"),
({'prior': 1}, "The index of the prior diagram is 1"),
({'connect': (-1, 1), 'prior': 0}, "At least one of the connection"),
({'connect': (2, 1), 'prior': 0}, "The connection index to the source"),
({'connect': (1, 3), 'prior': 0}, "The connection index to this dia"),
({'connect': (1, 1), 'prior': 0, 'flows': [-0.2, 0.2],
'orientations': [2]}, "The value of orientations"),
({'connect': (1, 1), 'prior': 0, 'flows': [-0.2, 0.2],
'pathlengths': [2]}, "The lengths of 'flows'"),
))
def test_sankey_add_errors(kwargs, msg):
sankey = Sankey()
with pytest.raises(ValueError, match=msg):
sankey.add(flows=[0.2, -0.2])
sankey.add(**kwargs)
def test_sankey2():
s = Sankey(flows=[0.25, -0.25, 0.5, -0.5], labels=['Foo'],
orientations=[-1], unit='Bar')
sf = s.finish()
assert_array_equal(sf[0].flows, [0.25, -0.25, 0.5, -0.5])
assert sf[0].angles == [1, 3, 1, 3]
assert all([text.get_text()[0:3] == 'Foo' for text in sf[0].texts])
assert all([text.get_text()[-3:] == 'Bar' for text in sf[0].texts])
assert sf[0].text.get_text() == ''
assert_allclose(sf[0].tips,
[(-1.375, -0.52011255),
(1.375, -0.75506044),
(-0.75, -0.41522509),
(0.75, -0.8599479)])
s = Sankey(flows=[0.25, -0.25, 0, 0.5, -0.5], labels=['Foo'],
orientations=[-1], unit='Bar')
sf = s.finish()
assert_array_equal(sf[0].flows, [0.25, -0.25, 0, 0.5, -0.5])
assert sf[0].angles == [1, 3, None, 1, 3]
assert_allclose(sf[0].tips,
[(-1.375, -0.52011255),
(1.375, -0.75506044),
(0, 0),
(-0.75, -0.41522509),
(0.75, -0.8599479)])
@check_figures_equal()
def test_sankey3(fig_test, fig_ref):
ax_test = fig_test.gca()
s_test = Sankey(ax=ax_test, flows=[0.25, -0.25, -0.25, 0.25, 0.5, -0.5],
orientations=[1, -1, 1, -1, 0, 0])
s_test.finish()
ax_ref = fig_ref.gca()
s_ref = Sankey(ax=ax_ref)
s_ref.add(flows=[0.25, -0.25, -0.25, 0.25, 0.5, -0.5],
orientations=[1, -1, 1, -1, 0, 0])
s_ref.finish()