def _improve_seeds(self, seeds): communities = [] isolated_vertices = self.__get_isolated_vertices(seeds) for seed in seeds: ic0 = cohesion_index(self._network, seed) self._expand_seed(seed, isolated_vertices, ic0) communities.append(seed) return communities
def _index_cohesion(self, communities): """ Compute means of Cohesion :param communities: :return: cohesion """ values = [] for community in communities: ic = cohesion_index(self._network, community) values.append(ic) return np.mean(values)
def _expand_seed(self, seed, isolated_vertices, ic0): l_seed = len(seed) self.__expand(seed, isolated_vertices, ic0) l_newseed = len(seed) ia = isolation_index(seed) ic = cohesion_index(self._network, seed) print('Expanding community ... {0} vertexs -> {1} vertexs (Ia = {2} | Ic = {3})'.format(l_seed, l_newseed, ia, ic)) if l_seed == l_newseed: sys.stdout.flush() return seed else: return self._expand_seed(seed, isolated_vertices, ic0)
def compute_results(graph, communities): values = [] print('{0} communities detected'.format(len(communities))) for idx, community in enumerate(communities): ia = isolation_index(community) ic = cohesion_index(graph, community) print('community {0} has Ia = {1} and Ic = {2}'.format(idx, ia, ic)) values.append((len(community), ia, ic)) length = np.mean([v[0] for v in values]) Ia = np.mean([v[1] for v in values]) Ic = np.mean([v[2] for v in values]) print( 'Detected {0} communities with mean length = {1} and mean Ia = {2} and mean Ic = {3}' .format(int(len(communities)), length, Ia, Ic))
def _improve_seeds(self, seeds, draw_seeds=None): communities = [] isolated_vertices = self.__get_isolated_vertices(seeds) for idx, seed in enumerate(seeds): ic0 = cohesion_index(self._network, seed) # ia0 = isolation_index(seed) self._expand_seed(seed, isolated_vertices, ic0) # ic = cohesion_index(self._network, seed) # ia = isolation_index(seed) if draw_seeds: self.paint(seed, idx) print('community {0} saved.'.format(idx + 1)) communities.append(seed) return communities
def _construct_rcl(self, candidates, communities): """ Construct Restricted candidate list with the best of both objetives(Isolation and cohesion) :return: RCL, rcl_ids_community """ rcl = [] # Restricted candidate list rcl_ids_community = [] # Id community possibles = [] possibles_id = [] g_isolation = [] g_cohesion = [] for candidate in candidates: for id_community, community in enumerate(communities): new_community = community + [candidate] # Index of the new communities g_isolation.append(isolation_index(new_community)) g_cohesion.append(cohesion_index(self._network, new_community)) possibles.append(candidate) possibles_id.append(id_community) # Select min/max for add to RCL the best candidates g_min_isolation = min(g_isolation) g_max_isolation = min(g_isolation) limit_g_isolation = g_min_isolation + self.alpha * (g_max_isolation - g_min_isolation) # Select min/max for add to RCL the best candidates g_min_cohesion = min(g_cohesion) g_max_cohesion = max(g_cohesion) # g(c) formula limit_g_cohesion = g_min_cohesion + self.alpha * (g_max_cohesion - g_min_cohesion) for id_candidate, localg in enumerate(g_cohesion): if localg <= limit_g_cohesion: rcl.append(possibles[id_candidate]) rcl_ids_community.append(possibles_id[id_candidate]) # Can be added two times if is good in the two objetives if g_isolation[id_candidate] <= limit_g_isolation: rcl.append(possibles[id_candidate]) rcl_ids_community.append(possibles_id[id_candidate]) return rcl, rcl_ids_community
def __expand(self, seed, isolated_vertices, ic0): newseed = list(seed) for vertex in newseed: in_neighbours = list(vertex.in_neighbors()) out_neighbours = list(vertex.out_neighbors()) neighbours = set(in_neighbours + out_neighbours) p_candidates = [candidate for candidate in list(neighbours.intersection(isolated_vertices)) if candidate not in seed] if len(p_candidates) > 0: for candidate in p_candidates: isolation_before = isolation_index(seed) new_seed = list(seed) + [candidate] isolation_after = isolation_index(new_seed) cohesion_after = cohesion_index(self._network, new_seed) if isolation_after > isolation_before and cohesion_after >= self._cohesion_factor * ic0 and candidate not in seed: seed[:] = seed + [candidate]
def _index_values(self, communities): """ Compute means of Isolation and Cohesion :param communities: :return: isolation, cohesion """ values = [] for community in communities: ia = isolation_index(community) ic = cohesion_index(self._network, community) values.append((ia, ic)) ''' isolation, cohesion = zip(*values) i_a = np.mean(isolation) i_c = np.mean(cohesion) ''' i_a = np.mean([v[0] for v in values]) i_c = np.mean([v[1] for v in values]) return i_a, i_c