def test_img_read(self): for test_case in self.test_cases: with self.subTest(msg=test_case.filename): file_path = os.path.join(self.INPUT_FOLDER, test_case.filename) img = read_data(file_path) self.assertEqual( img.shape, test_case.shape, 'Expected shape {}, got {}'.format(test_case.shape, img.shape)) if test_case.filename != 'img.jpg' or python_implementation( ) != 'PyPy': self.assertAlmostEqual( np.mean(img), test_case.mean, delta=1e-4, msg='Expected mean {}, got {}'.format( test_case.mean, np.mean(img))) self.assertTrue(img.flags['WRITEABLE'], msg='Obtained numpy array is not writeable') new_file_path = os.path.join(self.OUTPUT_FOLDER, test_case.filename) write_data(new_file_path, img) new_img = read_data(new_file_path) if not test_case.filename.endswith('jpg'): self.assertTrue( np.array_equal(img, new_img), msg="Original and new image are not the same")
def test_img_read(self): for test_case in self.test_cases: with self.subTest(msg=test_case.filename): file_path = os.path.join(self.INPUT_FOLDER, test_case.filename) img = read_data(file_path) self.assertEqual( img.shape, test_case.shape, 'Expected shape {}, got {}'.format(test_case.shape, img.shape)) self.assertAlmostEqual(np.mean(img), test_case.mean, delta=1e-4, msg='Expected mean {}, got {}'.format( test_case.mean, np.mean(img))) new_file_path = os.path.join(self.OUTPUT_FOLDER, test_case.filename) write_data(new_file_path, img) new_img = read_data(new_file_path) if not test_case.filename.endswith('jpg'): self.assertTrue( np.array_equal(img, new_img), msg="Original and new image are not the same")
def test_img_read(self): W, H = 2048, 2048 formats = [('tif', 13577.49), ('jpg', 52.41), ('png', 52.34), ('jp2', 47.09)] for ext, exp_mean in formats: with self.subTest(msg=ext): filename = os.path.join(self.INPUT_FOLDER, 'ml.{}'.format(ext)) img = read_data(filename) self.assertEqual(np.shape(img), (W, H, 3) if ext != 'jp2' else (343, 343, 3), "{} image dimension mismatch".format(ext)) self.assertAlmostEqual(np.mean(img), exp_mean, delta=1e-1, msg="{} image has incorrect values".format(ext)) new_filename = os.path.join(self.OUTPUT_FOLDER, os.path.split(filename)[-1]) write_data(new_filename, img) new_img = read_data(new_filename) if ext != 'jpg': self.assertTrue(np.array_equal(img, new_img), msg="Original and new image are not the same")
def save_truth(self): """ Use this method only to create new unittests """ write_data(self.get_filename(), self.get_request_data())