

"""simple docstring"""




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import VideoMAEImageProcessor

class    a__  (   unittest.TestCase     ):
			"""simple docstring"""







			def __init__(      self , lowercase , lowercase=7 , lowercase=3 , lowercase=10 , lowercase=18 , lowercase=30 , lowercase=400 , lowercase=True , lowercase=None , lowercase=True , lowercase=[0.5, 0.5, 0.5] , lowercase=[0.5, 0.5, 0.5] , lowercase=None , )					->  Tuple:
							'''simple docstring'''






							A__            =     size if size is not None else {"shortest_edge": 18}
							A__            =     crop_size if crop_size is not None else {"height": 18, "width": 18}

							A__            =     parent
							A__            =     batch_size
							A__            =     num_channels
							A__            =     num_frames
							A__            =     image_size
							A__            =     min_resolution
							A__            =     max_resolution
							A__            =     do_resize
							A__            =     size
							A__            =     do_normalize
							A__            =     image_mean
							A__            =     image_std
							A__            =     crop_size


			def        UpperCamelCase						(      self    )					->  Union[str, Any]:
							'''simple docstring'''






							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "crop_size": self.crop_size,
							}

@require_torch
@require_vision
class    a__  (   snake_case							,		unittest.TestCase     ):
			"""simple docstring"""







			__lowerCamelCase	    =    VideoMAEImageProcessor if is_vision_available() else None


			def        UpperCamelCase						(      self    )					->  Union[str, Any]:
							'''simple docstring'''






							A__            =     VideoMAEImageProcessingTester(self    )


			@property
			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							return self.image_processor_tester.prepare_image_processor_dict()


			def        UpperCamelCase						(      self    )					->  Optional[Any]:
							'''simple docstring'''






							A__            =     self.image_processing_class(**self.image_processor_dict    )
							self.assertTrue(hasattr(lowercase , "image_mean"    )    )
							self.assertTrue(hasattr(lowercase , "image_std"    )    )
							self.assertTrue(hasattr(lowercase , "do_normalize"    )    )
							self.assertTrue(hasattr(lowercase , "do_resize"    )    )
							self.assertTrue(hasattr(lowercase , "do_center_crop"    )    )
							self.assertTrue(hasattr(lowercase , "size"    )    )


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							A__            =     self.image_processing_class.from_dict(self.image_processor_dict    )
							self.assertEqual(image_processor.size , {"shortest_edge": 18}    )
							self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18}    )

							A__            =     self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84    )
							self.assertEqual(image_processor.size , {"shortest_edge": 42}    )
							self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84}    )


			def        UpperCamelCase						(      self    )					->  int:
							'''simple docstring'''






							pass


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							A__            =     self.image_processing_class(**self.image_processor_dict    )
							# create random PIL videos
							A__            =     prepare_video_inputs(self.image_processor_tester , equal_resolution=lowercase    )
							for video in video_inputs:
											self.assertIsInstance(lowercase , lowercase    )
											self.assertIsInstance(video[0] , Image.Image    )

							# Test not batched input
							A__            =     image_processing(video_inputs[0] , return_tensors="pt"    ).pixel_values
							self.assertEqual(
							    encoded_videos.shape , (
							        1,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ) , )

							# Test batched
							A__            =     image_processing(lowercase , return_tensors="pt"    ).pixel_values
							self.assertEqual(
							    encoded_videos.shape , (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ) , )


			def        UpperCamelCase						(      self    )					->  Dict:
							'''simple docstring'''






							A__            =     self.image_processing_class(**self.image_processor_dict    )
							# create random numpy tensors
							A__            =     prepare_video_inputs(self.image_processor_tester , equal_resolution=lowercase , numpify=lowercase    )
							for video in video_inputs:
											self.assertIsInstance(lowercase , lowercase    )
											self.assertIsInstance(video[0] , np.ndarray    )

							# Test not batched input
							A__            =     image_processing(video_inputs[0] , return_tensors="pt"    ).pixel_values
							self.assertEqual(
							    encoded_videos.shape , (
							        1,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ) , )

							# Test batched
							A__            =     image_processing(lowercase , return_tensors="pt"    ).pixel_values
							self.assertEqual(
							    encoded_videos.shape , (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ) , )


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							A__            =     self.image_processing_class(**self.image_processor_dict    )
							# create random PyTorch tensors
							A__            =     prepare_video_inputs(self.image_processor_tester , equal_resolution=lowercase , torchify=lowercase    )
							for video in video_inputs:
											self.assertIsInstance(lowercase , lowercase    )
											self.assertIsInstance(video[0] , torch.Tensor    )

							# Test not batched input
							A__            =     image_processing(video_inputs[0] , return_tensors="pt"    ).pixel_values
							self.assertEqual(
							    encoded_videos.shape , (
							        1,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ) , )

							# Test batched
							A__            =     image_processing(lowercase , return_tensors="pt"    ).pixel_values
							self.assertEqual(
							    encoded_videos.shape , (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    ) , )




