当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Reading nodes with pos attribute from file in networkx

问题描述:

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

网友答案:

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!

分享给朋友:
您可能感兴趣的文章:
随机阅读: