






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 					snake_case_							(       unittest.TestCase      ):







   """simple docstring"""



   def __init__(    self:	str   ,       snake_case__:	Any   ,       snake_case__:	Optional[Any]=7   ,       snake_case__:	Optional[Any]=3   ,       snake_case__:	List[str]=18   ,       snake_case__:	Any=30   ,       snake_case__:	List[Any]=400   ,       snake_case__:	Any=True   ,       snake_case__:	Tuple=None   ,       snake_case__:	Any=True   ,       snake_case__:	List[Any]=None   ,       snake_case__:	Optional[int]=True   ,       snake_case__:	Optional[int]=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]   ,       snake_case__:	str=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]   ,       snake_case__:	int=True   ,       )  ->		List[Any]:


       """simple docstring"""



       _lowercase     =	size if size is not None else {'shortest_edge': 20}
       _lowercase     =	crop_size if crop_size is not None else {'height': 18, 'width': 18}
       _lowercase     =	parent
       _lowercase     =	batch_size
       _lowercase     =	num_channels
       _lowercase     =	image_size
       _lowercase     =	min_resolution
       _lowercase     =	max_resolution
       _lowercase     =	do_resize
       _lowercase     =	size
       _lowercase     =	do_center_crop
       _lowercase     =	crop_size
       _lowercase     =	do_normalize
       _lowercase     =	image_mean
       _lowercase     =	image_std
       _lowercase     =	do_convert_rgb

   def 			SCREAMING_SNAKE_CASE__				(    self:	int	)  ->		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 			SCREAMING_SNAKE_CASE__				(    self:	Optional[Any]   ,       snake_case__:	Optional[int]=False   ,       snake_case__:	int=False   ,       snake_case__:	Dict=False	)  ->		int:


       """simple docstring"""



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

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

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

       if torchify:
           _lowercase     =	[torch.from_numpy(snake_case__	) for x in image_inputs]

       return image_inputs






@require_torch
@require_vision
class 					snake_case_							(       lowercase_      ,			unittest.TestCase      ):







   """simple docstring"""



   __lowerCAmelCase      =						CLIPImageProcessor if is_vision_available() else None

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


       """simple docstring"""



       _lowercase     =	CLIPImageProcessingTester(self	)

   @property
   def 			SCREAMING_SNAKE_CASE__				(    self:	Any	)  ->		List[str]:


       """simple docstring"""



       return self.image_processor_tester.prepare_image_processor_dict()

   def 			SCREAMING_SNAKE_CASE__				(    self:	str	)  ->		Any:


       """simple docstring"""



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


       """simple docstring"""



       _lowercase     =	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}	)

       _lowercase     =	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 			SCREAMING_SNAKE_CASE__				(    self:	List[str]	)  ->		str:


       """simple docstring"""



       pass

   def 			SCREAMING_SNAKE_CASE__				(    self:	Dict	)  ->		List[str]:


       """simple docstring"""



       _lowercase     =	self.image_processing_class(**self.image_processor_dict	)
       # create random PIL images
       _lowercase     =	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
       _lowercase     =	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
       _lowercase     =	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 			SCREAMING_SNAKE_CASE__				(    self:	Any	)  ->		Optional[Any]:


       """simple docstring"""



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


       """simple docstring"""



       _lowercase     =	self.image_processing_class(**self.image_processor_dict	)
       # create random PyTorch tensors
       _lowercase     =	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
       _lowercase     =	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
       _lowercase     =	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 					snake_case_							(       lowercase_      ,			unittest.TestCase      ):







   """simple docstring"""



   __lowerCAmelCase      =						CLIPImageProcessor if is_vision_available() else None

   def 			SCREAMING_SNAKE_CASE__				(    self:	Optional[Any]	)  ->		Tuple:


       """simple docstring"""



       _lowercase     =	CLIPImageProcessingTester(self   ,       num_channels=4	)
       _lowercase     =	3

   @property
   def 			SCREAMING_SNAKE_CASE__				(    self:	Dict	)  ->		Optional[int]:


       """simple docstring"""



       return self.image_processor_tester.prepare_image_processor_dict()

   def 			SCREAMING_SNAKE_CASE__				(    self:	Any	)  ->		List[Any]:


       """simple docstring"""



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


       """simple docstring"""



       pass

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


       """simple docstring"""



       _lowercase     =	self.image_processing_class(**self.image_processor_dict	)
       # create random PIL images
       _lowercase     =	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
       _lowercase     =	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
       _lowercase     =	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'],
           )   ,       )








from __future__ import annotations
def        UpperCAmelCase      (   _lowercase,      _lowercase      ):


    '''simple docstring'''



    _lowercase     =	[None] * (len(_lowercase      ) + len(_lowercase      ))

    _lowercase     =	0  # pointer to current index for left Half
    _lowercase     =	0  # pointer to current index for the right Half
    _lowercase     =	0  # pointer to current index for the sorted array Half

    while pointera < len(_lowercase      ) and pointera < len(_lowercase      ):
        if left_half[pointera] < right_half[pointera]:
            _lowercase     =	left_half[pointera]
            pointera += 1
            index += 1
        else:
            _lowercase     =	right_half[pointera]
            pointera += 1
            index += 1
    while pointera < len(_lowercase      ):
        _lowercase     =	left_half[pointera]
        pointera += 1
        index += 1

    while pointera < len(_lowercase      ):
        _lowercase     =	right_half[pointera]
        pointera += 1
        index += 1

    return sorted_array
def        UpperCAmelCase      (   _lowercase      ):


    '''simple docstring'''



    if len(_lowercase      ) <= 1:
        return array
    # the actual formula to calculate the middle element = left + (right - left) // 2
    # this avoids integer overflow in case of large N
    _lowercase     =	0 + (len(_lowercase      ) - 0) // 2

    # Split the array into halves till the array length becomes equal to One
    # merge the arrays of single length returned by mergeSort function and
    # pass them into the merge arrays function which merges the array
    _lowercase     =	array[:middle]
    _lowercase     =	array[middle:]

    return merge(merge_sort(_lowercase      ),      merge_sort(_lowercase      )      )


if __name__ == "__main__":
   import doctest

   doctest.testmod()
