diff --git a/hc/tests/__init__.py b/hc/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hc/tests/test_autoClustering.py b/hc/tests/test_autoClustering.py new file mode 100644 index 0000000..1f0e9b8 --- /dev/null +++ b/hc/tests/test_autoClustering.py @@ -0,0 +1,60 @@ +import pytest +import numpy as np +from hypergraph_clustering.clustering.auto_clustering import AutoClusterHypergraphClustering + + +@pytest.mark.parametrize("adjacency_matrix, max_clusters", [ + (np.array([ + [0, 1, 0, 0], + [1, 0, 1, 1], + [0, 1, 0, 1], + [0, 1, 1, 0], + ]), 3), + (np.array([ + [0, 1, 1], + [1, 0, 1], + [1, 1, 0], + ]), 2), +]) +def test_silhouette_score_calculation(adjacency_matrix, max_clusters): + clustering = AutoClusterHypergraphClustering(linkage="average", max_clusters=max_clusters, scoring="silhouette") + labels = clustering.fit(adjacency_matrix) + assert len(labels) == adjacency_matrix.shape[0] + assert clustering.best_n_clusters is not None + assert clustering.best_score is not None + + +@pytest.mark.parametrize("adjacency_matrix", [ + np.array([ + [0, 1, 0, 0], + [1, 0, 1, 1], + [0, 1, 0, 1], + [0, 1, 1, 0], + ]), +]) +def test_best_score_non_negative(adjacency_matrix): + clustering = AutoClusterHypergraphClustering(linkage="average", max_clusters=3, scoring="silhouette") + clustering.fit(adjacency_matrix) + assert clustering.best_score >= 0 + +def test_invalid_scoring_metric(): + adjacency_matrix = np.array([ + [0, 1, 0], + [1, 0, 1], + [0, 1, 0], + ]) + clustering = AutoClusterHypergraphClustering(linkage="average", max_clusters=5, scoring="invalid_metric") + with pytest.raises(ValueError, match=".*Неизвестная метрика оценки.*"): + clustering.fit(adjacency_matrix) + +@pytest.mark.parametrize("adjacency_matrix, scoring, expected_exception", [ + (np.array([ + [0, 1, 0], + [1, 0, 1], + [0, 1, 0], + ]), "unsupported_metric", ValueError), +]) +def test_invalid_scoring(adjacency_matrix, scoring, expected_exception): + clustering = AutoClusterHypergraphClustering(linkage="average", max_clusters=3, scoring=scoring) + with pytest.raises(expected_exception): + clustering.fit(adjacency_matrix) \ No newline at end of file diff --git a/hc/tests/test_clustering.py b/hc/tests/test_clustering.py new file mode 100644 index 0000000..20a74da --- /dev/null +++ b/hc/tests/test_clustering.py @@ -0,0 +1,36 @@ +import pytest +import numpy as np +from hypergraph_clustering.utils.graph_conversion import hypergraph_to_incidence_matrix, incidence_to_adjacency +from hypergraph_clustering.clustering.agglomerative import AgglomerativeHypergraphClustering + + +@pytest.mark.parametrize("hyperedges, expected_shape", [ + ([[0, 1, 2], [1, 2, 3], [3, 4]], (5, 3)), + ([[0, 1], [1, 2], [2, 3], [3, 4], [4, 0]], (5, 5)), +]) +def test_incidence_matrix_shape(hyperedges, expected_shape): + incidence_matrix = hypergraph_to_incidence_matrix(hyperedges) + assert incidence_matrix.shape == expected_shape + + +@pytest.mark.parametrize("hyperedges", [ + [[0, 1, 2], [1, 2, 3], [3, 4]], + [[0, 1], [2, 3], [4, 5]], +]) +def test_adjacency_matrix_symmetry(hyperedges): + incidence_matrix = hypergraph_to_incidence_matrix(hyperedges) + adjacency_matrix = incidence_to_adjacency(incidence_matrix) + assert np.allclose(adjacency_matrix, adjacency_matrix.T) + + +@pytest.mark.parametrize("hyperedges, n_clusters, expected_cluster_range", [ + ([[0, 1, 2], [1, 2, 3], [3, 4]], 2, {0, 1}), + ([[0, 1], [2, 3], [4, 5]], 3, {0, 1, 2}), +]) +def test_agglomerative_clustering_labels(hyperedges, n_clusters, expected_cluster_range): + incidence_matrix = hypergraph_to_incidence_matrix(hyperedges) + adjacency_matrix = incidence_to_adjacency(incidence_matrix) + clustering = AgglomerativeHypergraphClustering(n_clusters=n_clusters) + labels = clustering.fit(adjacency_matrix) + assert len(labels) == adjacency_matrix.shape[0] + assert set(labels).issubset(expected_cluster_range)