Skip to content

Commit

Permalink
Raise if nx input and copy=False
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Jul 24, 2024
1 parent 7ed4796 commit faf5bfc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/operators/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ def reverse(G, copy=True):
if not G.is_directed():
raise nx.NetworkXError("Cannot reverse an undirected graph.")
if isinstance(G, nx.Graph):
if not copy:
raise RuntimeError(
"Using `copy=False` is invalid when using a NetworkX graph "
"as input to `nx_cugraph.reverse`"
)
G = nxcg.from_networkx(G, preserve_all_attrs=True)
return G.reverse(copy=copy)
5 changes: 5 additions & 0 deletions python/nx-cugraph/nx_cugraph/relabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
@networkx_algorithm(version_added="24.08")
def relabel_nodes(G, mapping, copy=True):
if isinstance(G, nx.Graph):
if not copy:
raise RuntimeError(
"Using `copy=False` is invalid when using a NetworkX graph "
"as input to `nx_cugraph.relabel_nodes`"
)
G = nxcg.from_networkx(G, preserve_all_attrs=True)
it = range(G._N) if G.key_to_id is None else G.id_to_key
if callable(mapping):
Expand Down
9 changes: 9 additions & 0 deletions python/nx-cugraph/nx_cugraph/tests/test_relabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ def test_relabel_multigraph(create_using):
Hnx = nx.relabel_nodes(G, {2: 3, 1: 3, 0: 3})
Hcg = nxcg.relabel_nodes(G, {2: 3, 1: 3, 0: 3})
assert_graphs_equal(Hnx, Hcg)


def test_relabel_nx_input():
G = nx.complete_graph(3)
with pytest.raises(RuntimeError, match="Using `copy=False` is invalid"):
nxcg.relabel_nodes(G, {0: 1}, copy=False)
Hnx = nx.relabel_nodes(G, {0: 1}, copy=True)
Hcg = nxcg.relabel_nodes(G, {0: 1}, copy=True)
assert_graphs_equal(Hnx, Hcg)

0 comments on commit faf5bfc

Please sign in to comment.