

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:
   _lowerCamelCase      :					Any					    =      False

if is_vision_available():
   from PIL import Image

   from transformers import PixaStructImageProcessor





class        lowerCAmelCase__  (					unittest.TestCase			):



       '''simple docstring'''







       def __init__(							self ,   lowercase__ ,   lowercase__=7 ,   lowercase__=3 ,   lowercase__=1_8 ,   lowercase__=3_0 ,   lowercase__=4_0_0 ,   lowercase__=None ,   lowercase__=True ,   lowercase__=True ,   lowercase__=None ,   ):







             '''simple docstring'''



             __A    =size if size is not None else {'height': 2_0, 'width': 2_0}
             __A    =parent
             __A    =batch_size
             __A    =num_channels
             __A    =image_size
             __A    =min_resolution
             __A    =max_resolution
             __A    =size
             __A    =do_normalize
             __A    =do_convert_rgb
             __A    =[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
             __A    =patch_size if patch_size is not None else {'height': 1_6, 'width': 1_6}

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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


       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    ='https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
             __A    =Image.open(requests.get(lowerCamelCase_ ,   stream=lowerCamelCase_     ).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        lowerCAmelCase__  (					_UpperCamelCase				,							unittest.TestCase			):



       '''simple docstring'''







       lowercase_						=    PixaStructImageProcessor if is_vision_available() else None

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =PixaStructImageProcessingTester(self     )

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             return self.image_processor_tester.prepare_image_processor_dict()

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.image_processing_class(**self.image_processor_dict     )
             self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_normalize'''     )     )
             self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_convert_rgb'''     )     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.image_processor_tester.prepare_dummy_image()

             __A    =self.image_processing_class(**self.image_processor_dict     )
             __A    =2_0_4_8

             __A    =image_processor(lowerCamelCase_ ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     )
             self.assertTrue(torch.allclose(inputs.flattened_patches.mean() ,   torch.tensor(0.0606     ) ,   atol=1E-3 ,   rtol=1E-3     )     )

       def     __UpperCamelCase     (							self     ):







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

             # Test not batched input
             __A    =(
                 (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
                   __A    =image_processor(
                       image_inputs[0] ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (1, max_patch, expected_hidden_dim) ,   )

                   # Test batched
                   __A    =image_processor(
                       lowerCamelCase_ ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,   )

       def     __UpperCamelCase     (							self     ):







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

             # Test not batched input
             __A    =(
                 (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
                 * self.image_processor_tester.num_channels
             ) + 2

             __A    =True

             for max_patch in self.image_processor_tester.max_patches:
                   # Test not batched input
                   with self.assertRaises(lowerCamelCase_     ):
                         __A    =image_processor(
                             image_inputs[0] ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches

                   __A    ='Hello'

                   __A    =image_processor(
                       image_inputs[0] ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_ ,   header_text=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (1, max_patch, expected_hidden_dim) ,   )

                   # Test batched
                   __A    =image_processor(
                       lowerCamelCase_ ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_ ,   header_text=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,   )

       def     __UpperCamelCase     (							self     ):







             '''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=lowerCamelCase_ ,   numpify=lowerCamelCase_     )
             for image in image_inputs:
                   self.assertIsInstance(lowerCamelCase_ ,   np.ndarray     )

             __A    =(
                 (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
                   __A    =image_processor(
                       image_inputs[0] ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (1, max_patch, expected_hidden_dim) ,   )

                   # Test batched
                   __A    =image_processor(
                       lowerCamelCase_ ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,   )


       def     __UpperCamelCase     (							self     ):







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

             # Test not batched input
             __A    =(
                 (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
                   __A    =image_processor(
                       image_inputs[0] ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (1, max_patch, expected_hidden_dim) ,   )

                   # Test batched
                   __A    =image_processor(
                       lowerCamelCase_ ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).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        lowerCAmelCase__  (					_UpperCamelCase				,							unittest.TestCase			):



       '''simple docstring'''







       lowercase_						=    PixaStructImageProcessor if is_vision_available() else None

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =PixaStructImageProcessingTester(self ,   num_channels=4     )
             __A    =3

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             return self.image_processor_tester.prepare_image_processor_dict()

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.image_processing_class(**self.image_processor_dict     )
             self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_normalize'''     )     )
             self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_convert_rgb'''     )     )


       def     __UpperCamelCase     (							self     ):







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

             # Test not batched input
             __A    =(
                 (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
                   __A    =image_processor(
                       image_inputs[0] ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (1, max_patch, expected_hidden_dim) ,   )

                   # Test batched
                   __A    =image_processor(
                       lowerCamelCase_ ,   return_tensors='''pt''' ,   max_patches=lowerCamelCase_     ).flattened_patches
                   self.assertEqual(
                       encoded_images.shape ,   (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) ,   )









def       SCREAMING_SNAKE_CASE__  (       _lowercase     : int   ,    _lowercase     : int			)   ->							Optional[int]:




		'''simple docstring'''
		if b == 0:
				return 1
		if (b % 2) == 0:
				return actual_power(_lowercase   ,    int(b / 2			)			) * actual_power(_lowercase   ,    int(b / 2			)			)
		else:
				return a * actual_power(_lowercase   ,    int(b / 2			)			) * actual_power(_lowercase   ,    int(b / 2			)			)


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : int   ,    _lowercase     : int			)   ->							float:




		'''simple docstring'''
		if b < 0:
				return 1 / actual_power(_lowercase   ,    _lowercase			)
		return actual_power(_lowercase   ,    _lowercase			)


if __name__ == "__main__":
			print(power(-2, -3))


