本文整理汇总了Python中networkx.write_weighted_edgelist方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.write_weighted_edgelist方法的具体用法?Python networkx.write_weighted_edgelist怎么用?Python networkx.write_weighted_edgelist使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.write_weighted_edgelist方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_class
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_weighted_edgelist [as 别名]
def setup_class(self, tmpdir):
n = 10
p = 0.5
wt = np.random.exponential
wtargs = dict(scale=4)
np.random.seed(1)
self.A = gs.simulations.er_np(n, p)
self.B = gs.simulations.er_np(n, p, wt=wt, wtargs=wtargs)
G_A = nx.from_numpy_array(self.A)
G_B = nx.from_numpy_array(self.B)
G_B = nx.relabel_nodes(G_B, lambda x: x + 10) # relabel nodes to go from 10-19.
self.A_path = str(tmpdir / "A_unweighted.edgelist")
self.B_path = str(tmpdir / "B.edgelist")
self.root = str(tmpdir)
nx.write_edgelist(G_A, self.A_path, data=False)
nx.write_weighted_edgelist(G_B, self.B_path)
示例2: write_weighted_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_weighted_edgelist [as 别名]
def write_weighted_edgelist(G, path, comments="#",
delimiter=' ', encoding='utf-8'):
"""Write graph G as a list of edges with numeric weights.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode.
Filenames ending in .gz or .bz2 will be compressed.
comments : string, optional
The character used to indicate the start of a comment
delimiter : string, optional
The string used to separate values. The default is whitespace.
encoding: string, optional
Specify which encoding to use when writing file.
Examples
--------
>>> G=nx.Graph()
>>> G.add_edge(1,2,weight=7)
>>> nx.write_weighted_edgelist(G, 'test.weighted.edgelist')
See Also
--------
read_edgelist()
write_edgelist()
write_weighted_edgelist()
"""
write_edgelist(G,path, comments=comments, delimiter=delimiter,
data=('weight',), encoding = encoding)
示例3: write_weighted_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_weighted_edgelist [as 别名]
def write_weighted_edgelist(G, path, comments="#",
delimiter=' ', encoding='utf-8'):
"""Write graph G as a list of edges with numeric weights.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode.
Filenames ending in .gz or .bz2 will be compressed.
comments : string, optional
The character used to indicate the start of a comment
delimiter : string, optional
The string used to separate values. The default is whitespace.
encoding: string, optional
Specify which encoding to use when writing file.
Examples
--------
>>> G=nx.Graph()
>>> G.add_edge(1,2,weight=7)
>>> nx.write_weighted_edgelist(G, 'test.weighted.edgelist')
See Also
--------
read_edgelist()
write_edgelist()
write_weighted_edgelist()
"""
write_edgelist(G, path, comments=comments, delimiter=delimiter,
data=('weight',), encoding=encoding)
示例4: save_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_weighted_edgelist [as 别名]
def save_graph(G, output_path, delimiter=',', write_stats=True, write_weights=False, write_dir=True):
r"""
Saves a graph to a file as an edgelist of weighted edgelist. If the stats parameter is set to True the file
will include several lines containing the same basic graph statistics as provided by the get_stats function.
For undirected graphs, the method stores both directions of every edge.
Parameters
----------
G : graph
A NetworkX graph
output_path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode.
delimiter : string, optional
The string used to separate values. Default is ','.
write_stats : bool, optional
Sets if graph statistics should be added to the edgelist or not. Default is True.
write_weights : bool, optional
If True data will be stored as weighted edgelist (e.g. triplets src, dst, weight) otherwise as normal edgelist.
If the graph edges have no weight attribute and this parameter is set to True,
a weight of 1 will be assigned to each edge. Default is False.
write_dir : bool, optional
This option is only relevant for undirected graphs. If False, the graph will be stored with a single
direction of the edges. If True, both directions of edges will be stored. Default is True.
"""
# Write the graph stats in the file if required
if write_stats:
get_stats(G, output_path)
# Open the file where data should be stored
f = open(output_path, 'a+b')
# Write the graph to a file and use both edge directions if graph is undirected
if G.is_directed():
# Store edgelist
if write_weights:
J = nx.DiGraph()
J.add_weighted_edges_from(G.edges.data('weight', 1))
nx.write_weighted_edgelist(J, f, delimiter=delimiter)
else:
nx.write_edgelist(G, f, delimiter=delimiter, data=False)
else:
if write_dir:
H = nx.to_directed(G)
J = nx.DiGraph()
else:
H = G
J = nx.DiGraph()
# Store edgelist
if write_weights:
J.add_weighted_edges_from(H.edges.data('weight', 1))
nx.write_weighted_edgelist(J, f, delimiter=delimiter)
else:
nx.write_edgelist(H, f, delimiter=delimiter, data=False)
示例5: write_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_weighted_edgelist [as 别名]
def write_edgelist(G, path, comments="#", delimiter=' ', data=True,
encoding = 'utf-8'):
"""Write graph as a list of edges.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed.
comments : string, optional
The character used to indicate the start of a comment
delimiter : string, optional
The string used to separate values. The default is whitespace.
data : bool or list, optional
If False write no edge data.
If True write a string representation of the edge data dictionary..
If a list (or other iterable) is provided, write the keys specified
in the list.
encoding: string, optional
Specify which encoding to use when writing file.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_edgelist(G, "test.edgelist")
>>> G=nx.path_graph(4)
>>> fh=open("test.edgelist",'wb')
>>> nx.write_edgelist(G, fh)
>>> nx.write_edgelist(G, "test.edgelist.gz")
>>> nx.write_edgelist(G, "test.edgelist.gz", data=False)
>>> G=nx.Graph()
>>> G.add_edge(1,2,weight=7,color='red')
>>> nx.write_edgelist(G,'test.edgelist',data=False)
>>> nx.write_edgelist(G,'test.edgelist',data=['color'])
>>> nx.write_edgelist(G,'test.edgelist',data=['color','weight'])
See Also
--------
write_edgelist()
write_weighted_edgelist()
"""
for line in generate_edgelist(G, delimiter, data):
line+='\n'
path.write(line.encode(encoding))
示例6: write_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import write_weighted_edgelist [as 别名]
def write_edgelist(G, path, comments="#", delimiter=' ', data=True,
encoding='utf-8'):
"""Write graph as a list of edges.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed.
comments : string, optional
The character used to indicate the start of a comment
delimiter : string, optional
The string used to separate values. The default is whitespace.
data : bool or list, optional
If False write no edge data.
If True write a string representation of the edge data dictionary..
If a list (or other iterable) is provided, write the keys specified
in the list.
encoding: string, optional
Specify which encoding to use when writing file.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_edgelist(G, "test.edgelist")
>>> G=nx.path_graph(4)
>>> fh=open("test.edgelist",'wb')
>>> nx.write_edgelist(G, fh)
>>> nx.write_edgelist(G, "test.edgelist.gz")
>>> nx.write_edgelist(G, "test.edgelist.gz", data=False)
>>> G=nx.Graph()
>>> G.add_edge(1,2,weight=7,color='red')
>>> nx.write_edgelist(G,'test.edgelist',data=False)
>>> nx.write_edgelist(G,'test.edgelist',data=['color'])
>>> nx.write_edgelist(G,'test.edgelist',data=['color','weight'])
See Also
--------
write_edgelist()
write_weighted_edgelist()
"""
for line in generate_edgelist(G, delimiter, data):
line += '\n'
path.write(line.encode(encoding))