Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/isomorphism/tests/test_vf2userfunc.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 """ | |
2 Tests for VF2 isomorphism algorithm for weighted graphs. | |
3 """ | |
4 | |
5 from operator import eq | |
6 | |
7 import networkx as nx | |
8 import networkx.algorithms.isomorphism as iso | |
9 | |
10 | |
11 def test_simple(): | |
12 # 16 simple tests | |
13 w = "weight" | |
14 edges = [(0, 0, 1), (0, 0, 1.5), (0, 1, 2), (1, 0, 3)] | |
15 for g1 in [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]: | |
16 | |
17 g1.add_weighted_edges_from(edges) | |
18 g2 = g1.subgraph(g1.nodes()) | |
19 if g1.is_multigraph(): | |
20 em = iso.numerical_multiedge_match("weight", 1) | |
21 else: | |
22 em = iso.numerical_edge_match("weight", 1) | |
23 assert nx.is_isomorphic(g1, g2, edge_match=em) | |
24 | |
25 for mod1, mod2 in [(False, True), (True, False), (True, True)]: | |
26 # mod1 tests a regular edge | |
27 # mod2 tests a selfloop | |
28 if g2.is_multigraph(): | |
29 if mod1: | |
30 data1 = {0: {"weight": 10}} | |
31 if mod2: | |
32 data2 = {0: {"weight": 1}, 1: {"weight": 2.5}} | |
33 else: | |
34 if mod1: | |
35 data1 = {"weight": 10} | |
36 if mod2: | |
37 data2 = {"weight": 2.5} | |
38 | |
39 g2 = g1.subgraph(g1.nodes()).copy() | |
40 if mod1: | |
41 if not g1.is_directed(): | |
42 g2._adj[1][0] = data1 | |
43 g2._adj[0][1] = data1 | |
44 else: | |
45 g2._succ[1][0] = data1 | |
46 g2._pred[0][1] = data1 | |
47 if mod2: | |
48 if not g1.is_directed(): | |
49 g2._adj[0][0] = data2 | |
50 else: | |
51 g2._succ[0][0] = data2 | |
52 g2._pred[0][0] = data2 | |
53 | |
54 assert not nx.is_isomorphic(g1, g2, edge_match=em) | |
55 | |
56 | |
57 def test_weightkey(): | |
58 g1 = nx.DiGraph() | |
59 g2 = nx.DiGraph() | |
60 | |
61 g1.add_edge("A", "B", weight=1) | |
62 g2.add_edge("C", "D", weight=0) | |
63 | |
64 assert nx.is_isomorphic(g1, g2) | |
65 em = iso.numerical_edge_match("nonexistent attribute", 1) | |
66 assert nx.is_isomorphic(g1, g2, edge_match=em) | |
67 em = iso.numerical_edge_match("weight", 1) | |
68 assert not nx.is_isomorphic(g1, g2, edge_match=em) | |
69 | |
70 g2 = nx.DiGraph() | |
71 g2.add_edge("C", "D") | |
72 assert nx.is_isomorphic(g1, g2, edge_match=em) | |
73 | |
74 | |
75 class TestNodeMatch_Graph: | |
76 def setup_method(self): | |
77 self.g1 = nx.Graph() | |
78 self.g2 = nx.Graph() | |
79 self.build() | |
80 | |
81 def build(self): | |
82 self.nm = iso.categorical_node_match("color", "") | |
83 self.em = iso.numerical_edge_match("weight", 1) | |
84 | |
85 self.g1.add_node("A", color="red") | |
86 self.g2.add_node("C", color="blue") | |
87 | |
88 self.g1.add_edge("A", "B", weight=1) | |
89 self.g2.add_edge("C", "D", weight=1) | |
90 | |
91 def test_noweight_nocolor(self): | |
92 assert nx.is_isomorphic(self.g1, self.g2) | |
93 | |
94 def test_color1(self): | |
95 assert not nx.is_isomorphic(self.g1, self.g2, node_match=self.nm) | |
96 | |
97 def test_color2(self): | |
98 self.g1.nodes["A"]["color"] = "blue" | |
99 assert nx.is_isomorphic(self.g1, self.g2, node_match=self.nm) | |
100 | |
101 def test_weight1(self): | |
102 assert nx.is_isomorphic(self.g1, self.g2, edge_match=self.em) | |
103 | |
104 def test_weight2(self): | |
105 self.g1.add_edge("A", "B", weight=2) | |
106 assert not nx.is_isomorphic(self.g1, self.g2, edge_match=self.em) | |
107 | |
108 def test_colorsandweights1(self): | |
109 iso = nx.is_isomorphic(self.g1, self.g2, node_match=self.nm, edge_match=self.em) | |
110 assert not iso | |
111 | |
112 def test_colorsandweights2(self): | |
113 self.g1.nodes["A"]["color"] = "blue" | |
114 iso = nx.is_isomorphic(self.g1, self.g2, node_match=self.nm, edge_match=self.em) | |
115 assert iso | |
116 | |
117 def test_colorsandweights3(self): | |
118 # make the weights disagree | |
119 self.g1.add_edge("A", "B", weight=2) | |
120 assert not nx.is_isomorphic( | |
121 self.g1, self.g2, node_match=self.nm, edge_match=self.em | |
122 ) | |
123 | |
124 | |
125 class TestEdgeMatch_MultiGraph: | |
126 def setup_method(self): | |
127 self.g1 = nx.MultiGraph() | |
128 self.g2 = nx.MultiGraph() | |
129 self.GM = iso.MultiGraphMatcher | |
130 self.build() | |
131 | |
132 def build(self): | |
133 g1 = self.g1 | |
134 g2 = self.g2 | |
135 | |
136 # We will assume integer weights only. | |
137 g1.add_edge("A", "B", color="green", weight=0, size=0.5) | |
138 g1.add_edge("A", "B", color="red", weight=1, size=0.35) | |
139 g1.add_edge("A", "B", color="red", weight=2, size=0.65) | |
140 | |
141 g2.add_edge("C", "D", color="green", weight=1, size=0.5) | |
142 g2.add_edge("C", "D", color="red", weight=0, size=0.45) | |
143 g2.add_edge("C", "D", color="red", weight=2, size=0.65) | |
144 | |
145 if g1.is_multigraph(): | |
146 self.em = iso.numerical_multiedge_match("weight", 1) | |
147 self.emc = iso.categorical_multiedge_match("color", "") | |
148 self.emcm = iso.categorical_multiedge_match(["color", "weight"], ["", 1]) | |
149 self.emg1 = iso.generic_multiedge_match("color", "red", eq) | |
150 self.emg2 = iso.generic_multiedge_match( | |
151 ["color", "weight", "size"], | |
152 ["red", 1, 0.5], | |
153 [eq, eq, iso.matchhelpers.close], | |
154 ) | |
155 else: | |
156 self.em = iso.numerical_edge_match("weight", 1) | |
157 self.emc = iso.categorical_edge_match("color", "") | |
158 self.emcm = iso.categorical_edge_match(["color", "weight"], ["", 1]) | |
159 self.emg1 = iso.generic_multiedge_match("color", "red", eq) | |
160 self.emg2 = iso.generic_edge_match( | |
161 ["color", "weight", "size"], | |
162 ["red", 1, 0.5], | |
163 [eq, eq, iso.matchhelpers.close], | |
164 ) | |
165 | |
166 def test_weights_only(self): | |
167 assert nx.is_isomorphic(self.g1, self.g2, edge_match=self.em) | |
168 | |
169 def test_colors_only(self): | |
170 gm = self.GM(self.g1, self.g2, edge_match=self.emc) | |
171 assert gm.is_isomorphic() | |
172 | |
173 def test_colorsandweights(self): | |
174 gm = self.GM(self.g1, self.g2, edge_match=self.emcm) | |
175 assert not gm.is_isomorphic() | |
176 | |
177 def test_generic1(self): | |
178 gm = self.GM(self.g1, self.g2, edge_match=self.emg1) | |
179 assert gm.is_isomorphic() | |
180 | |
181 def test_generic2(self): | |
182 gm = self.GM(self.g1, self.g2, edge_match=self.emg2) | |
183 assert not gm.is_isomorphic() | |
184 | |
185 | |
186 class TestEdgeMatch_DiGraph(TestNodeMatch_Graph): | |
187 def setup_method(self): | |
188 TestNodeMatch_Graph.setup_method(self) | |
189 self.g1 = nx.DiGraph() | |
190 self.g2 = nx.DiGraph() | |
191 self.build() | |
192 | |
193 | |
194 class TestEdgeMatch_MultiDiGraph(TestEdgeMatch_MultiGraph): | |
195 def setup_method(self): | |
196 TestEdgeMatch_MultiGraph.setup_method(self) | |
197 self.g1 = nx.MultiDiGraph() | |
198 self.g2 = nx.MultiDiGraph() | |
199 self.GM = iso.MultiDiGraphMatcher | |
200 self.build() |