


"""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_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import ViTImageProcessor


class      __lowerCamelCase	(       unittest.TestCase					):

     '''simple docstring'''


     def __init__(   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=7     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=18     ,					__UpperCAmelCase=30     ,					__UpperCAmelCase=400     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=[0.5, 0.5, 0.5]     ,					__UpperCAmelCase=[0.5, 0.5, 0.5]     ,					)							->				Union[str, Any]:
          _a     						=					size if size is not None else {'''height''': 18, '''width''': 18}
          _a     						=					parent
          _a     						=					batch_size
          _a     						=					num_channels
          _a     						=					image_size
          _a     						=					min_resolution
          _a     						=					max_resolution
          _a     						=					do_resize
          _a     						=					size
          _a     						=					do_normalize
          _a     						=					image_mean
          _a     						=					image_std





     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
          }







@require_torch
@require_vision
class      __lowerCamelCase	(       a__	,       unittest.TestCase					):

     '''simple docstring'''
     A_							:						Union[str, Any]     =		ViTImageProcessor if is_vision_available() else None


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          _a     						=					ViTImageProcessingTester(self				)


     @property
     def 				_UpperCAmelCase   (   self				)							->				Tuple:
          return self.image_processor_tester.prepare_image_processor_dict()


     def 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          _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__     ,					'''size'''				)				)


     def 				_UpperCAmelCase   (   self				)							->				str:
          _a     						=					self.image_processing_class.from_dict(self.image_processor_dict				)
          self.assertEqual(image_processor.size     ,					{'''height''': 18, '''width''': 18}				)

          _a     						=					self.image_processing_class.from_dict(self.image_processor_dict     ,					size=42				)
          self.assertEqual(image_processor.size     ,					{'''height''': 42, '''width''': 42}				)


     def 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          pass


     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random PIL images
          _a     						=					prepare_image_inputs(self.image_processor_tester     ,					equal_resolution=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					Image.Image				)

          # Test not batched input
          _a     						=					image_processing(image_inputs[0]     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)

          # Test batched
          _a     						=					image_processing(lowercase__     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)


     def 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random numpy tensors
          _a     						=					prepare_image_inputs(self.image_processor_tester     ,					equal_resolution=lowercase__     ,					numpify=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					np.ndarray				)

          # Test not batched input
          _a     						=					image_processing(image_inputs[0]     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)

          # Test batched
          _a     						=					image_processing(lowercase__     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)





     def 				_UpperCAmelCase   (   self				)							->				str:
          _a     						=					self.image_processing_class(**self.image_processor_dict				)
          # create random PyTorch tensors
          _a     						=					prepare_image_inputs(self.image_processor_tester     ,					equal_resolution=lowercase__     ,					torchify=lowercase__				)
          for image in image_inputs:
               self.assertIsInstance(lowercase__     ,					torch.Tensor				)

          # Test not batched input
          _a     						=					image_processing(image_inputs[0]     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)

          # Test batched
          _a     						=					image_processing(lowercase__     ,					return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_images.shape     ,					(
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )     ,					)
import unittest

from cer import CER


lowerCAmelCase__      		=					CER()
class 							a_					(		unittest.TestCase		):






 '''simple docstring'''






 def 						__snake_case		(     self   :    Any):







        '''simple docstring'''



        lowerCAmelCase__	      =   ['White House']
        lowerCAmelCase__	      =   ['white house']
        # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
        lowerCAmelCase__	      =   cer.compute(predictions=lowercase__     ,       references=lowercase__)
        self.assertTrue(abs(char_error_rate - 0.1_818_181_818) < 1e-6)




 def 						__snake_case		(     self   :    List[Any]):







        '''simple docstring'''



        lowerCAmelCase__	      =   ['were wolf']
        lowerCAmelCase__	      =   ['werewolf']
        # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
        lowerCAmelCase__	      =   cer.compute(predictions=lowercase__     ,       references=lowercase__)
        self.assertTrue(abs(char_error_rate - 0.1_111_111) < 1e-6)

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

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

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




 def 						__snake_case		(     self   :    Optional[Any]):







        '''simple docstring'''



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




 def 						__snake_case		(     self   :    Tuple):







        '''simple docstring'''



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




 def 						__snake_case		(     self   :    Optional[int]):







        '''simple docstring'''



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




 def 						__snake_case		(     self   :    str):







        '''simple docstring'''



        lowerCAmelCase__	      =   ['werewolf']
        lowerCAmelCase__	      =   cer.compute(predictions=lowercase__     ,       references=lowercase__)
        self.assertEqual(lowercase__     ,       0.0)




 def 						__snake_case		(     self   :    Tuple):







        '''simple docstring'''



        lowerCAmelCase__	      =   ['werewolf', 'I am your father']
        lowerCAmelCase__	      =   cer.compute(predictions=lowercase__     ,       references=lowercase__)
        self.assertEqual(lowercase__     ,       0.0)

        lowerCAmelCase__	      =   ['werewolf', 'I am your father', 'doge']
        lowerCAmelCase__	      =   ['werxwolf', 'I       am your father', 'doge']
        # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
        lowerCAmelCase__	      =   cer.compute(predictions=lowercase__     ,       references=lowercase__)
        self.assertTrue(abs(char_error_rate - 0.03_571_428) < 1e-6)




 def 						__snake_case		(     self   :    Tuple):







        '''simple docstring'''



        lowerCAmelCase__	      =   ['My hovercraft', 'is full of eels']
        lowerCAmelCase__	      =   ['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
        lowerCAmelCase__	      =   cer.compute(predictions=lowercase__     ,       references=lowercase__     ,       concatenate_texts=lowercase__)
        self.assertTrue(abs(char_error_rate - 0.071_428) < 1e-6)




 def 						__snake_case		(     self   :    Dict):







        '''simple docstring'''



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

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

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




 def 						__snake_case		(     self   :    Dict):







        '''simple docstring'''



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


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