Revision 37200dce fiddle/boostnetworkxcomparision/src/networkx_graph.py
fiddle/boostnetworkxcomparision/src/networkx_graph.py  

5  5  
6  6 
MAIN_CODE_DIR = os.environ.get('MAIN_CODE_DIR', '') 
7  7  
8 
def calculate_and_print_centrality(graph, file_suffix): 

9 
print "\n### Centrality ###" 

8 
def calculate_and_write_centrality(graph, file_suffix): 

10  9 
score = nx.betweenness_centrality(graph) 
11 
for node, centrality in score.iteritems(): 

12 
print "%s\t%f" % (node, centrality) 

10 
# for node, centrality in score.iteritems():


11 
# print "%s\t%f" % (node, centrality)


13  12  
14 
with open(MAIN_CODE_DIR + '/output/networkx_%s.csv' % file_suffix, 'w') as output: 

13 
filepath = '/output/networkx_%s.csv' % file_suffix 

14 
print "Writing centrality to %s" % filepath 

15  
16 
with open(MAIN_CODE_DIR + filepath, 'w') as output: 

15  17 
for node, centrality in score.iteritems(): 
16  18 
output.write('%s, %s\n' % (node, centrality)) 
17  19  
18  20 
def handle_edgelist(filepath): 
19  21 
graph = nx.read_weighted_edgelist(filepath) 
20 
calculate_and_print_centrality(graph, 'edge_list')


22 
calculate_and_write_centrality(graph, 'edge_list')


21  23  
22  24 
def handle_json_olsr(filepath): 
23  25 
# create empty graph 
...  ...  
40  42 
# create graph with all nodes and edges and weights 
41  43 
graph.add_weighted_edges_from(graph_data) 
42  44  
43 
calculate_and_print_centrality(graph, 'json_olsr')


45 
calculate_and_write_centrality(graph, 'json_olsr')


44  46  
45  47 
def handle_json_topology(filepath): 
46  48 
# create empty graph 
...  ...  
63  65 
# create graph with all nodes and edges and weights 
64  66 
graph.add_weighted_edges_from(graph_data) 
65  67  
66 
calculate_and_print_centrality(graph, 'json_topology')


68 
calculate_and_write_centrality(graph, 'json_topology')


67  69  
68  70 
if __name__ == '__main__': 
69  71 
input_edgelist = MAIN_CODE_DIR + '/input/ninux_30_1.edges' 
Also available in: Unified diff