Python community.best_partition() Examples

The following are 21 code examples of community.best_partition(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module community , or try the search function .
Example #1
Source File: community.py    From MLPrimitives with MIT License 6 votes vote down vote up
def produce(self, X, best_partition=None, graph=None):
        best_partition = best_partition or co.best_partition(graph)
        values = [b for a, b in best_partition.items()]
        missing_community_index = np.max(values) + 10

        result = pd.Series(index=X.index)

        for i in X.index:
            node = X.loc[i][0]

            if node in best_partition:
                community = best_partition[node]

            elif str(node) in best_partition:
                community = best_partition[str(node)]

            else:
                community = missing_community_index

                # increment missing index
                missing_community_index += 1

            result.loc[i] = community

        return result.values 
Example #2
Source File: matrix_clusters.py    From RSN with MIT License 6 votes vote down vote up
def Louvain(self):
        print('initializing the graph...')
        g = nx.Graph()
        g.add_nodes_from(np.arange(len(self.dataset)).tolist())

        print('adding edges...')
        edge_list = np.int32(np.round(self.dist_list))
        true_edge_list = []
        for i in range(len(idx_list)):
            if edge_list[i]==0:
                true_edge_list.append(idx_list[i])
        g.add_edges_from(true_edge_list)

        print('Clustering...')
        partition = community.best_partition(g)
        label_list = [0]*len(dataset)
        for key in partition:
            label_list[key] = partition[key]

        return label_list 
Example #3
Source File: louvain.py    From SnapTools with Apache License 2.0 6 votes vote down vote up
def louvain(
    edge_file,
    output_file,
    resolution
    ):
    
    ################################################################################################      
    if not os.path.exists(edge_file):
        print(('Error: ' + edge_file + ' does not exist!'));
        sys.exit(1);
	
    ################################################################################################      
    G = nx.Graph();
    with open(edge_file) as fin:
        for line in fin:
            elems = line.split();
            G.add_edge(elems[0],elems[1], weight=float(elems[2]))
    
    partition = community.best_partition(G, resolution = resolution)        
    
    with open(output_file, "w") as fout:
        for node in list(partition.keys()):
            fout.write(str(node) + " " + str(partition[node] + 1) + "\n") 
Example #4
Source File: util.py    From diffpool with MIT License 6 votes vote down vote up
def plot_graph(plt, G):
    plt.title('num of nodes: '+str(G.number_of_nodes()), fontsize = 4)
    parts = community.best_partition(G)
    values = [parts.get(node) for node in G.nodes()]
    colors = []
    for i in range(len(values)):
        if values[i] == 0:
            colors.append('red')
        if values[i] == 1:
            colors.append('green')
        if values[i] == 2:
            colors.append('blue')
        if values[i] == 3:
            colors.append('yellow')
        if values[i] == 4:
            colors.append('orange')
        if values[i] == 5:
            colors.append('pink')
        if values[i] == 6:
            colors.append('black')
    plt.axis("off")
    pos = nx.spring_layout(G)
    # pos = nx.spectral_layout(G)
    nx.draw_networkx(G, with_labels=True, node_size=4, width=0.3, font_size = 3, node_color=colors,pos=pos) 
Example #5
Source File: louvain.py    From SnapTools with Apache License 2.0 6 votes vote down vote up
def louvain(
    edge_file,
    output_file,
    resolution
    ):
    
    ################################################################################################      
    if not os.path.exists(edge_file):
        print(('Error: ' + edge_file + ' does not exist!'));
        sys.exit(1);
	
    ################################################################################################      
    G = nx.Graph();
    with open(edge_file) as fin:
        for line in fin:
            elems = line.split();
            G.add_edge(elems[0],elems[1], weight=float(elems[2]))
    
    partition = community.best_partition(G, resolution = resolution)        
    
    with open(output_file, "w") as fout:
        for node in list(partition.keys()):
            fout.write(str(node) + " " + str(partition[node] + 1) + "\n") 
Example #6
Source File: entity_discoverer.py    From HarvestText with MIT License 6 votes vote down vote up
def clustering(self, threshold):
        """分不同词性的聚类

        :return: partition: dict {word_id: cluster_id}
        """
        print("Louvain clustering")
        partition = {}
        part_offset = 0
        for etype, ners in self.type_entity_dict.items():
            sub_id_mapping = [self.word2id[ner0] for ner0 in ners if ner0 in self.word2id]
            if len(sub_id_mapping) == 0:
                continue
            emb_mat_sub = self.emb_mat[sub_id_mapping, :]
            cos_sims = cosine_similarity(emb_mat_sub)
            cos_sims -= np.eye(len(emb_mat_sub))
            adj_mat = (cos_sims > threshold).astype(int)
            G = nx.from_numpy_array(adj_mat)
            partition_sub = community.best_partition(G)
            for sub_id, main_id in enumerate(sub_id_mapping):
                sub_part_id = partition_sub[sub_id]
                partition[main_id] = sub_part_id + part_offset
            part_offset += max(partition_sub.values()) + 1
        return partition 
Example #7
Source File: louvain.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def __get_partitions(self, resolution, weight="weight", **kwargs):
        """
        Protected method for computing the best
        partitions using python-louvain. This is
        a simple wrapper to avoid code repetition.

        Parameters
        ----------
        weight :
            Column to use that represents edge
            weights.

        resolution :
            Resolution of Louvain graph. Passed down
            to community.best_partition().

        **kwargs :
            Eventually passed to community.best_partition().

        Returns
        -------
        A pandas dataframe with the community
        members (subscribers) and their respective
        community identifier.

        """
        self.partition = community.best_partition(
            self.graph_louvain, weight=weight, resolution=resolution, **kwargs
        )

        return pd.DataFrame(
            list(self.partition.items()), columns=["subscriber", "community"]
        ) 
Example #8
Source File: edmot.py    From EdMot with GNU General Public License v3.0 5 votes vote down vote up
def fit(self):
        """
        Clustering the target graph.
        """
        self._calculate_motifs()
        self._extract_components()
        self._fill_blocks()
        partition = community.best_partition(self.graph)
        return partition 
Example #9
Source File: NodePerception.py    From cdlib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __GetModComms(self, G):
        dict_H = community.best_partition(G)
        H1 = {}
        for e in dict_H:
            if dict_H[e] not in H1:
                H1[dict_H[e]] = set([])
            H1[dict_H[e]].add(e)
        H = []
        for e in H1:
            H.append(H1[e])
        return H 
Example #10
Source File: networkclustering.py    From PyPSA with GNU General Public License v3.0 5 votes vote down vote up
def busmap_by_louvain(network):
        lines = network.lines.loc[:,['bus0', 'bus1']].assign(weight=network.lines.num_parallel).set_index(['bus0','bus1'])
        lines.weight+=0.1
        G = nx.Graph()
        G.add_nodes_from(network.buses.index)
        G.add_edges_from((u,v,dict(weight=w)) for (u,v),w in lines.itertuples())
        b=community.best_partition(G)
        list_cluster=[]
        for i in b:
            list_cluster.append(str(b[i]))
        return pd.Series(list_cluster,index=network.buses.index) 
Example #11
Source File: louvain.py    From teneto with GNU General Public License v3.0 5 votes vote down vote up
def _run_louvain(nxsupra, resolution, N, T):
    comtmp = np.zeros([N*T])
    com = community.best_partition(
        nxsupra, resolution=resolution, random_state=None)
    comtmp[np.array(list(com.keys()), dtype=int)] = list(com.values())
    return comtmp 
Example #12
Source File: ego_splitting.py    From Splitter with GNU General Public License v3.0 5 votes vote down vote up
def _create_partitions(self):
        """
        Creating a non-overlapping clustering of nodes in the persona graph.
        """
        print("Clustering the persona graph.")
        self.partitions = community.best_partition(self.persona_graph, resolution=self.resolution)
        self.overlapping_partitions = {node: [] for node in self.graph.nodes()}
        for node, membership in self.partitions.items():
            self.overlapping_partitions[self.personality_map[node]].append(membership) 
Example #13
Source File: ego_splitter.py    From karateclub with GNU General Public License v3.0 5 votes vote down vote up
def _create_partitions(self):
        """
        Creating a non-overlapping clustering of nodes in the persona graph.
        """
        self.partitions = community.best_partition(self.persona_graph, resolution=self.resolution)
        self.overlapping_partitions = {node: [] for node in self.graph.nodes()}
        for node, membership in self.partitions.items():
            self.overlapping_partitions[self.personality_map[node]].append(membership) 
Example #14
Source File: edmot.py    From karateclub with GNU General Public License v3.0 5 votes vote down vote up
def fit(self, graph):
        """
        Fitting an Edge Motif clustering model.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph to be clustered.
        """
        self._set_seed()
        self._check_graph(graph)
        self._graph = graph
        self._calculate_motifs()
        self._extract_components()
        self._fill_blocks()
        self._partition = community.best_partition(self._graph, random_state=self.seed) 
Example #15
Source File: ego_splitter.py    From EgoSplitting with GNU General Public License v3.0 5 votes vote down vote up
def _create_partitions(self):
        """
        Creating a non-overlapping clustering of nodes in the persona graph.
        """
        print("Clustering the persona graph.")
        self.partitions = community.best_partition(self.persona_graph, resolution=self.resolution)
        self.overlapping_partitions = {node: [] for node in self.graph.nodes()}
        for node, membership in self.partitions.items():
            self.overlapping_partitions[self.personality_map[node]].append(membership) 
Example #16
Source File: modularity.py    From Verum with Apache License 2.0 5 votes vote down vote up
def score(self, sg, *args, **xargs):  # get_modularity_cluster
        """

        :param sg: subgraph
        :return: A dictionary of the modularity scores of the nodes in the subgraph
        """
        # args/xargs collected so that passing a topic doesn't mess things up

        # Convert to diGraph
        if sg.is_multigraph():
            sg = self.multigraph_to_digraph(sg)
        # Convert to undirected
        sg = sg.to_undirected()

        return community.best_partition(sg) 
Example #17
Source File: app.py    From dataiku-contrib with Apache License 2.0 4 votes vote down vote up
def draw_graph():
    #get data
    project_key = dataiku.default_project_key()
    
    similarity = float(request.args.get('similarity'))
    node_source = request.args.get('node_source')
    node_target = request.args.get('node_target')
    interactions = request.args.get('interactions')
    dataset = request.args.get('dataset')
    name=project_key+'.'+dataset
    
    print name
   
    df=dataiku.Dataset(name).get_dataframe()
    
    df=df[df[interactions]>similarity]
    df=df[[node_source,node_target,interactions]]
    df.columns=['source','target','weight']
    
    print "%d rows" % df.shape[0]
    G=nx.Graph()
    G.add_edges_from(zip(df.source,df.target))

    print nx.info(G)

    # degree
    for node, val in dict(nx.degree(G)).iteritems(): 
        G.node[node]['degree'] = val
    # pagerank
    for node, val in dict(nx.pagerank(G)).iteritems(): 
        G.node[node]['pagerank'] = val
    # connected components
    components =  sorted(nx.connected_components(G), key = len, reverse=True)
    for component,nodes in enumerate(components):
        for node in nodes:
            G.node[node]['cc'] = component
    # community
    partition = best_partition(G)
    for node, cluster in dict(partition).iteritems():
        G.node[node]['community'] = cluster
    
    # convert to JSON
    data = json_graph.node_link_data(G)
    
    #fix for networkx>=2.0 change of API
    if nx.__version__ > 2:
        dict_name_id = {data["nodes"][i]["id"] : i for i in xrange(len(data["nodes"]))}
        for link in data["links"]:
            link["source"] = dict_name_id[link["source"]]
            link["target"] = dict_name_id[link["target"]]
        
    return json.dumps({"status" : "ok", "graph": data}) 
Example #18
Source File: networkx.py    From scikit-multilearn with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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')]) 
Example #19
Source File: clusters.py    From RSN with MIT License 4 votes vote down vote up
def Louvain(dataset, edge_measure, datatype=np.int32):

    print('initializing the graph...')
    g = nx.Graph()
    g.add_nodes_from(np.arange(len(dataset)).tolist())
    print('preparing idx_list...')
    idx_list = []
    for i in range(len(dataset)):
        for j in range(len(dataset)):
            if j == i:
                break
            idx_list.append((i,j))

    print('calculating edges...')
    batch_count = 0
    batch_size = 10000
    left_data = np.zeros(list((batch_size,)+ dataset[0].shape), dtype=datatype)
    right_data = np.zeros(list((batch_size,)+ dataset[0].shape), dtype=datatype)
    edge_list = []
    for count,idx_pair in enumerate(idx_list):
        left_data[batch_count]=dataset[idx_pair[0]]
        right_data[batch_count]=dataset[idx_pair[1]]
        batch_count += 1
        if batch_count == batch_size:
            print('predicting...',str(round(count/len(idx_list)*100,2))+'%')
            temp_edge_list = edge_measure(left_data,right_data)
            edge_list = edge_list + temp_edge_list.reshape(batch_size).tolist()
            batch_count = 0
    if batch_count !=0:
        print('predicting...')
        temp_edge_list = edge_measure(left_data[:batch_count],right_data[:batch_count])
        edge_list = edge_list + temp_edge_list.reshape(batch_count).tolist()
    edge_list = np.int32(np.round(edge_list))

    print('adding edges...')
    true_edge_list = []
    for i in range(len(idx_list)):
        if edge_list[i]==0:
            true_edge_list.append(idx_list[i])
    g.add_edges_from(true_edge_list)

    print('Clustering...')
    partition = community.best_partition(g)

    # decode to get label_list
    print('decoding to get label_list...')
    label_list = [0]*len(dataset)
    for key in partition:
        label_list[key] = partition[key]

    return label_list, create_msg(label_list) 
Example #20
Source File: crisp_partition.py    From cdlib with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def louvain(g_original, weight='weight', resolution=1., randomize=False):
    """
    Louvain  maximizes a modularity score for each community.
    The algorithm optimises the modularity in two elementary phases:
    (1) local moving of nodes;
    (2) aggregation of the network.
    In the local moving phase, individual nodes are moved to the community that yields the largest increase in the quality function.
    In the aggregation phase, an aggregate network is created based on the partition obtained in the local moving phase.
    Each community in this partition becomes a node in the aggregate network. The two phases are repeated until the quality function cannot be increased further.

    :param g_original: a networkx/igraph object
    :param weight: str, optional the key in graph to use as weight. Default to 'weight'
    :param resolution: double, optional  Will change the size of the communities, default to 1.
    :param randomize:  boolean, optional  Will randomize the node evaluation order and the community evaluation  order to get different partitions at each call, default False
    :return: NodeClustering object


    :Example:

    >>> from cdlib import algorithms
    >>> import networkx as nx
    >>> G = nx.karate_club_graph()
    >>> coms = algorithms.louvain(G, weight='weight', resolution=1., randomize=False)

    :References:

    Blondel, Vincent D., et al. `Fast unfolding of communities in large networks. <https://iopscience.iop.org/article/10.1088/1742-5468/2008/10/P10008/meta/>`_ Journal of statistical mechanics: theory and experiment 2008.10 (2008): P10008.

    .. note:: Reference implementation: https://github.com/taynaud/python-louvain
    """

    g = convert_graph_formats(g_original, nx.Graph)

    coms = louvain_modularity.best_partition(g, weight=weight, resolution=resolution, randomize=randomize)

    # Reshaping the results
    coms_to_node = defaultdict(list)
    for n, c in coms.items():
        coms_to_node[c].append(n)

    coms_louvain = [list(c) for c in coms_to_node.values()]
    return NodeClustering(coms_louvain, g_original, "Louvain", method_parameters={"weight": weight, "resolution": resolution,
                                                                         "randomize": randomize}) 
Example #21
Source File: NodePerception.py    From cdlib with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def __FirstPartition(self, edges, first_part_file):
        OUT = open(first_part_file, 'w')
        node_count = 0
        for n in edges:
            node_count = node_count + 1
            if node_count > 0:
                # the nodes
                index = {}
                reverse_index = {}
                count = 0
                to_add_edges = []
                adj = set([])
                for neighbor in edges[n]:
                    index[count] = neighbor
                    reverse_index[neighbor] = count
                    adj.add(neighbor)
                    count = count + 1
                for m in reverse_index:
                    for k in edges[m]:
                        if k in reverse_index and reverse_index[k] < reverse_index[m]:
                            to_add_edges.append((reverse_index[m], reverse_index[k]))
                G = nx.Graph()
                G.add_nodes_from([i for i in index])
                G.add_edges_from(to_add_edges)
                if len(to_add_edges) > 0:
                    dict_H = community.best_partition(G)
                    H = {}
                    for node in dict_H:
                        if dict_H[node] not in H:
                            H[dict_H[node]] = set([])
                        H[dict_H[node]].add(node)

                    for i in H:
                        comm = H[i]
                        if len(comm) > 0:
                            for c in comm:
                                OUT.write(str(index[int(c)]) + ' ')
                            OUT.write(str(n))
                            OUT.write('\n')
                        elif len(comm) > 0:
                            for c in comm:
                                if index[int(c)] in edges[n]:
                                    OUT.write(str(index[int(c)]) + ' ')
                            OUT.write(str(n))
                            OUT.write('\n')
        OUT.close()