Statistics
| Branch: | Revision:

root / fiddle / heuristic-betweenness-centrality / utility.py @ 3c6ce57c

History | View | Annotate | Download (1.24 KB)

1
import os
2
import networkx as nx
3
import betweenness_centrality as centrality
4

    
5
MAIN_CODE_DIR = os.environ.get('MAIN_CODE_DIR', '')
6

    
7
def weight_betweenness_centrality(filepath, file_suffix):
8
    graph = nx.read_weighted_edgelist(filepath)
9

    
10
    score = centrality.weight_betweenness_centrality(graph, rescale=True)
11
    filepath = '/output/weight_basic_%s.csv'  % file_suffix
12

    
13
    with open(MAIN_CODE_DIR + filepath, 'w') as output:
14
        for node, bc in score.iteritems():
15
            output.write('%s, %s\n' % (node, bc))
16

    
17
def brandes_betweenness_centrality(filepath, file_suffix):
18
    graph = nx.read_weighted_edgelist(filepath)
19

    
20
    score = nx.betweenness_centrality(graph)
21
    filepath = '/output/networkx_%s.csv'  % file_suffix
22

    
23
    with open(MAIN_CODE_DIR + filepath, 'w') as output:
24
        for node, bc in score.iteritems():
25
            output.write('%s, %s\n' % (node, bc))
26

    
27
def brandes_betweenness_centrality_endpoints(filepath, file_suffix):
28
    graph = nx.read_weighted_edgelist(filepath)
29

    
30
    score = nx.betweenness_centrality(graph, endpoints=True)
31
    filepath = '/output/heuristic_%s.csv'  % file_suffix
32

    
33
    with open(MAIN_CODE_DIR + filepath, 'w') as output:
34
        for node, bc in score.iteritems():
35
            output.write('%s, %s\n' % (node, bc))