root / fiddle / heuristic-betweenness-centrality / compare.py @ f1ee8a55
History | View | Annotate | Download (1.47 KB)
1 |
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 |
filepath_prefixes = ['/output/networkx_', '/output/heuristic_'] |
33 |
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 |
boost_scores = read_centrality_score(filepaths[1])
|
42 |
|
43 |
sorted_nx_scores = sort_value_of_dictionary(nx_scores) |
44 |
|
45 |
# write to file
|
46 |
# FORMAT: ip_address networkx_centrality boost_centrality
|
47 |
print "Compare" |
48 |
with open(MAIN_CODE_DIR + '/output/score_summary_%s.txt' % t, 'w') as output: |
49 |
for key, nx in sorted_nx_scores: |
50 |
boost = boost_scores[key] |
51 |
output.write('%s\t%s\t%s\n' % (key, nx, boost))
|
52 |
|
53 |
|