






'''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			:		List[str]   				=    False

if is_vision_available():
			from PIL import Image

			from transformers import PixaStructImageProcessor
class  lowerCAmelCase_       (				unittest.TestCase					):




							def __init__(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=7 ,							SCREAMING_SNAKE_CASE_     :   Dict=3 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=18 ,							SCREAMING_SNAKE_CASE_     :   List[str]=30 ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=400 ,							SCREAMING_SNAKE_CASE_     :   List[str]=None ,							SCREAMING_SNAKE_CASE_     :   List[Any]=True ,							SCREAMING_SNAKE_CASE_     :   List[Any]=True ,							SCREAMING_SNAKE_CASE_     :   Dict=None ,							):
											lowerCAmelCase__      			=						size if size is not None else {'''height''': 20, '''width''': 20}
											lowerCAmelCase__      			=						parent
											lowerCAmelCase__      			=						batch_size
											lowerCAmelCase__      			=						num_channels
											lowerCAmelCase__      			=						image_size
											lowerCAmelCase__      			=						min_resolution
											lowerCAmelCase__      			=						max_resolution
											lowerCAmelCase__      			=						size
											lowerCAmelCase__      			=						do_normalize
											lowerCAmelCase__      			=						do_convert_rgb
											lowerCAmelCase__      			=						[512, 1_024, 2_048, 4_096]
											lowerCAmelCase__      			=						patch_size if patch_size is not None else {'''height''': 16, '''width''': 16}




							def 						__snake_case					(   self     :   str	):
											return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'''
											lowerCAmelCase__      			=						Image.open(requests.get(SCREAMING_SNAKE_CASE_ ,							stream=SCREAMING_SNAKE_CASE_	).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_       (				snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Tuple		      =    PixaStructImageProcessor if is_vision_available() else None




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						PixaStructImageProcessingTester(self	)




							@property
							def 						__snake_case					(   self     :   List[Any]	):
											return self.image_processor_tester.prepare_image_processor_dict()




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,							'''do_normalize'''	)	)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,							'''do_convert_rgb'''	)	)




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						self.image_processor_tester.prepare_dummy_image()

											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											lowerCAmelCase__      			=						2_048

											lowerCAmelCase__      			=						image_processor(SCREAMING_SNAKE_CASE_ ,							return_tensors='''pt''' ,							max_patches=SCREAMING_SNAKE_CASE_	)
											self.assertTrue(torch.allclose(inputs.flattened_patches.mean() ,							torch.tensor(0.0_606	) ,							atol=1e-3 ,							rtol=1e-3	)	)




							def 						__snake_case					(   self     :   int	):
											# Initialize image_processor
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											# create random PIL images
											lowerCAmelCase__      			=						prepare_image_inputs(self.image_processor_tester ,							equal_resolution=SCREAMING_SNAKE_CASE_	)
											for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,							Image.Image	)

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

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




							def 						__snake_case					(   self     :   Optional[Any]	):
											# Initialize image_processor
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											# create random PIL images
											lowerCAmelCase__      			=						prepare_image_inputs(self.image_processor_tester ,							equal_resolution=SCREAMING_SNAKE_CASE_	)
											for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,							Image.Image	)

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

											lowerCAmelCase__      			=						True

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

															lowerCAmelCase__      			=						'''Hello'''

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

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




							def 						__snake_case					(   self     :   str	):
											# Initialize image_processor
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											# create random numpy tensors
											lowerCAmelCase__      			=						prepare_image_inputs(self.image_processor_tester ,							equal_resolution=SCREAMING_SNAKE_CASE_ ,							numpify=SCREAMING_SNAKE_CASE_	)
											for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,							np.ndarray	)

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

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




							def 						__snake_case					(   self     :   Union[str, Any]	):
											# Initialize image_processor
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											# create random PyTorch tensors
											lowerCAmelCase__      			=						prepare_image_inputs(self.image_processor_tester ,							equal_resolution=SCREAMING_SNAKE_CASE_ ,							torchify=SCREAMING_SNAKE_CASE_	)
											for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,							torch.Tensor	)

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

															# Test batched
															lowerCAmelCase__      			=						image_processor(
															    SCREAMING_SNAKE_CASE_ ,							return_tensors='''pt''' ,							max_patches=SCREAMING_SNAKE_CASE_	).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_       (				snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Optional[int]		      =    PixaStructImageProcessor if is_vision_available() else None




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						PixaStructImageProcessingTester(self ,							num_channels=4	)
											lowerCAmelCase__      			=						3




							@property
							def 						__snake_case					(   self     :   Optional[int]	):
											return self.image_processor_tester.prepare_image_processor_dict()




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,							'''do_normalize'''	)	)
											self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_ ,							'''do_convert_rgb'''	)	)




							def 						__snake_case					(   self     :   Tuple	):
											# Initialize image_processor
											lowerCAmelCase__      			=						self.image_processing_class(**self.image_processor_dict	)
											# create random PIL images
											lowerCAmelCase__      			=						prepare_image_inputs(self.image_processor_tester ,							equal_resolution=SCREAMING_SNAKE_CASE_	)
											for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_ ,							Image.Image	)

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

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













from __future__ import annotations





def 	lowerCAmelCase_		(lowercase__						:       list[int]       )		->    bool:


				'''simple docstring'''




				return len(set(lowercase__       )       ) == len(lowercase__       )


if __name__ == "__main__":
			import doctest

			doctest.testmod()





