/
test_visuals.py
195 lines (146 loc) · 5.81 KB
/
test_visuals.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import pytest
import numpy as np
import matplotlib.pyplot as plt
from ripser import plot_dgms
"""
Testing visualization is a little more difficult, but still necessary. An example of how to get started:
> https://stackoverflow.com/questions/27948126/how-can-i-write-unit-tests-against-code-that-uses-matplotlib
```
def test_plot_square2():
f, ax = plt.subplots()
x, y = [0, 1, 2], [0, 1, 2]
plot_square(x, y)
x_plot, y_plot = ax.lines[0].get_xydata().T
np.testing.assert_array_equal(y_plot, np.square(y))
```
Notes
-----
ax.get_children() gives all the pieces in the plot, very useful
Scatter data is of type `PathCollection` and will be a child of ax.
"""
class TestPlotting:
def test_single(self):
""" Most just test this doesn't crash
"""
diagram = np.array([[0, 1], [1, 1], [2, 4], [3, 5]])
f, ax = plt.subplots()
plot_dgms(diagram, show=False)
x_plot, y_plot = ax.lines[0].get_xydata().T
assert x_plot[0] <= np.min(diagram)
assert x_plot[1] >= np.max(diagram)
# get PathCollection
pathcols = [child for child in ax.get_children()
if child.__class__.__name__ == "PathCollection"]
assert len(pathcols) == 1
def test_multiple(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, show=False)
pathcols = [child for child in ax.get_children()
if child.__class__.__name__ == "PathCollection"]
assert len(pathcols) == 2
np.testing.assert_array_equal(pathcols[0].get_offsets(), diagrams[0])
np.testing.assert_array_equal(pathcols[1].get_offsets(), diagrams[1])
def test_plot_only(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, legend=False, show=False, plot_only=[1])
@pytest.mark.skip
def test_show(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, legend=False, show=True)
def test_legend_true(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, legend=True, show=False)
legend = [child for child in ax.get_children()
if child.__class__.__name__ == "Legend"]
assert len(legend) == 1
def test_legend_false(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, legend=False, show=False)
legend = [child for child in ax.get_children()
if child.__class__.__name__ == "Legend"]
assert len(legend) == 0
def test_set_title(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, title='my title', show=False)
assert ax.get_title() == 'my title'
f, ax = plt.subplots()
plot_dgms(diagrams, show=False)
assert ax.get_title() == ''
def test_default_square(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, show=False)
diagonal = ax.lines[0].get_xydata()
assert diagonal[0, 0] == diagonal[0, 1]
assert diagonal[1, 0] == diagonal[1, 1]
def test_default_label(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, show=False)
assert ax.get_ylabel() == 'Death'
assert ax.get_xlabel() == 'Birth'
def test_lifetime(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, lifetime=True, show=False)
assert ax.get_ylabel() == 'Lifetime'
assert ax.get_xlabel() == 'Birth'
line = ax.get_lines()[0]
np.testing.assert_array_equal(line.get_ydata(), [0, 0])
def test_lifetime_removes_birth(self):
diagrams = [
np.array([[0, 1], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, lifetime=True, show=False)
pathcols = [child for child in ax.get_children()
if child.__class__.__name__ == "PathCollection"]
modded1 = diagrams[0]
modded1[:, 1] = diagrams[0][:, 1] - diagrams[0][:, 0]
modded2 = diagrams[1]
modded2[:, 1] = diagrams[1][:, 1] - diagrams[1][:, 0]
assert len(pathcols) == 2
np.testing.assert_array_equal(pathcols[0].get_offsets(), modded1)
np.testing.assert_array_equal(pathcols[1].get_offsets(), modded2)
def test_infty(self):
diagrams = [
np.array([[0, np.inf], [1, 1], [2, 4], [3, 5]]),
np.array([[0.5, 3], [2, 4], [4, 5], [10, 15]])
]
f, ax = plt.subplots()
plot_dgms(diagrams, legend=True, show=False)
# Right now just make sure nothing breaks