



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		,			a		,			a=7		,			a=3		,			a=1_8		,			a=3_0		,			a=4_0_0		,			a=True		,			a=None		,			a=True		,			a=None		,			a=True		,			a=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,			a=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,			a=True		,			)		->							int:




   """simple docstring"""







   _A							=			size if size is not None else {'shortest_edge': 2_0}
   _A							=			crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}
   _A							=			parent
   _A							=			batch_size
   _A							=			num_channels
   _A							=			image_size
   _A							=			min_resolution
   _A							=			max_resolution
   _A							=			do_resize
   _A							=			size
   _A							=			do_center_crop
   _A							=			crop_size
   _A							=			do_normalize
   _A							=			image_mean
   _A							=			image_std
   _A							=			do_convert_rgb
 def lowercase_						(  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 lowercase_						(  self		,			a=False		,			a=False		,			a=False    )		->							Tuple:




   """simple docstring"""







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

   if equal_resolution:
     _A							=			[]
     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:
     _A							=			[]
     for i in range(self.batch_size    ):
       _A							=			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
     _A							=			[Image.fromarray(np.moveaxis(a		,			0		,			-1    )    ) for x in image_inputs]

   if torchify:
     _A							=			[torch.from_numpy(a    ) for x in image_inputs]

   return image_inputs







@require_torch
@require_vision
class 						_snake_case					(	a_    ,unittest.TestCase ):





 """simple docstring"""



 lowerCamelCase_								=	CLIPImageProcessor if is_vision_available() else None
 def lowercase_						(  self    )		->							int:




   """simple docstring"""







   _A							=			CLIPImageProcessingTester(self    )
 @property
 def lowercase_						(  self    )		->							List[str]:




   """simple docstring"""







   return self.image_processor_tester.prepare_image_processor_dict()
 def lowercase_						(  self    )		->							str:




   """simple docstring"""







   _A							=			self.image_processing_class(**self.image_processor_dict    )
   self.assertTrue(hasattr(a		,			'''do_resize'''    )    )
   self.assertTrue(hasattr(a		,			'''size'''    )    )
   self.assertTrue(hasattr(a		,			'''do_center_crop'''    )    )
   self.assertTrue(hasattr(a		,			'''center_crop'''    )    )
   self.assertTrue(hasattr(a		,			'''do_normalize'''    )    )
   self.assertTrue(hasattr(a		,			'''image_mean'''    )    )
   self.assertTrue(hasattr(a		,			'''image_std'''    )    )
   self.assertTrue(hasattr(a		,			'''do_convert_rgb'''    )    )
 def lowercase_						(  self    )		->							Union[str, Any]:




   """simple docstring"""







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

   _A							=			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 lowercase_						(  self    )		->							str:




   """simple docstring"""







   pass
 def lowercase_						(  self    )		->							Optional[int]:




   """simple docstring"""







   _A							=			self.image_processing_class(**self.image_processor_dict    )
   # create random PIL images
   _A							=			self.image_processor_tester.prepare_inputs(equal_resolution=a    )
   for image in image_inputs:
     self.assertIsInstance(a		,			Image.Image    )

   # Test not batched input
   _A							=			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
   _A							=			image_processing(a		,			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 lowercase_						(  self    )		->							List[str]:




   """simple docstring"""







   _A							=			self.image_processing_class(**self.image_processor_dict    )
   # create random numpy tensors
   _A							=			self.image_processor_tester.prepare_inputs(equal_resolution=a		,			numpify=a    )
   for image in image_inputs:
     self.assertIsInstance(a		,			np.ndarray    )

   # Test not batched input
   _A							=			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
   _A							=			image_processing(a		,			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 lowercase_						(  self    )		->							Any:




   """simple docstring"""







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

   # Test not batched input
   _A							=			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
   _A							=			image_processing(a		,			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					(	a_    ,unittest.TestCase ):





 """simple docstring"""



 lowerCamelCase_								=	CLIPImageProcessor if is_vision_available() else None
 def lowercase_						(  self    )		->							str:




   """simple docstring"""







   _A							=			CLIPImageProcessingTester(self		,			num_channels=4    )
   _A							=			3
 @property
 def lowercase_						(  self    )		->							List[str]:




   """simple docstring"""







   return self.image_processor_tester.prepare_image_processor_dict()
 def lowercase_						(  self    )		->							int:




   """simple docstring"""







   _A							=			self.image_processing_class(**self.image_processor_dict    )
   self.assertTrue(hasattr(a		,			'''do_resize'''    )    )
   self.assertTrue(hasattr(a		,			'''size'''    )    )
   self.assertTrue(hasattr(a		,			'''do_center_crop'''    )    )
   self.assertTrue(hasattr(a		,			'''center_crop'''    )    )
   self.assertTrue(hasattr(a		,			'''do_normalize'''    )    )
   self.assertTrue(hasattr(a		,			'''image_mean'''    )    )
   self.assertTrue(hasattr(a		,			'''image_std'''    )    )
   self.assertTrue(hasattr(a		,			'''do_convert_rgb'''    )    )
 def lowercase_						(  self    )		->							Union[str, Any]:




   """simple docstring"""







   pass



 def lowercase_						(  self    )		->							Any:




   """simple docstring"""







   _A							=			self.image_processing_class(**self.image_processor_dict    )
   # create random PIL images
   _A							=			self.image_processor_tester.prepare_inputs(equal_resolution=a    )
   for image in image_inputs:
     self.assertIsInstance(a		,			Image.Image    )

   # Test not batched input
   _A							=			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
   _A							=			image_processing(a		,			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 SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int						,  lowercase_:		int ):
       assert n > 1 and a > 0 and greatest_common_divisor(lowercase_						,  lowercase_ ) == 1
       ((_UpperCamelCase)  ,					(_UpperCamelCase)  ,					(_UpperCamelCase))   :				Optional[int]       =     extended_gcd(lowercase_						,  lowercase_ )  # Implemented below
       _UpperCamelCase   :				str       =     (b * s) % n
       return x


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       ((_UpperCamelCase)  ,					(_UpperCamelCase))   :				Union[str, Any]       =     extended_euclid(lowercase_						,  lowercase_ )  # Implemented below
       if b < 0:
              _UpperCamelCase   :				Union[str, Any]       =     (b % n + n) % n
       return b


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int						,  lowercase_:		int ):
       _UpperCamelCase   :				Union[str, Any]       =     invert_modulo(lowercase_						,  lowercase_ )
       _UpperCamelCase   :				str       =     (b * s) % n
       return x


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       assert a >= 0 and b >= 0

       if b == 0:
              _UpperCamelCase  ,					_UpperCamelCase  ,					_UpperCamelCase   :				Any       =     a, 1, 0
       else:
              ((_UpperCamelCase)  ,					(_UpperCamelCase)  ,					(_UpperCamelCase))   :				Dict       =     extended_gcd(lowercase_						,  a % b )
              _UpperCamelCase   :				List[str]       =     q
              _UpperCamelCase   :				Any       =     p - q * (a // b)

       assert a % d == 0 and b % d == 0
       assert d == a * x + b * y

       return (d, x, y)


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       if b == 0:
              return (1, 0)
       ((_UpperCamelCase)  ,					(_UpperCamelCase))   :				Optional[Any]       =     extended_euclid(lowercase_						,  a % b )
       _UpperCamelCase   :				List[Any]       =     a // b
       return (y, x - k * y)


def SCREAMING_SNAKE_CASE (   lowercase_:		int						,  lowercase_:		int ):
       if a < b:
              _UpperCamelCase  ,					_UpperCamelCase   :				Optional[int]       =     b, a

       while a % b != 0:
              _UpperCamelCase  ,					_UpperCamelCase   :				Union[str, Any]       =     b, a % b

       return b


if __name__ == "__main__":
 from doctest import testmod

 testmod(name='''modular_division''', verbose=True)
 testmod(name='''modular_division2''', verbose=True)
 testmod(name='''invert_modulo''', verbose=True)
 testmod(name='''extended_gcd''', verbose=True)
 testmod(name='''extended_euclid''', verbose=True)
 testmod(name='''greatest_common_divisor''', verbose=True)



