-
Notifications
You must be signed in to change notification settings - Fork 32
/
graph_utils.py
79 lines (72 loc) · 2.56 KB
/
graph_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import itertools
import matplotlib.pyplot as plt
import networkx as nx
def get_diff_pairs(
golden_pairs_set,
dedupe_found_pairs_set,
dedupe_unclustered_found_pairs_set,
diff_set_ids
):
diff_dedupe_clustered_pairs = [
(x, y) for x, y in dedupe_found_pairs_set
if x in diff_set_ids or y in diff_set_ids]
diff_dedupe_unclustered_pairs = [
(x, y) for x, y in dedupe_unclustered_found_pairs_set
if x in diff_set_ids or y in diff_set_ids]
diff_true_pairs = [
(x, y) for x, y in golden_pairs_set
if x in diff_set_ids or y in diff_set_ids]
diff_all_ids = set(itertools.chain.from_iterable(
diff_dedupe_clustered_pairs +
diff_dedupe_unclustered_pairs +
diff_true_pairs))
return (
diff_dedupe_clustered_pairs,
diff_dedupe_unclustered_pairs,
diff_true_pairs,
diff_all_ids
)
def draw_pairs_graph(df, edges, nodes, edge_labels_dict, title):
G = nx.Graph()
for node in nodes:
G.add_node(node,
name=str(node) + ':' + df.loc[node]['name'])
G.add_edges_from(edges)
plt.figure(figsize=(10, 6))
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, alpha=0.3, node_size=1000)
nx.draw_networkx_labels(G, pos, labels=nx.get_node_attributes(G, 'name'), font_size=20)
nx.draw_networkx_edges(G, pos, alpha=0.3, width=4)
edge_labels = {pair: edge_labels_dict[pair] for pair in edges
if pair in edge_labels_dict}
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=20)
plt.margins(0.5, 0.2)
plt.axis('off')
plt.title(title, fontdict={'fontsize': 24, 'fontweight': 'bold'})
plt.show()
def show_cluster_graphs(
df,
golden_pairs_set,
dedupe_found_pairs_set,
dedupe_unclustered_found_pairs_set,
dedupe_unclustered_pairs_score_dict,
diff_set_ids
):
(
diff_dedupe_clustered_pairs,
diff_dedupe_unclustered_pairs,
diff_true_pairs,
diff_all_ids
) = get_diff_pairs(
golden_pairs_set,
dedupe_found_pairs_set,
dedupe_unclustered_found_pairs_set,
diff_set_ids
)
display(df.loc[list(diff_all_ids)])
draw_pairs_graph(
df, diff_true_pairs, diff_set_ids, {}, "Truth")
draw_pairs_graph(
df, diff_dedupe_unclustered_pairs, diff_set_ids, dedupe_unclustered_pairs_score_dict, "Unclustered")
draw_pairs_graph(
df, diff_dedupe_clustered_pairs, diff_set_ids, dedupe_unclustered_pairs_score_dict, "Clustered")