Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the coordinates of a graph drawn by specific layout algorithm in graph-tool?

I want to do the example shown here. It works perfectly well on my machine. The problem is, I can't get the coordinates of the nodes as an array that are stored somewhere in the variable pos. How do I do this?

Thanks in advance for your response!

like image 838
Belphegor Avatar asked Jan 02 '13 11:01

Belphegor


1 Answers

I stuck a import pdb; pdb.Pdb().set_trace() right after the line assigning to pos, and poked at pos. It's a graph_tool.PropertyMap containing vertex attributes:

(Pdb) pos
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x2efbf90

There doesn't seem to be a direct way to iterate over each vertex from this object, but we can get the graph, and ask the graph for each vertex:

(Pdb) pos.get_graph().vertices()
<graph_tool.libgraph_tool_core.VertexIterator object at 0x2f99950>

This will iterate over each vertex, and we can use it as a key to pos, which is a mapping:

(Pdb) pos[pos.get_graph().vertices().next()]
array([-37.40184702,  25.3717068 ])

Or if you want all of them, you could put this in the sample program:

for vertex in g.vertices():
    print pos[vertex]

Which will print:

array([-37.40184702,  25.3717068 ])
array([-37.5790565 ,  26.77548156])
array([-35.57640651,  24.60125253])
array([-38.90262591,  24.30374   ])
array([-33.72945377,  24.31891936])
array([-32.68247364,  25.85861809])
[...]
like image 97
Phil Frost Avatar answered Sep 29 '22 21:09

Phil Frost