如何在NetworkX MultiDiGraph中访问节点和边缘中的键 [英] How to access keys in nodes and edges in NetworkX MultiDiGraph

查看:258
本文介绍了如何在NetworkX MultiDiGraph中访问节点和边缘中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是比较节点和边缘中的关键点.我已经创建了一个MultiDiGraph,它具有我称为节点的点的坐标,其中键是点的name,其属性在我的代码中列出如下.然后,我用station_nametarget_name创建了边作为参考,这些边是我在其他地方创建的用于存储数据的observations类中的数据所填充的引用.从文件中读取数据并将其存储在基于python内置字典类(2.7x)的一系列类中,这些类已被视为以下对象,即knowncoordsobservations以及作为简单的值持有者类.

What I want to do is compare keys in nodes and edges. I have created a MultiDiGraph with the coordinates of the points I know as nodes, where the key is the name of the point with attributes listed below in my code. I have then created edges with station_name and target_name as references populated by data from the observations class I created elsewhere to store the data This way each edge can be either be outgoing or incoming rays. The data is read in from a file and stored in a series of classes based on the built in dictionary class in python, (2.7x), these classes have been treated as objects below, namely knowncoords and observations with station as a simple value holder class.

我的代码的NetworkX部分设置如下:

The NetworkX part of my code is set-up as follows:

def Execute(self):
 G = nx.MultiDiGraph()
    #Adding known points as nodes to G
    for name, s in knowncoords.iteritems():
        #Below "Known":True because it is in the file, "OrientCorrn"
        #still has to be calculated for each station below, and I still
        #have to cycle through the points to check them hence "Checked"=False
        attr = {"Known":True,"Checked":False,"OrientCorr":0.0}
        G.add_node(name,attr)

     print G.nodes(data=True),"\n"   

     #print G.number_of_nodes()
     #print G.nodes(data=False)#Prints keys only
     #print G.nodes(data=True)#Prints keys with data as list
     #print G.node#Prints all nodes with key and data as embedded dict

    #Adds observations as edges based on station and targets
    for station_name,station in observations.iteritems():
            for target_name,target in station.iteritems():
                G.add_edge(station_name,target_name)
                G.edge[station_name][target_name] = {target_name:
                                                     (target.HA, 
                                                     target.VA, target.HD)}

                 #print G.get_edge_data(station_name,target_name)[0]
    print G.edges(data=True)

以上的输出如下:

