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


 '''simple docstring'''






 def __init__(				self       :     Tuple				,	SCREAMING_SNAKE_CASE__       :     Dict				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=7				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=3				,	SCREAMING_SNAKE_CASE__       :     Union[str, Any]=18				,	SCREAMING_SNAKE_CASE__       :     str=30				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=4_00				,	SCREAMING_SNAKE_CASE__       :     Union[str, Any]=True				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=None				,	SCREAMING_SNAKE_CASE__       :     Union[str, Any]=True				,	SCREAMING_SNAKE_CASE__       :     Any=None				,	SCREAMING_SNAKE_CASE__       :     Union[str, Any]=True				,	SCREAMING_SNAKE_CASE__       :     List[Any]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]				,	SCREAMING_SNAKE_CASE__       :     Any=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]				,	SCREAMING_SNAKE_CASE__       :     Tuple=True				,	)  ->   Tuple:
  __lowerCAmelCase    							=						size if size is not None else {"""shortest_edge""": 20}
  __lowerCAmelCase    							=						crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
  __lowerCAmelCase    							=						parent
  __lowerCAmelCase    							=						batch_size
  __lowerCAmelCase    							=						num_channels
  __lowerCAmelCase    							=						image_size
  __lowerCAmelCase    							=						min_resolution
  __lowerCAmelCase    							=						max_resolution
  __lowerCAmelCase    							=						do_resize
  __lowerCAmelCase    							=						size
  __lowerCAmelCase    							=						do_center_crop
  __lowerCAmelCase    							=						crop_size
  __lowerCAmelCase    							=						do_normalize
  __lowerCAmelCase    							=						image_mean
  __lowerCAmelCase    							=						image_std
  __lowerCAmelCase    							=						do_convert_rgb
 def a (				self       :     Optional[Any]							)  ->   Optional[int]:
  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 a (				self       :     str				,	SCREAMING_SNAKE_CASE__       :     Dict=False				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=False				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=False							)  ->   Optional[int]:
  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

  if torchify:
   __lowerCAmelCase    							=						[torch.from_numpy(SCREAMING_SNAKE_CASE__							) for x in image_inputs]

  return image_inputs







@require_torch
@require_vision
class     _lowercase					(    UpperCAmelCase__  ,							unittest.TestCase      ):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE					:	str				     = CLIPImageProcessor if is_vision_available() else None
 def a (				self       :     List[str]							)  ->   str:
  __lowerCAmelCase    							=						CLIPImageProcessingTester(self							)
 @property
 def a (				self       :     Optional[int]							)  ->   List[str]:
  return self.image_processor_tester.prepare_image_processor_dict()
 def a (				self       :     Optional[Any]							)  ->   Dict:
  __lowerCAmelCase    							=						self.image_processing_class(**self.image_processor_dict							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_resize"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""size"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_center_crop"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""center_crop"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_normalize"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""image_mean"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""image_std"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_convert_rgb"""							)							)
 def a (				self       :     int							)  ->   List[Any]:
  __lowerCAmelCase    							=						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}							)

  __lowerCAmelCase    							=						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 a (				self       :     List[Any]							)  ->   List[str]:
  pass
 def a (				self       :     List[str]							)  ->   Optional[Any]:
  # Initialize image_processing
  __lowerCAmelCase    							=						self.image_processing_class(**self.image_processor_dict							)
  # create random PIL images
  __lowerCAmelCase    							=						self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__							)
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE__				,	Image.Image							)

  # Test not batched input
  __lowerCAmelCase    							=						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    							=						image_processing(SCREAMING_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 a (				self       :     Dict							)  ->   Any:
  # Initialize image_processing
  __lowerCAmelCase    							=						self.image_processing_class(**self.image_processor_dict							)
  # create random numpy tensors
  __lowerCAmelCase    							=						self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__				,	numpify=SCREAMING_SNAKE_CASE__							)
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE__				,	np.ndarray							)

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

  # Test not batched input
  __lowerCAmelCase    							=						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    							=						image_processing(SCREAMING_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					(    UpperCAmelCase__  ,							unittest.TestCase      ):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE					:	Union[str, Any]				     = CLIPImageProcessor if is_vision_available() else None
 def a (				self       :     Tuple							)  ->   Union[str, Any]:
  __lowerCAmelCase    							=						CLIPImageProcessingTester(self				,	num_channels=4							)
  __lowerCAmelCase    							=						3
 @property
 def a (				self       :     Any							)  ->   Union[str, Any]:
  return self.image_processor_tester.prepare_image_processor_dict()
 def a (				self       :     Optional[int]							)  ->   Any:
  __lowerCAmelCase    							=						self.image_processing_class(**self.image_processor_dict							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_resize"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""size"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_center_crop"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""center_crop"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_normalize"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""image_mean"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""image_std"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,	"""do_convert_rgb"""							)							)
 def a (				self       :     str							)  ->   str:
  pass





 def a (				self       :     Optional[int]							)  ->   Any:
  # Initialize image_processing
  __lowerCAmelCase    							=						self.image_processing_class(**self.image_processor_dict							)
  # create random PIL images
  __lowerCAmelCase    							=						self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__							)
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE__				,	Image.Image							)

  # Test not batched input
  __lowerCAmelCase    							=						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    							=						image_processing(SCREAMING_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"""],
      )				,	)







