Repository navigation
Enable backend-only functions where NetworkX is just an API - #7690
Conversation
rossbar
left a comment
There was a problem hiding this comment.
+1 for supporting this use-case. Is this machinery necessary though? If someone were to add, e.g.
@nx._dispatchable(...)
def leiden(G, ...):
"""Leiden docstring"""
raise NotImplementedErrorwouldn't that be sufficient? IMO if we can get away with not expanding the configuration input (which is already quite complicated) that's a win!
Yeah, that's certainly the easiest option to begin with, so thanks for bringing it up! Where it falls short is introspection. With this PR, users can check The approach in this PR also allows for slightly better log and error messages. Is this worth the added complexity? I'm not sure, hence why I wanted to see it in PR form ;) |
There was a problem hiding this comment.
I'm -0.5 on this. [I'm -0 on this,] But mostly because I can't understand how this impacts the dispatching logic, and I don't have the time to figure that out right now.
Isn't a function that returns NotImplemented should be sufficient. Why does it also need to be marked in the decorator as such.
I'm also worried that figuring out the decorator will make it difficult for people to contribute code.
| backend implementation(s) using the `backend_info` entry-point.""" | ||
|
|
||
| if not self.backends: | ||
| if self.backends.issubset({"networkx"}): |
There was a problem hiding this comment.
Is this related to is_stub?
Can we just put it in a separate PR?
Why is it in this PR?
There was a problem hiding this comment.
Yes, it's related. This PR adds "networkx" to self.backends. This check means we don't need to add "Backends" section to the docstring if it is not implemented by any backends or is only implemented by "networkx".
Co-authored-by: Dan Schult <[email protected]>
|
I removed |
| def _does_backend_have(self, backend_name): | ||
| """Does the specified backend have this algorithm?""" | ||
| if backend_name == "networkx": | ||
| return True | ||
| return "networkx" in self.backends |
There was a problem hiding this comment.
@dschult, this is the key location to understand how this PR affects dispatch logic. _does_backend_have is the method that answers whether a backend has a function. NetworkX "has" a function only if "networkx" is in self.backends.
Other key changes are basically updating
if nx.config.fallback_to_nxto
if nx.config.fallback_to_nx and "networkx" in self.backendswhich we could write as
if nx.config.fallback_to_nx and self._does_backend_have("networkx")There was a problem hiding this comment.
Thanks for the pointer to this function. At the moment, it doesn't seem so central to the PR though -- the function _does_backend_have is only used to set up the error messages for NotImplemented cases.
But the phrase "networkx" in self.backends is key -- as you explain -- because it combines with fallback_to_nx to determine whether the networkx function is called. But couldn't this just be done by calling the networkx function to get the NotImplemented rather than storing and looking up this info in self.backends? I'm sure it is more complicated than that. There are always side-effects.
There was a problem hiding this comment.
...
_does_backend_haveis only used ...
Touche. Good catch. I also (just now) updated _can_backend_run to return False if "networkx" not in self.backends".
|
I've adjusted my scale for vote on this PR. I now understand that -0.5 is more negative than I was intending. I was -0 for adding the I would suggest a name other than |
Schefflera-Arboricola
left a comment
There was a problem hiding this comment.
Rather than adding stub functions in NetworkX to enable backend-only functions, could we instead temporarily add the stubs for backend-only functions from all the installed backends directly to NetworkX's namespace when NetworkX is imported? This way backend developers would be able to integrate backend-only functions without needing to request NetworkX to add stub functions, eliminating any bias toward specific backends.
Something like this:
In NetworkX(inside __init__.py):
backend_only_combined = []
backends = _get_backends("networkx.backends")
for backend_name in backends:
_backend = _load_backend(backend_name)
if hasattr(_backend, backend_only):
for func_name, func in _backend.backend_only.items():
if func_name not in backend_only_combined:
_dispatchable(func_name, is_stub=True)
backend_only_combined.append(func_name)In backend:
from nx_backend import leiden
BackendInterface.backend_only = {
"leiden": leiden
}here, BackendInterface is the object pointed by the "networkx.backends" entry_point. And backend_only doesn't have to be a dictionary, we can allow it to be any object, with the additional functions as its attributes.
Are there any major flaws/drawbacks to this approach?
Thank you :)
|
A similar approach would be to just have the backends provide functions on their own, without any NetworkX interfacing. The backend library doesn't hve to always work through a networkx interface -- only when it makes sense to do so. So the user would do something like: import networkx as nx
import nx_parallel as nxp
G = nx.path_graph(9)
result = nxp.new_feature(G) |
|
But we want to keep the API design within networkx right? The docs, and function signature needs to be in networkx codebase to make the dispatch work this way. |
That is how it is currently. And I think we want to enable dispatching of the backend-only algorithms from networkx because of the "0 code change" thing. So, if a user sets the
in the first comment? [EDIT] : One more reason why a user should call |
This is a good comment for a topic larger than what's covered in this PR, and I'm thinking we should have had a NXEP on "backend-only" APIs or "NetworkX as a standard graph analytics API" or some other name for this topic. My thoughts about supporting backend-only APIs are:
I'm a bit confused by this use case though. Is this saying the user has installed nx-cugraph, but doesn't want to use it for anything except functions not implemented in NX (leiden)? I'm just wondering if that's more of an uncommon corner case, and the recommendation for that is to set |
|
Sorry for being AFK for so long! (Taking time off is nice though :) ). I will begin to address comments in this PR, but it will take me some time. Thanks for the discussions!
I agree with that reasoning. I changed the name to Another reason to introduce this to dispatching machinery is so that when I'll reply more tomorrow. Cheers! |
I mean in terms of implementation, to reduce the number of times we need to do e.g. |
|
This now automatically adds an "Attention" admonition near the top of the function. Other types of admonitions could also work; see: https://sphinx-book-theme.readthedocs.io/en/stable/reference/kitchen-sink/admonitions.html Here's an example of online docs of a backend-only function: And here it is as text: |
|
I cleaned this up slightly to try to make the code a little clearer. Here's a summary (with remarks) of the changes in this PR:
We discussed how this impacts the zero-code change use case in the NetworkX community call yesterday. In short: it doesn't. Code doesn't change. It just happens that the code will fail if it needs to use networkx implementation. |
dschult
left a comment
There was a problem hiding this comment.
I've looked through this again and think it is ready to merge.
I hope that we don't get too many of these backend-only functions. That could get confusing for people looking at the NetworkX docs list of algorithms. They will see functions that they can't use without installing backends. Perhaps adding backend-only functions can give an expectation that NX will eventually implement them.
Anyway, I approve this PR
Thanks for taking a look @dschult! I agree with this sentiment. There is a specific need for Leiden, so hopefully there won't be that much demand/need to introduce too many backend-only functions. There should be a strong case for each one that gets added, and they should be appropriate to add to networkx and not just someones whim. Anyway, I consider each backend-only function to be an invitation for somebody to contribute an implementation ;) |
|
@MridulS your approval was long ago -- sorry for the delay while I get up to speed with this stuff. |

This adds
is_stubto_dispatchableas a quick way to enable backend-only functions. Is there a better name thanis_stub?Alternative approaches:
default_backend=to_dispatchableto encourage other backends or libraries to wrap functions with@_dispatchableto use the dispatch machineryThis will let us expose cugraph-only functions such as Leiden or node2vec. CC @rlratzel