

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							):




  def __init__(    self							,       lowerCAmelCase							,       lowerCAmelCase=7							,       lowerCAmelCase=3							,       lowerCAmelCase=18							,       lowerCAmelCase=30							,       lowerCAmelCase=400							,       lowerCAmelCase=True							,       lowerCAmelCase=None							,       lowerCAmelCase=True							,       lowerCAmelCase=[0.5, 0.5, 0.5]							,       lowerCAmelCase=[0.5, 0.5, 0.5]							,       )  ->			Union[str, Any]:
        '''simple docstring'''






        _lowercase     	=size if size is not None else {"height": 18, "width": 18}
        _lowercase     	=parent
        _lowercase     	=batch_size
        _lowercase     	=num_channels
        _lowercase     	=image_size
        _lowercase     	=min_resolution
        _lowercase     	=max_resolution
        _lowercase     	=do_resize
        _lowercase     	=size
        _lowercase     	=do_normalize
        _lowercase     	=image_mean
        _lowercase     	=image_std




  def   A__					(    self )  ->			str:
        '''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,
        }





@require_torch
@require_vision
class 			__lowerCAmelCase					( SCREAMING_SNAKE_CASE      ,      unittest.TestCase							):
  _a						      =  ViTImageProcessor if is_vision_available() else None




  def   A__					(    self )  ->			List[Any]:
        '''simple docstring'''






        _lowercase     	=ViTImageProcessingTester(self )




  @property
  def   A__					(    self )  ->			Dict:
        '''simple docstring'''






        return self.image_processor_tester.prepare_image_processor_dict()




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






        _lowercase     	=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							,       'size' ) )




  def   A__					(    self )  ->			List[Any]:
        '''simple docstring'''






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

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




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






        pass




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






        _lowercase     	=self.image_processing_class(**self.image_processor_dict )
        # create random PIL images
        _lowercase     	=prepare_image_inputs(self.image_processor_tester							,       equal_resolution=lowerCAmelCase )
        for image in image_inputs:
              self.assertIsInstance(lowerCAmelCase							,       Image.Image )

        # Test not batched input
        _lowercase     	=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
        _lowercase     	=image_processing(lowerCAmelCase							,       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   A__					(    self )  ->			Union[str, Any]:
        '''simple docstring'''






        _lowercase     	=self.image_processing_class(**self.image_processor_dict )
        # create random numpy tensors
        _lowercase     	=prepare_image_inputs(self.image_processor_tester							,       equal_resolution=lowerCAmelCase							,       numpify=lowerCAmelCase )
        for image in image_inputs:
              self.assertIsInstance(lowerCAmelCase							,       np.ndarray )

        # Test not batched input
        _lowercase     	=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
        _lowercase     	=image_processing(lowerCAmelCase							,       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   A__					(    self )  ->			Dict:
        '''simple docstring'''






        _lowercase     	=self.image_processing_class(**self.image_processor_dict )
        # create random PyTorch tensors
        _lowercase     	=prepare_image_inputs(self.image_processor_tester							,       equal_resolution=lowerCAmelCase							,       torchify=lowerCAmelCase )
        for image in image_inputs:
              self.assertIsInstance(lowerCAmelCase							,       torch.Tensor )

        # Test not batched input
        _lowercase     	=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
        _lowercase     	=image_processing(lowerCAmelCase							,       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


lowercase_     =       CER()





class 			__lowerCAmelCase					( unittest.TestCase							):




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






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




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






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

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

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

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




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






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




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






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




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






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




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






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




  def   A__					(    self )  ->			List[Any]:
        '''simple docstring'''






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

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




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






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




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






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

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

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




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






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


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


