-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d639fe
commit a199c60
Showing
5 changed files
with
69 additions
and
26 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
semeio/workflows/spearman_correlation_job/cluster_analysis.py
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,27 @@ | ||
from scipy.cluster.hierarchy import fcluster, linkage | ||
from sklearn.cluster import KMeans | ||
|
||
|
||
def fcluster_analysis( | ||
correlation_matrix, | ||
t=1.0, | ||
criterion="inconsistent", | ||
depth=2, | ||
method="single", | ||
metric="euclidean", | ||
): | ||
a = linkage(correlation_matrix, method, metric) | ||
return fcluster(a, t, criterion=criterion, depth=depth) | ||
|
||
|
||
def kmeans_analysis( | ||
correlation_matrix, n_clusters, n_init=10, max_iter=300, random_state=0 | ||
): | ||
kmeans = KMeans( | ||
init="random", | ||
n_clusters=n_clusters, | ||
n_init=n_init, | ||
max_iter=max_iter, | ||
random_state=random_state, | ||
).fit(correlation_matrix) | ||
return kmeans.labels_ + 1 # Scikit clusters are 0-indexed while scipy is 1-indexed |
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
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
25 changes: 25 additions & 0 deletions
25
tests/jobs/misfit_preprocessor/unit/test_cluster_functions.py
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,25 @@ | ||
# pylint: disable=unbalanced-tuple-unpacking | ||
from semeio.workflows.spearman_correlation_job.cluster_analysis import ( | ||
kmeans_analysis, | ||
fcluster_analysis, | ||
) | ||
from sklearn.datasets import make_blobs | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"func, kwargs", | ||
((fcluster_analysis, {"criterion": "maxclust"}), (kmeans_analysis, {})), | ||
) | ||
def test_same_format(func, kwargs): | ||
# The goal of this test is not to test the actual clustering functions, | ||
# but rather their format. Scipy clusters (labels) are 1-indexed while | ||
# sklearn are 0-indexed. We therefore set up a very simple dataset with | ||
# clearly defined clusters so the result will be the same for all functions. | ||
features, true_labels = make_blobs( | ||
n_samples=200, centers=3, cluster_std=0.1, random_state=42 | ||
) | ||
cluster_result = func(features, 3, **kwargs) | ||
# The clusters are typically the same, but the labels vary so we perform the | ||
# simplest test, just checking that the desired labels are present. | ||
assert set(cluster_result) == {1, 2, 3} |