Skip to content
Snippets Groups Projects
test_accessors.py 2.41 KiB
import unittest

import numpy as np

from model_server.conf.testing import monozstackmask
from model_server.extensions.chaeo.accessors import MonoPatchStackFromFile
from base.accessors import PatchStack


class TestMultipositionCziImageFileAccess(unittest.TestCase):
    def setUp(self) -> None:
        pass

    def test_make_patch_stack_from_3d_array(self):
        w = 256
        h = 512
        n = 4
        acc = PatchStack(np.random.rand(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))

    def test_make_patch_stack_from_list(self):
        w = 256
        h = 512
        n = 4
        acc = PatchStack([np.random.rand(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))
        return acc


    def test_make_patch_stack_from_file(self):
        h = monozstackmask['h']
        w = monozstackmask['w']
        c = monozstackmask['c']
        n = monozstackmask['z']
        acc = MonoPatchStackFromFile(monozstackmask['path'])
        self.assertEqual(acc.hw, (h, w))
        self.assertEqual(acc.count, n)
        self.assertEqual(acc.make_tczyx().shape, (n, c, 1, h, w))
        self.assertEqual(acc.fpath, monozstackmask['path'])

    def test_raises_filenotfound(self):
        from model_server.extensions.chaeo.accessors import FileNotFoundError
        with self.assertRaises(FileNotFoundError):
            acc = MonoPatchStackFromFile('c:/fake/file/name.tif')

    def test_make_3d_patch_stack_from_nonuniform_list(self):
        w = 256
        h = 512
        c = 1
        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))
        acc = PatchStack(patches)
        self.assertEqual(acc.count, n + 1)
        self.assertEqual(acc.hw, (h, 2 * w))
        self.assertEqual(acc.chroma, c)
        self.assertEqual(acc.iat(0).shape, (h, 2 * w, c, nz))
        self.assertEqual(acc.iat_yxcz(0).shape, (h, 2 * w, c, nz))

    def test_pczyx(self):
        w = 256
        h = 512
        n = 4
        nz = 15
        nc = 2
        acc = PatchStack(np.random.rand(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))