


import unittest

import numpy as np

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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import CLIPImageProcessor



class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







      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=True   ,			__SCREAMING_SNAKE_CASE=None   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=None   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]   ,			__SCREAMING_SNAKE_CASE=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]   ,			__SCREAMING_SNAKE_CASE=True   ,			)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=size if size is not None else {'shortest_edge': 20}
         UpperCamelCase__ :			Optional[Any] 						=crop_size if crop_size is not None else {'height': 18, 'width': 18}
         UpperCamelCase__ :			int 						=parent
         UpperCamelCase__ :			Optional[Any] 						=batch_size
         UpperCamelCase__ :			List[Any] 						=num_channels
         UpperCamelCase__ :			Any 						=image_size
         UpperCamelCase__ :			Union[str, Any] 						=min_resolution
         UpperCamelCase__ :			str 						=max_resolution
         UpperCamelCase__ :			str 						=do_resize
         UpperCamelCase__ :			Optional[Any] 						=size
         UpperCamelCase__ :			int 						=do_center_crop
         UpperCamelCase__ :			str 						=crop_size
         UpperCamelCase__ :			Optional[int] 						=do_normalize
         UpperCamelCase__ :			List[Any] 						=image_mean
         UpperCamelCase__ :			Tuple 						=image_std
         UpperCamelCase__ :			Optional[int] 						=do_convert_rgb




      def    UpperCAmelCase							(		self)					->						Optional[Any]:







         """simple docstring"""







         return {
             "do_resize": self.do_resize,
             "size": self.size,
             "do_center_crop": self.do_center_crop,
             "crop_size": self.crop_size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_convert_rgb": self.do_convert_rgb,
         }




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE=False   ,			__SCREAMING_SNAKE_CASE=False   ,			__SCREAMING_SNAKE_CASE=False)					->						Optional[int]:







         """simple docstring"""







         assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

         if equal_resolution:
            UpperCamelCase__ :			List[Any] 						=[]
            for i in range(self.batch_size):
               image_inputs.append(
                   np.random.randint(
                       2_55   ,			size=(self.num_channels, self.max_resolution, self.max_resolution)   ,			dtype=np.uinta))
         else:
            UpperCamelCase__ :			int 						=[]
            for i in range(self.batch_size):
               UpperCamelCase__ :			Any 						=np.random.choice(np.arange(self.min_resolution   ,			self.max_resolution)   ,			2)
               image_inputs.append(np.random.randint(2_55   ,			size=(self.num_channels, width, height)   ,			dtype=np.uinta))

         if not numpify and not torchify:
            # PIL expects the channel dimension as last dimension
            UpperCamelCase__ :			Any 						=[Image.fromarray(np.moveaxis(_snake_case   ,			0   ,			-1)) for x in image_inputs]

         if torchify:
            UpperCamelCase__ :			List[str] 						=[torch.from_numpy(_snake_case) for x in image_inputs]

         return image_inputs




@require_torch
@require_vision
class 							lowercase__(						snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					CLIPImageProcessor if is_vision_available() else None




      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=CLIPImageProcessingTester(self)




      @property
      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         return self.image_processor_tester.prepare_image_processor_dict()




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=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_center_crop"))
         self.assertTrue(hasattr(_snake_case   ,			"center_crop"))
         self.assertTrue(hasattr(_snake_case   ,			"do_normalize"))
         self.assertTrue(hasattr(_snake_case   ,			"image_mean"))
         self.assertTrue(hasattr(_snake_case   ,			"image_std"))
         self.assertTrue(hasattr(_snake_case   ,			"do_convert_rgb"))




      def    UpperCAmelCase							(		self)					->						Optional[int]:







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=self.image_processing_class.from_dict(self.image_processor_dict)
         self.assertEqual(image_processor.size   ,			{"shortest_edge": 20})
         self.assertEqual(image_processor.crop_size   ,			{"height": 18, "width": 18})

         UpperCamelCase__ :			Union[str, Any] 						=self.image_processing_class.from_dict(self.image_processor_dict   ,			size=42   ,			crop_size=84)
         self.assertEqual(image_processor.size   ,			{"shortest_edge": 42})
         self.assertEqual(image_processor.crop_size   ,			{"height": 84, "width": 84})




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         pass




      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







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

         # Test not batched input
         UpperCamelCase__ :			str 						=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.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)

         # Test batched
         UpperCamelCase__ :			Optional[int] 						=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.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)




      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.image_processing_class(**self.image_processor_dict)
         # create random numpy tensors
         UpperCamelCase__ :			Optional[int] 						=self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case   ,			numpify=_snake_case)
         for image in image_inputs:
            self.assertIsInstance(_snake_case   ,			np.ndarray)

         # Test not batched input
         UpperCamelCase__ :			Dict 						=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.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)

         # Test batched
         UpperCamelCase__ :			int 						=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.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__ :			str 						=self.image_processing_class(**self.image_processor_dict)
         # create random PyTorch tensors
         UpperCamelCase__ :			str 						=self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case   ,			torchify=_snake_case)
         for image in image_inputs:
            self.assertIsInstance(_snake_case   ,			torch.Tensor)

         # Test not batched input
         UpperCamelCase__ :			Dict 						=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.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)

         # Test batched
         UpperCamelCase__ :			str 						=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.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)




