root / globecomm / main.py @ fac6e5a4
History | View | Annotate | Download (1.29 KB)
1 | bd3d6dca | Quynh PX Nguyen | from os import listdir |
---|---|---|---|
2 | from os.path import isfile, join, splitext, basename |
||
3 | import sys |
||
4 | import networkx as nx |
||
5 | fac6e5a4 | Quynh PX Nguyen | import numpy |
6 | bd3d6dca | Quynh PX Nguyen | |
7 | sys.path.append("heuristic_bc/")
|
||
8 | from heuristic_betweenness_centrality import HeuristicBetweennessCentrality as HBC |
||
9 | |||
10 | from pdb import set_trace as debugger |
||
11 | |||
12 | def get_all_files_from_dir(dir): |
||
13 | return [join(dir, f) for f in listdir(dir) if isfile(join(dir, f))] |
||
14 | |||
15 | |||
16 | if __name__ == '__main__': |
||
17 | fac6e5a4 | Quynh PX Nguyen | input_dir = ''
|
18 | # Default input directory
|
||
19 | GRAPH_DIRS='/home/quynh/Thesis/quynhnguyen-ms/experiment_input_graphs/CNGraphs/snapshots/small_samples'
|
||
20 | |||
21 | if len(sys.argv) > 1: |
||
22 | input_dir = sys.argv[1]
|
||
23 | else:
|
||
24 | input_dir = GRAPH_DIRS |
||
25 | |||
26 | onlyfiles = get_all_files_from_dir(input_dir) |
||
27 | bd3d6dca | Quynh PX Nguyen | |
28 | for in_filepath in onlyfiles: |
||
29 | filename = splitext(basename(in_filepath))[0]
|
||
30 | graph = nx.read_weighted_edgelist(in_filepath) |
||
31 | out_filepath = join('output/', filename + '.hbcout') |
||
32 | fac6e5a4 | Quynh PX Nguyen | |
33 | # Brandes BC
|
||
34 | bbc = nx.betweenness_centrality(graph, weight=True, normalized=True, endpoints=True) |
||
35 | out_data = [[k, v] for (k, v) in bbc.iteritems()] |
||
36 | numpy.savetxt(out_filepath, out_data, delimiter=',', fmt='%s') |
||
37 | print "Finish calculating BC for %s" % filename |
||
38 | |||
39 | # # Heuristic BC
|
||
40 | # hbc = HBC(graph)
|
||
41 | # hbc.write(out_filepath)
|
||
42 | bd3d6dca | Quynh PX Nguyen | |
43 |