def calculate_auc(train_set, nodes, poss_set, neg_set, auc, gap, verbose, directed, bipartite, node2vec, max_iter, p): g = Graph(directed=False) g.add_vertex(max(nodes) + 1) for edge in train_set: if edge not in poss_set: u, w = map(int, edge.split()) g.add_edge(g.vertex(u), g.vertex(w)) add_auc(auc, "sfdp-default", calculate_auc_default(g, max_iter, poss_set, neg_set, p, directed)) if not directed: add_auc(auc, "PA", calculate_auc_PA(g, poss_set, neg_set)) add_auc(auc, "CN", calculate_auc_CN(g, poss_set, neg_set)) add_auc(auc, "Adamic-Adar", calculate_auc_Adamic_Adar(g, poss_set, neg_set)) add_auc( auc, "NMF-100", calculate_auc_NMF(100, train_set, nodes, poss_set, neg_set, directed)) add_auc( auc, "svds-100", calculate_auc_SVDS(100, train_set, nodes, poss_set, neg_set, directed)) if bipartite: is_bi, part = graph_tool.topology.is_bipartite(g, partition=True) if is_bi: groups = g.new_vertex_property("int") for u in g.vertices(): groups[u] = int(part[u]) left = "repulse-aliens" right = "repulse-aliens" pos_bip = sfdp_layout(g, groups=groups, verbose=verbose, bipartite=True, bipartite_method=[left, right], gap=gap) features = tools.TopologicalFeatures(g, pos_bip, gap=gap) X, Y = tools.make_dataset(poss_set, neg_set, [features.dist]) add_auc(auc, "sfdp-bipartite", roc_auc_score(Y, X)) if directed: g_di = Graph(directed=False) g_di.add_vertex(2 * max(nodes) + 2) for edge in train_set: if edge not in poss_set: u, w = map(int, edge.split()) g_di.add_edge(g_di.vertex(2 * u + 1), g_di.vertex(2 * w)) add_auc(auc, "sfdp-directed", calculate_auc_directed(g_di, verbose, gap, poss_set, neg_set)) if node2vec: add_auc( auc, 'Node2Vec-100-undir', calculate_auc_Node2Vec(train_set, poss_set, neg_set, 100, False))
def calculate_auc_PA(g, poss_set, neg_set): features = tools.TopologicalFeatures(g) edges = [e for e in g.edges()] poss_set = set(poss_set) neg_set = set(neg_set) for e in edges: e1 = str(e.source()) + ' ' + str(e.target()) e2 = str(e.target()) + ' ' + str(e.source()) X, Y = tools.make_dataset(poss_set, neg_set, [features.preferential_attachment]) return roc_auc_score(Y, X)
def calculate_auc_directed(g, verbose, gap, poss_set, neg_set): groups = g.new_vertex_property("int") for u in g.vertices(): groups[u] = int(u) % 2 pos_directed = sfdp_layout(g, groups=groups, verbose=verbose, bipartite=True, gap=gap) features = tools.TopologicalFeatures(g, pos_directed, directed=True, gap=gap) X, Y = tools.make_dataset(poss_set, neg_set, [features.dist]) return roc_auc_score(Y, X)
def calculate_auc_Adamic_Adar(g, poss_set, neg_set): features = tools.TopologicalFeatures(g) X, Y = tools.make_dataset(poss_set, neg_set, [features.Adamic_Adar_coefficient]) return roc_auc_score(Y, X)
def calculate_auc_CN(g, poss_set, neg_set): features = tools.TopologicalFeatures(g) X, Y = tools.make_dataset(poss_set, neg_set, [features.common_neighbors]) return roc_auc_score(Y, X)
def calculate_auc_default(g, max_iter, poss_set, neg_set, p, directed): pos_default = sfdp_layout(g, max_iter=max_iter, p=p) features = tools.TopologicalFeatures(g, pos_default, directed=directed) X, Y = tools.make_dataset(poss_set, neg_set, [features.dist]) return roc_auc_score(Y, X)