import hashlib
import unittest
from colicoords.cell import Cell, CellList
from colicoords.preprocess import data_to_cells
from test import testcase
from test.test_functions import load_testdata


class DataTest(testcase.ArrayTestCase):
    def setUp(self):
        self.data = load_testdata('ds1')

    def test_data_slicing(self):
        sl1 = self.data[2:5, :, :]
        self.assertEqual(sl1.shape, (3, 512, 512))

        sl2 = self.data[:, 20:40, 100:200]
        self.assertEqual(sl2.shape, (10, 20, 100))

    def test_data_copy(self):
        m0 = self.data.binary_img.mean()
        data_copy = self.data.copy()
        self.assertEqual(m0, self.data.binary_img.mean())
        data_copy.data_dict['binary'] += 20

        self.assertEqual(m0, self.data.binary_img.mean())
        self.assertEqual(data_copy.binary_img.mean(), m0 + 20)

    def _test_cell_list(self):
        #todo check order
        print(hashlib.md5(self.data).hexdigest())
        cell_list = data_to_cells(self.data, initial_crop=2, cell_frac=0.5, rotate='binary')
        print(hashlib.md5(self.data).hexdigest())
        cell_list = data_to_cells(self.data, initial_crop=2, cell_frac=0.5, rotate='binary')
        print(hashlib.md5(self.data).hexdigest())

        d = self.data.copy()
        print(d == self.data)

        cl = CellList(cell_list)
        self.assertEqual(len(cl), 48)
        c5 = cl[5]
        self.assertIsInstance(c5, Cell)

        del cl[5]
        self.assertEqual(len(cl), 47)
        self.assertTrue(cl[3] in cl)
        cl.append(c5)
        self.assertTrue(c5 in cl)

        vol = cl.volume
        self.assertEqual(len(vol), 48)


class CellListTest(testcase.ArrayTestCase):
    def setUp(self):
        data = load_testdata('ds1')
        self.cell_list = data_to_cells(data)

    def test_slicing(self):
        sliced = self.cell_list[:5]
        self.assertIsInstance(sliced, CellList)


if __name__ == '__main__':
    unittest.main()
[((66, 4, 66, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((11, 20, 11, 40), 'test.test_functions.load_testdata', 'load_testdata', ({(11, 34, 11, 39): '"""ds1"""'}, {}), "('ds1')", False, 'from test.test_functions import load_testdata\n'), ((32, 20, 32, 92), 'colicoords.preprocess.data_to_cells', 'data_to_cells', (), '', False, 'from colicoords.preprocess import data_to_cells\n'), ((34, 20, 34, 92), 'colicoords.preprocess.data_to_cells', 'data_to_cells', (), '', False, 'from colicoords.preprocess import data_to_cells\n'), ((40, 13, 40, 32), 'colicoords.cell.CellList', 'CellList', ({(40, 22, 40, 31): 'cell_list'}, {}), '(cell_list)', False, 'from colicoords.cell import Cell, CellList\n'), ((57, 15, 57, 35), 'test.test_functions.load_testdata', 'load_testdata', ({(57, 29, 57, 34): '"""ds1"""'}, {}), "('ds1')", False, 'from test.test_functions import load_testdata\n'), ((58, 25, 58, 44), 'colicoords.preprocess.data_to_cells', 'data_to_cells', ({(58, 39, 58, 43): 'data'}, {}), '(data)', False, 'from colicoords.preprocess import data_to_cells\n'), ((31, 14, 31, 36), 'hashlib.md5', 'hashlib.md5', ({(31, 26, 31, 35): 'self.data'}, {}), '(self.data)', False, 'import hashlib\n'), ((33, 14, 33, 36), 'hashlib.md5', 'hashlib.md5', ({(33, 26, 33, 35): 'self.data'}, {}), '(self.data)', False, 'import hashlib\n'), ((35, 14, 35, 36), 'hashlib.md5', 'hashlib.md5', ({(35, 26, 35, 35): 'self.data'}, {}), '(self.data)', False, 'import hashlib\n')]