Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_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_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_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)
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_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_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_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_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 testAlpha():
from ripser import plot_dgms
# Make a 3-sphere in 4 dimensions
X = np.random.randn(200, 4)
X = X/np.sqrt(np.sum(X**2, 1)[:, None])
tic = time.time()
dgms = alpha_filtration(X)
phattime = time.time() - tic
print("Elapsed Time: %.3g"%phattime)
plot_dgms([dgms[i] for i in range(len(dgms))])
plt.show()