import numpy as np
import pathlib
import unittest

try:
    import pivtestdata as ptd
except ImportError:
    ptd = None

import pivimage

__this_dir__ = pathlib.Path(__file__).parent


class TestPivImages(unittest.TestCase):

    def setUp(self) -> None:
        if ptd is None:
            self.skipTest('pivtestdata is required for dataset-backed tests')
        pc_1B = ptd.piv_challenge.pc_1B
        sample_folder_pc_1B = pc_1B.download()
        self.filenames = sorted(sample_folder_pc_1B.glob('*.tif*'))

    def test_PIVImages(self):
        pivimgs = pivimage.PIVImages(self.filenames)
        self.assertEqual(len(pivimgs), len(self.filenames))
        self.assertEqual(pivimgs[0].filename, self.filenames[0])

    def test_background(self):
        pivimgs = pivimage.PIVImages(self.filenames)
        bg = pivimgs.compute_background(np.min)
        bg.rot180()


class TestPivImagesFromArray(unittest.TestCase):

    def test_from_array_list(self):
        arr_a = np.zeros((4, 4), dtype=np.uint8)
        arr_b = np.ones((4, 4), dtype=np.uint8)
        pivimgs = pivimage.PIVImages.from_array_list([arr_a, arr_b])

        self.assertEqual(len(pivimgs), 2)
        np.testing.assert_array_equal(pivimgs[0][:], arr_a)
        np.testing.assert_array_equal(pivimgs[1][:], arr_b)
