
"""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 SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	def __init__(  self :List[Any]       ,    __magic_name__ :int       ,    __magic_name__ :List[str]=7       ,    __magic_name__ :Optional[Any]=3       ,    __magic_name__ :Tuple=10       ,    __magic_name__ :str=18       ,    __magic_name__ :Union[str, Any]=30       ,    __magic_name__ :Optional[int]=400       ,    __magic_name__ :Dict=True       ,    __magic_name__ :Union[str, Any]=None       ,    __magic_name__ :Dict=True       ,    __magic_name__ :Union[str, Any]=[0.5, 0.5, 0.5]       ,    __magic_name__ :List[Any]=[0.5, 0.5, 0.5]       ,    __magic_name__ :Tuple=None       ,    )			->   Union[str, Any]:

			'''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 :Any			)			->   Dict:

			'''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 SCREAMING_SNAKE_CASE       (      __UpperCamelCase					,   unittest.TestCase					):






	'''simple docstring'''







	snake_case__							:		Tuple         =		VideoMAEImageProcessor if is_vision_available() else None


	def    _UpperCamelCase							(  self :int			)			->   int:

			'''simple docstring'''






			a__   		=     VideoMAEImageProcessingTester(self			)


	@property
	def    _UpperCamelCase							(  self :int			)			->   Tuple:

			'''simple docstring'''






			return self.image_processor_tester.prepare_image_processor_dict()


	def    _UpperCamelCase							(  self :str			)			->   Any:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			self.assertTrue(hasattr(lowerCAmelCase__       ,    '''image_mean'''			)			)
			self.assertTrue(hasattr(lowerCAmelCase__       ,    '''image_std'''			)			)
			self.assertTrue(hasattr(lowerCAmelCase__       ,    '''do_normalize'''			)			)
			self.assertTrue(hasattr(lowerCAmelCase__       ,    '''do_resize'''			)			)
			self.assertTrue(hasattr(lowerCAmelCase__       ,    '''do_center_crop'''			)			)
			self.assertTrue(hasattr(lowerCAmelCase__       ,    '''size'''			)			)


	def    _UpperCamelCase							(  self :str			)			->   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 :Optional[int]			)			->   str:

			'''simple docstring'''






			pass


	def    _UpperCamelCase							(  self :Tuple			)			->   Optional[Any]:

			'''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=lowerCAmelCase__			)
			for video in video_inputs:
					self.assertIsInstance(lowerCAmelCase__       ,    lowerCAmelCase__			)
					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(lowerCAmelCase__       ,    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 :str			)			->   str:

			'''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=lowerCAmelCase__       ,    numpify=lowerCAmelCase__			)
			for video in video_inputs:
					self.assertIsInstance(lowerCAmelCase__       ,    lowerCAmelCase__			)
					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(lowerCAmelCase__       ,    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 :int			)			->   int:

			'''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=lowerCAmelCase__       ,    torchify=lowerCAmelCase__			)
			for video in video_inputs:
					self.assertIsInstance(lowerCAmelCase__       ,    lowerCAmelCase__			)
					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(lowerCAmelCase__       ,    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 ):
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  ['White House']
        lowercase__:  Optional[int]         =  ['white house']
        # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
        lowercase__:  str         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_8_1_8_1_8_1_8_1_8					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Union[str, Any]         =  ['were wolf']
        lowercase__:  Union[str, Any]         =  ['werewolf']
        # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
        lowercase__:  Tuple         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1					) < 1E-6					)

        lowercase__:  Dict         =  ['werewolf']
        lowercase__:  Optional[Any]         =  ['weae     wolf']
        # S = 1, D = 1, I = 0, N = 8, CER = 0.25
        lowercase__:  Optional[int]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.2_5					) < 1E-6					)

        # consecutive whitespaces case 1
        lowercase__:  Union[str, Any]         =  ['were wolf']
        lowercase__:  int         =  ['were               wolf']
        # S = 0, D = 0, I = 0, N = 9, CER = 0
        lowercase__:  int         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0					) < 1E-6					)

        # consecutive whitespaces case 2
        lowercase__:  List[str]         =  ['were   wolf']
        lowercase__:  Tuple         =  ['were               wolf']
        # S = 0, D = 0, I = 0, N = 9, CER = 0
        lowercase__:  Dict         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Optional[int]         =  ['werewolf']
        lowercase__:  Dict         =  ['weaewolf']
        # S = 1, D = 0, I = 0, N = 8, CER = 0.125
        lowercase__:  int         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_2_5					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  int         =  ['werewolf']
        lowercase__:  Optional[int]         =  ['wereawolf']
        # S = 0, D = 1, I = 0, N = 8, CER = 0.125
        lowercase__:  Dict         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_2_5					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  ['werewolf']
        lowercase__:  List[str]         =  ['wereolf']
        # S = 0, D = 0, I = 1, N = 8, CER = 0.125
        lowercase__:  Optional[Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_2_5					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  List[str]         =  ['werewolf']
        lowercase__:  List[Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertEqual(lowerCAmelCase__							,					0.0					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  int         =  ['werewolf', 'I am your father']
        lowercase__:  Union[str, Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertEqual(lowerCAmelCase__							,					0.0					)

        lowercase__:  List[str]         =  ['werewolf', 'I am your father', 'doge']
        lowercase__:  Any         =  ['werxwolf', 'I       am your father', 'doge']
        # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
        lowercase__:  int         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0_3_5_7_1_4_2_8					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  List[Any]         =  ['My hovercraft', 'is full of eels']
        lowercase__:  Union[str, Any]         =  ['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
        lowercase__:  Optional[int]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__							,					concatenate_texts=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0_7_1_4_2_8					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        lowercase__:  Dict         =  ['我能吞下玻璃而不伤身体']
        lowercase__:  Tuple         =  [' 能吞虾玻璃而 不霜身体啦']
        # S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
        lowercase__:  str         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5					) < 1E-6					)

        lowercase__:  Any         =  ['我能吞下玻璃', '而不伤身体']
        lowercase__:  Union[str, Any]         =  ['我    能 吞 下 玻 璃', '而不伤身体']
        # S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
        lowercase__:  Optional[Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5_4_5					) < 1E-6					)

        lowercase__:  int         =  ['我能吞下玻璃而不伤身体']
        lowercase__:  Any         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertFalse(lowerCAmelCase__							,					0.0					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Dict         =  ['']
        lowercase__:  int         =  ['Hypothesis']
        with self.assertRaises(lowerCAmelCase__					):
               cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)


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


