-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX]: fixed readme (added some tests for HC)
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |