def test_termination():
    # ensure termination of asyn_lpa_communities in two cases
    # that led to an endless loop in a previous version
    test1 = nx.karate_club_graph()
    test2 = nx.caveman_graph(2, 10)
    test2.add_edges_from([(0, 20), (20, 10)])
    asyn_lpa_communities(test1)
    asyn_lpa_communities(test2)
Exemple #2
0
def label_propagation(G, weight='weight'):
    '''Community detection using label propagation algorithm.
    
    Parameters
    ----------
    G : networkx.graph
    
    weight : edge attribute if G is weighted or None if G is unweighted

    Returns
    -------
    list_communities : list
        A list of sets, and each set contains vertices in one community.
    
    Notes
    -----
    This function only deals with weighted and unweighted undirected graph.
    '''
    # H is the undirected version of graph G
    H = G.to_undirected()
    if weight is None:
        communities = community.label_propagation_communities(H)
    else:
        communities = community.asyn_lpa_communities(H, weight=weight)
    list_communities = list(communities)
    return list_communities
def find_communities_citations_based(top_x_communities=100):
    """Returns a dict mapping an author_id to a community_id they belong to.
        The communities are created based on a citations graph.
        The community_id are sorted from largest to smallest with only top x
        communities created."""

    # Fetch the data from the db
    with connection.cursor() as cursor:
        # author | cited_author | amount_of_cites
        cursor.execute('''select app1.author_id, app2.author_id, count(*)
                    from author_paper_pairs app1
                    join publications p1 on app1.paper_id = p1.id
                    join cites c on p1.id = c.paper_id
                    join publications p2 on p2.id = c.cited_paper_id
                    join author_paper_pairs app2 on app2.paper_id = p2.id
                    group by app1.author_id, app2.author_id;''')

        author_cites_author = cursor.fetchall()

    # Create a directed graph and populate it with weighed edges
    g = nx.DiGraph()
    g.add_weighted_edges_from(author_cites_author)

    # Run community detection algorithm (asynchronous label propagation)
    communities = sorted(nxcom.asyn_lpa_communities(g, weight='weight'),
                         key=len, reverse=True)

    return communities_to_dict(communities, top_x_communities)
def label_propagation(network):
    coms_iter = nxcom.asyn_lpa_communities(network)
    communities = []
    for nodes in iter(coms_iter):
        communities.append(list(nodes))

    return communities
Exemple #5
0
def apply_lpa(UG):
    partition_list = list(community.asyn_lpa_communities(UG))
    partition_map = list_to_dict(partition_list)
    try:
        mod = nx.community.quality.modularity(UG, partition_list)
    except:
        mod = 0
    return mod, partition_map, partition_list
Exemple #6
0
def LabelPropagationAlgorithm(ground_truth_path, dataset_path, size):

    #initialize gorund truth
    ground_truth_set = groundTruth(ground_truth_path)
    #create network from edge list
    G = nx.read_edgelist(dataset_path)

    #list to store the result of each iteration
    iteration_accuracy = []
    iteration_time = []

    #define number of iterations
    iterations = 10
    for i in range(iterations):

        #call label propagation algorithm and time it
        start = time.time()
        res_lpa = community.asyn_lpa_communities(G)
        end = time.time()

        accuracy = round(percentage_of_nodes(ground_truth_set, res_lpa, size),
                         2)
        execTime = round((end - start) * 1000, 2)

        #append results (accuracy and time) for the current iteration
        iteration_accuracy.append(accuracy)
        iteration_time.append(execTime)

    #calculate and return average accuracy and average execution time
    avg_accuracy = round(sum(iteration_accuracy) / len(iteration_accuracy), 2)
    avg_time = round(sum(iteration_time) / len(iteration_time), 2)

    f = open("results.txt", "a")
    f.write("\nLabel Propagation Algorithm:\n")
    f.write("Algorithm is non-deterministic\n")
    f.write("Results are shown for 10 iterations\n")
    if (ground_truth_path == "datasets/Karate/karate-club-labels.txt"):
        f.write("Dataset: Karate Club\n")
    elif (ground_truth_path ==
          "datasets/email-Eu-Core/email-Eu-core-department-labels.txt"):
        f.write("Dataset: Email Eu Core\n")
    elif (ground_truth_path == "datasets/YouTube/youtube.top5000.txt"):
        f.write("Dataset: YouTube\n")
    elif (ground_truth_path == "datasets/Amazon/amazon.top5000.txt"):
        f.write("Dataset: Amazon\n")
    elif (ground_truth_path == "datasets/Football/football-comms.txt"):
        f.write("Dataset: College Football\n")

    f.write("{:<20s}{:<20s}\n".format("Accuracy[%]", "Run Time[ms]"))
    for i in range(iterations):
        f.write("{:<20f}{:<20f}".format(iteration_accuracy[i],
                                        iteration_time[i]))
        f.write("\n")
    f.write("\n{:<20s}{:<20s}\n".format("Average accuracy", "Average time"))
    f.write("{:<20f}{:<20f}\n\n".format(avg_accuracy, avg_time))
    f.close()
    def _check_communities(self, G, expected):
        """Checks that the communities computed from the given graph ``G``
        using the :func:`~networkx.asyn_lpa_communities` function match
        the set of nodes given in ``expected``.

        ``expected`` must be a :class:`set` of :class:`frozenset`
        instances, each element of which is a node in the graph.

        """
        communities = asyn_lpa_communities(G)
        result = {frozenset(c) for c in communities}
        assert_equal(result, expected)
