def test_slpa(self): g = get_string_graph() coms = algorithms.slpa(g) self.assertEqual(type(coms.communities), list) if len(coms.communities) > 0: self.assertEqual(type(coms.communities[0]), list) self.assertEqual(type(coms.communities[0][0]), str)
def find_communities(nnodes, edges, alg, params=None): def membership2cs(membership): cs = {} for i, m in enumerate(membership): cs.setdefault(m, []).append(i) return cs.values() def connected_subgraphs(G: nx.Graph): for comp in nx.connected_components(G): sub = nx.induced_subgraph(G, comp) sub = nx.convert_node_labels_to_integers(sub, label_attribute='old') yield sub def apply_subgraphs(algorithm, **params): cs = [] for sub in connected_subgraphs(G): if len(sub.nodes) <= 3: coms = [sub.nodes] # let it be a cluster else: coms = algorithm(sub, **params) if hasattr(coms, 'communities'): coms = coms.communities for com in coms: cs.append([sub.nodes[i]['old'] for i in set(com)]) return cs def karate_apply(algorithm, graph, **params): model = algorithm(**params) model.fit(graph) return membership2cs(model.get_memberships().values()) if alg == 'big_clam': c = -1 if params['c'] == 'auto' else int(params['c']) cs = BigClam('../../snap').run(edges, c=c, xc=int(params['xc'])) elif alg in ('gmm', 'kclique', 'lprop', 'lprop_async', 'fluid', 'girvan_newman', 'angel', 'congo', 'danmf', 'egonet_splitter', 'lfm', 'multicom', 'nmnf', 'nnsed', 'node_perception', 'slpa', 'GEMSEC', 'EdMot', 'demon'): G = nx.Graph() G.add_edges_from(edges) if alg == 'gmm': cs = community.greedy_modularity_communities(G) elif alg == 'kclique': params = {k: float(v) for k, v in params.items()} cs = community.k_clique_communities(G, **params) elif alg == 'lprop': cs = community.label_propagation_communities(G) elif alg == 'lprop_async': cs = community.asyn_lpa_communities(G, seed=0) elif alg == 'fluid': params = {k: int(v) for k, v in params.items()} params['seed'] = 0 cs = apply_subgraphs(community.asyn_fluidc, **params) elif alg == 'girvan_newman': comp = community.girvan_newman(G) for cs in itertools.islice(comp, int(params['k'])): pass elif alg == 'angel': params = {k: float(v) for k, v in params.items()} cs = cdlib.angel(G, **params).communities elif alg == 'congo': # too slow ncoms = int(params['number_communities']) cs = [] for sub in connected_subgraphs(G): if len(sub.nodes) <= max(3, ncoms): cs.append(sub.nodes) # let it be a cluster else: coms = cdlib.congo(sub, number_communities=ncoms, height=int(params['height'])) for com in coms.communities: cs.append([sub.nodes[i]['old'] for i in set(com)]) elif alg == 'danmf': # no overlapping cs = apply_subgraphs(cdlib.danmf) elif alg == 'egonet_splitter': params['resolution'] = float(params['resolution']) cs = apply_subgraphs(cdlib.egonet_splitter, **params) elif alg == 'lfm': coms = cdlib.lfm(G, float(params['alpha'])) cs = coms.communities elif alg == 'multicom': cs = cdlib.multicom(G, seed_node=0).communities elif alg == 'nmnf': params = {k: int(v) for k, v in params.items()} cs = apply_subgraphs(cdlib.nmnf, **params) elif alg == 'nnsed': cs = apply_subgraphs(cdlib.nnsed) elif alg == 'node_perception': # not usable params = {k: float(v) for k, v in params.items()} cs = cdlib.node_perception(G, **params).communities elif alg == 'slpa': params["t"] = int(params["t"]) params["r"] = float(params["r"]) cs = cdlib.slpa(G, **params).communities elif alg == 'demon': params = {k: float(v) for k, v in params.items()} cs = cdlib.demon(G, **params).communities elif alg == 'GEMSEC': # gamma = float(params.pop('gamma')) params = {k: int(v) for k, v in params.items()} # params['gamma'] = gamma params['seed'] = 0 _wrap = partial(karate_apply, karateclub.GEMSEC) cs = apply_subgraphs(_wrap, **params) elif alg == 'EdMot': params = {k: int(v) for k, v in params.items()} _wrap = partial(karate_apply, karateclub.EdMot) cs = apply_subgraphs(_wrap, **params) elif alg in ('infomap', 'community_leading_eigenvector', 'leig', 'multilevel', 'optmod', 'edge_betweenness', 'spinglass', 'walktrap', 'leiden', 'hlc'): G = igraph.Graph() G.add_vertices(nnodes) G.add_edges(edges) if alg == 'infomap': vcl = G.community_infomap(trials=int(params['trials'])) cs = membership2cs(vcl.membership) elif alg == 'leig': clusters = None if params['clusters'] == 'auto' else int( params['clusters']) vcl = G.community_leading_eigenvector(clusters=clusters) cs = membership2cs(vcl.membership) elif alg == 'multilevel': vcl = G.community_multilevel() cs = membership2cs(vcl.membership) elif alg == 'optmod': # too long membership, modularity = G.community_optimal_modularity() cs = membership2cs(vcl.membership) elif alg == 'edge_betweenness': clusters = None if params['clusters'] == 'auto' else int( params['clusters']) dendrogram = G.community_edge_betweenness(clusters, directed=False) try: clusters = dendrogram.as_clustering() except: return [] cs = membership2cs(clusters.membership) elif alg == 'spinglass': # only for connected graph vcl = G.community_spinglass(parupdate=True, update_rule=params['update_rule'], start_temp=float(params['start_temp']), stop_temp=float(params['stop_temp'])) cs = membership2cs(vcl.membership) elif alg == 'walktrap': dendrogram = G.community_walktrap(steps=int(params['steps'])) try: clusters = dendrogram.as_clustering() except: return [] cs = membership2cs(clusters.membership) elif alg == 'leiden': vcl = G.community_leiden( objective_function=params['objective_function'], resolution_parameter=float(params['resolution_parameter']), n_iterations=int(params['n_iterations'])) cs = membership2cs(vcl.membership) elif alg == 'hlc': algorithm = HLC(G, min_size=int(params['min_size'])) cs = algorithm.run(None) elif alg in ("sbm", "sbm_nested"): np.random.seed(42) gt.seed_rng(42) G = gt.Graph(directed=False) G.add_edge_list(edges) deg_corr = bool(params['deg_corr']) B_min = None if params['B_min'] == 'auto' else int(params['B_min']) B_max = None if params['B_max'] == 'auto' else int(params['B_max']) if alg == "sbm": state = gt.minimize_blockmodel_dl(G, deg_corr=deg_corr, B_min=B_min, B_max=B_max) membership = state.get_blocks() cs = membership2cs(membership) if alg == "sbm_nested": state = gt.minimize_nested_blockmodel_dl(G, deg_corr=deg_corr, B_min=B_min, B_max=B_max) levels = state.get_bs() level_max = int(params['level']) membership = {} for nid in range(nnodes): cid = nid level_i = len(levels) for level in levels: cid = level[cid] if level_i == level_max: membership.setdefault(cid, []).append(nid) break level_i -= 1 cs = membership.values() else: return None return list(cs)
elif (options.method == 'lais2'): communities = algorithms.lais2(g, **clust_kwargs) elif (options.method == 'big_clam'): communities = algorithms.big_clam(g, **clust_kwargs) elif (options.method == 'danmf'): communities = algorithms.danmf(g, **clust_kwargs) elif (options.method == 'ego_networks'): communities = algorithms.ego_networks(g, **clust_kwargs) elif (options.method == 'egonet_splitter'): communities = algorithms.egonet_splitter(g, **clust_kwargs) elif (options.method == 'nmnf'): communities = algorithms.nmnf(g, **clust_kwargs) elif (options.method == 'nnsed'): communities = algorithms.nnsed(g, **clust_kwargs) elif (options.method == 'slpa'): communities = algorithms.slpa(g, **clust_kwargs) elif (options.method == 'bimlpa'): communities = actlgorithms.bimlpa(g, **clust_kwargs) elif (options.method == 'wcommunity'): communities = algorithms.wCommunity(g, **clust_kwargs) elif (options.method == 'aslpaw'): import warnings with warnings.catch_warnings(): warnings.filterwarnings("ignore") communities = algorithms.aslpaw(g) elif (options.method == 'external'): from collections import defaultdict from cdlib import NodeClustering coms_to_node = defaultdict(list) f = open(options.external, 'r') for line in f: