Home > Enterprise >  Graph-tool vertex label with add_edges_from
Graph-tool vertex label with add_edges_from

Time:11-21

I tried my best to look for the solution online, but cannot seem to find one for my graph_tool network. I have a pandas dataframe df that has three columns: id_source, id_target, weight. The id_source and id_target contain names in text form. I want to use them as vertex labels. However, when adding edges from a pandas dataframe, I must set hashed=True. The code then looks like this

from graph_tool.all import *

g = Graph()
eweight = g.new_ep('double')
vmap = g.add_edge_list(df.values, eprops=[eweight], hashed=True)

My objective is to draw this small network with vertex labels. I am stuck and can't figure out the solution on how to add that vertex property when I do not know the order by which each new node is introduced in the graph through the g.add_edge_list() function.

Should I add the vertices first? And then perform the add_edge_list function on the graph. Will the vertex names (or labels) be recognized by the second step?

CodePudding user response:

From the documentation for Graph.add_edge_list:

Add a list of edges to the graph, given by edge_list, which can be an iterator of (source, target) pairs where both source and target are vertex indexes, or a numpy.ndarray of shape (E,2), where E is the number of edges, and each line specifies a (source, target) pair. If the list references vertices which do not exist in the graph, they will be created.

You have passed df.values for edge_list, which has three columns (ie a shape of (E, 3)), but two columns are expected (a shape of (E, 2)).

Try this:

g.add_edge_list(df[['id_source', 'id_target']].to_numpy(), eprops=[eweight], hashed=True)

I suspect that eprops should be df['weight'], but I am not sure.

CodePudding user response:

The answer is in the vmap. It's there all along. When you index the vmap, it will get the label names.

  • vmap[0] would be the label for the first vertex recorded.
  • vmap[1] for the second, etc.
  • Related