@require_torch
@require_vision
class 							lowercase__(						snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					CLIPImageProcessor if is_vision_available() else None




      def    UpperCAmelCase							(		self)					->						Optional[Any]:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=CLIPImageProcessingTester(self   ,			num_channels=4)
         UpperCamelCase__ :			Optional[Any] 						=3




      @property
      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         return self.image_processor_tester.prepare_image_processor_dict()




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=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_center_crop"))
         self.assertTrue(hasattr(_snake_case   ,			"center_crop"))
         self.assertTrue(hasattr(_snake_case   ,			"do_normalize"))
         self.assertTrue(hasattr(_snake_case   ,			"image_mean"))
         self.assertTrue(hasattr(_snake_case   ,			"image_std"))
         self.assertTrue(hasattr(_snake_case   ,			"do_convert_rgb"))




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







         """simple docstring"""







         pass




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=self.image_processing_class(**self.image_processor_dict)
         # create random PIL images
         UpperCamelCase__ :			Union[str, Any] 						=self.image_processor_tester.prepare_inputs(equal_resolution=_snake_case)
         for image in image_inputs:
            self.assertIsInstance(_snake_case   ,			Image.Image)

         # Test not batched input
         UpperCamelCase__ :			Optional[Any] 						=image_processing(image_inputs[0]   ,			return_tensors="pt").pixel_values
         self.assertEqual(
             encoded_images.shape   ,			(
                 1,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)

         # Test batched
         UpperCamelCase__ :			List[str] 						=image_processing(_snake_case   ,			return_tensors="pt").pixel_values
         self.assertEqual(
             encoded_images.shape   ,			(
                 self.image_processor_tester.batch_size,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.crop_size["height"],
                 self.image_processor_tester.crop_size["width"],
             )   ,			)









def       _a   (							lowerCamelCase__				)  ->      list[int]:
 if len(lowerCamelCase__				) > 1:
  lowerCamelCase_							:    Optional[Any]							=     len(lowerCamelCase__				) // 2  # Finds the middle of the array
  lowerCamelCase_							:    int							=     arr[
      :middle_length
  ]  # Creates an array of the elements in the first half.
  lowerCamelCase_							:    List[Any]							=     arr[
      middle_length:
  ]  # Creates an array of the elements in the second half.
  lowerCamelCase_							:    Optional[Any]							=     len(lowerCamelCase__				)
  lowerCamelCase_							:    Optional[Any]							=     len(lowerCamelCase__				)
  merge(lowerCamelCase__				)  # Starts sorting the left.
  merge(lowerCamelCase__				)  # Starts sorting the right
  lowerCamelCase_							:    Dict							=     0  # Left Counter
  lowerCamelCase_							:    str							=     0  # Right Counter
  lowerCamelCase_							:    Tuple							=     0  # Position Counter
  while (
      left_index < left_size and right_index < right_size
  ):  # Runs until the lowers size of the left and right are sorted.
   if left_array[left_index] < right_array[right_index]:
    lowerCamelCase_							:    Optional[int]							=     left_array[left_index]
    left_index += 1
   else:
    lowerCamelCase_							:    Union[str, Any]							=     right_array[right_index]
    right_index += 1
   index += 1
  while (
      left_index < left_size
  ):  # Adds the left over elements in the left half of the array
   lowerCamelCase_							:    Tuple							=     left_array[left_index]
   left_index += 1
   index += 1
  while (
      right_index < right_size
  ):  # Adds the left over elements in the right half of the array
   lowerCamelCase_							:    Optional[int]							=     right_array[right_index]
   right_index += 1
   index += 1
 return arr


if __name__ == "__main__":
       import doctest

       doctest.testmod()
