Skip to content

Commit

Permalink
nx-cugraph: rename plc= to _plc= (#4106)
Browse files Browse the repository at this point in the history
As discussed here: #4093 (comment)

Authors:
  - Erik Welch (https://github.com/eriknw)

Approvers:
  - Rick Ratzel (https://github.com/rlratzel)

URL: #4106
eriknw authored Jan 24, 2024
1 parent 6774abc commit 82552ab
Showing 18 changed files with 84 additions and 53 deletions.
6 changes: 3 additions & 3 deletions python/nx-cugraph/lint.yaml
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ repos:
- id: black
# - id: black-jupyter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
rev: v0.1.14
hooks:
- id: ruff
args: [--fix-only, --show-fixes] # --unsafe-fixes]
@@ -62,7 +62,7 @@ repos:
additional_dependencies: &flake8_dependencies
# These versions need updated manually
- flake8==7.0.0
- flake8-bugbear==23.12.2
- flake8-bugbear==24.1.17
- flake8-simplify==0.21.0
- repo: https://github.com/asottile/yesqa
rev: v1.5.0
@@ -77,7 +77,7 @@ repos:
additional_dependencies: [tomli]
files: ^(nx_cugraph|docs)/
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
rev: v0.1.14
hooks:
- id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
2 changes: 1 addition & 1 deletion python/nx-cugraph/nx_cugraph/algorithms/bipartite/basic.py
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
]


@networkx_algorithm(plc="triangle_count", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="triangle_count")
def is_bipartite(G):
G = _to_graph(G)
# Counting triangles may not be the fastest way to do this, but it is simple.
Original file line number Diff line number Diff line change
@@ -21,8 +21,8 @@
@networkx_algorithm(
is_incomplete=True, # weight not supported
is_different=True, # RNG with seed is different
plc="betweenness_centrality",
version_added="23.10",
_plc="betweenness_centrality",
)
def betweenness_centrality(
G, k=None, normalized=True, weight=None, endpoints=False, seed=None
@@ -54,8 +54,8 @@ def _(G, k=None, normalized=True, weight=None, endpoints=False, seed=None):
@networkx_algorithm(
is_incomplete=True, # weight not supported
is_different=True, # RNG with seed is different
plc="edge_betweenness_centrality",
version_added="23.10",
_plc="edge_betweenness_centrality",
)
def edge_betweenness_centrality(G, k=None, normalized=True, weight=None, seed=None):
"""`weight` parameter is not yet supported, and RNG with seed may be different."""
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@
@networkx_algorithm(
extra_params=_dtype_param,
is_incomplete=True, # nstart not supported
plc="eigenvector_centrality",
version_added="23.12",
_plc="eigenvector_centrality",
)
def eigenvector_centrality(
G, max_iter=100, tol=1.0e-6, nstart=None, weight=None, *, dtype=None
2 changes: 1 addition & 1 deletion python/nx-cugraph/nx_cugraph/algorithms/centrality/katz.py
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@
@networkx_algorithm(
extra_params=_dtype_param,
is_incomplete=True, # nstart and normalized=False not supported
plc="katz_centrality",
version_added="23.12",
_plc="katz_centrality",
)
def katz_centrality(
G,
16 changes: 12 additions & 4 deletions python/nx-cugraph/nx_cugraph/algorithms/cluster.py
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ def _triangles(G, nodes, symmetrize=None):


@not_implemented_for("directed")
@networkx_algorithm(plc="triangle_count", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="triangle_count")
def triangles(G, nodes=None):
G = _to_undirected_graph(G)
node_ids, triangles, is_single_node = _triangles(G, nodes)
@@ -57,9 +57,13 @@ def triangles(G, nodes=None):


@not_implemented_for("directed")
@networkx_algorithm(is_incomplete=True, plc="triangle_count", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="triangle_count")
def clustering(G, nodes=None, weight=None):
"""Directed graphs and `weight` parameter are not yet supported."""
if weight is not None:
raise NotImplementedError(
"Weighted implementation of clustering not currently supported"
)
G = _to_undirected_graph(G)
node_ids, triangles, is_single_node = _triangles(G, nodes)
if len(G) == 0:
@@ -83,9 +87,13 @@ def _(G, nodes=None, weight=None):


@not_implemented_for("directed")
@networkx_algorithm(is_incomplete=True, plc="triangle_count", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="triangle_count")
def average_clustering(G, nodes=None, weight=None, count_zeros=True):
"""Directed graphs and `weight` parameter are not yet supported."""
if weight is not None:
raise NotImplementedError(
"Weighted implementation of average_clustering not currently supported"
)
G = _to_undirected_graph(G)
node_ids, triangles, is_single_node = _triangles(G, nodes)
if len(G) == 0:
@@ -110,7 +118,7 @@ def _(G, nodes=None, weight=None, count_zeros=True):


@not_implemented_for("directed")
@networkx_algorithm(is_incomplete=True, plc="triangle_count", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="triangle_count")
def transitivity(G):
"""Directed graphs are not yet supported."""
G = _to_undirected_graph(G)
Original file line number Diff line number Diff line change
@@ -36,8 +36,8 @@
},
is_incomplete=True, # seed not supported; self-loops not supported
is_different=True, # RNG different
plc="louvain",
version_added="23.10",
_plc="louvain",
)
def louvain_communities(
G,
13 changes: 5 additions & 8 deletions python/nx-cugraph/nx_cugraph/algorithms/components/connected.py
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@


@not_implemented_for("directed")
@networkx_algorithm(plc="weakly_connected_components", version_added="23.12")
@networkx_algorithm(version_added="23.12", _plc="weakly_connected_components")
def number_connected_components(G):
G = _to_undirected_graph(G)
return _number_connected_components(G)
@@ -50,14 +50,11 @@ def _number_connected_components(G, symmetrize=None):
@number_connected_components._can_run
def _(G):
# NetworkX <= 3.2.1 does not check directedness for us
try:
return not G.is_directed()
except Exception:
return False
return not G.is_directed()


@not_implemented_for("directed")
@networkx_algorithm(plc="weakly_connected_components", version_added="23.12")
@networkx_algorithm(version_added="23.12", _plc="weakly_connected_components")
def connected_components(G):
G = _to_undirected_graph(G)
return _connected_components(G)
@@ -80,7 +77,7 @@ def _connected_components(G, symmetrize=None):


@not_implemented_for("directed")
@networkx_algorithm(plc="weakly_connected_components", version_added="23.12")
@networkx_algorithm(version_added="23.12", _plc="weakly_connected_components")
def is_connected(G):
G = _to_undirected_graph(G)
return _is_connected(G)
@@ -106,7 +103,7 @@ def _is_connected(G, symmetrize=None):


@not_implemented_for("directed")
@networkx_algorithm(plc="weakly_connected_components", version_added="23.12")
@networkx_algorithm(version_added="23.12", _plc="weakly_connected_components")
def node_connected_component(G, n):
# We could also do plain BFS from n
G = _to_undirected_graph(G)
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ def _strongly_connected_components(G):


@not_implemented_for("undirected")
@networkx_algorithm(version_added="24.02", plc="strongly_connected_components")
@networkx_algorithm(version_added="24.02", _plc="strongly_connected_components")
def strongly_connected_components(G):
G = _to_directed_graph(G)
if G.src_indices.size == 0:
@@ -62,7 +62,7 @@ def strongly_connected_components(G):


@not_implemented_for("undirected")
@networkx_algorithm(version_added="24.02", plc="strongly_connected_components")
@networkx_algorithm(version_added="24.02", _plc="strongly_connected_components")
def number_strongly_connected_components(G):
G = _to_directed_graph(G)
if G.src_indices.size == 0:
@@ -72,7 +72,7 @@ def number_strongly_connected_components(G):


@not_implemented_for("undirected")
@networkx_algorithm(version_added="24.02", plc="strongly_connected_components")
@networkx_algorithm(version_added="24.02", _plc="strongly_connected_components")
def is_strongly_connected(G):
G = _to_directed_graph(G)
if len(G) == 0:
Original file line number Diff line number Diff line change
@@ -27,21 +27,21 @@


@not_implemented_for("undirected")
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def weakly_connected_components(G):
G = _to_directed_graph(G)
return _connected_components(G, symmetrize="union")


@not_implemented_for("undirected")
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def number_weakly_connected_components(G):
G = _to_directed_graph(G)
return _number_connected_components(G, symmetrize="union")


@not_implemented_for("undirected")
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def is_weakly_connected(G):
G = _to_directed_graph(G)
return _is_connected(G, symmetrize="union")
4 changes: 2 additions & 2 deletions python/nx-cugraph/nx_cugraph/algorithms/core.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@

@not_implemented_for("directed")
@not_implemented_for("multigraph")
@networkx_algorithm(is_incomplete=True, plc="core_number", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="core_number")
def core_number(G):
"""Directed graphs are not yet supported."""
G = _to_undirected_graph(G)
@@ -55,7 +55,7 @@ def _(G):

@not_implemented_for("directed")
@not_implemented_for("multigraph")
@networkx_algorithm(is_incomplete=True, plc="k_truss_subgraph", version_added="23.12")
@networkx_algorithm(is_incomplete=True, version_added="23.12", _plc="k_truss_subgraph")
def k_truss(G, k):
"""
Currently raises `NotImplementedError` for graphs with more than one connected
4 changes: 2 additions & 2 deletions python/nx-cugraph/nx_cugraph/algorithms/dag.py
Original file line number Diff line number Diff line change
@@ -45,11 +45,11 @@ def _ancestors_and_descendants(G, source, *, is_ancestors):
return G._nodearray_to_set(node_ids[mask])


@networkx_algorithm(plc="bfs", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="bfs")
def descendants(G, source):
return _ancestors_and_descendants(G, source, is_ancestors=False)


@networkx_algorithm(plc="bfs", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="bfs")
def ancestors(G, source):
return _ancestors_and_descendants(G, source, is_ancestors=True)
Original file line number Diff line number Diff line change
@@ -33,8 +33,8 @@
),
**_dtype_param,
},
plc="hits",
version_added="23.12",
_plc="hits",
)
def hits(
G,
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@
@networkx_algorithm(
extra_params=_dtype_param,
is_incomplete=True, # dangling not supported
plc={"pagerank", "personalized_pagerank"},
version_added="23.12",
_plc={"pagerank", "personalized_pagerank"},
)
def pagerank(
G,
Original file line number Diff line number Diff line change
@@ -21,12 +21,12 @@
__all__ = ["single_source_shortest_path_length", "single_target_shortest_path_length"]


@networkx_algorithm(plc="bfs", version_added="23.12")
@networkx_algorithm(version_added="23.12", _plc="bfs")
def single_source_shortest_path_length(G, source, cutoff=None):
return _single_shortest_path_length(G, source, cutoff, "Source")


@networkx_algorithm(plc="bfs", version_added="23.12")
@networkx_algorithm(version_added="23.12", _plc="bfs")
def single_target_shortest_path_length(G, target, cutoff=None):
return _single_shortest_path_length(G, target, cutoff, "Target")

Original file line number Diff line number Diff line change
@@ -57,9 +57,17 @@ def _bfs(G, source, *, depth_limit=None, reverse=False):
return distances[mask], predecessors[mask], node_ids[mask]


@networkx_algorithm(is_incomplete=True, plc="bfs", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="bfs")
def generic_bfs_edges(G, source, neighbors=None, depth_limit=None, sort_neighbors=None):
"""`neighbors` and `sort_neighbors` parameters are not yet supported."""
if neighbors is not None:
raise NotImplementedError(
"neighbors argument in generic_bfs_edges is not currently supported"
)
if sort_neighbors is not None:
raise NotImplementedError(
"sort_neighbors argument in generic_bfs_edges is not currently supported"
)
return bfs_edges(source, depth_limit=depth_limit)


@@ -68,9 +76,13 @@ def _(G, source, neighbors=None, depth_limit=None, sort_neighbors=None):
return neighbors is None and sort_neighbors is None


@networkx_algorithm(is_incomplete=True, plc="bfs", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="bfs")
def bfs_edges(G, source, reverse=False, depth_limit=None, sort_neighbors=None):
"""`sort_neighbors` parameter is not yet supported."""
if sort_neighbors is not None:
raise NotImplementedError(
"sort_neighbors argument in bfs_edges is not currently supported"
)
G = _check_G_and_source(G, source)
if depth_limit is not None and depth_limit < 1:
return
@@ -95,9 +107,13 @@ def _(G, source, reverse=False, depth_limit=None, sort_neighbors=None):
return sort_neighbors is None


@networkx_algorithm(is_incomplete=True, plc="bfs", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="bfs")
def bfs_tree(G, source, reverse=False, depth_limit=None, sort_neighbors=None):
"""`sort_neighbors` parameter is not yet supported."""
if sort_neighbors is not None:
raise NotImplementedError(
"sort_neighbors argument in bfs_tree is not currently supported"
)
G = _check_G_and_source(G, source)
if depth_limit is not None and depth_limit < 1:
return nxcg.DiGraph.from_coo(
@@ -149,9 +165,13 @@ def _(G, source, reverse=False, depth_limit=None, sort_neighbors=None):
return sort_neighbors is None


@networkx_algorithm(is_incomplete=True, plc="bfs", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="bfs")
def bfs_successors(G, source, depth_limit=None, sort_neighbors=None):
"""`sort_neighbors` parameter is not yet supported."""
if sort_neighbors is not None:
raise NotImplementedError(
"sort_neighbors argument in bfs_successors is not currently supported"
)
G = _check_G_and_source(G, source)
if depth_limit is not None and depth_limit < 1:
yield (source, [])
@@ -173,7 +193,7 @@ def _(G, source, depth_limit=None, sort_neighbors=None):
return sort_neighbors is None


@networkx_algorithm(plc="bfs", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="bfs")
def bfs_layers(G, sources):
G = _to_graph(G)
if sources in G:
@@ -201,9 +221,13 @@ def bfs_layers(G, sources):
return (G._nodearray_to_list(groups[key]) for key in range(len(groups)))


@networkx_algorithm(is_incomplete=True, plc="bfs", version_added="24.02")
@networkx_algorithm(is_incomplete=True, version_added="24.02", _plc="bfs")
def bfs_predecessors(G, source, depth_limit=None, sort_neighbors=None):
"""`sort_neighbors` parameter is not yet supported."""
if sort_neighbors is not None:
raise NotImplementedError(
"sort_neighbors argument in bfs_predecessors is not currently supported"
)
G = _check_G_and_source(G, source)
if depth_limit is not None and depth_limit < 1:
return
@@ -227,7 +251,7 @@ def _(G, source, depth_limit=None, sort_neighbors=None):
return sort_neighbors is None


@networkx_algorithm(plc="bfs", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="bfs")
def descendants_at_distance(G, source, distance):
G = _check_G_and_source(G, source)
if distance is None or distance < 0:
8 changes: 4 additions & 4 deletions python/nx-cugraph/nx_cugraph/algorithms/tree/recognition.py
Original file line number Diff line number Diff line change
@@ -21,20 +21,20 @@


@not_implemented_for("undirected")
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def is_arborescence(G):
G = _to_directed_graph(G)
return is_tree(G) and int(G._in_degrees_array().max()) <= 1


@not_implemented_for("undirected")
@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def is_branching(G):
G = _to_directed_graph(G)
return is_forest(G) and int(G._in_degrees_array().max()) <= 1


@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def is_forest(G):
G = _to_graph(G)
if len(G) == 0:
@@ -60,7 +60,7 @@ def is_forest(G):
return True


@networkx_algorithm(plc="weakly_connected_components", version_added="24.02")
@networkx_algorithm(version_added="24.02", _plc="weakly_connected_components")
def is_tree(G):
G = _to_graph(G)
if len(G) == 0:
16 changes: 9 additions & 7 deletions python/nx-cugraph/nx_cugraph/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -59,18 +59,18 @@ def __new__(
version_added: str, # Required
is_incomplete: bool = False, # See self.extra_doc for details if True
is_different: bool = False, # See self.extra_doc for details if True
plc: str | set[str] | None = None, # Hidden from user, may be removed someday
_plc: str | set[str] | None = None, # Hidden from user, may be removed someday
):
if func is None:
return partial(
networkx_algorithm,
name=name,
extra_params=extra_params,
nodes_or_number=nodes_or_number,
plc=plc,
version_added=version_added,
is_incomplete=is_incomplete,
is_different=is_different,
_plc=_plc,
)
instance = object.__new__(cls)
if nodes_or_number is not None and nx.__version__[:3] > "3.2":
@@ -89,12 +89,14 @@ def __new__(
f"extra_params must be dict, str, or None; got {type(extra_params)}"
)
instance.extra_params = extra_params
if plc is None or isinstance(plc, set):
instance._plc_names = plc
elif isinstance(plc, str):
instance._plc_names = {plc}
if _plc is None or isinstance(_plc, set):
instance._plc_names = _plc
elif isinstance(_plc, str):
instance._plc_names = {_plc}
else:
raise TypeError(f"plc argument must be str, set, or None; got {type(plc)}")
raise TypeError(
f"_plc argument must be str, set, or None; got {type(_plc)}"
)
instance.version_added = version_added
instance.is_incomplete = is_incomplete
instance.is_different = is_different

0 comments on commit 82552ab

Please sign in to comment.