-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nx-cugraph: add triangles and clustering algorithms #4093
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bee0e9
nx-cugraph: add triangles and clustering algorithms
eriknw 355e2fd
Merge branch 'branch-24.02' into clustering
eriknw 12848fe
Merge branch 'branch-24.02' into clustering
eriknw ce80654
Add `is_bipartite`
eriknw 1a59f71
Merge branch 'branch-24.02' into clustering
eriknw f04ebc1
ignore selfloops and compare results with selfloops to networkx
eriknw eb55793
oops!
eriknw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 31 additions & 0 deletions
31
python/nx-cugraph/nx_cugraph/algorithms/bipartite/basic.py
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,31 @@ | ||
# Copyright (c) 2024, 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 | ||
|
||
from nx_cugraph.algorithms.cluster import _triangles | ||
from nx_cugraph.convert import _to_graph | ||
from nx_cugraph.utils import networkx_algorithm | ||
|
||
__all__ = [ | ||
"is_bipartite", | ||
] | ||
|
||
|
||
@networkx_algorithm(plc="triangle_count", version_added="24.02") | ||
def is_bipartite(G): | ||
G = _to_graph(G) | ||
# Counting triangles may not be the fastest way to do this, but it is simple. | ||
node_ids, triangles, is_single_node = _triangles( | ||
G, None, symmetrize="union" if G.is_directed() else None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this discovered by the new tests? :) |
||
) | ||
return int(cp.count_nonzero(triangles)) == 0 |
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,136 @@ | ||
# Copyright (c) 2024, 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 pylibcugraph as plc | ||
|
||
from nx_cugraph.convert import _to_undirected_graph | ||
from nx_cugraph.utils import networkx_algorithm, not_implemented_for | ||
|
||
__all__ = [ | ||
"triangles", | ||
"average_clustering", | ||
"clustering", | ||
"transitivity", | ||
] | ||
|
||
|
||
def _triangles(G, nodes, symmetrize=None): | ||
if nodes is not None: | ||
if is_single_node := (nodes in G): | ||
nodes = [nodes if G.key_to_id is None else G.key_to_id[nodes]] | ||
else: | ||
nodes = list(nodes) | ||
nodes = G._list_to_nodearray(nodes) | ||
else: | ||
is_single_node = False | ||
if len(G) == 0: | ||
return None, None, is_single_node | ||
node_ids, triangles = plc.triangle_count( | ||
resource_handle=plc.ResourceHandle(), | ||
graph=G._get_plc_graph(symmetrize=symmetrize), | ||
start_list=nodes, | ||
do_expensive_check=False, | ||
) | ||
return node_ids, triangles, is_single_node | ||
|
||
|
||
@not_implemented_for("directed") | ||
@networkx_algorithm(plc="triangle_count", version_added="24.02") | ||
def triangles(G, nodes=None): | ||
G = _to_undirected_graph(G) | ||
node_ids, triangles, is_single_node = _triangles(G, nodes) | ||
if len(G) == 0: | ||
return {} | ||
if is_single_node: | ||
return int(triangles[0]) | ||
return G._nodearrays_to_dict(node_ids, triangles) | ||
|
||
|
||
@not_implemented_for("directed") | ||
@networkx_algorithm(is_incomplete=True, plc="triangle_count", version_added="24.02") | ||
def clustering(G, nodes=None, weight=None): | ||
"""Directed graphs and `weight` parameter are not yet supported.""" | ||
G = _to_undirected_graph(G) | ||
node_ids, triangles, is_single_node = _triangles(G, nodes) | ||
if len(G) == 0: | ||
return {} | ||
if is_single_node: | ||
numer = int(triangles[0]) | ||
if numer == 0: | ||
return 0 | ||
degree = int((G.src_indices == nodes).sum()) | ||
return 2 * numer / (degree * (degree - 1)) | ||
degrees = G._degrees_array(ignore_selfloops=True)[node_ids] | ||
denom = degrees * (degrees - 1) | ||
results = 2 * triangles / denom | ||
results = cp.where(denom, results, 0) # 0 where we divided by 0 | ||
return G._nodearrays_to_dict(node_ids, results) | ||
|
||
|
||
@clustering._can_run | ||
def _(G, nodes=None, weight=None): | ||
return weight is None and not G.is_directed() | ||
|
||
|
||
@not_implemented_for("directed") | ||
@networkx_algorithm(is_incomplete=True, plc="triangle_count", version_added="24.02") | ||
def average_clustering(G, nodes=None, weight=None, count_zeros=True): | ||
"""Directed graphs and `weight` parameter are not yet supported.""" | ||
G = _to_undirected_graph(G) | ||
node_ids, triangles, is_single_node = _triangles(G, nodes) | ||
if len(G) == 0: | ||
raise ZeroDivisionError | ||
degrees = G._degrees_array(ignore_selfloops=True)[node_ids] | ||
if not count_zeros: | ||
mask = triangles != 0 | ||
triangles = triangles[mask] | ||
if triangles.size == 0: | ||
raise ZeroDivisionError | ||
degrees = degrees[mask] | ||
denom = degrees * (degrees - 1) | ||
results = 2 * triangles / denom | ||
if count_zeros: | ||
results = cp.where(denom, results, 0) # 0 where we divided by 0 | ||
return float(results.mean()) | ||
|
||
|
||
@average_clustering._can_run | ||
def _(G, nodes=None, weight=None, count_zeros=True): | ||
return weight is None and not G.is_directed() | ||
|
||
|
||
@not_implemented_for("directed") | ||
@networkx_algorithm(is_incomplete=True, plc="triangle_count", version_added="24.02") | ||
def transitivity(G): | ||
"""Directed graphs are not yet supported.""" | ||
G = _to_undirected_graph(G) | ||
if len(G) == 0: | ||
return 0 | ||
node_ids, triangles = plc.triangle_count( | ||
resource_handle=plc.ResourceHandle(), | ||
graph=G._get_plc_graph(), | ||
start_list=None, | ||
do_expensive_check=False, | ||
) | ||
numer = int(triangles.sum()) | ||
if numer == 0: | ||
return 0 | ||
degrees = G._degrees_array(ignore_selfloops=True)[node_ids] | ||
denom = int((degrees * (degrees - 1)).sum()) | ||
return 2 * numer / denom | ||
|
||
|
||
@transitivity._can_run | ||
def _(G): | ||
# Is transitivity supposed to work on directed graphs? | ||
return not G.is_directed() |
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,48 @@ | ||
# Copyright (c) 2024, 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 networkx as nx | ||
import pytest | ||
from packaging.version import parse | ||
|
||
nxver = parse(nx.__version__) | ||
|
||
if nxver.major == 3 and nxver.minor < 2: | ||
pytest.skip("Need NetworkX >=3.2 to test clustering", allow_module_level=True) | ||
|
||
|
||
def test_selfloops(): | ||
G = nx.complete_graph(5) | ||
H = nx.complete_graph(5) | ||
H.add_edge(0, 0) | ||
H.add_edge(1, 1) | ||
H.add_edge(2, 2) | ||
# triangles | ||
expected = nx.triangles(G) | ||
assert expected == nx.triangles(H) | ||
assert expected == nx.triangles(G, backend="cugraph") | ||
assert expected == nx.triangles(H, backend="cugraph") | ||
# average_clustering | ||
expected = nx.average_clustering(G) | ||
assert expected == nx.average_clustering(H) | ||
assert expected == nx.average_clustering(G, backend="cugraph") | ||
assert expected == nx.average_clustering(H, backend="cugraph") | ||
# clustering | ||
expected = nx.clustering(G) | ||
assert expected == nx.clustering(H) | ||
assert expected == nx.clustering(G, backend="cugraph") | ||
assert expected == nx.clustering(H, backend="cugraph") | ||
# transitivity | ||
expected = nx.transitivity(G) | ||
assert expected == nx.transitivity(H) | ||
assert expected == nx.transitivity(G, backend="cugraph") | ||
assert expected == nx.transitivity(H, backend="cugraph") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subjective feedback: I remember we discussed this but FWIW this is still a bit surprising. I have to take a beat and remember that these params are metadata and not important to functionality when reading the code. I suspect this could result in another issue like this someday since it's not self-documenting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it help to rename
plc
to_plc
to signal that this parameter doesn't matter if you don't know what it's for?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to use
version_added
to update the docstring, but I haven't fully baked that yet.We could also make
plc
a code comment, which is a less useful, but also potentially less confusing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, a leading underscore could help emphasize these are "internal" params. I was mainly bringing it up for awareness, and this can (and probably should, unless you'd actually prefer to address it here) be done in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh, so we have a reason to merge all open nx-cugraph PRs ;)