从networkx中的文件读取具有pos属性的节点 [英] Reading nodes with pos attribute from file in networkx

查看:565
本文介绍了从networkx中的文件读取具有pos属性的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Networkx的新手.我有一个文件,其中包含以下格式的节点位置

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

0 : 23.23 12.23

其中0是节点,23.2312.23分别是X和Y坐标. 有谁知道如何使用read_edgelist(...)之类的功能或类似的解决方法读取具有pos属性的节点?

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?

谢谢

推荐答案

对于read_edgelist,您假定已经存在边缘列表.但是,您提供的是节点+属性.

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

由于您从具有文件格式(在注释中说明)的文件开始,因此第一个挑战是将其设置为易于解析的格式.因此,我建议使用CSV文件格式.为了对您的文件执行此操作,我将终端(Linux& Mac),cd与您的文件一起启动到目录中,并运行以下两个命令:

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

这将读取nodes.txt(或您的文件),将所有出现的:(包括空格)替换为,,并将其另存为nodes_replaced1.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

除了读取nodes_replaced1.txt,将[spaces]替换为,,然后将其写为CSV文件之外,这将做类似的事情.

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

一旦有了CSV文件,我建议您使用Pandas打开CSV文件,并执行以下操作以将节点添加到图形中:

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})]

您会注意到,当我仅致电G.nodes()时,没有x& y位置数据.但是,当我调用G.nodes(data=True)时,将合并x和y位置数据.

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

最后,如果@Aric曾经出现回答这个问题,请纠正我,如果我错了!

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

这篇关于从networkx中的文件读取具有pos属性的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