Graph can now merge non adjacent nodes

This commit is contained in:
Vighnesh Birodkar
2014-06-22 13:58:00 +05:30
parent 711de464e5
commit 0e35c422b3
2 changed files with 6 additions and 11 deletions
-5
View File
@@ -28,10 +28,6 @@ class RAG(nx.Graph):
Node to be merged.
"""
if not self.has_edge(i, j):
raise ValueError('Cant merge non adjacent nodes')
for x in self.neighbors(i):
if x == j:
continue
@@ -41,7 +37,6 @@ class RAG(nx.Graph):
w2 = self.get_edge_data(x, j)['weight']
w = max(w1, w2)
self.add_edge(x, j, weight=w)
self.node[j]['labels'] += self.node[i]['labels']
+6 -6
View File
@@ -11,13 +11,13 @@ def test_rag_merge():
for i in range(9):
x = random.choice(g.nodes())
y = random.choice(g.neighbors(x))
g.merge_nodes(x, y)
np.testing.assert_raises(ValueError, g.merge_nodes, 7, 9)
y = random.choice(g.nodes())
while x == y :
y = random.choice(g.nodes())
g.merge_nodes(x,y)
idx = g.nodes()[0]
sorted(g.node[idx]['labels']) == range(10)
assert sorted(g.node[idx]['labels']) == range(10)
assert g.edges() == []