Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kostrykin committed Jul 16, 2024
1 parent ffafc3b commit 473c882
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest

import giatools.io


Expand Down
33 changes: 17 additions & 16 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import itertools
import unittest

import numpy as np
import skimage.util

import giatools.util


Expand All @@ -9,28 +12,26 @@ class convert_image_to_format_of(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
np.random.seed(0)
img_float = np.random.rand(16, 16)
self.testdata = {
'uint8': skimage.util.img_as_ubyte(img_float),
'uint16': skimage.util.img_as_uint(img_float),
'int16': skimage.util.img_as_int(img_float),
'float32': skimage.util.img_as_float32(img_float),
'float64': skimage.util.img_as_float64(img_float),
}
img_uint8 = (np.random.rand(16, 16) * 255).round().astype(np.uint8)
self.testdata = [
skimage.util.img_as_ubyte(img_uint8), # uint8
skimage.util.img_as_uint(img_uint8), # uint16
skimage.util.img_as_int(img_uint8), # int16
skimage.util.img_as_float32(img_uint8), # float32
skimage.util.img_as_float64(img_uint8), # float64
]

def test_self_conversion(self):
for dtype, img in self.testdata.items():
for img in self.testdata:
actual = giatools.util.convert_image_to_format_of(img, img)
self.assertIs(actual, img)
self.assertEqual(actual.dtype, getattr(np, dtype))

def test_cross_conversion(self):
for src_dtype, src_img in self.testdata.items():
for dst_img in self.testdata.values():
with self.subTest(src_dtype=src_dtype, dst_dtype=dst_img.dtype):
actual = giatools.util.convert_image_to_format_of(src_img, dst_img)
self.assertTrue(np.allclose(actual, dst_img))
self.assertEqual(actual.dtype, dst_img.dtype)
for src_img, dst_img in itertools.product(self.testdata, self.testdata):
with self.subTest(f'{src_img.dtype} -> {dst_img.dtype}'):
actual = giatools.util.convert_image_to_format_of(src_img, dst_img)
self.assertEqual(actual.dtype, dst_img.dtype)
self.assertTrue(np.allclose(actual, dst_img, rtol=1e-2))


if __name__ == '__main__':
Expand Down

0 comments on commit 473c882

Please sign in to comment.