'''simple docstring'''

import functools
from math import sqrt
from time import time







def 					UpperCamelCase_						( snake_case_	:	Dict						,		*snake_case_	:	Union[str, Any]						,		**snake_case_	:	List[str]				) ->					List[str]:






 '''simple docstring'''

 __lowerCAmelCase    							=						time()
 __lowerCAmelCase    							=						func(*snake_case_						,		**snake_case_				)
 __lowerCAmelCase    							=						time()
 if int(end - start				) > 0:
  print(f"""{func.__name__} runtime: {(end - start):0.4f} s"""				)
 else:
  print(f"""{func.__name__} runtime: {(end - start) * 10_00:0.4f} ms"""				)
 return output







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 if n == 0:
  return [0]
 __lowerCAmelCase    							=						[0, 1]
 for _ in range(n - 1				):
  fib.append(fib[-1] + fib[-2]				)
 return fib







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 def fib_recursive_term(snake_case_	:	int				) -> int:
  if i < 0:
   raise Exception("""n is negative"""				)
  if i < 2:
   return i
  return fib_recursive_term(i - 1				) + fib_recursive_term(i - 2				)

 if n < 0:
  raise Exception("""n is negative"""				)
 return [fib_recursive_term(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 @functools.cache
 def fib_recursive_term(snake_case_	:	int				) -> int:
  if i < 0:
   raise Exception("""n is negative"""				)
  if i < 2:
   return i
  return fib_recursive_term(i - 1				) + fib_recursive_term(i - 2				)

 if n < 0:
  raise Exception("""n is negative"""				)
 return [fib_recursive_term(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 # Cache must be outside recursuive function
 # other it will reset every time it calls itself.
 __lowerCAmelCase    							=						{0: 0, 1: 1, 2: 1}  # Prefilled cache

 def rec_fn_memoized(snake_case_	:	int				) -> int:
  if num in cache:
   return cache[num]

  __lowerCAmelCase    							=						rec_fn_memoized(num - 1				) + rec_fn_memoized(num - 2				)
  __lowerCAmelCase    							=						value
  return value

 return [rec_fn_memoized(snake_case_				) for i in range(n + 1				)]







def 					UpperCamelCase_						( snake_case_	:	int				) ->					list[int]:






 '''simple docstring'''

 if n < 0:
  raise Exception("""n is negative"""				)
 if n >= 14_75:
  raise Exception("""n is too large"""				)
 __lowerCAmelCase    							=						sqrt(5				)
 __lowerCAmelCase    							=						(1 + sqrt_a) / 2
 return [round(phi**i / sqrt_a				) for i in range(n + 1				)]


if __name__ == "__main__":
 _A      :				Optional[Any] 				=    30
 time_func(fib_iterative, num)
 time_func(fib_recursive, num)  # Around 3s runtime
 time_func(fib_recursive_cached, num)  # Around 0ms runtime
 time_func(fib_memoization, num)
 time_func(fib_binet, num)