Exemple #8
0
def adjacency_matrix_top_n():
    """
    adjacentcy matrix dara
    API format: /adjacency-matrix?size=<number of nodes>
    @param size: top n number of nodes. default value is 100 
    {
        'nodes': [
            {"group": 0, "index": 95, "name": "casualiama"},
            {...},
            ...
        ],
        'links': [
            {"source": "islam", "target": "worldnews", "value": 27},
            {...},
            ...
        ]
    } 
    """
    size = request.args.get('size', default=100, type=int)
    if 'multidigraph' not in globals:
        get_nodes_query = '''
            MATCH (n:Subreddit)-[LINK]->(Subreddit)
            RETURN n.id AS id
        '''
        nodes_result = execute_query(get_nodes_query)
        get_links_query = '''
            MATCH (s)-[LINK]->(t) 
            RETURN s.id AS source, t.id AS target
        '''
        links_result = execute_query(get_links_query)
        # nx multi-directional graph
        globals['multidigraph'] = make_multi_directed_graph(nodes=nodes_result,
                                                            links=links_result)
    G = globals['multidigraph'].copy()
    # downsizing using eigenvector centrality score
    ecs_G = eigenvector_centrality(G=G, size=size)
    # communities detection - label propagation algorithm
    communities_generator = community.asyn_lpa_communities(ecs_G)
    # update data
    index = 0
    for c in communities_generator:
        comm = list(c)
        for i, n in enumerate(comm):
            G.add_node(n, index=index, name=n, group=i)
            index += 1
    distinct_edges = list(dict.fromkeys(list(G.edges())))
    ret_edges = [{
        'source': s,
        'target': t,
        'value': len(G.get_edge_data(s, t))
    } for (s, t) in distinct_edges]
    ret_nodes = [data for (n, data) in list(G.nodes(data=True))]
    return jsonify({'nodes': ret_nodes, 'links': ret_edges})
    def _check_communities(self, G, expected):
        """Checks that the communities computed from the given graph ``G``
        using the :func:`~networkx.asyn_lpa_communities` function match
        the set of nodes given in ``expected``.

        ``expected`` must be a :class:`set` of :class:`frozenset`
        instances, each element of which is a node in the graph.

        """
        communities = asyn_lpa_communities(G)
        result = {frozenset(c) for c in communities}
        assert_equal(result, expected)
Exemple #10
0
    def get_alp_communities(self):
        comm_iterator = community.asyn_lpa_communities(self.ugc,
                                                       weight='weight')
        comm_part = list(comm_iterator)
        node_comm = {}
        for comm_i, node_set in enumerate(comm_part):
            for node in node_set:
                node_comm[node] = comm_i

        comm_mod = community.quality.modularity(G=self.ugc,
                                                communities=comm_part,
                                                weight='weight')

        self.communities['comm.ALP_%.2f'%comm_mod] = \
                                            node_comm
