






'''simple docstring'''

import unittest

import numpy as np
import requests

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

   from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
   UpperCAmelCase__						:str          =			False

if is_vision_available():
   from PIL import Image

   from transformers import PixaStructImageProcessor







class 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):




       def __init__(					self      : List[str]	,    A__      : Any	,    A__      : str=7	,    A__      : str=3	,    A__      : Any=18	,    A__      : List[Any]=30	,    A__      : Any=400	,    A__      : Union[str, Any]=None	,    A__      : str=True	,    A__      : Dict=True	,    A__      : Union[str, Any]=None	,    ):


            """simple docstring"""







            __lowerCamelCase						:       Union[str, Any]				   =				size if size is not None else {"""height""": 20, """width""": 20}
            __lowerCamelCase						:       List[str]				   =				parent
            __lowerCamelCase						:       Union[str, Any]				   =				batch_size
            __lowerCamelCase						:       List[Any]				   =				num_channels
            __lowerCamelCase						:       List[str]				   =				image_size
            __lowerCamelCase						:       Dict				   =				min_resolution
            __lowerCamelCase						:       Tuple				   =				max_resolution
            __lowerCamelCase						:       Union[str, Any]				   =				size
            __lowerCamelCase						:       Any				   =				do_normalize
            __lowerCamelCase						:       Any				   =				do_convert_rgb
            __lowerCamelCase						:       str				   =				[512, 1024, 2048, 4096]
            __lowerCamelCase						:       str				   =				patch_size if patch_size is not None else {"""height""": 16, """width""": 16}




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}




       def 					a_					(					self      : List[str]					):


            """simple docstring"""







            __lowerCamelCase						:       Tuple				   =				"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"""
            __lowerCamelCase						:       Any				   =				Image.open(requests.get(A__	,    stream=A__					).raw					).convert("""RGB"""					)
            return raw_image







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11    ,   reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'    ,   )
@require_torch
@require_vision
class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_    ,   unittest.TestCase  ):
       snake_case__    :     Optional[Any]   		=			PixaStructImageProcessor if is_vision_available() else None




       def 					a_					(					self      : int					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[Any]				   =				PixaStructImageProcessingTester(self					)




       @property
       def 					a_					(					self      : Dict					):


            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()




       def 					a_					(					self      : Any					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[Any]				   =				self.image_processing_class(**self.image_processor_dict					)
            self.assertTrue(hasattr(A__	,    """do_normalize"""					)					)
            self.assertTrue(hasattr(A__	,    """do_convert_rgb"""					)					)




       def 					a_					(					self      : List[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				self.image_processor_tester.prepare_dummy_image()

            __lowerCamelCase						:       str				   =				self.image_processing_class(**self.image_processor_dict					)
            __lowerCamelCase						:       List[str]				   =				2048

            __lowerCamelCase						:       List[str]				   =				image_processor(A__	,    return_tensors="""pt"""	,    max_patches=A__					)
            self.assertTrue(torch.allclose(inputs.flattened_patches.mean()	,    torch.tensor(0.0606					)	,    atol=1e-3	,    rtol=1e-3					)					)




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







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

            # Test not batched input
            __lowerCamelCase						:       Union[str, Any]				   =				(
                (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
                * self.image_processor_tester.num_channels
            ) + 2

            for max_patch in self.image_processor_tester.max_patches:
                 # Test not batched input
                 __lowerCamelCase						:       Tuple				   =				image_processor(
                     image_inputs[0]	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (1, max_patch, expected_hidden_dim)	,    )

                 # Test batched
                 __lowerCamelCase						:       str				   =				image_processor(
                     A__	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,    )




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				self.image_processing_class(**self.image_processor_dict					)
            # create random PIL images
            __lowerCamelCase						:       Any				   =				prepare_image_inputs(self.image_processor_tester	,    equal_resolution=A__					)
            for image in image_inputs:
                 self.assertIsInstance(A__	,    Image.Image					)

            # Test not batched input
            __lowerCamelCase						:       Any				   =				(
                (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
                * self.image_processor_tester.num_channels
            ) + 2

            __lowerCamelCase						:       Dict				   =				True

            for max_patch in self.image_processor_tester.max_patches:
                 # Test not batched input
                 with self.assertRaises(A__					):
                      __lowerCamelCase						:       str				   =				image_processor(
                          image_inputs[0]	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches

                 __lowerCamelCase						:       int				   =				"""Hello"""

                 __lowerCamelCase						:       List[Any]				   =				image_processor(
                     image_inputs[0]	,    return_tensors="""pt"""	,    max_patches=A__	,    header_text=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (1, max_patch, expected_hidden_dim)	,    )

                 # Test batched
                 __lowerCamelCase						:       List[Any]				   =				image_processor(
                     A__	,    return_tensors="""pt"""	,    max_patches=A__	,    header_text=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,    )




       def 					a_					(					self      : Optional[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				self.image_processing_class(**self.image_processor_dict					)
            # create random numpy tensors
            __lowerCamelCase						:       Any				   =				prepare_image_inputs(self.image_processor_tester	,    equal_resolution=A__	,    numpify=A__					)
            for image in image_inputs:
                 self.assertIsInstance(A__	,    np.ndarray					)

            __lowerCamelCase						:       str				   =				(
                (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
                * self.image_processor_tester.num_channels
            ) + 2

            for max_patch in self.image_processor_tester.max_patches:
                 # Test not batched input
                 __lowerCamelCase						:       Optional[int]				   =				image_processor(
                     image_inputs[0]	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (1, max_patch, expected_hidden_dim)	,    )

                 # Test batched
                 __lowerCamelCase						:       Union[str, Any]				   =				image_processor(
                     A__	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,    )




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				self.image_processing_class(**self.image_processor_dict					)
            # create random PyTorch tensors
            __lowerCamelCase						:       Dict				   =				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
            __lowerCamelCase						:       Any				   =				(
                (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
                * self.image_processor_tester.num_channels
            ) + 2

            for max_patch in self.image_processor_tester.max_patches:
                 # Test not batched input
                 __lowerCamelCase						:       Optional[Any]				   =				image_processor(
                     image_inputs[0]	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (1, max_patch, expected_hidden_dim)	,    )

                 # Test batched
                 __lowerCamelCase						:       List[Any]				   =				image_processor(
                     A__	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,    )







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11    ,   reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'    ,   )
@require_torch
@require_vision
class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_    ,   unittest.TestCase  ):
       snake_case__    :     List[str]   		=			PixaStructImageProcessor if is_vision_available() else None




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				PixaStructImageProcessingTester(self	,    num_channels=4					)
            __lowerCamelCase						:       List[Any]				   =				3




       @property
       def 					a_					(					self      : Tuple					):


            """simple docstring"""







            return self.image_processor_tester.prepare_image_processor_dict()




       def 					a_					(					self      : int					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[Any]				   =				self.image_processing_class(**self.image_processor_dict					)
            self.assertTrue(hasattr(A__	,    """do_normalize"""					)					)
            self.assertTrue(hasattr(A__	,    """do_convert_rgb"""					)					)




       def 					a_					(					self      : List[Any]					):


            """simple docstring"""







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

            # Test not batched input
            __lowerCamelCase						:       str				   =				(
                (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
                * (self.image_processor_tester.num_channels - 1)
            ) + 2

            for max_patch in self.image_processor_tester.max_patches:
                 # Test not batched input
                 __lowerCamelCase						:       List[Any]				   =				image_processor(
                     image_inputs[0]	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (1, max_patch, expected_hidden_dim)	,    )

                 # Test batched
                 __lowerCamelCase						:       Union[str, Any]				   =				image_processor(
                     A__	,    return_tensors="""pt"""	,    max_patches=A__					).flattened_patches
                 self.assertEqual(
                     encoded_images.shape	,    (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)	,    )











'''simple docstring'''

from ..utils import DummyObject, requires_backends







class 	SCREAMING_SNAKE_CASE	(      metaclass=lowerCAmelCase_  ):
       snake_case__    :     Union[str, Any]   		=			['torch', 'torchsde']




       def __init__(					self      : int	,    *A__      : Optional[Any]	,    **A__      : Dict					):


            """simple docstring"""







            requires_backends(self	,    ["""torch""", """torchsde"""]					)




       @classmethod
       def 					a_					(					cls      : Optional[Any]	,    *A__      : str	,    **A__      : Optional[Any]					):


            """simple docstring"""







            requires_backends(cls	,    ["""torch""", """torchsde"""]					)




       @classmethod
       def 					a_					(					cls      : Union[str, Any]	,    *A__      : Any	,    **A__      : Union[str, Any]					):


            """simple docstring"""







            requires_backends(cls	,    ["""torch""", """torchsde"""]					)



