本文整理匯總了Python中networkx.compose_all方法的典型用法代碼示例。如果您正苦於以下問題:Python networkx.compose_all方法的具體用法?Python networkx.compose_all怎麽用?Python networkx.compose_all使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類networkx
的用法示例。
在下文中一共展示了networkx.compose_all方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: simple_merge_graphs
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def simple_merge_graphs(graphs, clusters):
# Here, we only merge nodes that don't conflict
# merge graphs
merged_G = nx.compose_all(graphs)
node_count = max(merged_G.nodes()) + 1
for cluster in clusters:
# check if there are any to collapse
if len(cluster) <= 1: continue
# check for conflicts
seen = merged_G.nodes[cluster[0]]['members'].copy()
noconflict = True
for n in cluster[1:]:
if not seen.isdisjoint(merged_G.nodes[n]['members']):
noconflict = False
break
seen |= merged_G.nodes[n]['members']
if noconflict:
# no conflicts so merge
node_count += 1
merged_G = merge_node_cluster(merged_G,
cluster,
node_count,
multi_centroid=True,
check_merge_mems=True)
return merged_G
示例2: test_input_output
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_input_output():
l = [nx.Graph([(1,2)]),nx.Graph([(3,4)])]
U = nx.disjoint_union_all(l)
assert_equal(len(l),2)
C = nx.compose_all(l)
assert_equal(len(l),2)
l = [nx.Graph([(1,2)]),nx.Graph([(1,2)])]
R = nx.intersection_all(l)
assert_equal(len(l),2)
示例3: test_mixed_type_compose
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_mixed_type_compose():
G = nx.Graph()
H = nx.MultiGraph()
I = nx.Graph()
U = nx.compose_all([G,H,I])
示例4: test_input_output
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_input_output():
l = [nx.Graph([(1, 2)]), nx.Graph([(3, 4)])]
U = nx.disjoint_union_all(l)
assert_equal(len(l), 2)
C = nx.compose_all(l)
assert_equal(len(l), 2)
l = [nx.Graph([(1, 2)]), nx.Graph([(1, 2)])]
R = nx.intersection_all(l)
assert_equal(len(l), 2)
示例5: test_mixed_type_compose
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_mixed_type_compose():
G = nx.Graph()
H = nx.MultiGraph()
I = nx.Graph()
U = nx.compose_all([G, H, I])
示例6: test_empty_compose_all
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_empty_compose_all():
nx.compose_all([])
示例7: draw_stmt_graph
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def draw_stmt_graph(stmts):
"""Render the attributes of a list of Statements as directed graphs.
The layout works well for a single Statement or a few Statements at a time.
This function displays the plot of the graph using plt.show().
Parameters
----------
stmts : list[indra.statements.Statement]
A list of one or more INDRA Statements whose attribute graph should
be drawn.
"""
import networkx
try:
import matplotlib.pyplot as plt
except Exception:
logger.error('Could not import matplotlib, not drawing graph.')
return
try: # This checks whether networkx has this package to work with.
import pygraphviz
except Exception:
logger.error('Could not import pygraphviz, not drawing graph.')
return
import numpy
g = networkx.compose_all([stmt.to_graph() for stmt in stmts])
plt.figure()
plt.ion()
g.graph['graph'] = {'rankdir': 'LR'}
pos = networkx.drawing.nx_agraph.graphviz_layout(g, prog='dot')
g = g.to_undirected()
# Draw nodes
options = {
'marker': 'o',
's': 200,
'c': [0.85, 0.85, 1],
'facecolor': '0.5',
'lw': 0,
}
ax = plt.gca()
nodelist = list(g)
xy = numpy.asarray([pos[v] for v in nodelist])
node_collection = ax.scatter(xy[:, 0], xy[:, 1], **options)
node_collection.set_zorder(2)
# Draw edges
networkx.draw_networkx_edges(g, pos, arrows=False, edge_color='0.5')
# Draw labels
edge_labels = {(e[0], e[1]): e[2].get('label') for e in g.edges(data=True)}
networkx.draw_networkx_edge_labels(g, pos, edge_labels=edge_labels)
node_labels = {n[0]: n[1].get('label') for n in g.nodes(data=True)}
for key, label in node_labels.items():
if len(label) > 25:
parts = label.split(' ')
parts.insert(int(len(parts)/2), '\n')
label = ' '.join(parts)
node_labels[key] = label
networkx.draw_networkx_labels(g, pos, labels=node_labels)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
示例8: test_union_all_and_compose_all
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_union_all_and_compose_all():
K3=nx.complete_graph(3)
P3=nx.path_graph(3)
G1=nx.DiGraph()
G1.add_edge('A','B')
G1.add_edge('A','C')
G1.add_edge('A','D')
G2=nx.DiGraph()
G2.add_edge('1','2')
G2.add_edge('1','3')
G2.add_edge('1','4')
G=nx.union_all([G1,G2])
H=nx.compose_all([G1,G2])
assert_edges_equal(G.edges(),H.edges())
assert_false(G.has_edge('A','1'))
assert_raises(nx.NetworkXError, nx.union, K3, P3)
H1=nx.union_all([H,G1],rename=('H','G1'))
assert_equal(sorted(H1.nodes()),
['G1A', 'G1B', 'G1C', 'G1D',
'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
H2=nx.union_all([H,G2],rename=("H",""))
assert_equal(sorted(H2.nodes()),
['1', '2', '3', '4',
'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
assert_false(H1.has_edge('NB','NA'))
G=nx.compose_all([G,G])
assert_edges_equal(G.edges(),H.edges())
G2=nx.union_all([G2,G2],rename=('','copy'))
assert_equal(sorted(G2.nodes()),
['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4'])
assert_equal(G2.neighbors('copy4'),[])
assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4'])
assert_equal(len(G),8)
assert_equal(nx.number_of_edges(G),6)
E=nx.disjoint_union_all([G,G])
assert_equal(len(E),16)
assert_equal(nx.number_of_edges(E),12)
E=nx.disjoint_union_all([G1,G2])
assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
G1=nx.DiGraph()
G1.add_edge('A','B')
G2=nx.DiGraph()
G2.add_edge(1,2)
G3=nx.DiGraph()
G3.add_edge(11,22)
G4=nx.union_all([G1,G2,G3],rename=("G1","G2","G3"))
assert_equal(sorted(G4.nodes()),
['G1A', 'G1B', 'G21', 'G22',
'G311', 'G322'])
示例9: test_union_all_and_compose_all
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_union_all_and_compose_all():
K3 = nx.complete_graph(3)
P3 = nx.path_graph(3)
G1 = nx.DiGraph()
G1.add_edge('A', 'B')
G1.add_edge('A', 'C')
G1.add_edge('A', 'D')
G2 = nx.DiGraph()
G2.add_edge('1', '2')
G2.add_edge('1', '3')
G2.add_edge('1', '4')
G = nx.union_all([G1, G2])
H = nx.compose_all([G1, G2])
assert_edges_equal(G.edges(), H.edges())
assert_false(G.has_edge('A', '1'))
assert_raises(nx.NetworkXError, nx.union, K3, P3)
H1 = nx.union_all([H, G1], rename=('H', 'G1'))
assert_equal(sorted(H1.nodes()),
['G1A', 'G1B', 'G1C', 'G1D',
'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
H2 = nx.union_all([H, G2], rename=("H", ""))
assert_equal(sorted(H2.nodes()),
['1', '2', '3', '4',
'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
assert_false(H1.has_edge('NB', 'NA'))
G = nx.compose_all([G, G])
assert_edges_equal(G.edges(), H.edges())
G2 = nx.union_all([G2, G2], rename=('', 'copy'))
assert_equal(sorted(G2.nodes()),
['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4'])
assert_equal(sorted(G2.neighbors('copy4')), [])
assert_equal(sorted(G2.neighbors('copy1')), ['copy2', 'copy3', 'copy4'])
assert_equal(len(G), 8)
assert_equal(nx.number_of_edges(G), 6)
E = nx.disjoint_union_all([G, G])
assert_equal(len(E), 16)
assert_equal(nx.number_of_edges(E), 12)
E = nx.disjoint_union_all([G1, G2])
assert_equal(sorted(E.nodes()), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
G1 = nx.DiGraph()
G1.add_edge('A', 'B')
G2 = nx.DiGraph()
G2.add_edge(1, 2)
G3 = nx.DiGraph()
G3.add_edge(11, 22)
G4 = nx.union_all([G1, G2, G3], rename=("G1", "G2", "G3"))
assert_equal(sorted(G4.nodes()),
['G1A', 'G1B', 'G21', 'G22',
'G311', 'G322'])
示例10: test_union_all_and_compose_all
# 需要導入模塊: import networkx [as 別名]
# 或者: from networkx import compose_all [as 別名]
def test_union_all_and_compose_all():
K3=nx.complete_graph(3)
P3=nx.path_graph(3)
G1=nx.DiGraph()
G1.add_edge('A','B')
G1.add_edge('A','C')
G1.add_edge('A','D')
G2=nx.DiGraph()
G2.add_edge('1','2')
G2.add_edge('1','3')
G2.add_edge('1','4')
G=nx.union_all([G1,G2])
H=nx.compose_all([G1,G2])
assert_edges_equal(G.edges(),H.edges())
assert_false(G.has_edge('A','1'))
assert_raises(nx.NetworkXError, nx.union, K3, P3)
H1=nx.union_all([H,G1],rename=('H','G1'))
assert_equal(sorted(H1.nodes()),
['G1A', 'G1B', 'G1C', 'G1D',
'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
H2=nx.union_all([H,G2],rename=("H",""))
assert_equal(sorted(H2.nodes()),
['1', '2', '3', '4',
'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD'])
assert_false(H1.has_edge('NB','NA'))
G=nx.compose_all([G,G])
assert_edges_equal(G.edges(),H.edges())
G2=nx.union_all([G2,G2],rename=('','copy'))
assert_equal(sorted(G2.nodes()),
['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4'])
assert_equal(sorted(G2.neighbors('copy4')),[])
assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4'])
assert_equal(len(G),8)
assert_equal(nx.number_of_edges(G),6)
E=nx.disjoint_union_all([G,G])
assert_equal(len(E),16)
assert_equal(nx.number_of_edges(E),12)
E=nx.disjoint_union_all([G1,G2])
assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
G1=nx.DiGraph()
G1.add_edge('A','B')
G2=nx.DiGraph()
G2.add_edge(1,2)
G3=nx.DiGraph()
G3.add_edge(11,22)
G4=nx.union_all([G1,G2,G3],rename=("G1","G2","G3"))
assert_equal(sorted(G4.nodes()),
['G1A', 'G1B', 'G21', 'G22',
'G311', 'G322'])