Exemple #11
0
def edge_bundling_top_n_v2():
    """
    version 2 - using multi-directional graph
    edge bundling graph on top selected nodes
    API format: /edge-bundling?size=<number of nodes>
    @param size: top n number of nodes. default value is 100 
    [
        {'name': '61.pcmasterrace', 'size': 323, 'imports': ["0.personalfinance", "0.self", ...]},
        {...}
        ...
    ] 
    """
    size = request.args.get('size', default=100, type=int)
    if 'multidigraph' not in globals:
        get_nodes_query = '''
            MATCH (n:Subreddit)-[LINK]->(Subreddit)
            RETURN n.id AS id
        '''
        nodes_result = execute_query(get_nodes_query)
        get_links_query = '''
            MATCH (s)-[LINK]->(t) 
            RETURN s.id AS source, t.id AS target
        '''
        links_result = execute_query(get_links_query)
        # nx multi-directional graph
        globals['multidigraph'] = make_multi_directed_graph(nodes=nodes_result,
                                                            links=links_result)
    G = globals['multidigraph'].copy()
    # downsizing using eigenvector centrality score
    ecs_G = eigenvector_centrality(G=G, size=size)
    # communities detection - label propagation algorithm
    communities_generator = community.asyn_lpa_communities(ecs_G)
    # update name attr
    for c in communities_generator:
        comm = list(c)
        for i, n in enumerate(comm):
            G.add_node(n, name=str(i) + '.' + str(n))
    # update size and imports attrs
    ret_nodes = list(G.nodes(data=True))
    for n in ret_nodes:
        (node, attrs) = n
        attrs['size'] = G.out_degree(node)
        attrs['imports'] = list(
            dict.fromkeys([
                nx.get_node_attributes(G, 'name')[t]
                for (s, t) in list(G.out_edges(node))
            ]))
    return jsonify([data for (n, data) in ret_nodes])
Exemple #12
0
def clusters_lpa(evs, node_list, verbose=True):

    import networkx as nx
    from networkx.algorithms import community

    if len(evs) > 0:
        # clustering: create network
        if verbose:
            logging.info("Create network")
        evs_e = evs[["in_gene", "out_gene", "branch_support"]]
        evs_n = nx.convert_matrix.from_pandas_edgelist(
            evs_e,
            source="in_gene",
            target="out_gene",
            edge_attr="branch_support")
        evs_n.add_nodes_from(node_list)

        # clustering: asynchronous label propagation
        if verbose:
            logging.info("Find communities LPA")
        clu_c = community.asyn_lpa_communities(evs_n, seed=11)
        clu_c = {frozenset(c) for c in clu_c}
        if verbose:
            logging.info("Find communities LPA num clusters = %i" % len(clu_c))
        clu_c_clu = [i for i, cluster in enumerate(clu_c) for node in cluster]
        clu_c_noi = [
            node for i, cluster in enumerate(clu_c) for node in cluster
        ]

    else:

        if verbose:
            logging.info("There are no speciation events in this tree.")
        clu_c_noi = node_list
        clu_c_clu = [i for i in range(len(node_list))]

    # clustering: save output
    clu = pd.DataFrame({
        "node": clu_c_noi,
        "cluster": clu_c_clu,
    },
                       columns=["node", "cluster"])
    if verbose:
        logging.info("Find communities LPA | num clustered genes = %i" %
                     len(clu))

    return clu
    def fit_predict(self, X, y):
        """Performs clustering on y and returns list of label lists

        Builds a label graph using the provided graph builder's `transform` method
        on `y` and then detects communities using the selected `method`.

        Sets :code:`self.weights_` and :code:`self.graph_`.

        Parameters
        ----------
        X : None
            currently unused, left for scikit compatibility
        y : scipy.sparse
            label space of shape :code:`(n_samples, n_labels)`

        Returns
        -------
        arrray of arrays of label indexes (numpy.ndarray)
            label space division, each sublist represents labels that are in that community
        """
        edge_map = self.graph_builder.transform(y)

        if self.graph_builder.is_weighted:
            self.weights_ = dict(weight=list(edge_map.values()))
        else:
            self.weights_ = dict(weight=None)

        self.graph_ = nx.Graph()
        for n in range(y.shape[1]):
            self.graph_.add_node(n)

        for e, w in edge_map.items():
            self.graph_.add_edge(e[0], e[1], weight=w)

        if self.method == 'louvain':
            partition_dict = community.best_partition(self.graph_)
            memberships = [partition_dict[i] for i in range(y.shape[1])]

            return np.array(
                _membership_to_list_of_communities(
                    memberships,
                    1 + max(memberships)
                )
            )
        else:
            return np.array([list(i) for i in asyn_lpa_communities(self.graph_, 'weight')])
