|
| 1 | +""" |
| 2 | +Highlighting in three dimensions: ``highlight_edge``, ``highlight_path`` and |
| 3 | +``highlight_vertex`` draw in the axes that ``plot()`` made. |
| 4 | +
|
| 5 | +``highlight_edge`` assumed two dimensions, so on a 3D graph it added a flat |
| 6 | +patch to a 3D axes, which raised ``AttributeError`` when the figure was drawn, |
| 7 | +and ``highlight_vertex`` drew its marker at z = 0. In 0.6.5 ``highlight_path`` |
| 8 | +worked in 3D. |
| 9 | +""" |
| 10 | + |
| 11 | +import unittest |
| 12 | +import warnings |
| 13 | + |
| 14 | +import matplotlib |
| 15 | + |
| 16 | +matplotlib.use("Agg") |
| 17 | +import matplotlib.pyplot as plt |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +from pgraph import DGraph, UGraph |
| 21 | +from pgraph._plot import _Arrow3D, _Loop3D |
| 22 | + |
| 23 | + |
| 24 | +def graph(cls, dim=3): |
| 25 | + g = cls(dim=dim) |
| 26 | + coords = [[0, 0, 0], [1, 1, 0], [2, 0, 1], [0, 2, 2]] |
| 27 | + v = [g.add_vertex(c[:dim], name=n) for n, c in zip("abcd", coords)] |
| 28 | + edges = [g.add_edge(v[p], v[q]) for p, q in [(0, 1), (1, 2), (2, 3), (3, 0), (0, 1), (2, 2)]] |
| 29 | + return g, v, edges |
| 30 | + |
| 31 | + |
| 32 | +class TestHighlight3D(unittest.TestCase): |
| 33 | + def setUp(self): |
| 34 | + plt.close("all") |
| 35 | + warnings.simplefilter("ignore") |
| 36 | + |
| 37 | + def draws(self): |
| 38 | + fig = plt.gcf() |
| 39 | + fig.canvas.draw() # the failure was here, not when the patch was added |
| 40 | + return fig.axes[0] |
| 41 | + |
| 42 | + def test_highlight_edge(self): |
| 43 | + for cls in (UGraph, DGraph): |
| 44 | + with self.subTest(cls=cls.__name__): |
| 45 | + plt.close("all") |
| 46 | + g, v, edges = graph(cls) |
| 47 | + g.plot(block=None) |
| 48 | + ax = plt.gca() |
| 49 | + before = len(ax.patches) |
| 50 | + g.highlight_edge(edges[1]) |
| 51 | + self.assertEqual(len(ax.patches), before + 1) |
| 52 | + self.assertIsInstance(ax.patches[-1], _Arrow3D) |
| 53 | + self.draws() |
| 54 | + |
| 55 | + def test_highlight_a_loop(self): |
| 56 | + g, v, edges = graph(DGraph) |
| 57 | + g.plot(block=None) |
| 58 | + g.highlight_edge(edges[5]) # the loop on c |
| 59 | + self.assertIsInstance(plt.gca().patches[-1], _Loop3D) |
| 60 | + self.draws() |
| 61 | + |
| 62 | + def test_highlight_a_parallel_edge(self): |
| 63 | + g, v, edges = graph(UGraph) |
| 64 | + g.plot(block=None) |
| 65 | + g.highlight_edge(edges[4]) # the second edge from a to b |
| 66 | + self.assertIsInstance(plt.gca().patches[-1], _Arrow3D) |
| 67 | + self.draws() |
| 68 | + |
| 69 | + def test_highlight_path(self): |
| 70 | + g, v, edges = graph(UGraph) |
| 71 | + g.plot(block=None) |
| 72 | + g.highlight_path([v[0], v[1], v[2]], block=None) |
| 73 | + ax = self.draws() |
| 74 | + # the 5 edges that are not a loop, and the 2 of the path over them |
| 75 | + self.assertEqual(sum(isinstance(p, _Arrow3D) for p in ax.patches), 5 + 2) |
| 76 | + self.assertEqual(sum(isinstance(p, _Loop3D) for p in ax.patches), 1) |
| 77 | + |
| 78 | + def test_highlight_vertex_is_at_the_z_of_the_vertex(self): |
| 79 | + g, v, edges = graph(UGraph) |
| 80 | + g.plot(block=None) |
| 81 | + g.highlight_vertex(v[3]) |
| 82 | + ax = self.draws() |
| 83 | + x, y, z = ax.lines[-1].get_data_3d() |
| 84 | + np.testing.assert_allclose([x[0], y[0], z[0]], [0, 2, 2]) |
| 85 | + |
| 86 | + def test_highlight_several_vertices_by_name(self): |
| 87 | + g, v, edges = graph(UGraph) |
| 88 | + g.plot(block=None) |
| 89 | + n = len(plt.gca().lines) |
| 90 | + g.highlight_vertex(["a", v[2]]) |
| 91 | + self.assertEqual(len(plt.gca().lines), n + 2) |
| 92 | + self.draws() |
| 93 | + |
| 94 | + def test_a_3d_graph_plotted_in_2d_is_highlighted_in_2d(self): |
| 95 | + g, v, edges = graph(UGraph) |
| 96 | + g.plot(block=None, force_2d=True) |
| 97 | + g.highlight_path([v[0], v[1], v[2]], block=None) |
| 98 | + g.highlight_vertex(v[1]) |
| 99 | + ax = self.draws() |
| 100 | + self.assertNotEqual(getattr(ax, "name", None), "3d") |
| 101 | + np.testing.assert_allclose(ax.lines[-1].get_xydata(), [[1, 1]]) |
| 102 | + |
| 103 | + |
| 104 | +class TestHighlight2DUnchanged(unittest.TestCase): |
| 105 | + def setUp(self): |
| 106 | + plt.close("all") |
| 107 | + warnings.simplefilter("ignore") |
| 108 | + |
| 109 | + def test_vertex_marker(self): |
| 110 | + g, v, edges = graph(DGraph, dim=2) |
| 111 | + g.plot(block=None) |
| 112 | + g.highlight_vertex(v[2], color="g", scale=1.5, alpha=0.25) |
| 113 | + line = plt.gca().lines[-1] |
| 114 | + np.testing.assert_allclose(line.get_xydata(), [[2, 0]]) |
| 115 | + self.assertEqual(line.get_color(), "g") |
| 116 | + self.assertEqual(line.get_markersize(), 12 * 1.5) |
| 117 | + self.assertEqual(line.get_alpha(), 0.25) |
| 118 | + |
| 119 | + def test_edge_patch_is_flat(self): |
| 120 | + g, v, edges = graph(DGraph, dim=2) |
| 121 | + g.plot(block=None) |
| 122 | + g.highlight_edge(edges[0]) |
| 123 | + self.assertNotIsInstance(plt.gca().patches[-1], _Arrow3D) |
| 124 | + plt.gcf().canvas.draw() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + unittest.main() |
0 commit comments