

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

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






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       parent
   UpperCAmelCase__       :	List[str]        =       batch_size
   UpperCAmelCase__       :	List[str]        =       num_channels
   UpperCAmelCase__       :	Union[str, Any]        =       image_size
   UpperCAmelCase__       :	List[Any]        =       min_resolution
   UpperCAmelCase__       :	Optional[int]        =       max_resolution
   UpperCAmelCase__       :	str        =       do_resize
   UpperCAmelCase__       :	Tuple        =       size if size is not None else {"""height""": 18, """width""": 20}
   UpperCAmelCase__       :	List[str]        =       do_thumbnail
   UpperCAmelCase__       :	Optional[int]        =       do_align_axis
   UpperCAmelCase__       :	Union[str, Any]        =       do_pad
   UpperCAmelCase__       :	Tuple        =       do_normalize
   UpperCAmelCase__       :	Optional[Any]        =       image_mean
   UpperCAmelCase__       :	List[Any]        =       image_std




  def  __lowercase							(	self		:      Optional[int]     ):






   '''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   __lowercase      (     __lowerCamelCase						,      unittest.TestCase       ):
  snake_case_												=							DonutImageProcessor if is_vision_available() else None

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       DonutImageProcessingTester(self     )

  @property
  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   return self.image_processor_tester.prepare_image_processor_dict()

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       self.image_processing_class(**self.image_processor_dict     )
   self.assertTrue(hasattr(A       ,"""do_resize"""     )     )
   self.assertTrue(hasattr(A       ,"""size"""     )     )
   self.assertTrue(hasattr(A       ,"""do_thumbnail"""     )     )
   self.assertTrue(hasattr(A       ,"""do_align_long_axis"""     )     )
   self.assertTrue(hasattr(A       ,"""do_pad"""     )     )
   self.assertTrue(hasattr(A       ,"""do_normalize"""     )     )
   self.assertTrue(hasattr(A       ,"""image_mean"""     )     )
   self.assertTrue(hasattr(A       ,"""image_std"""     )     )

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       self.image_processing_class.from_dict(self.image_processor_dict     )
   self.assertEqual(image_processor.size       ,{"""height""": 18, """width""": 20}     )

   UpperCAmelCase__       :	str        =       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
   UpperCAmelCase__       :	str        =       self.image_processing_class.from_dict(self.image_processor_dict       ,size=(42, 84)     )
   self.assertEqual(image_processor.size       ,{"""height""": 84, """width""": 42}     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   pass

  @is_flaky()
  def  __lowercase							(	self		:      int     ):






   '''simple docstring'''
   # Initialize image_processing
   UpperCAmelCase__       :	Optional[int]        =       self.image_processing_class(**self.image_processor_dict     )
   # create random PIL images
   UpperCAmelCase__       :	Dict        =       prepare_image_inputs(self.image_processor_tester       ,equal_resolution=A     )
   for image in image_inputs:
    self.assertIsInstance(A       ,Image.Image     )

   # Test not batched input
   UpperCAmelCase__       :	int        =       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
   UpperCAmelCase__       :	Tuple        =       image_processing(A       ,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  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   # Initialize image_processing
   UpperCAmelCase__       :	Union[str, Any]        =       self.image_processing_class(**self.image_processor_dict     )
   # create random numpy tensors
   UpperCAmelCase__       :	Dict        =       prepare_image_inputs(self.image_processor_tester       ,equal_resolution=A       ,numpify=A     )
   for image in image_inputs:
    self.assertIsInstance(A       ,np.ndarray     )

   # Test not batched input
   UpperCAmelCase__       :	List[str]        =       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
   UpperCAmelCase__       :	Optional[int]        =       image_processing(A       ,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  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   # Initialize image_processing
   UpperCAmelCase__       :	int        =       self.image_processing_class(**self.image_processor_dict     )
   # create random PyTorch tensors
   UpperCAmelCase__       :	int        =       prepare_image_inputs(self.image_processor_tester       ,equal_resolution=A       ,torchify=A     )
   for image in image_inputs:
    self.assertIsInstance(A       ,torch.Tensor     )

   # Test not batched input
   UpperCAmelCase__       :	List[Any]        =       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
   UpperCAmelCase__       :	List[Any]        =       image_processing(A       ,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'''




from functools import lru_cache






@lru_cache
def 							snake_case__				(     _A:   int					)       ->      int:

		'''simple docstring'''


		if num < 0:
				raise ValueError("""Number should not be negative."""					)

		return 1 if num in (0, 1) else num * factorial(num - 1					)


if __name__ == "__main__":
						import doctest

						doctest.testmod()




