Skip to content

Commit

Permalink
tests before rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanklut committed Dec 11, 2023
1 parent d89806d commit 89d5645
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 13 deletions.
83 changes: 73 additions & 10 deletions tests/test_image_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import sys
import tempfile
import unittest
import uuid
from pathlib import Path

import numpy as np

sys.path.append(str(Path(__file__).resolve().parent.joinpath("..")))
from utils.image_utils import (
load_image_array_from_bytes,
load_image_array_from_path,
Expand All @@ -19,51 +21,112 @@ def setUp(self):
dir=Path(__file__).resolve().parent, prefix=".tmp", suffix=self.__class__.__name__
)
self.tmp_dir = Path(self._tmp_dir.name)
self.data_dir = Path(__file__).parent.joinpath("tutorial", "data")
assert self.data_dir.is_dir(), "Missing tutorial data"
self.data_dir = Path(__file__).parents[1].joinpath("tutorial", "data")
assert self.data_dir.is_dir(), f"Missing tutorial data {self.data_dir}"

def test_load_color_from_path(self):
image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg")
image = load_image_array_from_path(image_path, mode="color")
self.assertIsInstance(image, np.ndarray)
self.assertEqual(image.ndim, 3)
self.assertEqual(image.shape[-1], 3)
self.assertGreaterEqual(image.min(), 0)
self.assertLessEqual(image.max(), 255)
self.assertIsInstance(image[0, 0, 0], np.uint8)

def test_load_grayscale_from_path(self):
image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg")
image = load_image_array_from_path(image_path, mode="grayscale")
self.assertIsInstance(image, np.ndarray)
self.assertEqual(image.ndim, 2)
self.assertGreaterEqual(image.min(), 0)
self.assertLessEqual(image.max(), 255)
self.assertIsInstance(image[0, 0], np.uint8)

def test_load_color_from_bytes(self):
image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg")
with image_path.open(mode="rb") as f:
image_bytes = f.read()
image = load_image_array_from_bytes(image_bytes, image_path, mode="color")
self.assertIsInstance(image, np.ndarray)
self.assertEqual(image.ndim, 3)
self.assertEqual(image.shape[-1], 3)
self.assertGreaterEqual(image.min(), 0)
self.assertLessEqual(image.max(), 255)
self.assertIsInstance(image[0, 0, 0], np.uint8)

def test_save_color(self):
def test_load_grayscale_from_bytes(self):
image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg")
with image_path.open(mode="rb") as f:
image_bytes = f.read()
image = load_image_array_from_bytes(image_bytes, image_path, mode="grayscale")
self.assertIsInstance(image, np.ndarray)
self.assertEqual(image.ndim, 2)
self.assertGreaterEqual(image.min(), 0)
self.assertLessEqual(image.max(), 255)
self.assertIsInstance(image[0, 0], np.uint8)

def test_save_color_jpg(self):
image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg")
save_image_array_to_path(image_path, image)
self.assertTrue(image_path.is_file())

def test_save_grayscale(self):
def test_save_grayscale_jpg(self):
image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg")
save_image_array_to_path(image_path, image)
self.assertTrue(image_path.is_file())

def test_save_load_color(self):
def test_save_load_color_jpg(self):
image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg")
save_image_array_to_path(image_path, image)
image2 = load_image_array_from_path(image_path)
self.assertEqual(image, image2)
image2 = load_image_array_from_path(image_path, mode="color")
self.assertIsNotNone(image2)
self.assertTrue(np.array_equal(image, image2, equal_nan=True))

def test_save_load_grayscale(self):
def test_save_load_grayscale_jpg(self):
image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg")
save_image_array_to_path(image_path, image)
image2 = load_image_array_from_path(image_path)
self.assertEqual(image, image2)
image2 = load_image_array_from_path(image_path, mode="grayscale")
self.assertIsNotNone(image2)
self.assertTrue(np.array_equal(image, image2, equal_nan=True))

def test_save_color_png(self):
image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png")
save_image_array_to_path(image_path, image)
self.assertTrue(image_path.is_file())

def test_save_grayscale_png(self):
image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png")
save_image_array_to_path(image_path, image)
self.assertTrue(image_path.is_file())

def test_save_load_color_png(self):
image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png")
save_image_array_to_path(image_path, image)
image2 = load_image_array_from_path(image_path, mode="color")
self.assertIsNotNone(image2)
self.assertTrue(np.array_equal(image, image2, equal_nan=True))

def test_save_load_grayscale_png(self):
image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8)
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png")
save_image_array_to_path(image_path, image)
image2 = load_image_array_from_path(image_path, mode="grayscale")
self.assertIsNotNone(image2)
self.assertTrue(np.array_equal(image, image2, equal_nan=True))

def test_corrupt_image(self):
image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg")
image = load_image_array_from_path(image_path)
self.assertIsNone(image)


if __name__ == "__main__":
unittest.main()
45 changes: 45 additions & 0 deletions tests/test_input_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys
import unittest
from pathlib import Path

sys.path.append(str(Path(__file__).resolve().parent.joinpath("..")))
from utils.input_utils import clean_input_paths


class TestInputUtils(unittest.TestCase):
def test_single_string_input(self):
input_path = "/path/to/single/file.jpg"
expected_output = [Path("/path/to/single/file.jpg")]
self.assertEqual(clean_input_paths(input_path), expected_output)

def test_single_path_input(self):
input_path = Path("/path/to/single/file.jpg")
expected_output = [Path("/path/to/single/file.jpg")]
self.assertEqual(clean_input_paths(input_path), expected_output)

def test_sequence_string_input(self):
input_paths = ["/path/to/file1.jpg", "/path/to/file2.jpg"]
expected_output = [Path("/path/to/file1.jpg"), Path("/path/to/file2.jpg")]
self.assertEqual(clean_input_paths(input_paths), expected_output)

def test_sequence_path_input(self):
input_paths = [Path("/path/to/file1.jpg"), Path("/path/to/file2.jpg")]
expected_output = [Path("/path/to/file1.jpg"), Path("/path/to/file2.jpg")]
self.assertEqual(clean_input_paths(input_paths), expected_output)

def test_empty_input(self):
with self.assertRaises(ValueError):
clean_input_paths("")

def test_invalid_input_type(self):
with self.assertRaises(TypeError):
clean_input_paths(123)

def test_mixed_type_input(self):
mixed_input = [Path("/path/to/file1.jpg"), "/path/to/file2.jpg", 123]
with self.assertRaises(TypeError):
clean_input_paths(mixed_input)


if __name__ == "__main__":
unittest.main()
6 changes: 3 additions & 3 deletions utils/input_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def clean_input_paths(
Raises:
ValueError: Must provide input path
NotImplementedError: given input paths are the wrong class
TypeError: given input paths are the wrong class
Returns:
list[Path]: output paths of images
Expand All @@ -53,9 +53,9 @@ def clean_input_paths(
elif isinstance(path, Path):
output.append(path)
else:
raise NotImplementedError
raise TypeError(f"Unsupported type: {type(path)}")
else:
raise NotImplementedError
raise TypeError(f"Unsupported type: {type(input_paths)}")

return output

Expand Down

0 comments on commit 89d5645

Please sign in to comment.