-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for
jaccard_coefficient
(#62)
Adds support for `jaccard_coefficient` to nx-cugraph. This includes a test, but relies largely on the existing test coverage provided by NetworkX. The test included here could (should) be submitted to NetworkX though in a separate PR, since it is not covering anything unique to nx-cugraph. A benchmark is also included, with results showing 2-4X speedup. I've seen much, much larger speedup on a different graph (large movie review bipartite graph, showing 966s for NX, 2s for nx-cugraph = ~500X), so I need to investigate further. This investigation need not prevent this PR from being merged now though. ![image](https://github.com/user-attachments/assets/3ceb7d62-50c4-437e-96d2-0ab452dd39d2) Authors: - Rick Ratzel (https://github.com/rlratzel) Approvers: - Ralph Liu (https://github.com/nv-rliu) - Erik Welch (https://github.com/eriknw) URL: #62
- Loading branch information
Showing
7 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright (c) 2025, NVIDIA CORPORATION. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import cupy as cp | ||
import networkx as nx | ||
import pylibcugraph as plc | ||
|
||
from nx_cugraph.convert import _to_undirected_graph | ||
from nx_cugraph.utils import index_dtype, networkx_algorithm, not_implemented_for | ||
|
||
__all__ = [ | ||
"jaccard_coefficient", | ||
] | ||
|
||
|
||
@not_implemented_for("directed") | ||
@not_implemented_for("multigraph") | ||
@networkx_algorithm(version_added="25.02", _plc="jaccard_coefficients") | ||
def jaccard_coefficient(G, ebunch=None): | ||
G = _to_undirected_graph(G) | ||
|
||
# If ebunch is not specified, create pairs representing all non-edges. | ||
# This can be an extremely large set and is not realistic for large graphs, | ||
# but this is required for NX compatibility. | ||
if ebunch is None: | ||
A = cp.tri(G._N, G._N, dtype=bool) | ||
A[G.src_indices, G.dst_indices] = True | ||
u_indices, v_indices = cp.nonzero(~A) | ||
if u_indices.size == 0: | ||
return iter([]) | ||
u_indices = u_indices.astype(index_dtype) | ||
v_indices = v_indices.astype(index_dtype) | ||
|
||
else: | ||
(u, v) = zip(*ebunch) | ||
try: | ||
# Convert the ebunch lists to cupy arrays for passing to PLC, possibly | ||
# mapping to integers if the Graph was renumbered. | ||
# Allow the Graph renumber lookup (if renumbering was done) to check | ||
# for invalid node IDs in ebunch. | ||
u_indices = G._list_to_nodearray(u) | ||
v_indices = G._list_to_nodearray(v) | ||
except (KeyError, ValueError) as n: | ||
raise nx.NodeNotFound(f"Node {n} not in G.") | ||
|
||
(u, v, p) = plc.jaccard_coefficients( | ||
resource_handle=plc.ResourceHandle(), | ||
graph=G._get_plc_graph(), | ||
first=u_indices, | ||
second=v_indices, | ||
use_weight=False, | ||
do_expensive_check=False, | ||
) | ||
|
||
u = G._nodearray_to_list(u) | ||
v = G._nodearray_to_list(v) | ||
p = p.tolist() | ||
|
||
return zip(u, v, p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (c) 2025, NVIDIA CORPORATION. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from collections.abc import Iterable | ||
|
||
import networkx as nx | ||
import pytest | ||
|
||
# The tests in this file cover use cases unique to nx-cugraph. If the coverage | ||
# here is not unique to nx-cugraph, consider moving those tests to the NetworkX | ||
# project. | ||
|
||
|
||
def test_no_nonexistent_edges_no_ebunch(): | ||
"""Test no ebunch and G is fully connected | ||
Ensure function returns iter([]) or equivalent due to no nonexistent edges. | ||
""" | ||
G = nx.complete_graph(5) | ||
result = nx.jaccard_coefficient(G) | ||
assert isinstance(result, Iterable) | ||
assert pytest.raises(StopIteration, next, result) | ||
|
||
|
||
def test_node_not_found_in_ebunch(): | ||
"""Test that all nodes in ebunch are valid | ||
Ensure function raises NodeNotFound for invalid nodes in ebunch. | ||
""" | ||
G = nx.Graph([(0, 1), (1, 2)]) | ||
with pytest.raises(nx.NodeNotFound, match="Node [']*A[']* not in G."): | ||
nx.jaccard_coefficient(G, [("A", 1)]) | ||
with pytest.raises(nx.NodeNotFound, match=r"Node \(1,\) not in G."): | ||
nx.jaccard_coefficient(G, [(0, (1,))]) | ||
with pytest.raises(nx.NodeNotFound, match="Node 9999 not in G."): | ||
nx.jaccard_coefficient(G, [(0, 9999)]) |