我想将边列表加载到图形工具中.我列表中的顶点索引不是顺序的,因此我希望将它们自动添加为vertex_properties.据我了解,这应该使用add_edge_list完成,但是我发现未创建vertex_property"name".另一方面,load_graph_from_csv确实可以工作:
I want to load an edge-list into graph-tool. The vertex-indices in my list are not sequential, so I would like them to be automatically added as vertex_properties. As far as I understand this should be done with add_edge_list but I find that the vertex_property "name" is not created. On the other hand, load_graph_from_csv does work:
from graph_tool.all import * import numpy as np import pandas as pdedge_list = [[1,7,1],[7,4,5],[1,4,3]] G = Graph(directed=False) G.ep["length"] = G.new_edge_property("int") eprops = [G.ep["length"]] G.add_edge_list(edge_list, hashed=True, eprops=eprops) print(G.vp.keys()) print(G.ep.keys()) Out: [] ['length']
因此,您会看到G中没有vertex_properties.从图形工具文档中找到add_edge_list:
So you see there are no vertex_properties in G. From the graph-tool docs for add_edge_list:
(可选)如果hashed == True,则边缘列表中的顶点值为 不假定直接对应于顶点索引.在这种情况下 它们将根据其中的顺序映射到顶点索引 遇到它们,并带有顶点值的顶点属性映射 返回.
Optionally, if hashed == True, the vertex values in the edge list are not assumed to correspond to vertex indices directly. In this case they will be mapped to vertex indices according to the order in which they are encountered, and a vertex property map with the vertex values is returned.
现在,我发现load_graph_from_csv正在按预期工作:
Now I find that load_graph_from_csv is working as I expected:
df = pd.DataFrame.from_records(edge_list, columns=['node_1', 'node_2', 'length']) df.to_csv('edge_list.csv', sep=',', index=False) G2 = load_graph_from_csv('edge_list.csv', skip_first=True, directed=False, hashed=True, eprop_names=['length']) print(G2.vp.keys()) print(G2.ep.keys()) print([G2.vp['name'][v] for v in G2.get_vertices()]) Out: ['name'] ['length'] ['1', '7', '4']有人能指出我正确的方向吗?
Could somebody point me in the right direction?
推荐答案答案就在文档中:
"...a vertex property map with the vertex values is returned."请注意,它说的是已返回",而不是已添加到内部属性映射列表中".
Note it says "returned", not "added to the internal property map list".
只需:
name = G.add_edge_list(edge_list, hashed=True, eprops=eprops)和name是您想要的属性映射.
and name is the property map you want.