
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__    :		Union[str, Any]          =   False

if is_vision_available():
     from PIL import Image

     from transformers import PixaStructImageProcessor



class       _snake_case   (	unittest.TestCase							):


 def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_=7					,  SCREAMING_SNAKE_CASE_=3					,  SCREAMING_SNAKE_CASE_=18					,  SCREAMING_SNAKE_CASE_=30					,  SCREAMING_SNAKE_CASE_=4_00					,  SCREAMING_SNAKE_CASE_=None					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=None					,  ):


   '''simple docstring'''




   lowercase__  :   int    		=      size if size is not None else {"""height""": 20, """width""": 20}
   lowercase__  :   int    		=      parent
   lowercase__  :   Any    		=      batch_size
   lowercase__  :   str    		=      num_channels
   lowercase__  :   Union[str, Any]    		=      image_size
   lowercase__  :   Tuple    		=      min_resolution
   lowercase__  :   Any    		=      max_resolution
   lowercase__  :   Tuple    		=      size
   lowercase__  :   Any    		=      do_normalize
   lowercase__  :   Optional[int]    		=      do_convert_rgb
   lowercase__  :   Optional[Any]    		=      [5_12, 10_24, 20_48, 40_96]
   lowercase__  :   str    		=      patch_size if patch_size is not None else {"""height""": 16, """width""": 16}


 def      lowercase__     (					self):


   '''simple docstring'''




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



 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[Any]    		=      """https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"""
   lowercase__  :   Dict    		=      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       _snake_case   (	UpperCAmelCase_  ,						unittest.TestCase							):
 __lowerCAmelCase  :     Tuple        =			PixaStructImageProcessor if is_vision_available() else None


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      PixaStructImageProcessingTester(self)


 @property
 def      lowercase__     (					self):


   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      self.image_processor_tester.prepare_dummy_image()

   lowercase__  :   Union[str, Any]    		=      self.image_processing_class(**self.image_processor_dict)
   lowercase__  :   Optional[Any]    		=      20_48

   lowercase__  :   int    		=      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_6_0_6)					,  atol=1E-3					,  rtol=1E-3))


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.image_processing_class(**self.image_processor_dict)
   # create random PIL images
   lowercase__  :   Dict    		=      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
   lowercase__  :   Optional[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
     lowercase__  :   str    		=      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
     lowercase__  :   Tuple    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      self.image_processing_class(**self.image_processor_dict)
   # create random PIL images
   lowercase__  :   List[Any]    		=      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
   lowercase__  :   List[Any]    		=      (
       (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
       * self.image_processor_tester.num_channels
   ) + 2

   lowercase__  :   List[Any]    		=      True

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

     lowercase__  :   Any    		=      """Hello"""

     lowercase__  :   List[Any]    		=      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
     lowercase__  :   Dict    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[Any]    		=      self.image_processing_class(**self.image_processor_dict)
   # create random numpy tensors
   lowercase__  :   Optional[int]    		=      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)

   lowercase__  :   Optional[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
     lowercase__  :   Optional[int]    		=      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
     lowercase__  :   List[Any]    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   str    		=      self.image_processing_class(**self.image_processor_dict)
   # create random PyTorch tensors
   lowercase__  :   List[str]    		=      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
   lowercase__  :   int    		=      (
       (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
     lowercase__  :   Dict    		=      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
     lowercase__  :   str    		=      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       _snake_case   (	UpperCAmelCase_  ,						unittest.TestCase							):
 __lowerCAmelCase  :     List[str]        =			PixaStructImageProcessor if is_vision_available() else None


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      PixaStructImageProcessingTester(self					,  num_channels=4)
   lowercase__  :   Optional[Any]    		=      3


 @property
 def      lowercase__     (					self):


   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      self.image_processing_class(**self.image_processor_dict)
   # create random PIL images
   lowercase__  :   Optional[Any]    		=      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
   lowercase__  :   List[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
     lowercase__  :   Any    		=      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
     lowercase__  :   Optional[Any]    		=      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 		UpperCamelCase	(      lowercase_ = 10_00   )		->	int:


  '''simple docstring'''






  return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1   )   )


if __name__ == "__main__":
     print(solution())






