Skip to content

Add Leiden as a backend-only algorithm - #7743

Merged
dschult merged 31 commits into
networkx:mainfrom
eriknw:leiden
Jan 28, 2025
Merged

dschult merged 31 commits into
networkx:mainfrom
eriknw:leiden

Conversation

@eriknw

@eriknw eriknw commented Nov 27, 2024

Copy link
Copy Markdown
Contributor

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 beta paramter, some a theta paramter, 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

@eriknw

eriknw commented Dec 3, 2024

Copy link
Copy Markdown
Contributor Author

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:
leiden_doc_example

@eriknw eriknw changed the title Begin adding Leiden as a backend-only algorithm Add Leiden as a backend-only algorithm Dec 3, 2024
@eriknw

eriknw commented Jan 3, 2025

Copy link
Copy Markdown
Contributor Author

The API for Leiden is very similar to Louvain, but I did remove threshold= parameter that is in Louvain and was included in #5964. I don't think threshold makes as much sense for Leiden, and implementations I've seen typically don't include it.

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 resolution parameter that already control the granularity of the communities. In other words, Leiden is already well-behaved and well-parametrized without needing threshold, which would be redundant and unnecessary. That's my understanding anyway.

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 rlratzel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dschult

dschult commented Jan 9, 2025

Copy link
Copy Markdown
Member

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?

@MridulS

MridulS commented Jan 9, 2025

Copy link
Copy Markdown
Member

Just to confirm the pop up only comes in after #7690 right?

@eriknw

eriknw commented Jan 10, 2025

Copy link
Copy Markdown
Contributor Author

Just to confirm the pop up only comes in after #7690 right?

Correct.

Also, this PR and #7690 can be merged in any order. They are meant to support each other, but one does not require the other and they can be merged independently.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should "See Also" to functions that aren't implemented by NX.
This could get confusing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

networkx.utils.backends._dispatchable._is_testing = True
networkx.config.backend_priority.algos = [backend_name]

and is used here:
if self._is_testing and backend_priority and backend_name is None:
# Special path if we are running networkx tests with a backend.
# This even runs for (and handles) functions that mutate input graphs.
return self._convert_and_call_for_tests(
backend_priority[0],

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 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointers to the backend testing interface. (and catching the names which I did not catch :)

@eriknw

eriknw commented Jan 16, 2025

Copy link
Copy Markdown
Contributor Author

These looks good.

Thanks for looking and being thoughtful!

  • We can't check that these tests pass in our CI, right?

Correct. The tests are skipped. nx-cugraph runs (and passes) these tests. The tests are relatively minimal. I expect more tests to be added when networkx implements Leiden (and that nx-cugraph may skip the new tests).

  • 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?

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?

  • 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)

I think I understand. We are adding Leiden to this page:
https://networkx.org/documentation/stable/reference/algorithms/community.html
but we don't indicate here that these are backend-only (which is only indicated on the function doc pages).

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.
  • Does cugraph have docs for Leiden? These looks a lot like our Louvain docs... Maybe the docs for cugraph have some helpful info too.

Here they are. I'll take a closer look to see if any parameters can be better explained.
https://docs.rapids.ai/api/cugraph/stable/api_docs/api/cugraph/cugraph.leiden/

@MridulS MridulS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional backends admonition should pop up once rapidsai/nx-cugraph#50 is merged right?

@eriknw

eriknw commented Jan 26, 2025

Copy link
Copy Markdown
Contributor Author

The additional backends admonition should pop up once rapidsai/nx-cugraph#50 is merged right?

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?

@MridulS

MridulS commented Jan 26, 2025

Copy link
Copy Markdown
Member

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!

@rlratzel

Copy link
Copy Markdown
Contributor

FYI we just merged rapidsai/nx-cugraph#50

@eriknw

eriknw commented Jan 27, 2025

Copy link
Copy Markdown
Contributor Author

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread networkx/algorithms/community/leiden.py Resolved
Comment thread networkx/algorithms/community/leiden.py Resolved
Comment thread networkx/algorithms/community/leiden.py Resolved
Comment thread networkx/algorithms/community/leiden.py Resolved
Comment thread networkx/algorithms/community/leiden.py Resolved
Comment thread networkx/algorithms/community/leiden.py Resolved
@eriknw

eriknw commented Jan 28, 2025

Copy link
Copy Markdown
Contributor Author

Excellent improvements to the docs @dschult! I carefully reviewed and accepted all suggestions 👍

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

Development

Successfully merging this pull request may close these issues.

6 participants