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 pd
edge_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']
So you see there are no vertex_properties in G. From the graph-tool docs for add_edge_list:
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.
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?