Skip to content

Commit

Permalink
[FIX]: fixed readme (added some tests for HC)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZISST committed Jan 10, 2025
1 parent 425306e commit dacf202
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
Empty file added hc/tests/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions hc/tests/test_autoClustering.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions hc/tests/test_clustering.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit dacf202

Please sign in to comment.