Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,35 @@ G_nxadb = nxadb.Graph(
assert G_nxadb.number_of_nodes() == G_nx.number_of_nodes()
assert G_nxadb.number_of_edges() == G_nx.number_of_edges()
```

## Direct Conversion Helpers

Use the conversion helpers in `nx_arangodb.convert` to move between ecosystems while preserving directed/multi properties.

```python
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 via as_directed=True)
G_adb = nxadb.convert._to_nxadb_graph(G_nx)

# Convert back to NetworkX
G_nx2 = nxadb.convert._to_nx_graph(G_adb)

# Convert to nx-cugraph (if installed)
try:
G_cg = nxadb.convert._to_nxcg_graph(G_adb)
except NotImplementedError:
# nx-cugraph/CuPy not available in this environment
pass
```

Notes:
- Database-backed graphs: converting `nxadb.Graph` to NetworkX will fetch node and adjacency dictionaries from ArangoDB and build a plain NetworkX graph.
- Local `nxadb.Graph` (not yet persisted) is already NetworkX-compatible and may be returned as-is.
- `as_directed` forces directed variants without changing multiplicity.
- GPU conversions require `nx-cugraph` and CuPy; otherwise `_to_nxcg_graph` raises `NotImplementedError`.
- When converting to `nx-cugraph`, enabling `G_adb.use_nxcg_cache = True` caches the constructed GPU graph for reuse.
75 changes: 75 additions & 0 deletions doc/convert.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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``
(requires ``nx-cugraph`` and CuPy)

Behavior notes
--------------

- Database-backed ``nxadb`` graphs: converting to NetworkX pulls node and
adjacency dictionaries from ArangoDB and constructs a plain NetworkX graph.
- Local ``nxadb`` graphs (``graph_exists_in_db`` is ``False``) are already
NetworkX-compatible and may be returned as-is.
- Directed/multi settings are preserved; ``as_directed`` forces directed
variants without changing multiplicity.
- If ``nx-cugraph`` or CuPy is unavailable, GPU conversions are disabled and
``_to_nxcg_graph`` raises ``NotImplementedError``.

Performance and caching
-----------------------

- Database pulls are timed and logged.
- ``nxadb_to_nx`` returns a standard NetworkX graph and does not re-wrap the
custom dict implementations in ``nx_arangodb.classes.dict``.
- ``nxadb_to_nxcg`` can cache the constructed GPU graph when
``G.use_nxcg_cache`` is ``True`` on the ``nxadb.Graph`` instance.

Examples
--------

.. code-block:: python

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

.. autofunction:: nx_arangodb.convert._to_nx_graph

.. autofunction:: nx_arangodb.convert._to_nxadb_graph

.. autofunction:: nx_arangodb.convert._to_nxcg_graph

.. autofunction:: nx_arangodb.convert.nx_to_nxadb

.. autofunction:: nx_arangodb.convert.nxadb_to_nx

.. autofunction:: nx_arangodb.convert.nxadb_to_nxcg


3 changes: 2 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@ Expect documentation to grow over time:
classes/index
dict/index
algorithms/index
views/index
views/index
convert