Statistics
| Branch: | Revision:

root / fiddle / heuristic-betweenness-centrality / compare.py @ 82c7c698

History | View | Annotate | Download (2.01 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/weight_basic_', '/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
        weight_basic_scores = read_centrality_score(filepaths[1])
42
        heuristic_scores = read_centrality_score(filepaths[2])
43

    
44
        sorted_nx_scores = sort_value_of_dictionary(nx_scores)
45

    
46
        # write to file
47
        # FORMAT: ip_address    networkx_centrality     boost_centrality
48
        print "Compare"
49
        vertices_with_mismatched_score = list()
50
        with open(MAIN_CODE_DIR + '/output/score_summary_%s.txt' % t, 'w') as output:
51
            for key, nx in sorted_nx_scores:
52
                weight_basic = float(weight_basic_scores[key])
53
                heuristic = float(heuristic_scores[key])
54
                nx = float(nx)
55

    
56

    
57
                if heuristic != weight_basic:
58
                    vertices_with_mismatched_score.append(key)
59

    
60
                output.write('%s\t%.4f\t%.4f\t%.4f\n' % (key, nx, weight_basic, heuristic))
61

    
62
        print "Vertices with mismatched score between WBBC and HBC: %s" % len(vertices_with_mismatched_score)
63

    
64
        print sorted(vertices_with_mismatched_score)
65

    
66

    
67