예제 #1
0
 def test_greedy_modularity(self):
     g = get_string_graph()
     coms = algorithms.greedy_modularity(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 test_greedy_modularity(self):
     if leidenalg is not None:
         g = get_string_graph()
         try:
             coms = algorithms.greedy_modularity(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)
         except:
             pass
예제 #3
0
#%%

# to indirected
unet=net.to_undirected(reciprocal=True)
# removing isolates
unet.remove_nodes_from(list(nx.isolates(unet)))


#%%

# bring algorithm
from cdlib import algorithms

# Find the communities
modCommunity = algorithms.greedy_modularity(unet).communities


#%%
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import rgb2hex

palette = plt.get_cmap("Set1")

pos=nx.kamada_kawai_layout(unet)

# for each community
colorIndex=0
for community in modCommunity:
    # INSTEAD of rgb to hexadecimal: repeating list
예제 #4
0
     communities = algorithms.leiden(g, weights='weight', **clust_kwargs)
 elif (options.method == 'louvain'):
     communities = algorithms.louvain(g, weight='weight', **clust_kwargs)
 elif (options.method == 'cpm'):
     communities = algorithms.cpm(g, weights='weight', **clust_kwargs)
 elif (options.method == 'der'):
     communities = algorithms.der(g, **clust_kwargs)
 elif (options.method == 'edmot'):
     communities = algorithms.edmot(g, **clust_kwargs)
 elif (options.method == 'eigenvector'):
     communities = algorithms.eigenvector(g, **clust_kwargs)
 elif (options.method == 'gdmp2'):
     communities = algorithms.gdmp2(g, **clust_kwargs)
 elif (options.method == 'greedy_modularity'):
     communities = algorithms.greedy_modularity(g,
                                                weight='weight',
                                                **clust_kwargs)
 #elif(options.method == 'infomap'):
 #	communities = algorithms.infomap(g)
 elif (options.method == 'label_propagation'):
     communities = algorithms.label_propagation(g, **clust_kwargs)
 elif (options.method == 'markov_clustering'):
     communities = algorithms.markov_clustering(g, **clust_kwargs)
 elif (options.method == 'rber_pots'):
     communities = algorithms.rber_pots(g, weights='weight', **clust_kwargs)
 elif (options.method == 'rb_pots'):
     communities = algorithms.rb_pots(g, weights='weight', **clust_kwargs)
 elif (options.method == 'significance_communities'):
     communities = algorithms.significance_communities(g, **clust_kwargs)
 elif (options.method == 'spinglass'):
     communities = algorithms.spinglass(g, **clust_kwargs)
    leiden_partition = cd.leiden(LFR_G)
    leiden_labels = extract_communities_list(leiden_partition.communities)
    nmi_leiden.append(normalized_mutual_info_score(true_labels, louvian_labels))

    ############################### Walktrap ###############################
    walktrap_partition = cd.walktrap(LFR_G)
    walktrap_labels = extract_communities_list(walktrap_partition.communities)
    nmi_walktrap.append(normalized_mutual_info_score(true_labels, walktrap_labels))

    ############################### Markov Clustering ###############################
    markov_partition = cd.markov_clustering(LFR_G)
    markov_labels = extract_communities_list(markov_partition.communities)
    nmi_markov.append(normalized_mutual_info_score(true_labels, markov_labels))

    ############################### Greedy ###############################
    greedy_partition = cd.greedy_modularity(LFR_G)
    greedy_labels = extract_communities_list(greedy_partition.communities)
    nmi_greedy.append(normalized_mutual_info_score(true_labels, greedy_labels))

    ############################### Label Propagation ###############################
    propagation_partition = cd.label_propagation(LFR_G)
    propagation_labels = extract_communities_list(propagation_partition.communities)
    nmi_propagation.append(normalized_mutual_info_score(true_labels, propagation_labels))

#Plot NMI scores
nmi_graph = plt.gca()
nmi_graph.set_xlim([0, 0.9])
nmi_graph.set_ylim([-0.1, 1])
nmi_graph.plot(np.arange(0.1, 1, 0.1), nmi_infomap, color='#575757',
               marker='o', mfc='#f1362b', mec='#f1362b', label="Infomap")
nmi_graph.plot(np.arange(0.1, 1, 0.1), nmi_eigenvector, color='#575757',
ari_walktrap = adjusted_rand_score(true_communities, walktrap_communities)
vi_walktrap = variation_of_information(true_partitions, walktrap_partitions)

############################### Markov Clustering ###############################
start_time = time.time()
markov_partition = cd.markov_clustering(G)
markov_time = time.time() - start_time
markov_communities = extract_communities_list(markov_partition.communities, G)
markov_partitions = get_partitions(markov_communities)
nmi_markov = normalized_mutual_info_score(true_communities, markov_communities)
ari_markov = adjusted_rand_score(true_communities, markov_communities)
vi_markov = variation_of_information(true_partitions, markov_partitions)

############################### Greedy ###############################
start_time = time.time()
greedy_partition = cd.greedy_modularity(G)
greedy_time = time.time() - start_time
greedy_communities = extract_communities_list(greedy_partition.communities, G)
greedy_partitions = get_partitions(greedy_communities)
nmi_greedy = normalized_mutual_info_score(true_communities, greedy_communities)
ari_greedy = adjusted_rand_score(true_communities, greedy_communities)
vi_greedy = variation_of_information(true_partitions, greedy_partitions)

############################### Label Propagation ###############################
start_time = time.time()
propagation_partition = cd.label_propagation(G)
propagation_time = time.time() - start_time
propagation_communities = extract_communities_list(
    propagation_partition.communities, G)
propagation_partitions = get_partitions(propagation_communities)
nmi_propagation = normalized_mutual_info_score(true_communities,
예제 #7
0
import networkx as nx
import argparse


def get_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('--edgelist',
                        dest='edgelist',
                        required=True,
                        help='tab separated edge list')

    parser.add_argument('--output',
                        dest='output',
                        required=True,
                        help='name of file to save the community to')

    args = parser.parse_args()
    return args


args = get_args()

G = nx.read_edgelist(args.edgelist)
coms = algorithms.greedy_modularity(G)

with open(args.output, 'w') as outfile:
    for i, com in enumerate(coms.communities):
        outfile.write('\t'.join([str(i)] + com))
        outfile.write('\n')