


"""simple docstring"""





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						):







      def __init__(      self :							List[str]		,  _lowercase :							str		,  _lowercase :							Union[str, Any]=7		,  _lowercase :							Tuple=3		,  _lowercase :							Union[str, Any]=1_8		,  _lowercase :							Dict=3_0		,  _lowercase :							List[Any]=4_0_0		,  _lowercase :							Tuple=True		,  _lowercase :							str=None		,  _lowercase :							Optional[Any]=True		,  _lowercase :							List[Any]=None		,  _lowercase :							Optional[int]=True		,  _lowercase :							Any=[0.4814_5466, 0.457_8275, 0.4082_1073]		,  _lowercase :							List[Any]=[0.2686_2954, 0.2613_0258, 0.2757_7711]		,  _lowercase :							List[Any]=True		,  ):
        lowerCAmelCase__    :    Tuple								=					size if size is not None else {"shortest_edge": 2_0}
        lowerCAmelCase__    :    Union[str, Any]								=					crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
        lowerCAmelCase__    :    str								=					parent
        lowerCAmelCase__    :    Union[str, Any]								=					batch_size
        lowerCAmelCase__    :    Optional[Any]								=					num_channels
        lowerCAmelCase__    :    Tuple								=					image_size
        lowerCAmelCase__    :    Any								=					min_resolution
        lowerCAmelCase__    :    int								=					max_resolution
        lowerCAmelCase__    :    List[Any]								=					do_resize
        lowerCAmelCase__    :    List[str]								=					size
        lowerCAmelCase__    :    Optional[int]								=					do_center_crop
        lowerCAmelCase__    :    Dict								=					crop_size
        lowerCAmelCase__    :    Tuple								=					do_normalize
        lowerCAmelCase__    :    Optional[Any]								=					image_mean
        lowerCAmelCase__    :    List[Any]								=					image_std
        lowerCAmelCase__    :    Union[str, Any]								=					do_convert_rgb







      def 				_lowerCAmelCase       (      self :							Dict    ):
        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 				_lowerCAmelCase       (      self :							str		,  _lowercase :							List[Any]=False		,  _lowercase :							Any=False		,  _lowercase :							str=False    ):
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

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

        return image_inputs

