1

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' ?

Community
  • 1
  • 1
me47
  • 215
  • 1
  • 6
  • Can you try to use it in vectorized manner: `pos = coordinates[['X','Y']].values` instead of looping? I don't know `graph-tool` module, but i guess it should be able to work with numpy arrays or maybe even with pandas data frames... – MaxU - stand with Ukraine Jun 26 '16 at 21:36

2 Answers2

1

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.

me47
  • 215
  • 1
  • 6
0

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]
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • Thanks for your answer ! Unfortunately both didn't work, I suspect it is because 'pos' looses its type when doing this operation. For example, if I do for e in pos: print e I get array([ 2.5, 3.8]) array([ 3.4, 2.9]) ... – me47 Jun 27 '16 at 01:46