Exemple #14
0
    def fit_predict(self, X, y):
        """Performs clustering on y and returns list of label lists

        Builds a label graph using the provided graph builder's `transform` method
        on `y` and then detects communities using the selected `method`.

        Sets :code:`self.weights_` and :code:`self.graph_`.

        Parameters
        ----------
        X : None
            currently unused, left for scikit compatibility
        y : scipy.sparse
            label space of shape :code:`(n_samples, n_labels)`

        Returns
        -------
        arrray of arrays of label indexes (numpy.ndarray)
            label space division, each sublist represents labels that are in that community
        """
        edge_map = self.graph_builder.transform(y)

        if self.graph_builder.is_weighted:
            self.weights_ = dict(weight=list(edge_map.values()))
        else:
            self.weights_ = dict(weight=None)

        self.graph_ = nx.Graph()
        for n in range(y.shape[1]):
            self.graph_.add_node(n)

        for e, w in edge_map.items():
            self.graph_.add_edge(e[0], e[1], weight=w)

        if self.method == 'louvain':
            partition_dict = community.best_partition(self.graph_)
            memberships = [partition_dict[i] for i in range(y.shape[1])]

            return np.array(
                _membership_to_list_of_communities(memberships,
                                                   1 + max(memberships)))
        else:
            return np.array(
                [list(i) for i in asyn_lpa_communities(self.graph_, 'weight')])
def create_figure(truth):
    ''' Create image '''
    graph = nx.read_edgelist('datasets/karate/edges.txt')
    communities = list(community.asyn_lpa_communities(graph))
    communities = convert_communities(communities)

    edgecolors = [COLORS[i] for i in truth]
    nodecolors = [COLORS[i] for i in communities]
    position = nx.spring_layout(graph)
    nx.draw(graph,
            position,
            node_color=nodecolors,
            edgecolors=edgecolors,
            linewidths=2)

    # plt.axis('off')
    # plt.subplots_adjust(top=5)
    plt.suptitle(f"Zachary karate network compared to ground-truth")
    plt.savefig('plots/karate-graph.png', bbox_inches='tight')
Exemple #16
0
def part_b_nx(adapters):
    s_adapters = sorted(adapters)
    device_joltage = s_adapters[-1] + 3

    G = create_graph(s_adapters, device_joltage)

    # Remove "linear" edges to split the graph into components
    edges_to_remove = find_graph_cuts(G)
    G.remove_edges_from(edges_to_remove)

    #nx.nx_agraph.write_dot(G, "cut-graph.dot")

    # Iterate over components of graph
    result = 1
    for G_c in nxcomm.asyn_lpa_communities(G):
        if len(G_c) > 1:
            paths = list(nx.all_simple_paths(G, min(G_c), max(G_c)))
            result *= len(paths)

    return result
Exemple #17
0
 def do_clustering(self):
     if len(self.clusters) == 0 or self.clusterid != str(id(self)):
         self.clusterid = str(id(self))
         self.build_nx_graph()
         clusters = list(asyn_lpa_communities(self.graph, weight="weight"))
         new_clusters = []
         for c in clusters:
             new_clusters.append([])
             for el in c:
                 if self.nodes_inv[el].startswith("event_id="):
                     new_clusters[-1].append(self.events_corr_inv[self.nodes_inv[el]])
             if len(new_clusters[-1]) == 0:
                 del new_clusters[-1]
         new_clusters = sorted(new_clusters, key=lambda x: len(x), reverse=True)
         self.clusters = {}
         for i in range(len(new_clusters)):
             self.clusters["Cluster " + self.fill_string(str(i + 1)) + " (" + str(len(new_clusters[i])) + ")"] = \
                 new_clusters[i]
         clusters_keys = sorted(list(self.clusters.keys()))
         self.clustersrepr = "@@@".join(clusters_keys)
