


'''simple docstring'''

import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor
class     __lowerCAmelCase		(unittest.TestCase	):





      '''simple docstring'''






      def __init__(self       :       Optional[Any]  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       List[str]=7  ,		UpperCamelCase       :       int=3  ,		UpperCamelCase       :       str=18  ,		UpperCamelCase       :       Optional[int]=30  ,		UpperCamelCase       :       Dict=400  ,		UpperCamelCase       :       Dict=True  ,		UpperCamelCase       :       List[Any]=None  ,		UpperCamelCase       :       Dict=True  ,		UpperCamelCase       :       List[str]=False  ,		UpperCamelCase       :       Tuple=True  ,		UpperCamelCase       :       str=True  ,		UpperCamelCase       :       Tuple=[0.5, 0.5, 0.5]  ,		UpperCamelCase       :       Dict=[0.5, 0.5, 0.5]  ,		):

             '''simple docstring'''







             lowercase__      =					parent
             lowercase__      =					batch_size
             lowercase__      =					num_channels
             lowercase__      =					image_size
             lowercase__      =					min_resolution
             lowercase__      =					max_resolution
             lowercase__      =					do_resize
             lowercase__      =					size if size is not None else {'''height''': 18, '''width''': 20}
             lowercase__      =					do_thumbnail
             lowercase__      =					do_align_axis
             lowercase__      =					do_pad
             lowercase__      =					do_normalize
             lowercase__      =					image_mean
             lowercase__      =					image_std






      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







             return {
                 "do_resize": self.do_resize,
                 "size": self.size,
                 "do_thumbnail": self.do_thumbnail,
                 "do_align_long_axis": self.do_align_axis,
                 "do_pad": self.do_pad,
                 "do_normalize": self.do_normalize,
                 "image_mean": self.image_mean,
                 "image_std": self.image_std,
             }






@require_torch
@require_vision
class     __lowerCAmelCase		(lowercase_    ,	unittest.TestCase	):





      '''simple docstring'''






      lowerCAmelCase__   :	Optional[int]       =				DonutImageProcessor if is_vision_available() else None



      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             lowercase__      =					DonutImageProcessingTester(self					)



      @property
      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             return self.image_processor_tester.prepare_image_processor_dict()



      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             lowercase__      =					self.image_processing_class(**self.image_processor_dict					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''do_resize'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''size'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''do_thumbnail'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''do_align_long_axis'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''do_pad'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''do_normalize'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''image_mean'''					)					)
             self.assertTrue(hasattr(UpperCamelCase  ,		'''image_std'''					)					)



      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







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

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

             # Previous config had dimensions in (width, height) order
             lowercase__      =					self.image_processing_class.from_dict(self.image_processor_dict  ,		size=(42, 84)					)
             self.assertEqual(image_processor.size  ,		{'''height''': 84, '''width''': 42}					)



      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             pass



      @is_flaky()
      def  UpperCamelCase__						(self       :       List[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=UpperCamelCase					)
             for image in image_inputs:
                    self.assertIsInstance(UpperCamelCase  ,		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(UpperCamelCase  ,		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'''],
                 )  ,		)



      @is_flaky()
      def  UpperCamelCase__						(self       :       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=UpperCamelCase  ,		numpify=UpperCamelCase					)
             for image in image_inputs:
                    self.assertIsInstance(UpperCamelCase  ,		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(UpperCamelCase  ,		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'''],
                 )  ,		)






      @is_flaky()
      def  UpperCamelCase__						(self       :       List[str]					):

             '''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=UpperCamelCase  ,		torchify=UpperCamelCase					)
             for image in image_inputs:
                    self.assertIsInstance(UpperCamelCase  ,		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(UpperCamelCase  ,		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'''],
                 )  ,		)








'''simple docstring'''

def _SCREAMING_SNAKE_CASE (A			)			->			bool:






       """simple docstring"""







       if not isinstance(A    ,					A			):
              raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
       if len(A			) == 0:
              raise ValueError('''Input list must be a non empty list'''			)
       if len(A			) == 1:
              return True
       lowercase__      =					series[1] - series[0]
       for index in range(len(A			) - 1			):
              if series[index + 1] - series[index] != common_diff:
                     return False
       return True

def _SCREAMING_SNAKE_CASE (A			)			->			float:






       """simple docstring"""







       if not isinstance(A    ,					A			):
              raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
       if len(A			) == 0:
              raise ValueError('''Input list must be a non empty list'''			)
       lowercase__      =					0
       for val in series:
              answer += val
       return answer / len(A			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()




