root / fiddle / heuristic-betweenness-centrality / compare.py @ 739fe075
History | View | Annotate | Download (1.64 KB)
1 | 181e7c50 | Quynh PX Nguyen | import csv |
---|---|---|---|
2 | import os |
||
3 | from pdb import set_trace as debugger |
||
4 | |||
5 | MAIN_CODE_DIR = os.environ.get('MAIN_CODE_DIR', '') |
||
6 | |||
7 | def read_centrality_score(filepath): |
||
8 | scores = dict()
|
||
9 | with open(filepath) as f: |
||
10 | csv_reader = csv.reader(f) |
||
11 | try:
|
||
12 | for row in csv_reader: |
||
13 | if len(row) != 2: |
||
14 | print row
|
||
15 | scores[row[0]] = row[1] |
||
16 | except Exception as e: |
||
17 | print e
|
||
18 | return 0 |
||
19 | |||
20 | return scores
|
||
21 | |||
22 | def sort_value_of_dictionary(dictionary): |
||
23 | l = list()
|
||
24 | for k, v in dictionary.iteritems(): |
||
25 | l.append((k, v)) |
||
26 | |||
27 | l.sort(key=lambda tup: tup[1]) |
||
28 | return l
|
||
29 | |||
30 | |||
31 | if __name__ == '__main__': |
||
32 | 739fe075 | Quynh PX Nguyen | filepath_prefixes = ['/output/networkx_', '/output/weight_basic_', '/output/heuristic_'] |
33 | 181e7c50 | Quynh PX Nguyen | types = ['edge_list']
|
34 | extension = '.csv'
|
||
35 | |||
36 | for t in types: |
||
37 | # TODO: any other way to handle the relative path???
|
||
38 | filepaths = [MAIN_CODE_DIR + p + t + extension for p in filepath_prefixes] |
||
39 | |||
40 | nx_scores = read_centrality_score(filepaths[0])
|
||
41 | 739fe075 | Quynh PX Nguyen | weight_basic_scores = read_centrality_score(filepaths[1])
|
42 | heuristic_scores = read_centrality_score(filepaths[2])
|
||
43 | 181e7c50 | Quynh PX Nguyen | |
44 | sorted_nx_scores = sort_value_of_dictionary(nx_scores) |
||
45 | |||
46 | # write to file
|
||
47 | # FORMAT: ip_address networkx_centrality boost_centrality
|
||
48 | f1ee8a55 | Quynh PX Nguyen | print "Compare" |
49 | 181e7c50 | Quynh PX Nguyen | with open(MAIN_CODE_DIR + '/output/score_summary_%s.txt' % t, 'w') as output: |
50 | for key, nx in sorted_nx_scores: |
||
51 | 739fe075 | Quynh PX Nguyen | weight_basic = weight_basic_scores[key] |
52 | heuristic = heuristic_scores[key] |
||
53 | output.write('%s\t%s\t%s\t%s\n' % (key, nx, weight_basic, heuristic))
|
||
54 | 181e7c50 | Quynh PX Nguyen |