-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nx-cugraph: handle louvain with isolated nodes (#3897)
This handles isolated nodes in `louvain_communities` similar to what is done in #3886. This is expected to be a temporary fix until pylibcugraph can handle isolated nodes. As a bonus, I added `isolates` algorithm 🎉 CC @naimnv @rlratzel Authors: - Erik Welch (https://github.com/eriknw) Approvers: - Rick Ratzel (https://github.com/rlratzel) URL: #3897
- Loading branch information
Showing
9 changed files
with
151 additions
and
13 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,63 @@ | ||
# Copyright (c) 2023, 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 __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import cupy as cp | ||
|
||
from nx_cugraph.convert import _to_graph | ||
from nx_cugraph.utils import networkx_algorithm | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from nx_cugraph.typing import IndexValue | ||
|
||
__all__ = ["is_isolate", "isolates", "number_of_isolates"] | ||
|
||
|
||
@networkx_algorithm | ||
def is_isolate(G, n): | ||
G = _to_graph(G) | ||
index = n if G.key_to_id is None else G.key_to_id[n] | ||
return not ( | ||
(G.row_indices == index).any().tolist() | ||
or G.is_directed() | ||
and (G.col_indices == index).any().tolist() | ||
) | ||
|
||
|
||
def _mark_isolates(G) -> cp.ndarray[bool]: | ||
"""Return a boolean mask array indicating indices of isolated nodes.""" | ||
mark_isolates = cp.ones(len(G), bool) | ||
mark_isolates[G.row_indices] = False | ||
if G.is_directed(): | ||
mark_isolates[G.col_indices] = False | ||
return mark_isolates | ||
|
||
|
||
def _isolates(G) -> cp.ndarray[IndexValue]: | ||
"""Like isolates, but return an array of indices instead of an iterator of nodes.""" | ||
G = _to_graph(G) | ||
return cp.nonzero(_mark_isolates(G))[0] | ||
|
||
|
||
@networkx_algorithm | ||
def isolates(G): | ||
G = _to_graph(G) | ||
return G._nodeiter_to_iter(iter(_isolates(G).tolist())) | ||
|
||
|
||
@networkx_algorithm | ||
def number_of_isolates(G): | ||
G = _to_graph(G) | ||
return _mark_isolates(G).sum().tolist() |
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,53 @@ | ||
# Copyright (c) 2023, 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 | ||
|
||
import nx_cugraph as nxcg | ||
|
||
|
||
def test_louvain_isolated_nodes(): | ||
is_nx_30_or_31 = hasattr(nx.classes, "backends") | ||
|
||
def check(left, right): | ||
assert len(left) == len(right) | ||
assert set(map(frozenset, left)) == set(map(frozenset, right)) | ||
|
||
# Empty graph (no nodes) | ||
G = nx.Graph() | ||
if is_nx_30_or_31: | ||
with pytest.raises(ZeroDivisionError): | ||
nx.community.louvain_communities(G) | ||
else: | ||
nx_result = nx.community.louvain_communities(G) | ||
cg_result = nxcg.community.louvain_communities(G) | ||
check(nx_result, cg_result) | ||
# Graph with no edges | ||
G.add_nodes_from(range(5)) | ||
if is_nx_30_or_31: | ||
with pytest.raises(ZeroDivisionError): | ||
nx.community.louvain_communities(G) | ||
else: | ||
nx_result = nx.community.louvain_communities(G) | ||
cg_result = nxcg.community.louvain_communities(G) | ||
check(nx_result, cg_result) | ||
# Graph with isolated nodes | ||
G.add_edge(1, 2) | ||
nx_result = nx.community.louvain_communities(G) | ||
cg_result = nxcg.community.louvain_communities(G) | ||
check(nx_result, cg_result) | ||
# Another one | ||
G.add_edge(4, 4) | ||
nx_result = nx.community.louvain_communities(G) | ||
cg_result = nxcg.community.louvain_communities(G) | ||
check(nx_result, cg_result) |