根据给定类型的节点和权重,随机分配大型网络

时间:2019-01-25 14:05:08

标签: python-2.7 networkx

我有一个小型网络,具有两种类型的节点并具有权重。我想从大型网络中随机抽样类似的网络。

我有一个较小的网络,它使用python2.7中的NetworkX构建,具有两种类型的节点和特定的权重。我还有另一个庞大的网络/图表。我想随机采样庞大的网络,以保持较小网络的连接性(节点和边缘的类型)。可以说是对网络进行随机采样。

我的初始数据如下:

Connections                                       Weight
chr22,43266979,43269138:chr22,43400294,43400976    15
chr22,43409329,43433936:chr22,43290595,43292626    15
chr22,43409329,43433936:chr22,43277663,43278699    17
chr22,43266979,43269138:chr22,43399124,43400293    15
chr22,43409329,43433936:chr22,43399124,43400293    1
chr22,43409329,43433936:chr22,43278700,43281477    16
chr22,43409329,43433936:chr22,43262566,43266683    18
chr22,43409329,43433936:chr22,43281478,43285053    16
chr22,43266979,43269138:chr22,43350529,43356580    9
chr22,43409329,43433936:chr22,43400294,43400976    1
chr22,43433937,43434687:chr22,43277663,43278699    18
chr22,43266979,43269138:chr22,43437394,43454270    21

我有一个关于节点类型的字典:

node_type = {"chr22,43266979,43269138":"TypeA","chr22,43433937,43434687":"TypeA","chr22,43409329,43433936":"TypeA"}
import networkx as nx
import matplotlib.pyplot as pat

G = nx.Graph()

with open("data.txt") as f:
    header=f.readline()
    for line in f:
        conn, weight = line.strip().split()
        node1,node2=conn.split(":")
        G.add_edge(node1,node2, weight=int(weight))


for node in G.nodes:
    G.node[node]['category'] = node_type.get(node,"TypeB")

color_map = {'TypeA':'blue', 'TypeB':'red'}
nx.draw(G, node_size=100,node_color=[color_map[G.node[node]['category']] for node in G])
pat.show()

小型网络看起来像这样[1]

"small network"

现在我的网络非常大,我想选择一个子图,该子图的节点类型和连接性(及权重)与我的较小网络相同。在此先感谢您的任何建议。

0 个答案:

没有答案