Skip to content

Commit 14ae1ec

Browse files
committed
Handle BC normalization by adding docstring node and skipping test
1 parent b321c16 commit 14ae1ec

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

_nx_cugraph/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
"average_clustering": "Directed graphs and `weight` parameter are not yet supported.",
175175
"bellman_ford_path": "Negative cycles are not yet supported. ``NotImplementedError`` will be raised if there are negative edge weights. We plan to support negative edge weights soon. Also, callable ``weight`` argument is not supported.",
176176
"bellman_ford_path_length": "Negative cycles are not yet supported. ``NotImplementedError`` will be raised if there are negative edge weights. We plan to support negative edge weights soon. Also, callable ``weight`` argument is not supported.",
177-
"betweenness_centrality": "`weight` parameter is not yet supported, and RNG with seed may be different.",
177+
"betweenness_centrality": "`weight` parameter is not yet supported, and RNG with seed may be different. Normalization matches NetworkX version 3.5, which fixed normalization when using k (see https://github.com/networkx/networkx/pull/7908 for details).",
178178
"bfs_edges": "`sort_neighbors` parameter is not yet supported.",
179179
"bfs_predecessors": "`sort_neighbors` parameter is not yet supported.",
180180
"bfs_successors": "`sort_neighbors` parameter is not yet supported.",

nx_cugraph/algorithms/centrality/betweenness.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,34 @@
1414
import pylibcugraph as plc
1515
from networkx.utils import create_py_random_state
1616

17+
from nx_cugraph import _nxver
1718
from nx_cugraph.convert import _to_graph
1819
from nx_cugraph.utils import index_dtype, networkx_algorithm
1920

2021
__all__ = ["betweenness_centrality", "edge_betweenness_centrality"]
2122

2223

24+
if _nxver < (3, 5):
25+
EXTRA_DOCSTRING = (
26+
" Normalization matches NetworkX version 3.5, which fixed normalization when "
27+
"using k (see https://github.com/networkx/networkx/pull/7908 for details)."
28+
)
29+
else:
30+
EXTRA_DOCSTRING = ""
31+
32+
2333
@networkx_algorithm(
2434
is_incomplete=True, # weight not supported
2535
version_added="23.10",
2636
_plc="betweenness_centrality",
37+
docstring=(
38+
"`weight` parameter is not yet supported, and RNG with seed may be different."
39+
f"{EXTRA_DOCSTRING}"
40+
),
2741
)
2842
def betweenness_centrality(
2943
G, k=None, normalized=True, weight=None, endpoints=False, seed=None
3044
):
31-
"""`weight` parameter is not yet supported, and RNG with seed may be different."""
3245
if weight is not None:
3346
raise NotImplementedError(
3447
"Weighted implementation of betweenness centrality not currently supported"

nx_cugraph/interface.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
1+
# Copyright (c) 2023-2025, NVIDIA CORPORATION.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -72,6 +72,8 @@ def key(testpath):
7272
"nx-cugraph Graph is incompatible in test setup in nx versions < 3.3"
7373
)
7474
different_iteration_order = "Different graph data iteration order"
75+
# For nx version <= 3.4
76+
bc_normalization_fixed = "BC normalization fixed in 3.5"
7577
# For all versions
7678
louvain_different = "Louvain may be different due to RNG"
7779
sssp_path_different = "sssp may choose a different valid path"
@@ -270,7 +272,15 @@ def key(testpath):
270272
key("test_louvain.py:test_max_level"): louvain_different,
271273
}
272274
)
273-
275+
if _nxver < (3, 5):
276+
xfail.update(
277+
{
278+
key(
279+
"test_betweenness_centrality.py:"
280+
"TestBetweennessCentrality.test_sample_from_P3"
281+
): bc_normalization_fixed,
282+
}
283+
)
274284
xfail.update(
275285
{
276286
key("test_louvain.py:test_karate_club_partition"): louvain_different,

nx_cugraph/utils/decorators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def __new__(
6161
extra_params: dict[str, str] | str | None = None,
6262
# Applies `nodes_or_number` decorator compatibly across versions (3.3 changed)
6363
nodes_or_number: list[int] | int | None = None,
64+
# Alternative way to provide the docstring to add to the networkx docstring
65+
docstring: str | None = None,
6466
# Metadata (for introspection only)
6567
version_added: str, # Required
6668
is_incomplete: bool = False, # See self.extra_doc for details if True
@@ -76,13 +78,16 @@ def __new__(
7678
name=name,
7779
extra_params=extra_params,
7880
nodes_or_number=nodes_or_number,
81+
docstring=docstring,
7982
version_added=version_added,
8083
is_incomplete=is_incomplete,
8184
is_different=is_different,
8285
fallback=fallback,
8386
create_using_arg=create_using_arg,
8487
_plc=_plc,
8588
)
89+
if docstring:
90+
func.__doc__ = docstring
8691
instance = object.__new__(cls)
8792
if nodes_or_number is not None and _nxver >= (3, 3):
8893
func = nx.utils.decorators.nodes_or_number(nodes_or_number)(func)

0 commit comments

Comments
 (0)