4

I am new to Networkx. I have a file containing position of nodes in following format

0 : 23.23 12.23

where 0 is a node, 23.23 and 12.23 are X and Y co-ordinates respectively. Does anyone know how to read nodes with pos attribute, using function like read_edgelist(...) or similar work around?

Thanks

soni
  • 43
  • 1
  • 3
  • Not sure whether NetworkX provides this functionality, but you can always just read the file in with basic python and then pass the nodes to NetworkX. Also I think there is no specific way of specifying a position for nodes. You may only use the position as generic node attribute. Another point is that you are missing edges. Is this supposed to be? What are the positions going to be used for? –  Dec 27 '13 at 00:37
  • May I ask, is the purpose to do visualization or is it to do analysis? NetworkX, according to my experience, has been more useful for analysis than visualization. I hacked up some d3 visualizations for that reason. – ericmjl Dec 27 '13 at 00:45
  • I forgot to mention, I am new to python also!!!. As I understood, I should read dict from file using basic python. @Nabla I have another file having edgelist. Positions are used to perform greedy routing(for nodes having info about neighbors only). Thanks for the reply. – soni Dec 27 '13 at 11:49
  • @ericmjl Visualization is not important, because my graph has some 10K nodes and 20K edges. Analysis is important. Though, it would be nice to see the graph as it can be drawn in 2D Thanks for the reply. – soni Dec 27 '13 at 11:50

1 Answers1

6

With read_edgelist, you are assuming that you have an edge list already present. However, what you've provided is a node + properties.

Since you start off with a file that has the file format (stated in your comments), the first challenge is getting it into a format that would be easily parseable. I suggested the CSV file format for that reason. To do this with your file, I would fire up the Terminal (Linux & Mac), cd into the directory with your files, and run the following two commands:

sed -n 's/ : /,/gpw nodes_replaced1.txt' nodes.txt

This reads nodes.txt (or your file), replaces all occurrences of the : (including spaces) with ,, and saves it as nodes_replaced1.txt. You can change the file names at will.

Once that is done, run the following command in terminal

sed -n 's/ /,/gwp nodes.csv' nodes_replaced1.txt

This will do a similar thing, except read in nodes_replaced1.txt, replace [spaces] with ,, and write it as a CSV file.

Once you have a CSV file, I would suggest using Pandas to open up the CSV file and do the following to add nodes into your graph:

In [1]: import pandas as pd

In [2]: import networkx as nx

In [5]: nodes = pd.read_csv('nodes.csv', header=None)

In [6]: nodes
Out[6]: 
   0      1      2
0  0  52.88  52.53
1  1  56.63  49.53
2  2  38.60  69.81
3  3  43.00   2.88

In [7]: G = nx.Graph()

In [8]: G
Out[8]: <networkx.classes.graph.Graph at 0x105e94cd0>

In [9]: for row in nodes.iterrows():
   ...:     G.add_node(row[1][0], x=row[1][1], y=row[1][2])
   ...: 

In [10]: G.nodes(data=True)
Out[10]: 
[(0.0, {'x': 52.880000000000003, 'y': 52.530000000000001}),
 (1.0, {'x': 56.630000000000003, 'y': 49.530000000000001}),
 (2.0, {'x': 38.600000000000001, 'y': 69.810000000000002}),
 (3.0, {'x': 43.0, 'y': 2.8799999999999999})]

You will note that when I call G.nodes() only, there is no x & y position data. However, when I call G.nodes(data=True), the x and y position data is incorporated.

For more information on how to create a graph, and how to put in 'attributes' associated with any node, edge, or graph, see this page: http://networkx.github.io/documentation/latest/tutorial/tutorial.html#nodes

Finally, if @Aric ever shows up to answer this question, please correct me if I'm wrong!

ericmjl
  • 13,541
  • 12
  • 51
  • 80
  • On another note, I think that Pandas is a great tool for network people to use, and if possible, I'd put in a feature request to have a NetworkX API to access Pandas objects of some sort, perhaps? – ericmjl Dec 27 '13 at 14:07
  • I have following file format `0 : 52.88 52.53` `1 : 56.63 49.53` `2 : 38.60 69.81` `3 : 43.00 2.88` – soni Dec 27 '13 at 14:34
  • The main problem, I am facing is to read nodes and positions written in a file and convert it to built-in type python object. After that I can iterate and put it into `dict` argument of `add_node` – soni Dec 27 '13 at 14:46
  • Ok, got it. Please take a look at my edited answer (coming up in about 10 min.) – ericmjl Dec 27 '13 at 15:39
  • thanks. Earlier, I was reluctant to modify input file. – soni Dec 27 '13 at 16:34
  • No worries. That is part of the reason I chose not to overwrite the file but instead chose to make a separate file that gets read in. – ericmjl Dec 27 '13 at 16:44