Skip to content

Commit

Permalink
Add error handling and input validation to calculate_kmeans function
Browse files Browse the repository at this point in the history
  • Loading branch information
cobanov committed Mar 11, 2024
1 parent 09e2a79 commit 04e8b28
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tasnif/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ def calculate_kmeans(pca_embeddings, num_classes):
labels and centroids.
"""
print("KMeans processing...")
centroid, labels = kmeans2(data=pca_embeddings, k=num_classes, minit="points")
counts = np.bincount(labels)
print("Kmeans done!")
return centroid, labels, counts
if not isinstance(pca_embeddings, np.ndarray):
raise ValueError("pca_embeddings must be a numpy array")

if num_classes > len(pca_embeddings):
raise ValueError(
"num_classes must be less than or equal to the number of samples in pca_embeddings"
)

try:
centroid, labels = kmeans2(data=pca_embeddings, k=num_classes, minit="points")
counts = np.bincount(labels)
return centroid, labels, counts
except Exception as e:
raise RuntimeError(f"An error occurred during KMeans processing: {e}")

0 comments on commit 04e8b28

Please sign in to comment.