How to access keys in nodes and edges in NetworkX

2019-05-21 09:53发布

问题:

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.

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)

The output for the above is as follows:

[('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))]

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.

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

Thanks in advance

回答1:

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

To this:

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

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

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