Conversions between NetworkX, nx-arangodb, and nx-cugraph
This page describes how to convert graphs across ecosystems using
nx_arangodb.convert for interoperability and performance.
Overview
_to_nx_graph:nxadb.Graph|nx.Graph->nx.Graph_to_nxadb_graph:nx.Graph|nxadb.Graph->nxadb.Graph_to_nxcg_graph:nxadb.Graph|nxcg.Graph->nxcg.Graph(requiresnx-cugraphand CuPy)
Behavior notes
Database-backed
nxadbgraphs: converting to NetworkX pulls node and adjacency dictionaries from ArangoDB and constructs a plain NetworkX graph.Local
nxadbgraphs (graph_exists_in_dbisFalse) are already NetworkX-compatible and may be returned as-is.Directed/multi settings are preserved;
as_directedforces directed variants without changing multiplicity.If
nx-cugraphor CuPy is unavailable, GPU conversions are disabled and_to_nxcg_graphraisesNotImplementedError.
Performance and caching
Database pulls are timed and logged.
nxadb_to_nxreturns a standard NetworkX graph and does not re-wrap the custom dict implementations innx_arangodb.classes.dict.nxadb_to_nxcgcan cache the constructed GPU graph whenG.use_nxcg_cacheisTrueon thenxadb.Graphinstance.
Examples
import networkx as nx
import nx_arangodb as nxadb
# Start with a NetworkX graph
G_nx = nx.karate_club_graph()
# Convert to nx-arangodb (optionally force directed)
G_adb = nxadb.convert._to_nxadb_graph(G_nx, as_directed=False)
# Convert back to NetworkX
G_nx2 = nxadb.convert._to_nx_graph(G_adb)
# Convert to nx-cugraph if available
try:
G_cg = nxadb.convert._to_nxcg_graph(G_adb)
except NotImplementedError:
# nx-cugraph/CuPy not available
pass
API Reference
- nx_arangodb.convert._to_nx_graph(G: Any, *args: Any, **kwargs: Any) Graph[source]
Convert a graph to a NetworkX graph.
Parameters
- GAny
The graph to convert.
Currently supported types: - nx.Graph - nxadb.Graph
Returns
- nx.Graph
The converted graph.
- nx_arangodb.convert._to_nxadb_graph(G: Any, *args: Any, as_directed: bool = False, **kwargs: Any) Graph[source]
Convert a graph to a NetworkX-ArangoDB graph.
Parameters
- GAny
The graph to convert.
Currently supported types: - nx.Graph - nxadb.Graph
- as_directedbool, optional
Whether to convert the graph to a directed graph. Default is False.
Returns
- nxadb.Graph
The converted graph.
- nx_arangodb.convert.nx_to_nxadb(graph: Graph, *args: Any, as_directed: bool = False, **kwargs: Any) Graph[source]
Convert a NetworkX graph to a NetworkX-ArangoDB graph.
Parameters
- graphnx.Graph
The NetworkX graph to convert.
- as_directedbool, optional
Whether to convert the graph to a directed graph. Default is False.
Returns
- nxadb.Graph
The converted graph.
- nx_arangodb.convert.nxadb_to_nx(G: Graph) Graph[source]
Convert a NetworkX-ArangoDB graph to a NetworkX graph.
This function will pull the graph from the database if it does not exist in the cache. A new NetworkX graph will be created using the node and adjacency dictionaries that are fetched.
NOTE: The current downside of this approach is that we are not able to take advantage of the custom Dictionary classes that we have implemented in nx_arangodb.classes.dict. This is because the node and adjacency dictionaries are fetched as regular Python dictionaries. Furthermore, we don’t cache the dictionaries themselves, so we have to fetch them every time we convert the graph, which is currently being invoked on every algorithm call. See the note below for a potential solution. As a temporary workaround, users can do the following:
- ```
import networkx as nx import nx_arangodb as nxadb
G_ADB = nxadb.Graph(name=”MyGraph”) # Connect to the graph G_NX = nxadb.convert._to_nx_graph(G_ADB) # Pull the graph
nx.pagerank(G_NX) nx.betweenness_centrality(G_NX) …
Parameters
- Gnxadb.Graph
The NetworkX-ArangoDB graph to convert.
Returns
- nx.Graph
The converted graph.