Skip to content

Commit

Permalink
some first test
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanklut committed Dec 8, 2023
1 parent df1d5bc commit f46b287
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ tutorial/*results*/

# Transcibus
doc.xml

# Tests
tests/.tmp*
69 changes: 69 additions & 0 deletions tests/test_image_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import tempfile
import unittest
import uuid
from pathlib import Path

import numpy as np

from utils.image_utils import (
load_image_array_from_bytes,
load_image_array_from_path,
save_image_array_to_path,
)


class TestImageUtils(unittest.TestCase):
def setUp(self):
# Tempdir for working directory of tests
self._tmp_dir = tempfile.TemporaryDirectory(
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"

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.shape[-1], 3)
self.assertGreaterEqual(image.min(), 0)
self.assertLessEqual(image.max(), 255)
self.assertIsInstance(image[0, 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.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):
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):
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):
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)

def test_save_load_grayscale(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)

0 comments on commit f46b287

Please sign in to comment.