Skip to content

Commit

Permalink
add python tests for Leiden numbering
Browse files Browse the repository at this point in the history
  • Loading branch information
jnke2016 committed Jan 10, 2025
1 parent c9e2781 commit c9945cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 20 additions & 1 deletion python/cugraph/cugraph/tests/community/test_leiden.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2024, NVIDIA CORPORATION.
# Copyright (c) 2019-2025, 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
Expand All @@ -19,6 +19,7 @@

import cugraph
import cudf
from cudf.testing.testing import assert_series_equal
from cugraph.testing import utils, UNDIRECTED_DATASETS
from cugraph.datasets import karate_asymmetric

Expand Down Expand Up @@ -185,6 +186,15 @@ def test_leiden(graph_file):
leiden_parts, leiden_mod = cugraph_leiden(G)
louvain_parts, louvain_mod = cugraph_louvain(G)

unique_parts = leiden_parts["partition"].drop_duplicates().sort_values(
ascending=True).reset_index(drop=True)

idx_col = cudf.Series(unique_parts.index)

# Ensure Leiden cluster's ID are numbered consecutively
assert_series_equal(
unique_parts, idx_col, check_dtype=False, check_names=False)

# Leiden modularity score is smaller than Louvain's
assert leiden_mod >= (0.75 * louvain_mod)

Expand All @@ -202,6 +212,15 @@ def test_leiden_nx(graph_file):
leiden_parts, leiden_mod = cugraph_leiden(G)
louvain_parts, louvain_mod = cugraph_louvain(G)

unique_parts = cudf.Series(leiden_parts.values()).drop_duplicates().sort_values(
ascending=True).reset_index(drop=True)

idx_col = cudf.Series(unique_parts.index)

# Ensure Leiden cluster's ID are numbered consecutively
assert_series_equal(
unique_parts, idx_col, check_dtype=False, check_names=False)

# Calculating modularity scores for comparison
# Leiden modularity score is smaller than Louvain's
assert leiden_mod >= (0.75 * louvain_mod)
Expand Down
13 changes: 12 additions & 1 deletion python/cugraph/cugraph/tests/community/test_leiden_mg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
# Copyright (c) 2020-2025, 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
Expand All @@ -16,6 +16,8 @@
import cugraph
import cugraph.dask as dcg
from cugraph.datasets import karate_asymmetric, karate, dolphins
import cudf
from cudf.testing.testing import assert_series_equal


# =============================================================================
Expand Down Expand Up @@ -64,6 +66,15 @@ def test_mg_leiden_with_edgevals_undirected_graph(dask_client, dataset):
dg = get_mg_graph(dataset, directed=False)
parts, mod = dcg.leiden(dg)

unique_parts = parts["partition"].compute().drop_duplicates().sort_values(
ascending=True).reset_index(drop=True)

idx_col = cudf.Series(unique_parts.index)

# Ensure Leiden cluster's ID are numbered consecutively
assert_series_equal(
unique_parts, idx_col, check_dtype=False, check_names=False)

# FIXME: either call Nx with the same dataset and compare results, or
# hardcode golden results to compare to.
print()
Expand Down

0 comments on commit c9945cd

Please sign in to comment.