[('WTOP', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('KB', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('TS8', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('STEP', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('WBOT', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('CNSTA', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('TS7', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('DP', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('FRNWD', {'Known': True, 'OrientCorr': 0.0, 'Checked': False})]

[('WTOP', 'TS7', (0.8767806941497847, None, None)), ('WTOP', 'STEP', (0.3830609857182666, None, None)), ('WTOP', 'N5', (2.2121078641665908, None, 62.281)), ('WTOP', 'TS8', (4.882321023750393, None, None)), ('TS8', 'WTOP', (1.098965956065474, None, 41.425)), ('TS8', 'N1', (2.6658692290010606, None, 116.121)), ('TS8', 'DP', (1.9014004722171114, None, None)), ('TS8', 'WBOT', (5.6203528905034394, None, 36.558)), ('N1', 'N2', (0.859046209694798, None, 271.342)), ('N1', 'DP', (0.6897638166617812, None, None)), ('N1', 'TS8', (4.640059627299959, None, 116.021)), ('N1', 'FRNWD', (6.1694140806336115, None, None)), ('N2', 'N1', (5.266419510746235, None, 271.418)), ('N2', 'N3', (1.0780607901360308, None, 166.267)), ('N2', 'CNSTA', (0.5640807179709452, None, None)), ('N2', 'FRNWD', (1.0797770305671586, None, None)), ('N2', 'DP', (1.9260968811328312, None, None)), ('N3', 'N2', (5.326260063405584, None, 166.258)), ('N3', 'N4', (5.86409296868126, None, 193.935)), ('N3', 'FRNWD', (2.1863642576996742, None, None)), ('N3', 'DP', (3.1192912242587547, None, None)), ('N4', 'N3', (5.294305993683654, None, 193.9380377)), ('N4', 'FRNWD', (4.789624647922251, None, None)), ('N4', 'DP', (5.645577746331569, None, None)), ('N4', 'N5', (3.048295108797074, None, 213.277)), ('N5', 'WTOP', (4.892555440558616, None, 62.282)), ('N5', 'N4', (2.384876067566785, None, 213.275)), ('N5', 'FRNWD', (1.2586829751701993, None, None)), ('N5', 'DP', (2.1078729227280406, None, None))]

我现在想做的是比较节点中的name键和边缘中的target_name键,如果它们相等,则执行计算并更新节点中的OrientCorrn值.即我是在WTOP上观察TS7的,并且我想检查TS7是否在我的节点中,如果可以,那么我可以基于此执行一些计算以更新WTOP节点的"OrientCorr"值.

What I would like to do now is compare the name key in nodes with the target_name key in the edges and if they are equal perform a calculation and update the OrientCorrn value in nodes. i.e. I am setup at WTOP observing to TS7 and I want to check if TS7 is in my nodes, if it is then I can perform some calculation based on that to update the "OrientCorr" value for the WTOP node.

我尝试了G.get_edge_data(station_name,target_name)[0],但是没有返回我期望的target_name密钥,而是上面的target.HA的值,我可以使用G.nodes(data=False)来获取节点的密钥,但是我该如何遍历他们吗?或者有没有一种方法可以检查所有节点的所有边缘,然后根据我的标准返回匹配的边缘,例如:

I have tried G.get_edge_data(station_name,target_name)[0] but that doesn't return the target_name key I expected but rather the value of target.HA above, I can use the G.nodes(data=False) to get the keys for the nodes but how do I iterate through them? Or is there a way to check all edges against all nodes and just return the ones that match based my criteria such as:

if (G.edge[station_name][target_name])==(G.node[name]): #do stuff

if (G.edge[station_name][target_name])==(G.node[name]): #do stuff

预先感谢

推荐答案

事实证明,我在创建边缘时效果很差,即每个属性都没有键.我通过更改下面的代码来修复它,该代码创建了一个基于station_nametarget_name的边,但是为所有属性(target_name)使用了一个键:

It turns out I was creating the edges badly, i.e. without a key for each attribute. I fixed it by changing the code below which creates an edge based on station_name and target_name but with a single key for all the attributes(target_name):

G.add_edge(station_name,target_name)
            G.edge[station_name][target_name] = {target_name:(target.HA, 
                                                 target.VA, target.HD)}

对此:

G.add_edge(station_name,
           target_name,
           target=target_name,
           HA=target.HA, 
           VA=target.VA, 
           HD=target.HD)

这使我像以前一样基于station_nametarget_name调用每个边,但是现在我可以基于所讨论的边的特定键来调用特定属性.然后,我使用以下循环调用所需的名称(在我基于边缘和节点的observationsknowncoords词典中的名称):

This let me call each edge based on station_name and target_name as before but now I can call a particular attribute based on a particular key for the edge in question. I then used the following loop to call the names I wanted (the ones which were in both the observations and knowncoords dictionaries I based the edges and nodes upon):

for station_name, station in observations.iteritems():
     for target_name, target in station.iteritems():
         for name, s in knowncoords.iteritems():
             if (G.get_edge_data(station_name,target_name)[0]['target']) == name:
                print '@',station_name,'Target: ',
                         G.get_edge_data(station_name,target_name)[0]['target'], 
                         'which matches with', name, 'in knowncoords'

哪个提供了我想要的输出:

Which gave the output I wanted:

@ WTOP Target:  TS7 which matches with TS7 in knowncoords
@ WTOP Target:  STEP which matches with STEP in knowncoords
@ WTOP Target:  TS8 which matches with TS8 in knowncoords
@ TS8 Target:  WTOP which matches with WTOP in knowncoords
@ TS8 Target:  WBOT which matches with WBOT in knowncoords
@ TS8 Target:  DP which matches with DP in knowncoords
@ N1 Target:  TS8 which matches with TS8 in knowncoords
@ N1 Target:  DP which matches with DP in knowncoords
@ N1 Target:  FRNWD which matches with FRNWD in knowncoords
@ N2 Target:  DP which matches with DP in knowncoords
@ N2 Target:  FRNWD which matches with FRNWD in knowncoords
@ N2 Target:  CNSTA which matches with CNSTA in knowncoords
@ N3 Target:  DP which matches with DP in knowncoords
@ N3 Target:  FRNWD which matches with FRNWD in knowncoords
@ N4 Target:  DP which matches with DP in knowncoords
@ N4 Target:  FRNWD which matches with FRNWD in knowncoords
@ N5 Target:  WTOP which matches with WTOP in knowncoords
@ N5 Target:  DP which matches with DP in knowncoords
@ N5 Target:  FRNWD which matches with FRNWD in knowncoords

但是,所有荣誉归功于@EdChum,它为我指明了正确的方向!

All credit however goes to @EdChum for pointing me in the right direction!

这篇关于如何在NetworkX MultiDiGraph中访问节点和边缘中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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