Repository navigation
Dispatch get_node_attributes and a few more from nx.classes.function - #7824
Conversation
|
@eriknw interesting idea, could you give an example of how this could be used? As it stands, import os
import network as nx
import nx_arangodb as nxadb
# Database credentials
os.envrion["..."] = ...
G = nxadb.Graph(name="KarateGraph") # Connect to existing graph
res = nx.pagerank(G) # Fetches graph from DB
nx.set_node_attributes(G, res, "pagerank") # Updates graph in DBAre you suggesting something like this? import networkx as nx
G = nx.karate_club_graph()
res = nx.pagerank(G)
nx.set_node_attributes(G, res, "pagerank", backend="arangodb") I'm not quite sure if this would work for nx-arangodb, we would be assuming |
| "set_node_attributes", | ||
| "remove_node_attributes", | ||
| }: | ||
| G1 = backend.convert_to_nx(bound.arguments["G"]) |
There was a problem hiding this comment.
(continuing my comment from above) I see, you would be converting nx.Graph to nxadb.Graph before invoking the function.
G1 = backend.convert_to_nx(bound.arguments["G"])This is good to see now, because unfortunately this won't be enough to load G into ArangoDB.
Persisting persisting an nx Graph into ArangoDB requires that:
- You have provided database credentials as environment variables, or you pass the
dbparameter object in thenxadb.Graph()constructor (python client) - You have passed the
nameparameter tonxadb.Graph(...)
I think I might have to introduce support for naming your graphs by env var for this line to work properly ^ 🤔
There was a problem hiding this comment.
Thanks for the quick response.
Since nx.set_node_attributes mutates the graph and doesn't return anything, I don't know if it would make much sense to use it with backend="arangodb". Basically, the user wants to update a graph, not load a graph to a different location.
The use case I want to better support is when the user already has a backend graph and wants to do e.g.:
nx.set_node_attributes(Gadb, res, "pagerank")Making this dispatchable to the backend allows the backend to write a better implementation. If the default networkx versions works fine for a backend, then nothing should change: it will continue to work the same way. But, for e.g. nx-cugraph, we would want to be smarter about our implementation so that we would avoid creating the "fallback" networkx data structures.
In downstream code that I've looked at, getting and setting attributes appears to be pretty common.
There was a problem hiding this comment.
Understood!
Making this dispatchable to the backend allows the backend to write a better implementation
And good point! We currently get away with nx.set_node_attributes(Gadb, res, "pagerank") working because of our dict factory overrides, but we could definitely optimize this operation via a bulk update API call to the database (instead of individual API calls)
Thank you for clarifying! Feel free to resolve
I think it would be useful to dispatch
{get,set}_{node,edge}_attributeswhen using backends such as nx-cugraph and nx-arangodb (CC @aMahanna), and I went ahead and made a few other (but not all) functions from this file dispatchable.