From 5f245c683d1d0195a2245fd09d066b9547f6d2cd Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Fri, 22 Mar 2024 06:22:49 +0100 Subject: [PATCH] Tests that use random arrays now call a single function to ensure integer type --- tests/test_accessors.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/test_accessors.py b/tests/test_accessors.py index bc5b4065..fc94c6fb 100644 --- a/tests/test_accessors.py +++ b/tests/test_accessors.py @@ -7,6 +7,9 @@ from model_server.base.accessors import PatchStack, make_patch_stack_from_file, from model_server.conf.testing import czifile, output_path, monopngfile, rgbpngfile, tifffile, monozstackmask from model_server.base.accessors import CziImageFileAccessor, DataShapeError, generate_file_accessor, InMemoryDataAccessor, PngFileAccessor, write_accessor_data_to_file, TifSingleSeriesFileAccessor +def _random_int(*args): + return np.random.randint(0, 2 ** 8, size=args, dtype='uint8') + class TestCziImageFileAccess(unittest.TestCase): def setUp(self) -> None: @@ -40,7 +43,7 @@ class TestCziImageFileAccess(unittest.TestCase): nc = 4 nz = 11 c = 3 - cf = InMemoryDataAccessor(np.random.rand(h, w, nc, nz)) + cf = InMemoryDataAccessor(_random_int(h, w, nc, nz)) sc = cf.get_one_channel_data(c) self.assertEqual(sc.shape, (h, w, 1, nz)) @@ -70,7 +73,7 @@ class TestCziImageFileAccess(unittest.TestCase): def test_conform_data_shorter_than_xycz(self): h = 256 w = 512 - data = np.random.rand(h, w, 1) + data = _random_int(h, w, 1) acc = InMemoryDataAccessor(data) self.assertEqual( InMemoryDataAccessor.conform_data(data).shape, @@ -82,7 +85,7 @@ class TestCziImageFileAccess(unittest.TestCase): ) def test_conform_data_longer_than_xycz(self): - data = np.random.rand(256, 512, 12, 8, 3) + data = _random_int(256, 512, 12, 8, 3) with self.assertRaises(DataShapeError): acc = InMemoryDataAccessor(data) @@ -93,7 +96,7 @@ class TestCziImageFileAccess(unittest.TestCase): c = 3 nz = 10 - yxcz = (2**8 * np.random.rand(h, w, c, nz)).astype('uint8') + yxcz = _random_int(h, w, c, nz) acc = InMemoryDataAccessor(yxcz) fp = output_path / f'rand3d.tif' self.assertTrue( @@ -138,7 +141,7 @@ class TestPatchStackAccessor(unittest.TestCase): w = 256 h = 512 n = 4 - acc = PatchStack(np.random.rand(n, h, w, 1, 1)) + acc = PatchStack(_random_int(n, h, w, 1, 1)) self.assertEqual(acc.count, n) self.assertEqual(acc.hw, (h, w)) self.assertEqual(acc.pyxcz.shape, (n, h, w, 1, 1)) @@ -147,7 +150,7 @@ class TestPatchStackAccessor(unittest.TestCase): w = 256 h = 512 n = 4 - acc = PatchStack([np.random.rand(h, w, 1, 1) for _ in range(0, n)]) + acc = PatchStack([_random_int(h, w, 1, 1) for _ in range(0, n)]) self.assertEqual(acc.count, n) self.assertEqual(acc.hw, (h, w)) self.assertEqual(acc.pyxcz.shape, (n, h, w, 1, 1)) @@ -176,8 +179,8 @@ class TestPatchStackAccessor(unittest.TestCase): nz = 5 n = 4 - patches = [np.random.rand(h, w, c, nz) for _ in range(0, n)] - patches.append(np.random.rand(h, 2 * w, c, nz)) + patches = [_random_int(h, w, c, nz) for _ in range(0, n)] + patches.append(_random_int(h, 2 * w, c, nz)) acc = PatchStack(patches) self.assertEqual(acc.count, n + 1) self.assertEqual(acc.hw, (h, 2 * w)) @@ -191,7 +194,8 @@ class TestPatchStackAccessor(unittest.TestCase): n = 4 nz = 15 nc = 2 - acc = PatchStack(np.random.rand(n, h, w, nc, nz)) + acc = PatchStack(_random_int(n, h, w, nc, nz)) self.assertEqual(acc.count, n) self.assertEqual(acc.pczyx.shape, (n, nc, nz, h, w)) self.assertEqual(acc.hw, (h, w)) + return acc \ No newline at end of file -- GitLab