Repository navigation
Add Leiden as a backend-only algorithm - #7743
Conversation
|
I merged #5964 PR to get the docstrings, which added @juanis2112 as an author. I also updated #7690 to automatically add an "Attention" admonition at the beginning of the extended summary. Here's what it currently looks like: |
|
The API for Leiden is very similar to Louvain, but I did remove Louvain uses a greedy optimization method, so using a threshold can make sense here to stop considering updates when they only marginally improve modularity. Leiden, on the other hand, includes an additional refinement step that help ensure the communities are well-connected and meaningful. Hence, I think refinement in Leiden makes the threshold parameter unnecessary. Both have the So, I think this is ready to go in pending review of the docstring and general approval of adding a backend-only algorithm (which there seems to be). |
rlratzel
left a comment
There was a problem hiding this comment.
LGTM. We've discussed this PR quite a bit in a number of NX community and dispatch meetings and it didn't seem like there are any outstanding concerns. @MridulS @dschult @rossbar any thoughts?
Once this is merged, we'll merge rapidsai/nx-cugraph#50 and users will have Leiden in NetworkX via networkx and nx-cugraph nightlies.
|
How does Leiden know when to stop? The resolution parameter doesn't have anything to do with stopping or iteration. It changes the function you are optimizing. Isn't Leiden an iterative optimization solver? |
|
Just to confirm the pop up only comes in after #7690 right? |
dschult
left a comment
There was a problem hiding this comment.
These looks good.
Some comments (to check that I understand):
- We can't check that these tests pass in our CI, right?
- How should we handle "See Also" to non-NX functions in NX functions (See comment below). My initial thought is that we should not do that... but maybe with a note would be OK?
- How do we handle the docs lists of functions, e.g. for the communities page? This PR gives a whole section on Leiden Communjity Detection and we don't include that -- but that isn't indicated outside of the function docs themselves. I'm guessing we should include a note near each place that Leiden is listed. Perhaps
leiden_communities (not NetworkX code)
leiden_partitions (not NetworkX code)
- Does cugraph have docs for Leiden? These looks a lot like our Louvain docs... Maybe the docs for cugraph have some helpful info too.
| See Also | ||
| -------- | ||
| louvain_partitions | ||
| :any:`leiden_communities` |
There was a problem hiding this comment.
I'm not sure we should "See Also" to functions that aren't implemented by NX.
This could get confusing.
There was a problem hiding this comment.
I don't have a general recommendation other than we should be thoughtful when doing this.
The sentiment I have heard about Leiden over the last couple of years is that when people reach for Louvain, they would probably get better results if they used Leiden, so they should at least consider using Leiden. Although the "See Also" doesn't give such a strong suggestion, it does provide a clear hint, which is what "See Also" is for IMHO. I think it makes sense for Leiden to "See Also" to Louvain in case they don't have a backend installed that implements Leiden.
| # backends that implement Leiden. | ||
| backend_has_leiden_communities = ( | ||
| "not nx.config.backend_priority.algos " | ||
| "or nx.config.backend_priority.algos[0] not in leiden_communities.backends" |
There was a problem hiding this comment.
IIUC, this checks that the first backend_priority.algos backend is not in the function's backends.
Is there a reason for doing that instead of checking all the backend_priority.algos values?
There was a problem hiding this comment.
Great question. I had done this assuming tests were being run as e.g.
NETWORKX_TEST_BACKEND=<mybackend> pytest ...
# or
pytest --backend <mybackend> ...which sets the config here:
Lines 84 to 85 in 6d37cc9
and is used here:
networkx/networkx/utils/backends.py
Lines 1039 to 1043 in 6d37cc9
Anyway, I updated the tests to check whether any backend in the config implement Leiden, which shouldn't hurt anything and may help non-standard (but perhaps valid) way to run tests. I also fixed the variable names, which were logically backwards!
Thanks for the close scrutiny 👍
There was a problem hiding this comment.
Thanks for the pointers to the backend testing interface. (and catching the names which I did not catch :)
Co-authored-by: Dan Schult <[email protected]>
Also, check if any backends implement Leiden
Thanks for looking and being thoughtful!
Correct. The tests are skipped.
I think we can handle each case as they come up. My understanding is that most people who use Louvain would be better served by Leiden, so I think there's a strong case to make that linking Louvain/Leiden with "See Also" makes sense. What kind of note are you thinking?
I think I understand. We are adding Leiden to this page: How about adding a note to the module docstring that gets included in the docs, such as These function do not have default NetworkX implementations.
They may only be run with an installable :doc:`backend </backends>`
that support them.
Here they are. I'll take a closer look to see if any parameters can be better explained. |
MridulS
left a comment
There was a problem hiding this comment.
The additional backends admonition should pop up once rapidsai/nx-cugraph#50 is merged right?
Co-authored-by: Mridul Seth <[email protected]>
Presumably ;) (I have tested this locally). Should we go ahead and merge rapidsai/nx-cugraph#50 so we can test that part of the generated admonition? It's ready, but we were holding off in case the API for leiden changed, but it seems like the current API is probably good for now, right? |
Yes, I think it's good to go! |
|
FYI we just merged rapidsai/nx-cugraph#50 |
Done! See for yourself: |
dschult
left a comment
There was a problem hiding this comment.
I went through the docs one more time. I think similar suggestions will need to be done with Louvain, so we can instead do them both at once along with other community detection changes. I think this is ready!
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]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
|
Excellent improvements to the docs @dschult! I carefully reviewed and accepted all suggestions 👍 |


Note that a WIP implementation is being done in #5964. It may be nice to include documentation and authorship from there.
If we want this as a backend-only function for now, I think we should consider adding #7690.
There are different "flavors" of Leiden floating around; some have a
betaparamter, some athetaparamter, others indicate the minimum number in a community, other set the maximum number in a community, some implentations have none of these, and so on. I think a deeper discussion regarding prior research and implementations should be done in a separate issue. This PR takes a minimalist approach: if backends have extra parameters, they can add them as backend-only argument. How convenient! We may want to revisit #7297 if we expect backends to have different additional arguments for Leiden.The tests are relatively basic, but I figure something is better than nothing and may aid backend implementers.
I think we should figure out how to document backend-only functions. Perhaps an admonishment can be automatically handled as part of #7690.
We have begin to add Leiden to nx-cugraph: rapidsai/nx-cugraph#50