python 2.7 - How to access keys in nodes and edges in NetworkX MultiDiGraph -
what want compare keys in nodes , edges. have created multidigraph coordinates of points know nodes, key name
of point attributes listed below in code. have created edges station_name
, target_name
references populated data observations
class created elsewhere store data way each edge can either outgoing or incoming rays. data read in file , stored in series of classes based on built in dictionary class in python, (2.7x), these classes have been treated objects below, namely knowncoords
, observations
station
simple value holder class.
the networkx part of code set-up follows:
def execute(self): g = nx.multidigraph() #adding known points nodes g name, s in knowncoords.iteritems(): #below "known":true because in file, "orientcorrn" #still has calculated each station below, , still #have cycle through points 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 #print g.nodes(data=true)#prints keys data list #print g.node#prints nodes key , data embedded dict #adds observations edges based on station , targets station_name,station in observations.iteritems(): 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 above 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 compare name
key in nodes target_name
key in edges , if equal perform calculation , update orientcorrn
value in nodes. i.e. setup @ wtop observing ts7 , want check if ts7 in nodes, if can perform calculation based on update "orientcorr" value wtop node.
i have tried g.get_edge_data(station_name,target_name)[0]
doesn't return target_name
key expected rather value of target.ha
above, can use g.nodes(data=false)
keys nodes how iterate through them? or there way check edges against nodes , return ones match based criteria such as:
if (g.edge[station_name][target_name])==(g.node[name]): #do stuff
thanks in advance
it turns out creating edges badly, i.e. without key each attribute. fixed changing code below creates edge based on station_name
, target_name
single key 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
, target_name
before can call particular attribute based on particular key edge in question. used following loop call names wanted (the ones in both observations
, knowncoords
dictionaries based edges , nodes upon):
for station_name, station in observations.iteritems(): target_name, target in station.iteritems(): 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 output wanted:
@ wtop target: ts7 matches ts7 in knowncoords @ wtop target: step matches step in knowncoords @ wtop target: ts8 matches ts8 in knowncoords @ ts8 target: wtop matches wtop in knowncoords @ ts8 target: wbot matches wbot in knowncoords @ ts8 target: dp matches dp in knowncoords @ n1 target: ts8 matches ts8 in knowncoords @ n1 target: dp matches dp in knowncoords @ n1 target: frnwd matches frnwd in knowncoords @ n2 target: dp matches dp in knowncoords @ n2 target: frnwd matches frnwd in knowncoords @ n2 target: cnsta matches cnsta in knowncoords @ n3 target: dp matches dp in knowncoords @ n3 target: frnwd matches frnwd in knowncoords @ n4 target: dp matches dp in knowncoords @ n4 target: frnwd matches frnwd in knowncoords @ n5 target: wtop matches wtop in knowncoords @ n5 target: dp matches dp in knowncoords @ n5 target: frnwd matches frnwd in knowncoords
all credit goes @edchum pointing me in right direction!
Comments
Post a Comment