Skip to content

Dispatch get_node_attributes and a few more from nx.classes.function - #7824

Merged
MridulS merged 1 commit into
networkx:mainfrom
eriknw:dispatch_get_node_attributes
Feb 11, 2025
Merged

MridulS merged 1 commit into
networkx:mainfrom
eriknw:dispatch_get_node_attributes

Conversation

@eriknw

@eriknw eriknw commented Feb 4, 2025

Copy link
Copy Markdown
Contributor

I think it would be useful to dispatch {get,set}_{node,edge}_attributes when 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.

@rossbar rossbar added type: Enhancements Dispatching Related to dispatching and backend support labels Feb 4, 2025

@rossbar rossbar 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.

Seems reasonable to me, thanks @eriknw !

@aMahanna

aMahanna commented Feb 4, 2025

Copy link
Copy Markdown
Contributor

@eriknw interesting idea, could you give an example of how this could be used?

As it stands, nx-arangodb already supports something like this:

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 DB

Are 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 G already exists in the database if set_node_attributes(..., backend="arangodb") is called 🤔

"set_node_attributes",
"remove_node_attributes",
}:
G1 = backend.convert_to_nx(bound.arguments["G"])

@aMahanna aMahanna Feb 4, 2025 •

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.

(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 db parameter object in the nxadb.Graph() constructor (python client)
  • You have passed the name parameter to nxadb.Graph(...)

I think I might have to introduce support for naming your graphs by env var for this line to work properly ^ 🤔

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.

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.

@aMahanna aMahanna Feb 6, 2025 •

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.

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

@MridulS MridulS added this to the 3.5 milestone Feb 11, 2025
@MridulS
MridulS merged commit 8a9ec3c into networkx:main Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Dispatching Related to dispatching and backend support type: Enhancements

Development

Successfully merging this pull request may close these issues.

4 participants