import unittest

from cer import CER


lowerCAmelCase__						=	CER()

class    a__  (   unittest.TestCase     ):
			"""simple docstring"""







			def        UpperCamelCase						(      self    )					->  Tuple:
							'''simple docstring'''






							A__            =     ["White House"]
							A__            =     ["white house"]
							# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.18_1818_1818    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  Any:
							'''simple docstring'''






							A__            =     ["were wolf"]
							A__            =     ["werewolf"]
							# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.111_1111    ) < 1e-6    )

							A__            =     ["werewolf"]
							A__            =     ["weae     wolf"]
							# S = 1, D = 1, I = 0, N = 8, CER = 0.25
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.25    ) < 1e-6    )

							# consecutive whitespaces case 1
							A__            =     ["were wolf"]
							A__            =     ["were               wolf"]
							# S = 0, D = 0, I = 0, N = 9, CER = 0
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.0    ) < 1e-6    )

							# consecutive whitespaces case 2
							A__            =     ["were   wolf"]
							A__            =     ["were               wolf"]
							# S = 0, D = 0, I = 0, N = 9, CER = 0
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.0    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






							A__            =     ["werewolf"]
							A__            =     ["weaewolf"]
							# S = 1, D = 0, I = 0, N = 8, CER = 0.125
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.125    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  Tuple:
							'''simple docstring'''






							A__            =     ["werewolf"]
							A__            =     ["wereawolf"]
							# S = 0, D = 1, I = 0, N = 8, CER = 0.125
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.125    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






							A__            =     ["werewolf"]
							A__            =     ["wereolf"]
							# S = 0, D = 0, I = 1, N = 8, CER = 0.125
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.125    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






							A__            =     ["werewolf"]
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertEqual(lowercase , 0.0    )


			def        UpperCamelCase						(      self    )					->  Any:
							'''simple docstring'''






							A__            =     ["werewolf", "I am your father"]
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertEqual(lowercase , 0.0    )

							A__            =     ["werewolf", "I am your father", "doge"]
							A__            =     ["werxwolf", "I       am your father", "doge"]
							# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.0357_1428    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  str:
							'''simple docstring'''






							A__            =     ["My hovercraft", "is full of eels"]
							A__            =     ["My hovercraft is full", " of eels"]
							# S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
							# whitespace at the front of " of eels" will be strip during preporcessing
							# so need to insert 2 whitespaces
							A__            =     cer.compute(predictions=lowercase , references=lowercase , concatenate_texts=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.07_1428    ) < 1e-6    )


			def        UpperCamelCase						(      self    )					->  List[str]:
							'''simple docstring'''






							A__            =     ["我能吞下玻璃而不伤身体"]
							A__            =     [" 能吞虾玻璃而 不霜身体啦"]
							# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.45_4545_4545    ) < 1e-6    )

							A__            =     ["我能吞下玻璃", "而不伤身体"]
							A__            =     ["我    能 吞 下 玻 璃", "而不伤身体"]
							# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertTrue(abs(char_error_rate - 0.4545_4545_4545    ) < 1e-6    )

							A__            =     ["我能吞下玻璃而不伤身体"]
							A__            =     cer.compute(predictions=lowercase , references=lowercase    )
							self.assertFalse(lowercase , 0.0    )


			def        UpperCamelCase						(      self    )					->  Optional[Any]:
							'''simple docstring'''






							A__            =     [""]
							A__            =     ["Hypothesis"]
							with self.assertRaises(lowercase    ):
											cer.compute(predictions=lowercase , references=lowercase    )


if __name__ == "__main__":
	unittest.main()

