me47
me47

Reputation: 215

Vertices coordinates in graph-tool

I'd like to specify the coordinates of the vertices of a graph in graph-tool in an efficient way.

Given a csv which looks like:

Node,X,Y

1,2.5,3.8

2,3.4,2.9

...

I'd like graph-tool to plot vertex 1 at position (2.5,3.8) etc...

A non efficient solution is given in : Explicit vertex position in python graph-tool , so I can basically use a for loop over all my coordinates and save them in the property map 'pos'. If my graph is 'g' and my csv is read using pandas in the dataframe 'coordinates', I can do:

for i in range(1,numnodes+1):
    pos[g.vertex(i)] = (coordinates.values[i-1,1],coordinates.values[i-1,2]) 

The problem is that my number of nodes, numnodes is big (~10^7), and this can take some time.

Is there a more efficient way to do this operation by inputting directly the data in the property map 'pos' ?

Upvotes: 1

Views: 770

Answers (2)

me47
me47

Reputation: 215

I found an answer to my question, an efficient way to do this is to use the .set_2d_array() function;

pos.set_2d_array(coordinates[['X','Y']].values.T)

does the trick. Here ".T" is the transposition function, part of the numpy library.

Upvotes: 1

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

I would try this:

pos = coordinates[['X','Y']].values

if graph-tool accepts numpy arrays, otherwise:

pos = [tuple(t) for t in coordinates[['X','Y']].values]

Upvotes: 0

Related Questions