@require_torch
@require_vision
class  lowercase_  ( a_							,							unittest.TestCase						):
      __magic_name__     :							Optional[int]          =				CLIPImageProcessor if is_vision_available() else None







      def 				_lowerCAmelCase       (      self :							str    ):
        lowerCAmelCase__    :    int								=					CLIPImageProcessingTester(self    )







      @property
      def 				_lowerCAmelCase       (      self :							str    ):
        return self.image_processor_tester.prepare_image_processor_dict()







      def 				_lowerCAmelCase       (      self :							Optional[int]    ):
        lowerCAmelCase__    :    int								=					self.image_processing_class(**self.image_processor_dict    )
        self.assertTrue(hasattr(_lowercase		,  "do_resize"    )    )
        self.assertTrue(hasattr(_lowercase		,  "size"    )    )
        self.assertTrue(hasattr(_lowercase		,  "do_center_crop"    )    )
        self.assertTrue(hasattr(_lowercase		,  "center_crop"    )    )
        self.assertTrue(hasattr(_lowercase		,  "do_normalize"    )    )
        self.assertTrue(hasattr(_lowercase		,  "image_mean"    )    )
        self.assertTrue(hasattr(_lowercase		,  "image_std"    )    )
        self.assertTrue(hasattr(_lowercase		,  "do_convert_rgb"    )    )







      def 				_lowerCAmelCase       (      self :							Union[str, Any]    ):
        lowerCAmelCase__    :    Union[str, Any]								=					self.image_processing_class.from_dict(self.image_processor_dict    )
        self.assertEqual(image_processor.size		,  {"shortest_edge": 2_0}    )
        self.assertEqual(image_processor.crop_size		,  {"height": 1_8, "width": 1_8}    )

        lowerCAmelCase__    :    List[Any]								=					self.image_processing_class.from_dict(self.image_processor_dict		,  size=4_2		,  crop_size=8_4    )
        self.assertEqual(image_processor.size		,  {"shortest_edge": 4_2}    )
        self.assertEqual(image_processor.crop_size		,  {"height": 8_4, "width": 8_4}    )







      def 				_lowerCAmelCase       (      self :							Tuple    ):
        pass







      def 				_lowerCAmelCase       (      self :							List[str]    ):
        # Initialize image_processing
        lowerCAmelCase__    :    List[str]								=					self.image_processing_class(**self.image_processor_dict    )
        # create random PIL images
        lowerCAmelCase__    :    Tuple								=					self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase    )
        for image in image_inputs:
          self.assertIsInstance(_lowercase		,  Image.Image    )

        # Test not batched input
        lowerCAmelCase__    :    Optional[Any]								=					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
        lowerCAmelCase__    :    Any								=					image_processing(_lowercase		,  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 				_lowerCAmelCase       (      self :							Any    ):
        # Initialize image_processing
        lowerCAmelCase__    :    str								=					self.image_processing_class(**self.image_processor_dict    )
        # create random numpy tensors
        lowerCAmelCase__    :    int								=					self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase		,  numpify=_lowercase    )
        for image in image_inputs:
          self.assertIsInstance(_lowercase		,  np.ndarray    )

        # Test not batched input
        lowerCAmelCase__    :    int								=					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
        lowerCAmelCase__    :    Union[str, Any]								=					image_processing(_lowercase		,  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 				_lowerCAmelCase       (      self :							List[str]    ):
        # Initialize image_processing
        lowerCAmelCase__    :    int								=					self.image_processing_class(**self.image_processor_dict    )
        # create random PyTorch tensors
        lowerCAmelCase__    :    Optional[int]								=					self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase		,  torchify=_lowercase    )
        for image in image_inputs:
          self.assertIsInstance(_lowercase		,  torch.Tensor    )

        # Test not batched input
        lowerCAmelCase__    :    List[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
        lowerCAmelCase__    :    int								=					image_processing(_lowercase		,  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_  ( a_							,							unittest.TestCase						):
      __magic_name__     :							int          =				CLIPImageProcessor if is_vision_available() else None







      def 				_lowerCAmelCase       (      self :							Optional[int]    ):
        lowerCAmelCase__    :    Any								=					CLIPImageProcessingTester(self		,  num_channels=4    )
        lowerCAmelCase__    :    int								=					3







      @property
      def 				_lowerCAmelCase       (      self :							Dict    ):
        return self.image_processor_tester.prepare_image_processor_dict()







      def 				_lowerCAmelCase       (      self :							List[Any]    ):
        lowerCAmelCase__    :    Tuple								=					self.image_processing_class(**self.image_processor_dict    )
        self.assertTrue(hasattr(_lowercase		,  "do_resize"    )    )
        self.assertTrue(hasattr(_lowercase		,  "size"    )    )
        self.assertTrue(hasattr(_lowercase		,  "do_center_crop"    )    )
        self.assertTrue(hasattr(_lowercase		,  "center_crop"    )    )
        self.assertTrue(hasattr(_lowercase		,  "do_normalize"    )    )
        self.assertTrue(hasattr(_lowercase		,  "image_mean"    )    )
        self.assertTrue(hasattr(_lowercase		,  "image_std"    )    )
        self.assertTrue(hasattr(_lowercase		,  "do_convert_rgb"    )    )







      def 				_lowerCAmelCase       (      self :							Optional[int]    ):
        pass







      def 				_lowerCAmelCase       (      self :							Optional[int]    ):
        # Initialize image_processing
        lowerCAmelCase__    :    List[Any]								=					self.image_processing_class(**self.image_processor_dict    )
        # create random PIL images
        lowerCAmelCase__    :    Optional[int]								=					self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase    )
        for image in image_inputs:
          self.assertIsInstance(_lowercase		,  Image.Image    )

        # Test not batched input
        lowerCAmelCase__    :    Dict								=					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
        lowerCAmelCase__    :    Union[str, Any]								=					image_processing(_lowercase		,  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"],
            )		,  )




"""simple docstring"""





from __future__ import annotations





def     lowercase__	(  lowerCamelCase     :  list				,	lowerCamelCase     :  list				)					->     list:
  lowerCAmelCase__    :    Dict								=					[None] * (len(lowerCamelCase				) + len(lowerCamelCase				))

  lowerCAmelCase__    :    Optional[Any]								=					0  # pointer to current index for left Half
  lowerCAmelCase__    :    Any								=					0  # pointer to current index for the right Half
  lowerCAmelCase__    :    Dict								=					0  # pointer to current index for the sorted array Half

  while pointera < len(lowerCamelCase				) and pointera < len(lowerCamelCase				):
    if left_half[pointera] < right_half[pointera]:
      lowerCAmelCase__    :    Tuple								=					left_half[pointera]
      pointera += 1
      index += 1
    else:
      lowerCAmelCase__    :    int								=					right_half[pointera]
      pointera += 1
      index += 1
  while pointera < len(lowerCamelCase				):
    lowerCAmelCase__    :    Tuple								=					left_half[pointera]
    pointera += 1
    index += 1

  while pointera < len(lowerCamelCase				):
    lowerCAmelCase__    :    Optional[int]								=					right_half[pointera]
    pointera += 1
    index += 1

  return sorted_array





def     lowercase__	(  lowerCamelCase     :  list				)					->     list:
  if len(lowerCamelCase				) <= 1:
    return array
  # the actual formula to calculate the middle element = left + (right - left) // 2
  # this avoids integer overflow in case of large N
  lowerCAmelCase__    :    Optional[int]								=					0 + (len(lowerCamelCase				) - 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
  lowerCAmelCase__    :    Union[str, Any]								=					array[:middle]
  lowerCAmelCase__    :    List[str]								=					array[middle:]

  return merge(merge_sort(lowerCamelCase				)				,	merge_sort(lowerCamelCase				)				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()
