def gen_group_graph(self): group_graph = nx.Graph() seed_groups = set() groups_dic = {} for group in self.groups: group_type = self.get_group_type(self.groups[group]) if group_type == 'Seed': seed_groups.add(group) groups_dic[group] = Node(group, group_type) for group in seed_groups: groups_dic[group].init_rank = 1 / len(seed_groups) valid_pairs = {} for n1 in self.graph.nodes: neighbors = self.graph.neighbors(n1) for n2 in neighbors: for g1 in n1.groups: for g2 in n2.groups: valid_pairs[(g1, g2)] = True pairs = itertools.combinations(self.groups.keys(), 2) pairs = sorted([(f, t) if f < t else (t, f) for f, t in pairs], key=lambda pair: str(pair)) for source_group, target_group in pairs: if (source_group, target_group) not in valid_pairs: continue removed = set() weight = 0 source_nodes = self.groups[source_group] target_nodes = self.groups[target_group] for source_node in source_nodes: if source_node in removed: continue for target_node in target_nodes: if source_node in removed: break if target_node in removed: continue if not self.graph.has_edge(source_node, target_node): continue # set number of common neighbors as the weight of the edge (trustworthy of connection) n1 = self.graph.neighbors(source_node) n2 = self.graph.neighbors(target_node) edge_weight = len(set(list(n1)) & set(list(n2))) if not edge_weight: continue weight += edge_weight removed.add(source_node) removed.add(target_node) if weight > 0: num = len(source_nodes) + len(target_nodes) group_graph.add_edge(groups_dic[source_group], groups_dic[target_group], weight=weight / num) return group_graph
def gen_group_graph(self): group_graph = nx.Graph() seed_groups = set() groups_dic = {} for group in self.groups: group_type = self.get_group_type(self.groups[group]) if group_type == 'Seed': seed_groups.add(group) groups_dic[group] = Node(group, group_type) for group in seed_groups: groups_dic[group].init_rank = 1 / len(seed_groups) pairs = itertools.combinations(self.groups.keys(), 2) pairs = sorted([(f, t) if f < t else (t, f) for f, t in pairs], key=lambda pair: str(pair)) for source_group, target_group in pairs: removed = set() weight = 0 source_nodes = self.groups[source_group] target_nodes = self.groups[target_group] if self.min_group_req > 1: source_nodes = filter( lambda n: len(n.groups) >= self.min_group_req, source_nodes) target_nodes = filter( lambda n: len(n.groups) >= self.min_group_req, target_nodes) for source_node in source_nodes: if source_node in removed: continue for target_node in target_nodes: if source_node in removed: break if target_node in removed: continue if not self.graph.has_edge(source_node, target_node): continue removed.add(source_node) removed.add(target_node) weight += 1 if weight > 0: num = len(source_nodes) + len(target_nodes) group_graph.add_edge(groups_dic[source_group], groups_dic[target_group], weight=1.0 * weight / num) return group_graph