Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/tests/test_voronoi.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import networkx as nx | |
2 from networkx.utils import pairwise | |
3 | |
4 | |
5 class TestVoronoiCells: | |
6 """Unit tests for the Voronoi cells function.""" | |
7 | |
8 def test_isolates(self): | |
9 """Tests that a graph with isolated nodes has all isolates in | |
10 one block of the partition. | |
11 | |
12 """ | |
13 G = nx.empty_graph(5) | |
14 cells = nx.voronoi_cells(G, {0, 2, 4}) | |
15 expected = {0: {0}, 2: {2}, 4: {4}, "unreachable": {1, 3}} | |
16 assert expected == cells | |
17 | |
18 def test_undirected_unweighted(self): | |
19 G = nx.cycle_graph(6) | |
20 cells = nx.voronoi_cells(G, {0, 3}) | |
21 expected = {0: {0, 1, 5}, 3: {2, 3, 4}} | |
22 assert expected == cells | |
23 | |
24 def test_directed_unweighted(self): | |
25 # This is the singly-linked directed cycle graph on six nodes. | |
26 G = nx.DiGraph(pairwise(range(6), cyclic=True)) | |
27 cells = nx.voronoi_cells(G, {0, 3}) | |
28 expected = {0: {0, 1, 2}, 3: {3, 4, 5}} | |
29 assert expected == cells | |
30 | |
31 def test_directed_inward(self): | |
32 """Tests that reversing the graph gives the "inward" Voronoi | |
33 partition. | |
34 | |
35 """ | |
36 # This is the singly-linked reverse directed cycle graph on six nodes. | |
37 G = nx.DiGraph(pairwise(range(6), cyclic=True)) | |
38 G = G.reverse(copy=False) | |
39 cells = nx.voronoi_cells(G, {0, 3}) | |
40 expected = {0: {0, 4, 5}, 3: {1, 2, 3}} | |
41 assert expected == cells | |
42 | |
43 def test_undirected_weighted(self): | |
44 edges = [(0, 1, 10), (1, 2, 1), (2, 3, 1)] | |
45 G = nx.Graph() | |
46 G.add_weighted_edges_from(edges) | |
47 cells = nx.voronoi_cells(G, {0, 3}) | |
48 expected = {0: {0}, 3: {1, 2, 3}} | |
49 assert expected == cells | |
50 | |
51 def test_directed_weighted(self): | |
52 edges = [(0, 1, 10), (1, 2, 1), (2, 3, 1), (3, 2, 1), (2, 1, 1)] | |
53 G = nx.DiGraph() | |
54 G.add_weighted_edges_from(edges) | |
55 cells = nx.voronoi_cells(G, {0, 3}) | |
56 expected = {0: {0}, 3: {1, 2, 3}} | |
57 assert expected == cells | |
58 | |
59 def test_multigraph_unweighted(self): | |
60 """Tests that the Voronoi cells for a multigraph are the same as | |
61 for a simple graph. | |
62 | |
63 """ | |
64 edges = [(0, 1), (1, 2), (2, 3)] | |
65 G = nx.MultiGraph(2 * edges) | |
66 H = nx.Graph(G) | |
67 G_cells = nx.voronoi_cells(G, {0, 3}) | |
68 H_cells = nx.voronoi_cells(H, {0, 3}) | |
69 assert G_cells == H_cells | |
70 | |
71 def test_multidigraph_unweighted(self): | |
72 # This is the twice-singly-linked directed cycle graph on six nodes. | |
73 edges = list(pairwise(range(6), cyclic=True)) | |
74 G = nx.MultiDiGraph(2 * edges) | |
75 H = nx.DiGraph(G) | |
76 G_cells = nx.voronoi_cells(G, {0, 3}) | |
77 H_cells = nx.voronoi_cells(H, {0, 3}) | |
78 assert G_cells == H_cells | |
79 | |
80 def test_multigraph_weighted(self): | |
81 edges = [(0, 1, 10), (0, 1, 10), (1, 2, 1), (1, 2, 100), (2, 3, 1), (2, 3, 100)] | |
82 G = nx.MultiGraph() | |
83 G.add_weighted_edges_from(edges) | |
84 cells = nx.voronoi_cells(G, {0, 3}) | |
85 expected = {0: {0}, 3: {1, 2, 3}} | |
86 assert expected == cells | |
87 | |
88 def test_multidigraph_weighted(self): | |
89 edges = [ | |
90 (0, 1, 10), | |
91 (0, 1, 10), | |
92 (1, 2, 1), | |
93 (2, 3, 1), | |
94 (3, 2, 10), | |
95 (3, 2, 1), | |
96 (2, 1, 10), | |
97 (2, 1, 1), | |
98 ] | |
99 G = nx.MultiDiGraph() | |
100 G.add_weighted_edges_from(edges) | |
101 cells = nx.voronoi_cells(G, {0, 3}) | |
102 expected = {0: {0}, 3: {1, 2, 3}} | |
103 assert expected == cells |