def label_propagation(G, weight='weight', iterNum=6):
    '''Community detection using label propagation algorithm.
    
    Parameters
    ----------
    G : networkx.graph
    
    weight : edge attribute if G is weighted or None if G is unweighted

    iterNum : number to repeat label propagation algorithm

    Returns
    -------
    list_communities : list
        A list of sets, and each set contains vertices in one community.
    
    Notes
    -----
    This function only deals with weighted and unweighted undirected graph.
    '''
    # H is the undirected version of graph G
    H = G.to_undirected()
    max_modularity = float('-inf')
    for i in range(iterNum):
        if weight is None:
            cur_list_communities = list(
                community.label_propagation_communities(H))
        else:
            cur_list_communities = list(
                community.asyn_lpa_communities(H, weight=weight))

        cur_modularity = quality.modularity(H, cur_list_communities)
        if (cur_modularity > max_modularity):
            list_communities = cur_list_communities
            max_modularity = cur_modularity

    return list_communities
Exemple #19
0
import matplotlib.pyplot as plt

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

# https://medium.com/@sddkal/random-walks-on-adjacency-matrices-a127446a6777#:~:text=A%20random%20walk%20is%20a,can%20perform%20a%20random%20walk.
# %%
# G = nx.karate_club_graph()
# G = nx.barbell_graph(5, 2)
# # G = nx.bull_graph()
# # G = nx.generators.erdos_renyi_graph(10, 0.5)
# # G = nx.generators.cubical_graph()
# G = generator.planted_partition_graph(4, 50, p_in=0.9, p_out=0.05)
G, pos = generate_benchmark_graph(250,0.1)
pos = nx.spring_layout(G)
true_partition_map = community_louvain.best_partition(G)
lpa_prt = extract_partition_map(algorithms.asyn_lpa_communities(G))
communities = extract_community_map(true_partition_map)

nx.draw_networkx(G, pos, edgecolors="black", node_size=600, cmap=plt.cm.RdYlBu, node_color=list(true_partition_map.values()))

# %%
def random_walk(a, i, iters):
    # a -> adj
    # i -> starting row
    walk = np.zeros(iters+1) # holds transitions
    walk[0] = i
    elements = np.arange(a.shape[0]) # for our graph [0,1,2,3]
    c_index = i # current index for this iteration
    for k in range(iters):
        count = 0 # count of transitions
        probs = a[c_index].reshape((-1,))  # probability of transitions
Exemple #20
0
    def get_communities(self, station_df):

        import networkx as nx
        import networkx.algorithms.community as nx_comm

        g_communities_ = []

        try:
            ''' sample the graph '''
            g_simple_ = self.get_simple_graph(station_df)

            if nx.is_empty(g_simple_):
                raise ValueError(
                    'A simple graph with %d stations was not created' %
                    station_df.shape[0])

            if self.name == 'ASYNC-LPA':  #asyn_lpa_communities
                g_communities_ = list(
                    nx_comm.asyn_lpa_communities(g_simple_,
                                                 weight=self.weight,
                                                 seed=self.seed))

            elif self.name == 'LPC':  #label_propagation_communities
                g_communities_ = list(
                    nx_comm.label_propagation_communities(g_simple_))

            elif self.name == 'GREEDY':  # greedy_modularity_communities
                g_communities_ = list(
                    nx_comm.greedy_modularity_communities(g_simple_))

            elif self.name == 'NAIVE-GREEDY':  #_naive_greedy_modularity_communities
                g_communities_ = list(
                    nx_comm._naive_greedy_modularity_communities(g_simple_))

            elif self.name == 'LUKES':  # lukes_partitioning
                # TODO: create MST of g_simple first but removing the mimum weigted edge doesn't seem right
                g_communities_ = list(
                    nx_comm.lukes_partitioning(
                        g_simple_,
                        edge_weight=self.weight,
                        max_size=self.maximum_node_weight))

            elif self.name == 'ASYNC-FLUID':  # asyn_fluidc
                # TODO: create complete graph for g_simple but a complete graph would not work
                g_communities_ = list(
                    nx_comm.asyn_fluidc(g_simple_,
                                        k=15,
                                        max_iter=300,
                                        seed=self.seed))

            elif self.name == 'GIRVAN-NEWMAN':  # girvan_newman
                #                g_communities_ = list(nx_comm.girvan_newman(g_simple_))
                #                tmp_communities = nx_comm.girvan_newman(g_simple_)
                #                g_communities_  = next(tmp_communities)
                g_communities_ = list(next(nx_comm.girvan_newman(g_simple_)))
