Skip to content
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

Add many graph generators to nx-cugraph #3954

Merged
merged 23 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions python/nx-cugraph/_nx_cugraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,67 @@
# "description": "TODO",
"functions": {
# BEGIN: functions
"barbell_graph",
"betweenness_centrality",
"bull_graph",
"caveman_graph",
"chvatal_graph",
"circular_ladder_graph",
"complete_bipartite_graph",
"complete_graph",
"complete_multipartite_graph",
"cubical_graph",
"cycle_graph",
"davis_southern_women_graph",
"degree_centrality",
"desargues_graph",
"diamond_graph",
"dodecahedral_graph",
"edge_betweenness_centrality",
"empty_graph",
"florentine_families_graph",
"from_pandas_edgelist",
"from_scipy_sparse_array",
"frucht_graph",
"heawood_graph",
"house_graph",
"house_x_graph",
"icosahedral_graph",
"in_degree_centrality",
"is_isolate",
"isolates",
"k_truss",
"karate_club_graph",
"krackhardt_kite_graph",
"ladder_graph",
"les_miserables_graph",
"lollipop_graph",
"louvain_communities",
"moebius_kantor_graph",
"null_graph",
"number_of_isolates",
"number_of_selfloops",
"octahedral_graph",
"out_degree_centrality",
"pappus_graph",
"path_graph",
"petersen_graph",
"sedgewick_maze_graph",
"star_graph",
"tetrahedral_graph",
"trivial_graph",
"truncated_cube_graph",
"truncated_tetrahedron_graph",
"turan_graph",
"tutte_graph",
"wheel_graph",
# END: functions
},
"extra_docstrings": {
# BEGIN: extra_docstrings
"betweenness_centrality": "`weight` parameter is not yet supported.",
"edge_betweenness_centrality": "`weight` parameter is not yet supported.",
"from_pandas_edgelist": "cudf.DataFrame inputs also supported.",
"louvain_communities": "`seed` parameter is currently ignored.",
# END: extra_docstrings
},
Expand Down
8 changes: 4 additions & 4 deletions python/nx-cugraph/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ repos:
- id: pyupgrade
args: [--py39-plus]
- repo: https://github.com/psf/black
rev: 23.10.0
rev: 23.10.1
rlratzel marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: black
# - id: black-jupyter
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.1
rev: v0.1.3
hooks:
- id: ruff
args: [--fix-only, --show-fixes]
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
args: ['--per-file-ignores=_nx_cugraph/__init__.py:E501'] # Why is this necessary?
args: ['--per-file-ignores=_nx_cugraph/__init__.py:E501', '--extend-ignore=SIM105'] # Why is this necessary?
additional_dependencies: &flake8_dependencies
# These versions need updated manually
- flake8==6.1.0
Expand All @@ -77,7 +77,7 @@ repos:
additional_dependencies: [tomli]
files: ^(nx_cugraph|docs)/
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.1
rev: v0.1.3
hooks:
- id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
8 changes: 4 additions & 4 deletions python/nx-cugraph/nx_cugraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
from . import convert
from .convert import *

# from . import convert_matrix
# from .convert_matrix import *
from . import convert_matrix
from .convert_matrix import *

# from . import generators
# from .generators import *
from . import generators
from .generators import *
Comment on lines -23 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Told you this would be coming soon ;)
#3848 (comment)


from . import algorithms
from .algorithms import *
Expand Down
3 changes: 2 additions & 1 deletion python/nx-cugraph/nx_cugraph/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# 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 . import centrality, community
from . import bipartite, centrality, community
from .bipartite import complete_bipartite_graph
from .centrality import *
from .core import *
from .isolate import *
13 changes: 13 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/bipartite/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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 .generators import *
62 changes: 62 additions & 0 deletions python/nx-cugraph/nx_cugraph/algorithms/bipartite/generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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 numbers import Integral

import cupy as cp
import networkx as nx
import numpy as np

from nx_cugraph.generators._utils import _create_using_class, _number_and_nodes
from nx_cugraph.utils import index_dtype, networkx_algorithm, nodes_or_number

__all__ = [
"complete_bipartite_graph",
]


@nodes_or_number([0, 1])
@networkx_algorithm
def complete_bipartite_graph(n1, n2, create_using=None):
graph_class, inplace = _create_using_class(create_using)
if graph_class.is_directed():
raise nx.NetworkXError("Directed Graph not supported")
rlratzel marked this conversation as resolved.
Show resolved Hide resolved
orig_n1, unused_nodes1 = n1
orig_n2, unused_nodes2 = n2
n1, nodes1 = _number_and_nodes(n1)
n2, nodes2 = _number_and_nodes(n2)
all_indices = cp.indices((n1, n2), dtype=index_dtype)
indices0 = all_indices[0].ravel()
indices1 = all_indices[1].ravel() + n1
del all_indices
src_indices = cp.hstack((indices0, indices1))
dst_indices = cp.hstack((indices1, indices0))
bipartite = cp.zeros(n1 + n2, np.int8)
bipartite[n1:] = 1
if isinstance(orig_n1, Integral) and isinstance(orig_n2, Integral):
nodes = None
else:
nodes = list(range(n1)) if nodes1 is None else nodes1
nodes.extend(range(n2) if nodes2 is None else nodes2)
if len(set(nodes)) != len(nodes):
raise nx.NetworkXError("Inputs n1 and n2 must contain distinct nodes")
G = graph_class.from_coo(
n1 + n2,
src_indices,
dst_indices,
node_values={"bipartite": bipartite},
id_to_key=nodes,
name=f"complete_bipartite_graph({orig_n1}, {orig_n2})",
)
if inplace:
return create_using._become(G)
return G
11 changes: 6 additions & 5 deletions python/nx-cugraph/nx_cugraph/algorithms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
# limitations under the License.
import cupy as cp
import networkx as nx
import numpy as np
import pylibcugraph as plc

import nx_cugraph as nxcg
from nx_cugraph.utils import networkx_algorithm, not_implemented_for
from nx_cugraph.utils import _get_int_dtype, networkx_algorithm, not_implemented_for

__all__ = ["k_truss"]

Expand Down Expand Up @@ -51,8 +50,8 @@ def k_truss(G, k):
edge_values = {key: val.copy() for key, val in G.edge_values.items()}
edge_masks = {key: val.copy() for key, val in G.edge_masks.items()}
else:
# int dtype for edge_indices would be preferred
edge_indices = cp.arange(G.src_indices.size, dtype=np.float64)
edge_dtype = _get_int_dtype(G.src_indices.size - 1)
edge_indices = cp.arange(G.src_indices.size, dtype=edge_dtype)
src_indices, dst_indices, edge_indices, _ = plc.k_truss_subgraph(
resource_handle=plc.ResourceHandle(),
graph=G._get_plc_graph(edge_array=edge_indices),
Expand All @@ -62,7 +61,9 @@ def k_truss(G, k):
# Renumber step 0: node indices
node_indices = cp.unique(cp.concatenate([src_indices, dst_indices]))
# Renumber step 1: edge values
edge_indices = edge_indices.astype(np.int64)
if edge_indices.dtype != edge_dtype:
# The returned edge_indices may have different dtype (and float)
edge_indices = edge_indices.astype(edge_dtype)
edge_values = {key: val[edge_indices] for key, val in G.edge_values.items()}
edge_masks = {key: val[edge_indices] for key, val in G.edge_masks.items()}
# Renumber step 2: edge indices
Expand Down
Loading
Loading