def test_image_channel_non_image(shape): test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) img = np.random.randint(0, 256, shape[:-1]) save_name = os.path.join(test_dir, 'dat.npy') misc_utils.save_file(save_name, img) assert os.path.exists(save_name) assert img_utils.get_img_channel_num(save_name) == shape[-1] shutil.rmtree(test_dir)
def test_io_funcs_txt(s): test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) assert os.path.exists(test_dir) save_name = os.path.join(test_dir, f'dat.txt') misc_utils.save_file(save_name, s) assert os.path.exists(save_name) data_read = misc_utils.load_file(save_name) assert ''.join(data_read) == ''.join(s) shutil.rmtree(test_dir)
def test_io_funcs(ext): test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) assert os.path.exists(test_dir) data = np.random.random((512, 512, 3)) save_name = os.path.join(test_dir, f'dat.{ext}') misc_utils.save_file(save_name, data) assert os.path.exists(save_name) data_read = misc_utils.load_file(save_name) np.testing.assert_array_almost_equal(data_read, data) shutil.rmtree(test_dir)
def test_pb(shape, ext): def func(shape): return np.random.randint(0, 256, shape) test_dir = './temp_pb' misc_utils.make_dir_if_not_exist(test_dir) assert os.path.exists(test_dir) save_name = os.path.join(test_dir, f'dat.{ext}') pb = process_block.ProcessBlock(func, save_name) img = pb.run(shape=shape) np.testing.assert_array_equal(shape, img.shape) shutil.rmtree(test_dir)
def test_image_channel_raise_error(shape): test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) img = np.random.randint(0, 256, shape) save_name = os.path.join(test_dir, f'dat.npy') misc_utils.save_file(save_name, img) assert os.path.exists(save_name) with pytest.raises(ValueError, match=r'Image can only have 2 or 3 dimensions'): img_utils.get_img_channel_num(save_name) shutil.rmtree(test_dir)
def test_io_funcs_json(): temp = 'c' d = {'a': 123, 'b': 'abc', temp: [1, 2, 3]} test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) assert os.path.exists(test_dir) save_name = os.path.join(test_dir, f'dat.json') misc_utils.save_file(save_name, d) assert os.path.exists(save_name) data_read = misc_utils.load_file(save_name) assert data_read == d shutil.rmtree(test_dir)
def test_get_file_length(): test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) assert os.path.exists(test_dir) for i in range(10): length = np.random.randint(100, 501) text = [] for _ in range(length): text.append('abcdefg\n') save_name = os.path.join(test_dir, 'dat.txt') misc_utils.save_file(save_name, text) assert misc_utils.get_file_length(save_name) == length shutil.rmtree(test_dir)
def test_io_funcs_image_pil(): img = np.random.randint(0, 256, (512, 512, 3)) test_dir = './temp' misc_utils.make_dir_if_not_exist(test_dir) assert os.path.exists(test_dir) save_name = os.path.join(test_dir, 'dat.png') misc_utils.save_file(save_name, img) assert os.path.exists(save_name) data_read = misc_utils.load_file(save_name, pil=True) np.testing.assert_array_almost_equal(np.array(data_read), img) data_read = misc_utils.load_file(save_name, pil=True, to_numpy=True) np.testing.assert_array_almost_equal(data_read, img) shutil.rmtree(test_dir)