本文整理汇总了Python中networkx.write_graphml方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.write_graphml方法的具体用法?Python networkx.write_graphml怎么用?Python networkx.write_graphml使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.write_graphml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_graphml
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def to_graphml(graph: BELGraph, path: Union[str, BinaryIO], schema: Optional[str] = None) -> None:
"""Write a graph to a GraphML XML file using :func:`networkx.write_graphml`.
:param graph: BEL Graph
:param path: Path to the new exported file
:param schema: Type of export. Currently supported: "simple" and "umbrella".
The .graphml file extension is suggested so Cytoscape can recognize it.
By default, this function exports using the PyBEL schema of including modifier information into the edges.
As an alternative, this function can also distinguish between
"""
if schema is None or schema == 'simple':
rv = _to_graphml_simple(graph)
elif schema == 'umbrella':
rv = _to_graphml_umbrella(graph)
else:
raise ValueError('Unhandled schema: {}'.format(schema))
nx.write_graphml(rv, path)
示例2: write_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def write_graph(self, G=None, subgraph_file=None):
if G is None:
G = self.context_graph
if subgraph_file is None:
subgraph_file = self.context_graph_file
logging.info("Writing graph.")
# write the graph out
file_format = subgraph_file.split(".")[-1]
if file_format == "graphml":
nx.write_graphml(G, subgraph_file)
elif file_format == "gml":
nx.write_gml(G, subgraph_file)
elif file_format == "gexf":
nx.write_gexf(G, subgraph_file)
elif file_format == "net":
nx.write_pajek(G, subgraph_file)
elif file_format == "yaml":
nx.write_yaml(G, subgraph_file)
elif file_format == "gpickle":
nx.write_gpickle(G, subgraph_file)
else:
print "File format not found, writing graphml."
nx.write_graphml(G, subgraph_file)
示例3: main
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def main():
# Step 1: Build up a graph
'''
G = buildupgraph.build_graph_wikipedia_pagerank_example()
out_file = 'wikipedia_pagerank_example.graphml'
nx.write_graphml(G, out_file)
'''
in_file = 'wikipedia_pagerank_example_layout.graphml' # Visualize the graph with the help of Graphviz
G = buildupgraph.read_graphml_with_position(in_file)
# Step 2: PageRank calculation
node_and_pr = pagerank_iterative(G)
# Normalized PageRank
total_pr = sum(node_and_pr.values()) # 0.843339703286
node_and_pr = {node : pr/total_pr for node, pr in node_and_pr.items()}
# Plot the graph
node_size = [pr*30000 for node, pr in node_and_pr.items()]
node_and_labels = {node : node+'\n'+str(round(pr, 3))
for node, pr in node_and_pr.items()}
plotnxgraph.plot_graph(G, node_size=node_size, node_and_labels=node_and_labels)
示例4: realign_all_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def realign_all_nodes(inGraph, input_dict):
logging.info('Running realign_all_nodes')
realign_node_list = []
iso_list = inGraph.graph['isolates'].split(',')
# Load genomes into memory
# Only need to realign nodes with more than one isolate in them
for node, data in inGraph.nodes(data=True):
# print(data)
if len(data['ids'].split(',')) > 1:
realign_node_list.append(node)
# Realign the nodes. This is where multiprocessing will come in.
for a_node in realign_node_list:
inGraph = local_node_realign_new(inGraph, a_node, input_dict[1])
nx.write_graphml(inGraph, 'intermediate_split_unlinked.xml')
return inGraph
示例5: test_create_genome_alignment_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def test_create_genome_alignment_graph(self):
"""Is the correct block graph created?"""
test_bbone_file = './unitTestFiles/globalAlignment_phyloTest.backbone'
parsed_input_dict = parse_seq_file('./unitTestFiles/multiGenome.txt')
created_test_graph = create_genome_alignment_graph(test_bbone_file, parsed_input_dict[0], parsed_input_dict[1])
#nx.write_graphml(created_test_graph, './test_files/unit_tests/bbone_to_graph_test_out.xml')
test_pass = True
#print test_pass
self.assertTrue(test_pass)
示例6: save
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def save(self, path, **kwargs):
# self.graph contains python lists in its meta obj, but write_graphml function does not know
# how to serialize python lists, i.e. graphml does not support lists as a valid type for serialization.
# As a workaround, we converts lists into comma-separated strings.
# We use deepcopy to avoid modifying original self.graph object.
dgraph = copy.deepcopy(self.graph)
for n, nbrs in dgraph.adjacency():
for nbr, eattr in nbrs.items():
for entry, adjitem in eattr.items():
for prop_uri, obj_curies in adjitem.items():
if obj_curies is None:
adjitem[prop_uri] = ""
else:
if isinstance(obj_curies, list):
adjitem[prop_uri] = ','.join(obj_curies)
else:
adjitem[prop_uri] = str(obj_curies)
nx.write_graphml(dgraph, path)
示例7: WriteAdjList
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def WriteAdjList(adjList, graphName, colorLabels=None):
g = nx.Graph()
for i in range(0,len(adjList)):
if (colorLabels is not None):
g.add_node(i, color=colorLabels[i], id=str(i))
else:
g.add_node(i)
idx = 0
for i in range(0,len(adjList)):
for j in range(0,len(adjList[i])):
g.add_edge(i, adjList[i][j][0], capacity=adjList[i][j][1])
if (graphName.find("gml") >= 0):
nx.write_gml(g, graphName)
elif (graphName.find("gexf") >= 0):
nx.write_gexf(g, graphName)
elif (graphName.find("graphml") >= 0):
nx.write_graphml(g, graphName)
示例8: test_default_attribute
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def test_default_attribute(self):
G=nx.Graph()
G.add_node(1,label=1,color='green')
G.add_path([0,1,2,3])
G.add_edge(1,2,weight=3)
G.graph['node_default']={'color':'yellow'}
G.graph['edge_default']={'weight':7}
fh = io.BytesIO()
nx.write_graphml(G,fh)
fh.seek(0)
H=nx.read_graphml(fh,node_type=int)
assert_equal(sorted(G.nodes()),sorted(H.nodes()))
assert_equal(
sorted(sorted(e) for e in G.edges()),
sorted(sorted(e) for e in H.edges()))
assert_equal(G.graph,H.graph)
示例9: test_unicode
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def test_unicode(self):
G = nx.Graph()
try: # Python 3.x
name1 = chr(2344) + chr(123) + chr(6543)
name2 = chr(5543) + chr(1543) + chr(324)
node_type=str
except ValueError: # Python 2.6+
name1 = unichr(2344) + unichr(123) + unichr(6543)
name2 = unichr(5543) + unichr(1543) + unichr(324)
node_type=unicode
G.add_edge(name1, 'Radiohead', attr_dict={'foo': name2})
fd, fname = tempfile.mkstemp()
nx.write_graphml(G, fname)
H = nx.read_graphml(fname,node_type=node_type)
assert_equal(G.adj, H.adj)
os.close(fd)
os.unlink(fname)
示例10: gera_graphml_G
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def gera_graphml_G(self, G, path):
nx.write_graphml(G, path)
示例11: write_graphml
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def write_graphml(self, path):
"""
Serialize the graph as .graphml.
Args:
path (str)
"""
nx.write_graphml(self.graph, path)
示例12: dump_i_cfg
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def dump_i_cfg(self):
"""Dump interim CFG for debugging purposes
"""
print("[DEBUG] Dumping CFG")
for u, v in self.i_cfg.edges():
print("{} -> {}".format(u, v))
print("[DEBUG] Writing GraphML...")
# Labeling the nodes
for node in self.i_cfg.nodes():
self.i_cfg.node[node]['label'] = "{}".format(node)
nx.write_graphml(self.i_cfg, r"D:\graphs\di.graphml")
示例13: generate_output_file
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def generate_output_file(users_collection, edges_collection, keep_empty_nodes):
graph = nx.DiGraph()
nonempty_user_ids = set()
for user in users_collection.find():
nonempty_user_ids.add(user['id'])
attrs_to_keep = [
'name',
'id',
'geo_enabled',
'followers_count',
'protected',
'lang',
'utc_offset',
'statuses_count',
'friends_count',
'screen_name',
'url',
'location',
]
user_attrs = { key : user[key] or '' for key in attrs_to_keep }
graph.add_node(user['id'], attr_dict=user_attrs)
for edge in edges_collection.find():
if keep_empty_nodes or edge['from'] in nonempty_user_ids and edge['to'] in nonempty_user_ids:
graph.add_edge(edge['from'], edge['to'])
nx.write_graphml(graph, sys.stdout)
示例14: main
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def main():
# Step 1: Build up a graph
G = build_graph_wikipedia_pagerank_example()
out_file = 'wikipedia_pagerank_example.graphml'
nx.write_graphml(G, out_file)
'''
in_file = 'wikipedia_pagerank_example_layout.graphml'
G = read_graphml_with_position(in_file)
'''
示例15: write_graphml
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_graphml [as 别名]
def write_graphml(self, path):
"""
Write a GraphML file.
Args:
path (str): The file path.
"""
nx.write_graphml(self.graph, path)