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




  '''simple docstring'''







  def __init__(						self							,  lowercase_							,  lowercase_=7							,  lowercase_=3							,  lowercase_=18							,  lowercase_=30							,  lowercase_=400							,  lowercase_=True							,  lowercase_=None							,  lowercase_=True							,  lowercase_=None							,  lowercase_=True							,  lowercase_=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]							,  lowercase_=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]							,  lowercase_=True							,  ):

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







  def 			UpperCamelCase__   (						self    ):

     """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							,  lowercase_=False							,  lowercase_=False							,  lowercase_=False    ):

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

     if equal_resolution:
        UpperCAmelCase_   :   Optional[int]     	=       []
        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:
        UpperCAmelCase_   :   List[str]     	=       []
        for i in range(self.batch_size    ):
           UpperCAmelCase_	,  UpperCAmelCase_   :   List[Any]     	=       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
        UpperCAmelCase_   :   Dict     	=       [Image.fromarray(np.moveaxis(lowercase_							,  0							,  -1    )    ) for x in image_inputs]

     if torchify:
        UpperCAmelCase_   :   List[Any]     	=       [torch.from_numpy(lowercase_    ) for x in image_inputs]

     return image_inputs




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




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		Dict    	=						CLIPImageProcessor if is_vision_available() else None







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       CLIPImageProcessingTester(self    )







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.image_processor_tester.prepare_image_processor_dict()







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       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 			UpperCamelCase__   (						self    ):

     """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_   :   List[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    ):

     """simple docstring"""
     pass







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     # Initialize image_processing
     UpperCAmelCase_   :   Dict     	=       self.image_processing_class(**self.image_processor_dict    )
     # create random PIL images
     UpperCAmelCase_   :   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
     UpperCAmelCase_   :   Optional[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
     UpperCAmelCase_   :   List[str]     	=       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 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     # Initialize image_processing
     UpperCAmelCase_   :   str     	=       self.image_processing_class(**self.image_processor_dict    )
     # create random numpy tensors
     UpperCAmelCase_   :   Optional[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
     UpperCAmelCase_   :   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
     UpperCAmelCase_   :   Optional[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"],
         )							,  )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     # Initialize image_processing
     UpperCAmelCase_   :   List[Any]     	=       self.image_processing_class(**self.image_processor_dict    )
     # create random PyTorch tensors
     UpperCAmelCase_   :   Optional[Any]     	=       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
     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_   :   List[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"],
         )							,  )




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




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		Any    	=						CLIPImageProcessor if is_vision_available() else None







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       CLIPImageProcessingTester(self							,  num_channels=4    )
     UpperCAmelCase_   :   Dict     	=       3







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.image_processor_tester.prepare_image_processor_dict()







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       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 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     pass







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     # Initialize image_processing
     UpperCAmelCase_   :   Dict     	=       self.image_processing_class(**self.image_processor_dict    )
     # create random PIL images
     UpperCAmelCase_   :   str     	=       self.image_processor_tester.prepare_inputs(equal_resolution=lowercase_    )
     for image in image_inputs:
        self.assertIsInstance(lowercase_							,  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_   :   Dict     	=       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"""





import math
from timeit import timeit







def       __a	(						__lowerCamelCase    ):
   UpperCAmelCase_   :   Tuple     	=       0
   UpperCAmelCase_   :   Any     	=       abs(__lowerCamelCase    )
   while True:
      UpperCAmelCase_   :   Optional[int]     	=       n // 10
      digits += 1
      if n == 0:
         break
   return digits







def       __a	(						__lowerCamelCase    ):
   return 1 if n == 0 else math.floor(math.log(abs(__lowerCamelCase    ),						10    ) + 1    )







def       __a	(						__lowerCamelCase    ):
   return len(str(abs(__lowerCamelCase    )    )    )







def       __a	(						):
   from collections.abc import Callable

   def benchmark_a_function(__lowerCamelCase,						__lowerCamelCase    ) -> None:
      UpperCAmelCase_   :   Optional[Any]     	=       f"""{func.__name__}({value})"""
      UpperCAmelCase_   :   Optional[int]     	=       timeit(f"""__main__.{call}""",						setup="import __main__"    )
      print(f"""{call}: {func(__lowerCamelCase    )} -- {timing} seconds"""    )

   for value in (26_2144, 1125_8999_0684_2624, 126_7650_6002_2822_9401_4967_0320_5376):
      for func in (num_digits, num_digits_fast, num_digits_faster):
         benchmark_a_function(__lowerCamelCase,						__lowerCamelCase    )
      print()


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()


