本文整理汇总了Python中networkx.write_gml方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.write_gml方法的具体用法?Python networkx.write_gml怎么用?Python networkx.write_gml使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.write_gml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [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)
示例2: create_report_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def create_report_graph(self):
for (fallacy_type, localized) in get_fallacy_types():
node = unidecode(self.normalize(localized))
graph.add_node(node, type="fallacy", Weight=10)
for premise in Premise.objects.filter(
reports__fallacy_type=fallacy_type):
#graph.add_node(premise.argument.pk, type="argument")
#graph.add_edge(premise.argument.pk, node, type="reported")
if premise.argument.channel:
channel_node = unidecode(premise.argument.channel.title)
graph.add_node(channel_node, type="channel",
Weight=premise.argument.channel.contentions.count() * 30)
graph.add_edge(channel_node, node, type="reported")
nx.write_gml(graph, 'reports.gml')
示例3: WriteAdjList
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [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)
示例4: test_quotes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def test_quotes(self):
# https://github.com/networkx/networkx/issues/1061
# Encoding quotes as HTML entities.
G = nx.path_graph(1)
attr = 'This is "quoted" and this is a copyright: ' + unichr(169)
G.node[0]['demo'] = attr
fobj = tempfile.NamedTemporaryFile()
nx.write_gml(G, fobj)
fobj.seek(0)
# Should be bytes in 2.x and 3.x
data = fobj.read().strip().decode('ascii')
answer = """graph [
name "path_graph(1)"
node [
id 0
label 0
demo "This is "quoted" and this is a copyright: ©"
]
]"""
assert_equal(data, answer)
示例5: test_quotes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def test_quotes(self):
# https://github.com/networkx/networkx/issues/1061
# Encoding quotes as HTML entities.
G = nx.path_graph(1)
G.name = "path_graph(1)"
attr = 'This is "quoted" and this is a copyright: ' + unichr(169)
G.nodes[0]['demo'] = attr
fobj = tempfile.NamedTemporaryFile()
nx.write_gml(G, fobj)
fobj.seek(0)
# Should be bytes in 2.x and 3.x
data = fobj.read().strip().decode('ascii')
answer = """graph [
name "path_graph(1)"
node [
id 0
label "0"
demo "This is "quoted" and this is a copyright: ©"
]
]"""
assert_equal(data, answer)
示例6: test_unicode_node
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def test_unicode_node(self):
node = 'node' + unichr(169)
G = nx.Graph()
G.add_node(node)
fobj = tempfile.NamedTemporaryFile()
nx.write_gml(G, fobj)
fobj.seek(0)
# Should be bytes in 2.x and 3.x
data = fobj.read().strip().decode('ascii')
answer = """graph [
node [
id 0
label "node©"
]
]"""
assert_equal(data, answer)
示例7: test_float_label
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def test_float_label(self):
node = 1.0
G = nx.Graph()
G.add_node(node)
fobj = tempfile.NamedTemporaryFile()
nx.write_gml(G, fobj)
fobj.seek(0)
# Should be bytes in 2.x and 3.x
data = fobj.read().strip().decode('ascii')
answer = """graph [
node [
id 0
label "1.0"
]
]"""
assert_equal(data, answer)
示例8: _write_gml
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def _write_gml(G, path):
"""
Wrapper around nx.write_gml
"""
import networkx as nx
return nx.write_gml(G, path, stringizer=str)
示例9: to_gml
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def to_gml(graph, outfilename, logfile=sys.stderr):
"""Write the given read graph to a GML file."""
if not outfilename.endswith('.gml'):
print('[kevlar] WARNING: GML files usually need extension .gml',
file=logfile)
networkx.write_gml(graph, outfilename, stringizer=str)
message = '[kevlar] graph written to {}'.format(outfilename)
print(message, file=logfile)
示例10: dag
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def dag(recipe_folder, config, packages="*", format='gml', hide_singletons=False):
"""
Export the DAG of packages to a graph format file for visualization
"""
dag, name2recipes = graph.build(utils.get_recipes(recipe_folder, "*"), config)
if packages != "*":
dag = graph.filter(dag, packages)
if hide_singletons:
for node in nx.nodes(dag):
if dag.degree(node) == 0:
dag.remove_node(node)
if format == 'gml':
nx.write_gml(dag, sys.stdout.buffer)
elif format == 'dot':
write_dot(dag, sys.stdout)
elif format == 'txt':
subdags = sorted(map(sorted, nx.connected_components(dag.to_undirected())))
subdags = sorted(subdags, key=len, reverse=True)
singletons = []
for i, s in enumerate(subdags):
if len(s) == 1:
singletons += s
continue
print("# subdag {0}".format(i))
subdag = dag.subgraph(s)
recipes = [
recipe for package in nx.topological_sort(subdag)
for recipe in name2recipes[package]]
print('\n'.join(recipes) + '\n')
if not hide_singletons:
print('# singletons')
recipes = [recipe for package in singletons for recipe in
name2recipes[package]]
print('\n'.join(recipes) + '\n')
示例11: create_conjunction_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def create_conjunction_graph(self):
fallacy_map = {
unidecode(key): value
for (key, value) in
get_fallacy_types()
}
for contention in Contention.objects.all():
for premise in contention.premises.all():
fallacies = filter(None, premise.reports.values_list(
'fallacy_type', flat=True))
fallacies = [
fallacy_map[unidecode(_f)]
for _f in fallacies
]
fallacies_set = set(fallacies)
for fallacy in fallacies_set:
graph.add_edges_from(
[
(unidecode(self.normalize(fallacy)),
unidecode(self.normalize(_f)))
for _f in fallacies_set
if _f != fallacy
]
)
nx.write_gml(graph, 'conjunction.gml')
示例12: reformat_network
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def reformat_network(single_gml, output_dir, isolateName):
"""Reformats the output of generate_network() for linear graphs to allow input into merge_graphs()"""
for adj in single_gml._adj:
for x in single_gml._adj[adj]:
y = single_gml._adj[adj][x]
y.pop('members')
zero = {'members': 0}
y.update(zero)
genomes = {'genomeIDs': '0'}
y.update(genomes)
for node in single_gml._node:
y = single_gml._node[node]
y.pop('members')
zero = {
'members': 0
} #members are assigned intbitset[0]. needs to be 0
y.update(zero)
to_replace = {"[": "", "]": "", "'": ""}
y['centroid'] = replace_all(str(y['centroid']), to_replace)
y['dna'] = replace_all(str(y['dna']), to_replace)
y['protein'] = replace_all(str(y['protein']), to_replace)
y['hasEnd'] = int(y['hasEnd'])
y['mergedDNA'] = int(y['mergedDNA'])
y['paralog'] = int(y['paralog'])
y['longCentroidID'] = list(y['longCentroidID'])
y['seqIDs'] = list(y['seqIDs'])
single_gml.graph.update({'isolateNames':
'x'}) # isolateName from gff filename
nx.write_gml(single_gml, output_dir + "final_graph.gml")
return single_gml
示例13: write_gml
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def write_gml(self, path):
"""
Write a GML file.
Args:
path (str): The file path.
"""
nx.write_gml(self.graph, path)
示例14: construct_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def construct_graph(frame, ligands=None, prefix="frame", threshold=6.7, save_graph=True):
atom_filter = "(name CB and protein) or (name CA and resname GLY)"
if ligands:
ligands = ligands.split(",")
for ligand in ligands:
arr = ligand.split(":")
atom_filter += " or (name %s and resname %s)" % (arr[1], arr[0])
atoms = frame.topology.select(atom_filter)
nodes_range = len(atoms)
nodes = range(0, len(atoms))
edges = []
for i in range(nodes_range - 1):
for j in range(i + 1, nodes_range):
dist = calc_distance(frame, atoms[i], atoms[j]) * 10
if dist < threshold:
edges.append((i, j))
protein_graph = nx.Graph()
protein_graph.add_nodes_from(nodes)
protein_graph.add_edges_from(edges)
if save_graph:
nx.write_gml(protein_graph, "%s_graph.gml" % prefix)
nx.write_graphml(protein_graph, "%s_graph.graphml" % prefix)
return protein_graph
示例15: WriteGraph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_gml [as 别名]
def WriteGraph(g, graphName, colorLabels=None):
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)