vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
596 stars 78 forks source link

CPMVertexPartition,How does this work? #80

Closed 18438602970 closed 3 years ago

18438602970 commented 3 years ago

When I call CPMVertexPartition, the community returned is 0,1,2,3,4,5... Number of nodes. But all other methods can output partition results normally. Is the function invalid, or am I using it incorrectly? My calling code is as follows:

    def leiden_CPM_community_discover(self):   
        clusters = la.find_partition(self.g, la.CPMVertexPartition)
        # print(clusters)
        nodes = [{"name": node["label"]} for node in self.g.vs]
        # print(nodes)
        community = {}
        for node in nodes:
            idx = self.g.vs.find(label=node['name']).index 
            node["community"] = clusters.membership[idx]
            if node["community"] not in community:
                community[node["community"]] = [node["name"]]
            else:
                community[node["community"]].append(node["name"])
        return community
vtraag commented 3 years ago

When you do not provide a resolution parameter when using a CPMVertexPartition, it will use the default of 1. For unweighted graphs, the optimal partition will be the singleton partition in that case. Hence, please provide a resolution parameter, e.g. as la.find_partition(self.g, la.CPMVertexPartition, resolution_parameter=0.1). For unweighted graphs, the resolution parameters of interest are somewhere between 0 and 1. Please see the documentation for more details.

Regarding the rest of your code, please note that clusters itself already provides the results as a list of vertices. Just do list(clusters) to retrieve it, or use for c in clusters: to iterate through them. See the documentation of igraph for more details.