2

I'm trying to get started using graph-tool for my company's analytics as a higher-performance replacement for networkx. I've managed to figure out a lot of things (plotting, getting centralities, degrees, etc.), but there's one thing that I cannot figure out. How do I access the original vertex values that were loaded via add_edge_list? I need this ability in order to find a specific vertex by value, e.g. to find its connected components or some other structure.

According to the documentation 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.

My goal is to load a CSV into a pandas dataframe, do some stuff to the dataframe to get it in an edge-list format, then load it into the graph. Here is my code so far:

import graph_tool as gt
import pandas as pd
df = pd.read_csv('data/2019-data.csv')
G = gt.Graph(directed=False)
props = G.add_edge_list(df[['vertex1', 'vertex2']].values, hashed=True)

This works just fine to fill the graph, but props returns empty.

len(G.get_vertices())
  183298

len(props.a)
  Traceback (most recent call last):
    File "<input>", line 1, in <module>
  TypeError: object of type 'NoneType' has no len()

Does anyone know how this works?

I'm running this in a Python 3.7 virtual environment on graph-tool 2.29.

Note: There is a similar question here, but the accepted answer doesn't work for me.

Thanks!

Bat Masterson
  • 1,164
  • 3
  • 11
  • 27

1 Answers1

2

Solved! Per this question, you can index the prop variable by the vertex id like a list, and can even create a mapping dict for quick lookups.

e.g.

mapping_dict = {props[i]: i for i in range(G.num_vertices())}

Although I don't completely understand why props.a doesn't return anything, since it is a VertexPropertyMap...

Bat Masterson
  • 1,164
  • 3
  • 11
  • 27
  • 1
    Just stumbled across this in a search, maybe it helps other foragers: If `props` is a non-scalar type, i.e. any `vector<>`, `string`, or object-type, `props.get_array()` as well as any convenience-accessors such as `props.a` will return `None`. As per [the docs](https://graph-tool.skewed.de/static/doc/graph_tool.html#graph_tool.PropertyMap.get_array), use `props.get_2d_array()`. – Oliver Baumann Aug 04 '21 at 10:13