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

History | View | Annotate | Download (2.01 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 | 82c7c698 | Quynh PX Nguyen | ```
vertices_with_mismatched_score = list()
``` |

50 | 181e7c50 | Quynh PX Nguyen | with open(MAIN_CODE_DIR + '/output/score_summary_%s.txt' % t, 'w') as output: |

51 | for key, nx in sorted_nx_scores: |
||

52 | 82c7c698 | Quynh PX Nguyen | ```
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 | 181e7c50 | Quynh PX Nguyen |