def setUp(self): self.preprocessor = Processor( X_imgpath='./tests/data/valid/noise.png', y_dirpath='./tests/data/test/', border=1, rnd=rnd)
class TestProcessor(unittest.TestCase): def setUp(self): self.preprocessor = Processor( X_imgpath='./tests/data/valid/noise.png', y_dirpath='./tests/data/test/', border=1, rnd=rnd) def test_sliding_window(self): x1 = self.preprocessor.get_X_fast() x2 = self.preprocessor.get_X() y1 = self.preprocessor.get_y_fast() y2 = self.preprocessor.get_y() pass def test_get_random_patch(self): patch = self.preprocessor.get_random_patch() patch2 = self.preprocessor.get_random_patch() patch3 = self.preprocessor.get_random_patch() patch = patch[0].flatten() patch2 = patch2[0].flatten() patch3 = patch3[0].flatten() center_index = (len(patch) - 1) / 2 self.assertNotEqual(patch[center_index], patch2[center_index]) self.assertNotEqual(patch[center_index], patch3[center_index]) self.assertNotEqual(patch2[center_index], patch3[center_index]) def test_fully_random_consistency(self): self.preprocessor.load_images('./tests/data/train/gradient.png', './tests/data/train/') x, y = self.preprocessor.get_random_patch() x = x.flatten() center_index = (len(x) - 1) / 2 self.assertEqual(x[center_index], y) def test_get_fast_x_consistency(self): self.preprocessor.load_images('./tests/data/train/gradient.png', './tests/data/train/') x = self.preprocessor.get_X_fast()[:2] y = self.preprocessor.get_y_fast()[:2] center_index = (len(x[0]) - 1) / 2 self.assertEqual(x[0][center_index], y[0][0]) self.assertEqual(x[1][center_index], y[1][0]) def test_patchsize_according_bordersize(self): # The patch has to have (2*border+1)**2 pixels for border in [3, 4]: self.preprocessor.set_border(border) ds = self.preprocessor.get_dataset() self.assertEqual(len(ds[0][0]), (2*border+1)**2) def test_patches_amounth(self): shape = np.array(PIL.Image.open(self.preprocessor.X_imgpath)).shape num_pixels = shape[0] * shape[1] num_entires = len(self.preprocessor.get_dataset()) self.assertEqual(num_entires, num_pixels) def test_center_pixels(self): # Prepare test image img_path = self.preprocessor.X_imgpath img_array = np.array(PIL.Image.open(img_path)).flatten() img_array = img_array / 255. # Generate Patches border = 3 self.preprocessor.set_border(border) X, y = zip(*self.preprocessor.get_dataset()) center_index = (len(X[0]) - 1) / 2 # The first patch's center pixel must be the first pixel of the image first_pixel = img_array[0] first_center_pixel = X[0][center_index] self.assertEqual(first_center_pixel, first_pixel) # The next pixel nighbour of the first patch's center pixel # must be the second pixel of the image next_pixel = img_array[1] next_center_pixel = X[0][center_index + 1] self.assertEqual(next_center_pixel, next_pixel) # The last patch's center pixel must be the last pixel in the image last_pixel = img_array[-1] last_center_pixel = X[-1][center_index] self.assertEqual(last_center_pixel, last_pixel) # The previous pixel nighbour of the last patch's center pixel # must be the second last pixel of the image second_last_pixel = img_array[-2] second_last_center_pixel = X[-1][center_index - 1] self.assertEqual(second_last_center_pixel, second_last_pixel) def test_y_pixels(self): name = os.path.basename(self.preprocessor.X_imgpath) y_imgpath = os.path.join(self.preprocessor.y_dirpath, name) img_array = np.array(PIL.Image.open(y_imgpath)).flatten() / 255. border = 1 self.preprocessor.border = border X, y = zip(*self.preprocessor.get_dataset()) self.assertEqual(y[0], img_array[0]) self.assertEqual(y[len(y) / 2], img_array[len(y) / 2]) self.assertEqual(y[-1], img_array[-1])