Skip to content

Louvain Modularity Community Detection #951

Closed

Description

@SuperAce99

Overview

NetworkX only provides the K-Clique algorithm for community detection, despite past efforts to add more options such as Louvain Modularity. These efforts should be completed and integrated into the main branch.

Current Status

Code for louvain community detection has been submitted and partially tested in #239. This code appears to be maintained by @taynaud on bitbucket

@bjedwards proposed to add several community detection methods during Summer of Code 2011. The directed laplaian portion of this was eventually merged via #741, but it appears that this was the only portion of the proposal that was completed.

Proposal

Merge @taynaud's python-louvain implementation into Network X.

Activity

  1. ghost assigned on Sep 12, 2013
  2. hagberg commented on Sep 12, 2013

    @hagberg
    Member

    Yes, we could add that code to NetworkX. PR welcome.

  3. Midnighter commented on Sep 16, 2013

    @Midnighter

    I think what's needed to merge this and other similar projects in future, is a definitive decision on what the structure returned by such algorithms should look like. If I remember correctly the louvain code returns a dictionary, where keys are module indices and values are lists with the respective nodes. A list of sets with nodes belonging to one cluster also seems a sensible structure.
    I don't know if there any preferences out there but I strongly believe that a unified return structure should be implemented in order to make the detection algorithms interchangeable.

  4. SuperAce99 commented on Sep 16, 2013

    @SuperAce99
    Author

    I agree that a standard structure is needed. The existing interfaces are:

    • The K-cliques algorithm yields a frozenset of node IDs until all communities have been yielded.
    • @bjedwards implemented several algorithms that each return a list of sets of node IDs.
    • @taynaud wrote a stand-alone louvain implementation that returns a dict "with communities numbered from 0 to number of communities."

    K-clique is currently the only community algorithm and has been satisfactorily tested #640. There are currently no issues reporting problems with this interface, and using the existing interface would protect existing code. Let's not invent a new standard if we don't have to.

    Most community algorithms produce groupings that are not ordered or named, so list and dict datatypes add superfluous structure. @taynaud's work needs a dict to work with a dendogram internally, which is a more complex structure but does not need to be returned to the user in most cases. Thus a collection of of sets, as used in K-clique and @bjedwards code, seems sufficient.

    k-clique and @bjedwards differ mainly in their strictness (yield frozenset vs return list of sets). The k-implementation clique seems a bit excessive. The communities of a graph are very often used together (e.g. to color the nodes) so there is little to be gained by using a generator. Similarly, users may want to modify the returned communities, which is inconvenient when the algorithm returns an immutable frozenset.

    I suggest that we require that community detection algorithms must return an iterable object that yields objects that support set operations. Both k-clique and @bjedwards are correct under this definition, but @taynaud's would need to be modified.

  5. hagberg commented on Sep 17, 2013

    @hagberg
    Member

    Thanks @SuperAce99 for the clear summary. I agree with you on the data structure ( iterable object that yields objects that support set operations) and I think we should proceed that way.
    That handles the case of overlapping partitions easily too.

    There are likely some operations that are easier to perform with an inverted representation like a dict with labeled communities. In particular finding which group a node is in. But it is straightforward to convert to that structure if needed.

  6. bjedwards commented on Sep 17, 2013

    @bjedwards
    Member

    Just wanted to chime in. I am +1 for an iterable object that yields objects that support set operations. I would argue for set instead of frozenset.

    Most algorithms will create communities iteratively, meaning that most would require the construction of a set only to have it recast to a frozenset before yielding. Though I suppose we could leave those details up to whoever writes the algorithm.

    In some cases having hashable communities might be useful. If information about the communities is also provided by the algorithm, or if information is to be added later it would be useful to return a dict keyed by frozenset communities. But I think @hagberg 's suggestion to convert it to a dict of labeled communities (through a provided function or by the user) is straightforward.

  7. chebee7i commented on Sep 17, 2013

    @chebee7i
    Member

    Glad to see this discussion happening. @hagberg's point about overlapping partitions is spot on for me. I'd want the data structure returned by such an algorithm to be similar to the rest of the community detection algorithms.

    Also, if conversion to dict or frozensets was super important, NetworkX could provide some functions that receive the output and convert them appropriately. They wouldn't need to be imported into the top level module networkx, but we could make them available networkx.algorithms.community. This would save people from typing the same boilerplate conversion each time.

  8. bjedwards commented on Sep 17, 2013

    @bjedwards
    Member

    One other option @hagberg and I considered when I first started the community work was a new class Community. This could have some of the built in functionality like affiliation representations. I messed with the idea at first but we decided it was probably a little more than we needed. A rough implementation can be found here.

    It built heavily on the Graph class as it was at the time. I am not necessarily advocating for this, but it is an option...

  9. cindeem commented on Sep 17, 2013

    @cindeem

    I have recently started working on Brainx. We have implemented two community finding algorithms (Simulated Annealing, Newmans Spectral Partitioning from 2006 PNAS paper).

    This code has implemented a GraphPartition class, and returns the partition GraphPartition.index as a dict of sets, keys are just ints 0->number_of_partitions. I have been doing quite a bit of refactoring on this code recently, and one of the main issues is moving away from a dict, to a list of sets. This is primarily to benefit the algorithms, but this lack of structure is in some ways more intuitive. Having a frozenset as a final result is appealing, but agree it would be nice to have some nice functions for converting.

    I was planning on submitting a pull request after I finish (somehwhat substantial) refactoring of the code, but wanted to chime in on this discussion in case our experience is useful. I have been meaning to look at @taynaud python-louvain implementation, to gain inspiration for the GraphPartition class, and (thanks to this discussion) also plan to look at @haberg Community class; this discussion is timely.

    Just wanted to add my 2c

  10. hagberg commented on Sep 18, 2013

    @hagberg
    Member

    It seems we are all in agreement on how to proceed. So bring on the pull requests 👍

  11. SuperAce99 commented on Sep 20, 2013

    @SuperAce99
    Author

    Working on it! (and trying not to bump the close button...)

  12. stared commented on Jan 28, 2014

    @stared

    +1

  13. harrismirza commented on Mar 31, 2019

    @harrismirza

    Is there any update on this? Seems to be held up but I can't quite work out why.

  14. dschult commented on Mar 31, 2019

    @dschult
    Member

    Looks like it is waiting for code to be submitted as a Pull Request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions