通过唯一标签合并networkx中的两个网络图 [英] Merging two network maps in networkx by unique labels

查看:517
本文介绍了通过唯一标签合并networkx中的两个网络图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python networkx模块具有用于合并两个网络映射的nx.union方法:

The python networkx module has a method nx.union for merging two network maps:

C = nx.union(G,H)

其中,G和H是网络映射,C是组合版本.它按编号查找所有节点,并合并具有相同编号的节点.

where G and H are network maps, and C in the combined version. It looks up all the nodes by number and merges nodes with the same number.

以GEXF格式存储的节点示例:

  <node id="0" label="walking">
    <ns0:color b="200" g="11" r="11" />
    <attvalues>
      <attvalue for="0" value="2" />
      <attvalue for="1" value="26" />
    </attvalues>
  </node>

这对我来说是个问题,因为我的地图具有任意节点号,但是每个节点属性中的标签都是唯一的.当我分配节点号时,我只是遍历一个列表,并使用列表索引作为节点号,但是map1可能在位置84处具有"walking",而map2可能在位置157处具有"walking"-因此节点号并不容易与节点标签有关.

This is a problem for me, because my maps have arbitrary node numbers but unique labels in each node attribute. When I assign node numbers, I am just going through a list and using the list index as the node number, but map1 might have "walking" in position 84 and map2 might have "walking" in position 157 - so node numbers are not easy to relate to node labels.

有人知道我应该如何用networkx合并到网络映射并按节点标签名称指定匹配项吗?

Does anyone know how I am supposed to union to network maps with networkx and specify matching by node label names?

我可能必须编写自己的方法,但只是想确保自己没有遗漏任何东西.有一个union_disjoint(),但似乎在同一文件中创建了两个单独的未连接地图,这没有帮助.

I probably have to write my own method, but just wanted to be sure I wasn't missing something. There is a union_disjoint() but that seems to create two separate unconnected maps in the same file, which isn't helpful.

第二个networkx问题:有没有一种方法可以指定GEXF文件格式的节点标签大小和标签颜色?没有列在GEXF v1.2规范草案中.

Also 2nd networkx question: Is there a way to specify node label size and label color in the GEXF file format? Wasn't listed in the GEXF v1.2 draft specifications.

推荐答案

尝试使用nx.read_gexf(file,relabel = True).这将使用GEXF节点标签作为NetworkX节点标识符.然后,您可以通过将一个图的节点和边添加到另一个图来合并两个图.例如

Try using nx.read_gexf(file,relabel=True). That will use the GEXF node labels as the NetworkX node identifiers. Then you can merge the two graphs by adding the nodes and edges of one to the other. e.g.

>>> G = nx.read_gexf(file1, relabel=True)
>>> H = nx.read_gexf(file2, relabel=True)
>>> G.add_nodes_from(H.nodes(data=True))
>>> G.add_edges_from(H.edges(data=True))

这篇关于通过唯一标签合并networkx中的两个网络图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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