Skip to content

Commit fbd37f5

Browse files
committed
Update version number and fix formatting in calculations.py and logger.py
1 parent 1adc725 commit fbd37f5

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="tasnif",
5-
version="0.1.9",
5+
version="0.1.10",
66
install_requires=[
77
"numpy",
88
"scikit-learn",

tasnif/calculations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from img2vec_pytorch import Img2Vec
33
from scipy.cluster.vq import kmeans2
44
from sklearn.decomposition import PCA
5+
56
from .logger import info
67

78

@@ -67,7 +68,9 @@ def calculate_kmeans(pca_embeddings, num_classes, iter=10):
6768
)
6869

6970
try:
70-
centroid, labels = kmeans2(data=pca_embeddings, k=num_classes, minit="points", iter=iter)
71+
centroid, labels = kmeans2(
72+
data=pca_embeddings, k=num_classes, minit="points", iter=iter
73+
)
7174
counts = np.bincount(labels)
7275
info("KMeans calculated.")
7376
return centroid, labels, counts

tasnif/logger.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import logging
2+
23
from rich.logging import RichHandler
34

45
log_format = "%(asctime)s - %(levelname)s - %(message)s"
56
logging.basicConfig(
6-
level="INFO", format=log_format, datefmt="[%X]", handlers=[RichHandler(show_time=False, show_level=False)]
7+
level="INFO",
8+
format=log_format,
9+
datefmt="[%X]",
10+
handlers=[RichHandler(show_time=False, show_level=False)],
711
)
812

913

@@ -15,6 +19,6 @@ def error(msg):
1519
logging.error(msg)
1620

1721

18-
if __name__ == '__main__':
22+
if __name__ == "__main__":
1923
info("info message")
2024
error("error message")

tasnif/tasnif.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
import shutil
33
import warnings
44
from itertools import compress
5+
56
import numpy as np
67
from tqdm import tqdm
8+
79
from .calculations import calculate_kmeans, calculate_pca, get_embeddings
10+
from .logger import error, info
811
from .utils import (
912
create_dir,
1013
create_image_grid,
1114
read_images_from_directory,
1215
read_with_pil,
1316
)
14-
from .logger import info, error
1517

1618
warnings.filterwarnings("ignore")
1719

@@ -52,15 +54,19 @@ def calculate(self, pca=True, iter=10):
5254
"""
5355

5456
if not self.images:
55-
raise ValueError("The images list can not be empty. Please call the read method before calculating.")
57+
raise ValueError(
58+
"The images list can not be empty. Please call the read method before calculating."
59+
)
5660

5761
self.embeddings = get_embeddings(use_gpu=self.use_gpu, images=self.images)
5862
if pca:
5963
self.pca_embeddings = calculate_pca(self.embeddings, self.pca_dim)
60-
self.centroid, self.labels, self.counts = calculate_kmeans(self.pca_embeddings, self.num_classes, iter = iter)
64+
self.centroid, self.labels, self.counts = calculate_kmeans(
65+
self.pca_embeddings, self.num_classes, iter=iter
66+
)
6167
else:
6268
self.centroid, self.labels, self.counts = calculate_kmeans(
63-
self.embeddings, self.num_classes, iter = iter
69+
self.embeddings, self.num_classes, iter=iter
6470
)
6571

6672
def export(self, output_folder="./"):
@@ -76,7 +82,6 @@ def export(self, output_folder="./"):
7682
create_dir(project_path)
7783

7884
for label_number in tqdm(range(self.num_classes)):
79-
8085
label_mask = self.labels == label_number
8186
path_images = list(compress(self.image_paths, label_mask))
8287
target_directory = os.path.join(project_path, f"cluster_{label_number}")
@@ -106,8 +111,12 @@ def export_embeddings(self, output_folder="./"):
106111
"""
107112

108113
if self.embeddings is None:
109-
raise ValueError("Embeddings can not be empty. Please call the calculate method first.")
114+
raise ValueError(
115+
"Embeddings can not be empty. Please call the calculate method first."
116+
)
110117

111-
embeddings_path = os.path.join(output_folder, f"{self.project_name}_embeddings.npy")
118+
embeddings_path = os.path.join(
119+
output_folder, f"{self.project_name}_embeddings.npy"
120+
)
112121
np.save(embeddings_path, self.embeddings)
113122
info(f"Embeddings have been saved to {embeddings_path}")

tasnif/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import glob
22
import os
33
from pathlib import Path
4+
45
import matplotlib.pyplot as plt
56
from PIL import Image
67
from tqdm import tqdm
8+
79
from .logger import info
810

911

0 commit comments

Comments
 (0)