Skip to content

Commit

Permalink
Add unit tests for calculations and utils modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cobanov committed Mar 11, 2024
1 parent 6421a11 commit 08b715a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 26 deletions.
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from setuptools import setup, find_packages

from setuptools import find_packages, setup

setup(
name="tasnif",
Expand Down
10 changes: 3 additions & 7 deletions tasnif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
__url__ = "https://github.com/cobanov/tasnif"


from tasnif.calculations import calculate_kmeans, calculate_pca, get_embeddings
from tasnif.tasnif import Tasnif
from tasnif.calculations import get_embeddings, calculate_pca, calculate_kmeans
from tasnif.utils import (
read_images_from_directory,
read_with_pil,
create_dir,
create_image_grid,
)
from tasnif.utils import (create_dir, create_image_grid,
read_images_from_directory, read_with_pil)
10 changes: 3 additions & 7 deletions tasnif/tasnif.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@

from tqdm import tqdm

from .calculations import get_embeddings, calculate_pca, calculate_kmeans
from .utils import (
read_images_from_directory,
read_with_pil,
create_dir,
create_image_grid,
)
from .calculations import calculate_kmeans, calculate_pca, get_embeddings
from .utils import (create_dir, create_image_grid, read_images_from_directory,
read_with_pil)

warnings.filterwarnings("ignore")

Expand Down
12 changes: 5 additions & 7 deletions tests/test_calculations.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import numpy as np
from tasnif.calculations import get_embeddings, calculate_pca, calculate_kmeans
from tasnif.utils import read_with_pil, read_images_from_directory

from tasnif.calculations import calculate_kmeans, calculate_pca, get_embeddings
from tasnif.utils import read_images_from_directory, read_with_pil


def test_get_embeddings():
# Assuming "test_images" contains at least one image for embedding generation
image_paths = read_images_from_directory("tests/test_images")
images = read_with_pil(image_paths)
embeddings = get_embeddings(images=images, use_gpu=False)
assert embeddings is not None, "Embeddings were not generated."


def test_calculate_pca():
# Generate some random data for PCA
embeddings = np.random.rand(20, 2048) # Simulate 10 embeddings of dimension 2048
embeddings = np.random.rand(20, 2048)
pca_embeddings = calculate_pca(embeddings, pca_dim=16)
assert pca_embeddings.shape[1] == 16, "PCA did not reduce to the correct dimension."


def test_calculate_kmeans():
# Use PCA embeddings from the previous test or generate new random data
pca_embeddings = np.random.rand(10, 16) # Simulate 10 samples in 16 dimensions
pca_embeddings = np.random.rand(10, 16)
num_classes = 2
centroid, labels, counts = calculate_kmeans(pca_embeddings, num_classes)
assert (
Expand Down
3 changes: 0 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import os
from tasnif.utils import read_images_from_directory, read_with_pil


def test_read_images_from_directory():
# Assuming you have a directory named "test_images" with some image files
images = read_images_from_directory("tests/test_images")
assert len(images) > 0, "No images found in the directory."


def test_read_with_pil():
# Use the same directory as above or make sure it contains at least one image
image_paths = read_images_from_directory("tests/test_images")
images = read_with_pil(image_paths)
assert len(images) == len(image_paths), "Not all images were read successfully."

0 comments on commit 08b715a

Please sign in to comment.