Repository navigation
Conversation
|
Thanks for this -- there's a lot here and we may need to split it up to get it reviewed effectively. But first and foremost, thanks for this work. :) My work with modularity and Louvain specifically shows that it is pretty crucial to compute the change in modularity instead of recomputing the full modularity and taking the difference. Publications hint at this need too. Removing this change (and the The rest is still quite large and I would suggest breaking it up to make review possible and timely. Some ideas along these lines:
Those two PRs can be submitted at the same time as long as you make them independent of each other. The next PR would presumably combine these two features with tests using Leiden and CPM. Another PR would involve rewriting Louvain -- but I'm not sure it will be worth doing. Maybe you can write some benchmarks of Louvain as a separate PR so it will be easy to test the timing across commits. I believe there already is one for Louvain in I think the best things to spend time on first are the bullets above: get CI "passing" (with xfail), put CPM into its own PR, get Leiden as a separate PR. Push back if this doesn't seem like the right path. Ask questions if that will help. We can keep this PR as a conversation point to help organize the discussion. And it can point to other PRs that are better "bite-size" for the reviewers. Thanks! |
|
Thanks for the feedback, I appreciate it's a large PR and I agree it makes sense to split. I've been working on this incrementally, and found myself making changes back and forth between louvain.py, quality.py and leiden.py, and the order the code is written is not necessarily the logical order to submit as PRs -- I appreciate your thoughts on how to break it up into manageable pieces. I think I agree that this naturally fits into three separate PRs (if refactoring louvain is to be included). PR1 - Refactor Louvain (optional)This PR would refactor This PR will change the way Regarding the question as to whether refactoring louvain is worth it, I can see three benefits of this refactoring, and one potential drawback: Benefits
Drawbacks
I will look at benchmarking, and maybe we can revisit the discussion on this later. I think in principle there are meaningful benefits. Am I missing anything else in terms of drawbacks? PR2 - Implement CPMThis simple PR will introduce PR3 - Implement LeidenThis PR will introduce the full implementation of I think I will need to do PR2 then PR3 sequentially, as leiden requires ###Next Steps I will go ahead with PR2 and PR3, and revisit PR1 once I have had a chance to look at performance benchmarking. I think this is in line with your comments above -- am I missing anything here? Other CommentsProperties of a "Quality Function" When thinking about abstracting to a more general quality function, the key property for the quality function This is a kind of "localness" condition; the changes of moving a node from one community to another can be determined locally (i.e. just in terms of the source and target communities), rather than relying on a "global" property of the graph and/or partition. The functions Converting MultiGraph to Graph This helper function was carried over from |
|
Can you add the Can't the leiden function use the modularity function instead of the CPM function? That would allow the CPM PR to be independent of the Leiden function.... (practically it may not matter because I suspect CPM will be easier to review than the Leiden anyway so it would be merged first.) Can we have the Louvin and Leiden functions accept a delta-function instead? So instead of computing the full modulatity, they compute the change in the modularity on each step. We'd have to derive a formula for the change in CPM on one step, but I think that would be straightfoward. But I haven't tried it. I haven't worked through what the :) |
|
Can you add the xfail pytest mark to the failing test of this PR and resubmit? That will allow us to see if there are any failures on the jobs that got canceled due to previous CI jobs failing. I have added the xfail decorator for the failing test now. Can't the leiden function use the modularity function instead of the CPM function? That would allow the CPM PR to be independent of the Leiden function.... (practically it may not matter because I suspect CPM will be easier to review than the Leiden anyway so it would be merged first.) Not without changes to
The only thing Can we have the Louvin and Leiden functions accept a delta-function instead? So instead of computing the full modulatity, they compute the change in the modularity on each step. We'd have to derive a formula for the change in CPM on one step, but I think that would be straightfoward. But I haven't tried it. Do you mean a function for the delta at the point of moving each node? This is basically how each Or do you mean tracking the incremental changes all the way through each iteration of aggregating nodes? I.e. to test whether the change meets the threshold to determine whether to continue or return the current partition. I haven't worked through what the allow_partial feature actually does. I get that it bypasses the check that the partition is actually a partition. But does it do anything else? I'm looking for a way to bypass calling the quality measure function. (of course benchmarking should back that up -- so I'm basing this on my ten years ago crude benchmarking that showed quite large differences. And that make sense because we are only swapping nodes from one community to another -- as you point out.) The |
|
Thanks for that -- the xfail makes it easier to see how these changes have affected the other tests. And the discussion of function input to both Louvain and Leiden is helpful. What I'm envisioning is a function parameter that is used to compute I'd rather not have any constraints on how that change is computed inside the main functions. Let's move as much as possible to these helper fuctions that we can then provide, or a user can write themselves for new cases. Thoughts? I do wonder at what point these functions are no longer Louvain or Leiden, but some new generalization of them. I'll take a look at the papers again... |
|
Thanks, It will be easy to modify my implementation of leiden and louvain to use something like the
with something like
in a few places. For any given quality function, the delta function could either be based on a highly optimised formula, or for functions where this hasn't been developed yet, it could just be a wrapper that still applies the I think looking into this further will fit naturally with benchmarking, the new louvain implementation (which uses exactly the same In the meantime I'll get the cpm and leiden PRs submitted. I do appreciate the comments and feedback. |
I have implemented the leiden community detection algorithm within networkx. The original Leiden paper [1] gives pseudocode for the louvain and leiden algorithms side-by-side. My approach was to use the side-by-side pseudocode comparison as a guide to incrementally adapt the networkx implementation of louvain. This required changes in three broad areas:
No AI/LLM tools have been used at any stage -- the main reason I undertook this project was to better understand the louvain and leiden algorithms.
Changes to Louvain:
In order to make these changes possible I started with refactoring parts of louvain. In order to implement i) I changed louvain to accept quality_function as parameter with modularity being the default. This allows the louvain algorithm to be run using quality functions other than modularity (e.g. CPM). In order to do this I also had to refactor modularity.
One of the challenges when abstracting modularity to more general quality function was that louvain implementation had parts of modularity hard-coded into _one_level(...). This was to calculate the modularity deltas from moving nodes between communities.
Quality Functions:
A quality function Q accepts a graph G and a partition P=[A, B, C, D], and returns a numerical value Q(G, P). However, quality functions like modularity and CPM are the sum of values calculated for each community within the partition, which means that computing the effect of moving a node to another can be calculated without computing Q(G, P) in full.
Let P = [A, B, C, D] be a partition, and suppose we move node u from community A to community B. This gives a new partition P' = [A', B', C, D]. To compute the change in quality from Q(G, P) to Q(G, P'), we only need to calculate the contribution of the terms A and B compared against that of A' and B' because of the property
Q(G, P) - Q(G, P') = Q(G, [A, B]) - Q(G, [A', B'])
However, the problem with the existing implementation of modularity is that we cannot run modularity(G, [A, B]) because the list of communities does not form a partition of G -- this would raise a NotAPartition exception. Therefore I included another parameter in modularity: allow_partial (set to False by default). If allow_partial=True then the quality function will compute the contribution of the lists of communities, even if these do not form a full partition. This keeps existing behaviour by default of modularity (original tests for louvain and modularity still passing).
I have implemented the quality function constant_potts_model(...) following [2], which has the same API as the refactored modularity(...), including the allow_partial parameter, and constant_potts_model can be used interchangeably with modularity in both louvain and leiden. Note that I used the formulation of CPM from [2] as it slightly more general than that described in [1]. The formula in [1] requires positive integer node sizes (node_weight). Following [2] the implementation allows for node_weight to be a float (set to 1.0 by default). The node_weight of the aggregated community is the sum of the node_weights of its members.
I haven't benchmarked performance for the new implementation of louvain against the existing version.
Adapting Louvain to Leiden:
The Leiden implementation is then an adaptation of the refactored louvain code. Like the updated louvain code, leiden can accept quality_function as a parameter, but uses constant_potts_model by default.
I have created tests for CPM, contant_potts_model(...) I have created a number of tests for Leiden by copying the Louvain tests. I have also included tests that resulting communities are connected.
Further tests for Leiden are probably useful. Because the algorithm is non-deterministic, it is difficult to test on known inputs and outputs. The randomness in leiden comes from two parts of the algorithm:
Both of these are controlled by the same seed, resulting in deterministic behaviour when using a set seed value.
From a quick test against the outputs of leidenalg, the community structures seem similar at the same resolution and theta values, however, more thorough testing may be required to ensure the correctness of this implementation. Overall the results seem to be less stable than leidenalg (greater variance in structure of resulting communities). I don't know why this is, and understanding the difference may come down to looking at implementation details of leidenalg (my initial guess would be it has something to do with the randomness in the order that nodes are visited, but this is not a well-informed view).
TODO:
[1] "From Louvain to Leiden: guaranteeing well-connected communities" Vincent Traag, Ludo Waltman, Nees Jan van Eck
https://arxiv.org/pdf/1810.08473
[2] "Narrow scope for resolution-limit-free community detection" V.A. Traag, P. Van Dooren, Y. Nesterov
https://arxiv.org/abs/1104.3083