#                print(list(g_communities_))

            else:
                raise AttributeError("something was not right")

#            g_simple_ = self.set_graph_cluster_labels(g_simple_, g_communities_)
            if isinstance(g_communities_, list):  #len(g_communities_)>0
                g_simple_ = self.set_graph_cluster_labels(
                    g_simple_, g_communities_)

#d            return g_simple_, g_communities_

        except Exception as err:
            print("Class community_detection [get_communities] Error message:",
                  err)

        return g_simple_, g_communities_
Exemple #21
0
# g=nx.fast_gnp_random_graph(8,0.7)
# g=nx.windmill_graph(8,4)
N = len(g)
W = np.zeros((N, N))
for i in g:
    print(i, end="-> ")
    for j in nx.neighbors(g, i):
        print(j, end=" ")
        W[i][j] = 1
        W[j][i] = 1
    print()

# networkx community detection

gmc = list(greedy_modularity_communities(g))
alc = list(asyn_lpa_communities(g))
lpac = list(label_propagation_communities(g))
asfl = list(asyn_fluidc(g, 3))

# inititalization
anchorList = set([])
U = {}
U[N] = np.zeros(N)
randomFirstAnchor = random.randint(0, N - 1)
anchorList.add(randomFirstAnchor)
U[randomFirstAnchor] = np.zeros(N)
phi = 0.25
itermax = 15
K = 1
adjacentNodes = {}
for i in range(N):
 def test_seed_argument(self):
     G = nx.Graph(["ab", "ac", "bc", "de", "df", "fe"])
     ground_truth = {frozenset("abc"), frozenset("def")}
     communities = asyn_lpa_communities(G, seed=1)
     result = {frozenset(c) for c in communities}
     assert result == ground_truth
def func_propagation(graph):
    # cm = nx_comm.label_propagation_communities(graph)
    cm = nx_comm.asyn_lpa_communities(graph)
    cm = tuple(set(e) for e in cm)
    return cm
Exemple #24
0
def calculate_community():
    cate = list(community.asyn_lpa_communities(G, 'weight'))
Exemple #25
0
                       center=(0.5, 0.5),
                       scale=0.5,
                       k=1 / len(graph)**0.1,
                       seed=1)
# pos = nx.kamada_kawai_layout(graph, center=(0.5, 0.5), scale=0.5)

# Base edges
nx.draw_networkx_edges(graph, pos=pos, width=0.2)

# Bridges
if type(graph) is not nx.DiGraph and nx.has_bridges(graph):
    nx.draw_networkx_edges(graph,
                           edgelist=list(nx.bridges(graph)),
                           pos=pos,
                           width=3,
                           alpha=0.5,
                           edge_color="r")

# Nodes with colors
groups = list(asyn_lpa_communities(graph, weight="h"))
colors = [[state in com for com in groups].index(True) for state in graph]
nx.draw_networkx_nodes(graph, pos=pos, node_color=colors, cmap="tab20")

# Labels
nx.draw_networkx_labels(graph, pos, font_size=10)

if type(graph) is nx.DiGraph:
    sorts = list(reversed(list(topological_sort(graph))))

plt.show()
    # return nx.draw_networkx(G, 
    # pos, 
    # edge_color="black", 
    # with_labels=False, 
    # node_size=50, 
    # cmap=plt.cm.viridis, 
    # node_color=list(partition.values()) if partition else None, 
    # ax=ax)

# %%
print(f"Computing the ground truth of the LFR graph")
G, pos = generate_benchmark_graph(250,0.1)
true_partition_map, communities = extract_true_communities(G)
communities = algorithms.greedy_modularity_communities(G)
cnm_partition_map = extract_partition_map(communities)
communities = algorithms.asyn_lpa_communities(G)
lpa_partition_map = extract_partition_map(communities)
louvain_partition_map = community_louvain.best_partition(G)

#%%
print(f"Drawing the the LFR graphs")
fig, ax = plt.subplots(2, 2)
fig.set_size_inches(10, 10)
ax[0][0].set_title(f"Initial Graph", fontsize=10)
ax[0][0].set_axis_off()
ax[0][1].set_title(f"Ground-Truth", fontsize=10)
ax[0][1].set_axis_off()
# ax[0][1].set_title(f"Louvain", fontsize=10)
# ax[0][1].set_axis_off()
ax[1][0].set_title(f"Greedy Max Modularity ", fontsize=10)
ax[1][0].set_axis_off()
Exemple #27
0
def get_communities_async_label_propagation(G):
    return list(community.asyn_lpa_communities(G))
Exemple #28
0
def add_asyn_lpa_communities(graph, weight=None, seed=None):
    communities_result = nx_community.asyn_lpa_communities(graph, weight, seed)
    _nx_community_data_to_graph(graph, communities_result)
    return graph
 def test_seed_argument(self):
     G = nx.Graph(['ab', 'ac', 'bc', 'de', 'df', 'fe'])
     ground_truth = {frozenset('abc'), frozenset('def')}
     communities = asyn_lpa_communities(G, seed=1)
     result = {frozenset(c) for c in communities}
     assert_equal(result, ground_truth)
""" Creating the graph given the nodes and edges """
nodes = data["nodes"]
for i in nodes:
    g.add_node(i["label"])

for i in data_read:
    g.add_edge(find_node(i["properties"]["start"]),
               find_node(i["properties"]["end"]),
               frequency=int(i["frequency"]))
""" For saving the graph object """
# with open('graph.pkl', 'wb') as f:
#     pickle.dump(g, f)
""" Directed Community detection methods """
# communities_generator = community.girvan_newman(g)
communities_generator = community.asyn_lpa_communities(g)
""" Undirected community detection methods """
# communities_generator = community.kernighan_lin_bisection(g)
# communities_generator = community.k_clique_communities(g,2)
# communities_generator = community.label_propagation_communities(g)

# communities_generator = community.greedy_modularity_communities(g) #Remains
# communities_generator = community.asyn_fluidc(g,2) #Remains
""" For Girvan-Newman algo """
# top_level_communities = next(communities_generator)
# next_level_communities = next(communities_generator)
""" For remaining algos """
next_level_communities = communities_generator
next_level_communities = sorted(map(sorted, next_level_communities))
""" Writing clusters detected to the schema """
counter = 0
Exemple #31
0
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)
Exemple #32
0
from networkx.algorithms.community.quality import performance
from networkx.algorithms.community.centrality import girvan_newman

# Graph=nx.karate_club_graph()
# Graph=nx.read_gml('datasets/dolphins.gml',label='id')
Graph = nx.fast_gnp_random_graph(25, 0.7)
# Graph=nx.windmill_graph(8,4)

# sizes = [5, 5, 10]
# probs = [[0.05, 0.05, 0.02],
#          [0.05, 0.15, 0.07],
#          [0.02, 0.07, 0.04]]
# Graph = nx.stochastic_block_model(sizes, probs, seed=0)

gmc = list(greedy_modularity_communities(Graph))
alc = list(asyn_lpa_communities(Graph))
lpac = list(label_propagation_communities(Graph))
asfl = list(asyn_fluidc(Graph, 3))
girvanNewmanCommunities = list(girvan_newman(Graph))
W2 = np.zeros((len(Graph), len(Graph)))
for i in Graph:
    for j in nx.neighbors(Graph, i):
        W2[i][j] = 1
        W2[j][i] = 1
for i in range(len(Graph)):
    print(i, end="->")
    for j in range(len(Graph)):
        if (W2[i][j] == 1):
            print(j, end=" ")
    print()
W = W2