




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




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




    '''simple docstring'''

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




   def 							__magic_name__							(   self						:	Optional[int]				)				->							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      UpperCAmelCase_  (						A_,       unittest.TestCase							):
   lowercase__						 =				DonutImageProcessor if is_vision_available() else None




   def 							__magic_name__							(   self						:	Dict				)				->							Any:




    '''simple docstring'''

    A__	=    DonutImageProcessingTester(self				)




   @property
   def 							__magic_name__							(   self						:	Optional[Any]				)				->							int:




    '''simple docstring'''

    return self.image_processor_tester.prepare_image_processor_dict()




   def 							__magic_name__							(   self						:	List[str]				)				->							Optional[Any]:




    '''simple docstring'''

    A__	=    self.image_processing_class(**self.image_processor_dict				)
    self.assertTrue(hasattr(snake_case_     ,			"do_resize"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"size"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"do_thumbnail"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"do_align_long_axis"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"do_pad"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"do_normalize"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"image_mean"				)				)
    self.assertTrue(hasattr(snake_case_     ,			"image_std"				)				)




   def 							__magic_name__							(   self						:	str				)				->							int:




    '''simple docstring'''

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

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




   def 							__magic_name__							(   self						:	Union[str, Any]				)				->							Optional[Any]:




    '''simple docstring'''

    pass




   @is_flaky()
   def 							__magic_name__							(   self						:	Tuple				)				->							Tuple:




    '''simple docstring'''

    A__	=    self.image_processing_class(**self.image_processor_dict				)
    # create random PIL images
    A__	=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=snake_case_				)
    for image in image_inputs:
     self.assertIsInstance(snake_case_     ,			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(snake_case_     ,			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 							__magic_name__							(   self						:	Optional[int]				)				->							int:




    '''simple docstring'''

    A__	=    self.image_processing_class(**self.image_processor_dict				)
    # create random numpy tensors
    A__	=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=snake_case_     ,			numpify=snake_case_				)
    for image in image_inputs:
     self.assertIsInstance(snake_case_     ,			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(snake_case_     ,			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 							__magic_name__							(   self						:	Dict				)				->							int:




    '''simple docstring'''

    A__	=    self.image_processing_class(**self.image_processor_dict				)
    # create random PyTorch tensors
    A__	=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=snake_case_     ,			torchify=snake_case_				)
    for image in image_inputs:
     self.assertIsInstance(snake_case_     ,			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(snake_case_     ,			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"""



SCREAMING_SNAKE_CASE    =	"\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

SCREAMING_SNAKE_CASE    =	[{"type": "code", "content": INSTALL_CONTENT}]
SCREAMING_SNAKE_CASE    =	{
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}





