Repository navigation
Implement Constant-Potts Model (for use with Leiden community detection) - #8507
Conversation
|
The circle CI tests were failing because this PR is started from an old version of the Should be just (while at the |
|
Thanks |
dschult
left a comment
There was a problem hiding this comment.
This looks good! And its a size I can bite for review. :)
I have a few nit-picky comments -- most are suggestions to improve readability, which is a judgement call anyway. So think of them as suggestions and make changes or not accordingly. (the dict form of attribute assignment could be used in other places than I suggested)
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
|
Thanks for the comments and feedback. Still welcome further comments on the doc string for constant_potts_model. |
rossbar
left a comment
There was a problem hiding this comment.
I have a high-level question: do you envision constant_potts_model to be used generally, or is it intended mostly for internal use w.r.t louvain/leiden?
The answer can help inform how best to integrate the functionality. For example, if this is really only intended for use within leiden and/or louvain, then it might make more sense to make this a private function (i.e prefixed with _) and perhaps simplify the API (i.e. get rid of the is_partition checks and/or the allow_partial).
If the goal is general use, then a follow-up questions becomes: how does this differ from partition_quality? And... should this be an independent function, or should it be an option to existing functions (e.g partition_quality(..., method="constant_potts"))?
|
In my most recent commit in PR #8509 I have changed the way the quality deltas are computed within leiden, which changes the role that The idea is that for common quality functions like CPM or modularity, the pair of add/remove delta functions can be more highly optimised (i.e. using a more efficient formula, and without any The The pre-defined and optimised delta functions are likely only ever to be used within leiden/louvain and would make sense to have as private functions, but to me it makes sense to expose Regarding your questions on |
|
Just FYI, I think it is worth thinking about removing the The advantage of removing all such checks is that we don't need to introduce any I guess the key question here is "what happens if someone provides a communities input that has overlapping communities. That is, are quality measures dependent on the communities being disjoint? If so, let's change the @dk-gh do you know what happens to say modularity or cpm if the communities are overlapping sets? |
|
I only included the My understanding is that, by definition, a partition must consist of disjoint communities. It looks like From what I can see all/most partition quality functions If |
…algorithm when computing cpm deltas as nodes are moved between communities.
|
I have added two new delta functions The leiden algorithm can be made to invoke these optimised add/remove cost functions -- this can be seen in #8509. If leiden receives a Fundamentally, leiden no longer needs the From some basic profiling, the majority of execution time for the leiden algorithm is spent in |
|
Niiiice!!! It seems to me that the delta for adding a node to the community should just be the negative of the delta for removing it from the community. So maybe we only need one function. Can you check that? Also, does Leiden (or Louvain) actually need the quality function itself? I guess it is called to look for improvement between levels. I'd prefer to the API to avoid prescribing both main function and the efficient delta function. Thoughts? |
|
You're right that the cost of removing Another reason I'd like to keep them separate is that the cost of adding nodes the most critical aspect for performance, so I would want to be able to optimise the add and remove functions separately (for example, there might be a way to pre-compute values for even faster add cost functions defined at each iteration of The leiden algorithm will only use the This will be updated so that Using the |
|
Interesting perspective. I'm still wrapping my head around how an add_cost function could be optimized in a way that remove_cost would not. Hope that works. I'm looking forward to your other ideas to improve this. I was envisioning that we make a function for The problem with looking up the add_cost function based on the quality function is that we can only use our known add_cost functions. The user can't specify an add_cost function. Only the quality function. Is there a way users could plug into the
Still looking for best API... |
|
@dschult I agree "cost" is not necessarily the best term for this -- I also used "contribution" thinking in terms of the overall contribution to the In terms of the API, I would include the pair of "cost" functions (or whatever terminology we settle on) as kwargs, with default being None, in which case the |
…es to tests to reflect this. Also removed the allow_partial parameter from cpm, as it will no longer be required for leiden
…nclude self-loops in tests to ensure cancellations of terms in partial evaluations are correct
dschult
left a comment
There was a problem hiding this comment.
I'm still not convinced we should have separate functions for add and remove. But let's figure that out when we see how the Leiden algorithm interfaces with this. We can change it in that PR if needed.
I think these are my final comments on the CPM function PR.... but no promises. :} Anyway, we're close...
We need to turn on the documentation for constant_potts_model. Add it to the file doc/reference/algorithms/community.rst in the section for measuring partitions. You can check if it looks good after the CI is completed using the "view details" link for the test named "documentation artifact".
Why are we constructing the set community - {node} twice inside the remove function? Maybe just call the add function with community- {node} and negate the answer? At least store the new set and use it twice instead of constructing it twice. :)
I agree with this approach. I feel like you probably are right about reusing the add function for remove, but these functions only exist to support leiden, so makes sense to think about these as part of that PR.
I have added this.
Fixed the duplicate construction within the remove function. |
dschult
left a comment
There was a problem hiding this comment.
The quote in the doc_string is being formatted strangely with the first line bigger and Boldface. I think the quotation mark is not aligned with the rest of the paragraph, so the text->html machinery (Sphinx) is interpreting is differently.
Since the indentation makes it a "quotation" you can probably leave off the double quotes around it. At least in a textbook that's how they would do it. If you prefer to leave the double quotes that is fine, but add a space before the 1st one so it treats the whole thing as one quotation.
I approve this PR
Thanks, I've removed the quote marks from the indented quotation -- hopefully this looks OK now |
Add _bipartite_modularity_delta_partial_eval_{remove,add}, the
bipartite modularity counterpart to the CPM delta helpers introduced
in networkx#8507. They compute the change in Barber's Q_B for moving a single
node between communities without recomputing Q_B over the whole
partition. The invariant
q_rem + q_add == Q_B(P') - Q_B(P)
is validated against the full quality function over unweighted,
weighted, and random-graph moves. The composition property
q_rem == -add(u, A\{u}) is also tested, confirming compatibility
with the single quality_delta pattern used in networkx#8509.
* ENH: Add Barber's bipartite modularity quality function
Add `bipartite.modularity` implementing Barber's Q_B (2007) as a
quality function for bipartite community partitions. The
implementation uses the community-sum form
Q_B = sum_c [ L_c/m - gamma * k_c * d_c / m^2 ]
where k_c and d_c are the degree sums of the red and blue nodes in
community c. This mirrors the per-community form used by
`nx.community.modularity` but with a bipartite null model: the
product of two distinct degree sums rather than the square of a
single one.
Suggested by @dk-gh in PR review.
* ENH: Add partial-evaluation delta helpers for bipartite modularity
Add _bipartite_modularity_delta_partial_eval_{remove,add}, the
bipartite modularity counterpart to the CPM delta helpers introduced
in #8507. They compute the change in Barber's Q_B for moving a single
node between communities without recomputing Q_B over the whole
partition. The invariant
q_rem + q_add == Q_B(P') - Q_B(P)
is validated against the full quality function over unweighted,
weighted, and random-graph moves. The composition property
q_rem == -add(u, A\{u}) is also tested, confirming compatibility
with the single quality_delta pattern used in #8509.
* ENH: Add bipartite modularity merge-delta helper
Add a private helper `_bipartite_modularity_merge_delta` that computes
Q_B(P') - Q_B(P) when two disjoint communities A and B are merged:
merge_delta(A, B) = E(A, B) / m
- gamma * (k_A d_B + k_B d_A) / m**2
where k_C and d_C are sums of per-node `red_degree` and `blue_degree`
attributes over the nodes of C, and E(A, B) is the cross-community
edge weight.
The helper reads red/blue degree contributions from node attributes
rather than inferring them from bipartite-set membership. On an
original bipartite graph each node carries its degree in one attribute
and zero in the other; on an aggregated super-node graph (as built by
multilevel optimizers such as Louvain/Leiden) both attributes can be
nonzero and represent the summed contributions of the super-node's
constituents.
Self-loops on aggregated super-nodes are possible: they encode
intra-community edges and are correctly excluded from E(A, B) by the
`v in community_b` filter. `m` is passed in as a graph-level invariant
preserved across aggregation.
* Speed up bipartite modularity merge-delta inner loop
Compute the red/blue degree sums for A and B by iterating over the
community members and looking up node attributes directly, rather than
scanning all of G.nodes and filtering with `v in community`. The
complexity of the degree-sum step drops from O(|V|) to O(|A| + |B|).
This matters because _bipartite_modularity_merge_delta is designed to
run in the inner loop of multilevel optimizers (Louvain/Leiden), where
candidate communities are typically much smaller than the graph and
the helper is invoked many times per node visit.
* DOC: Add new module/fn to ref guide.
---------
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: evk <[email protected]>
This PR is a small component related to the larger PR gh-8505 which is being split into at least three smaller PRs.
This PR implements the Constant-Potts Model quality function for partitions
constant_potts_model. This is the quality function typically used by default in the leiden community detection algorithm. This has been implemented along with the leiden algorithm, details can be found in #8505, and this will be submitted as a separate PR.