

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, prepare_video_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import VideoMAEImageProcessor





class    __UpperCAmelCase	(			unittest.TestCase		):



   def __init__(				self				:	str,						__A				:	Optional[Any],						__A				:	Union[str, Any]=7,						__A				:	Dict=3,						__A				:	Tuple=1_0,						__A				:	Dict=1_8,						__A				:	Optional[Any]=3_0,						__A				:	Dict=4_0_0,						__A				:	Union[str, Any]=True,						__A				:	str=None,						__A				:	Tuple=True,						__A				:	str=[0.5, 0.5, 0.5],						__A				:	Tuple=[0.5, 0.5, 0.5],						__A				:	List[Any]=None,						):
     UpperCAmelCase			:				Tuple										=					size if size is not None else {'''shortest_edge''': 1_8}
     UpperCAmelCase			:				Dict										=					crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

     UpperCAmelCase			:				List[Any]										=					parent
     UpperCAmelCase			:				List[Any]										=					batch_size
     UpperCAmelCase			:				Tuple										=					num_channels
     UpperCAmelCase			:				Tuple										=					num_frames
     UpperCAmelCase			:				Optional[Any]										=					image_size
     UpperCAmelCase			:				Optional[int]										=					min_resolution
     UpperCAmelCase			:				int										=					max_resolution
     UpperCAmelCase			:				Any										=					do_resize
     UpperCAmelCase			:				Union[str, Any]										=					size
     UpperCAmelCase			:				List[Any]										=					do_normalize
     UpperCAmelCase			:				List[Any]										=					image_mean
     UpperCAmelCase			:				Optional[int]										=					image_std
     UpperCAmelCase			:				Tuple										=					crop_size





   def 			__magic_name__       (				self				:	Tuple		):
     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "do_resize": self.do_resize,
         "size": self.size,
         "crop_size": self.crop_size,
     }






@require_torch
@require_vision
class    __UpperCAmelCase	(			lowerCamelCase__     ,    unittest.TestCase		):
   UpperCamelCase  	=	VideoMAEImageProcessor if is_vision_available() else None



   def 			__magic_name__       (				self				:	List[Any]		):
     UpperCAmelCase			:				str										=					VideoMAEImageProcessingTester(self		)



   @property
   def 			__magic_name__       (				self				:	Optional[int]		):
     return self.image_processor_tester.prepare_image_processor_dict()



   def 			__magic_name__       (				self				:	List[Any]		):
     UpperCAmelCase			:				int										=					self.image_processing_class(**self.image_processor_dict		)
     self.assertTrue(hasattr(__A,						'''image_mean'''		)		)
     self.assertTrue(hasattr(__A,						'''image_std'''		)		)
     self.assertTrue(hasattr(__A,						'''do_normalize'''		)		)
     self.assertTrue(hasattr(__A,						'''do_resize'''		)		)
     self.assertTrue(hasattr(__A,						'''do_center_crop'''		)		)
     self.assertTrue(hasattr(__A,						'''size'''		)		)



   def 			__magic_name__       (				self				:	str		):
     UpperCAmelCase			:				int										=					self.image_processing_class.from_dict(self.image_processor_dict		)
     self.assertEqual(image_processor.size,						{'''shortest_edge''': 1_8}		)
     self.assertEqual(image_processor.crop_size,						{'''height''': 1_8, '''width''': 1_8}		)

     UpperCAmelCase			:				Optional[int]										=					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 			__magic_name__       (				self				:	int		):
     pass



   def 			__magic_name__       (				self				:	str		):
     # Initialize image_processing
     UpperCAmelCase			:				Optional[Any]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random PIL videos
     UpperCAmelCase			:				Any										=					prepare_video_inputs(self.image_processor_tester,						equal_resolution=__A		)
     for video in video_inputs:
       self.assertIsInstance(__A,						__A		)
       self.assertIsInstance(video[0],						Image.Image		)

     # Test not batched input
     UpperCAmelCase			:				Tuple										=					image_processing(video_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_videos.shape,						(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),						)

     # Test batched
     UpperCAmelCase			:				Dict										=					image_processing(__A,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_videos.shape,						(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),						)



   def 			__magic_name__       (				self				:	str		):
     # Initialize image_processing
     UpperCAmelCase			:				List[Any]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random numpy tensors
     UpperCAmelCase			:				Tuple										=					prepare_video_inputs(self.image_processor_tester,						equal_resolution=__A,						numpify=__A		)
     for video in video_inputs:
       self.assertIsInstance(__A,						__A		)
       self.assertIsInstance(video[0],						np.ndarray		)

     # Test not batched input
     UpperCAmelCase			:				Dict										=					image_processing(video_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_videos.shape,						(
             1,
             self.image_processor_tester.num_frames,
             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(__A,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_videos.shape,						(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),						)





   def 			__magic_name__       (				self				:	str		):
     # Initialize image_processing
     UpperCAmelCase			:				List[Any]										=					self.image_processing_class(**self.image_processor_dict		)
     # create random PyTorch tensors
     UpperCAmelCase			:				Optional[int]										=					prepare_video_inputs(self.image_processor_tester,						equal_resolution=__A,						torchify=__A		)
     for video in video_inputs:
       self.assertIsInstance(__A,						__A		)
       self.assertIsInstance(video[0],						torch.Tensor		)

     # Test not batched input
     UpperCAmelCase			:				Any										=					image_processing(video_inputs[0],						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_videos.shape,						(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),						)

     # Test batched
     UpperCAmelCase			:				int										=					image_processing(__A,						return_tensors='''pt'''		).pixel_values
     self.assertEqual(
         encoded_videos.shape,						(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),						)








import timeit





class    __UpperCAmelCase	:



   def __init__(				self				:	Dict,						__A				:	Union[str, Any]		):
     if isinstance(__A,						__A		):  # Initializes a matrix identical to the one provided.
       UpperCAmelCase			:				str										=					arg
       UpperCAmelCase			:				Optional[Any]										=					len(__A		)
     else:  # Initializes a square matrix of the given size and set values to zero.
       UpperCAmelCase			:				List[str]										=					arg
       UpperCAmelCase			:				List[Any]										=					[[0 for _ in range(self.n		)] for _ in range(self.n		)]





   def __mul__(				self				:	Dict,						__A				:	List[Any]		):
     UpperCAmelCase			:				Dict										=					Matrix(self.n		)
     for i in range(self.n		):
       for j in range(self.n		):
         for k in range(self.n		):
           matrix.t[i][j] += self.t[i][k] * b.t[k][j]
     return matrix






def      a__	(						UpperCAmelCase				:     List[str]							,					UpperCAmelCase				:     List[Any]     )    ->		str:
  UpperCAmelCase			:				Tuple										=					Matrix([[1, 0], [0, 1]]     )
  while b > 0:
    if b & 1:
      matrix *= a
    a *= a
    b >>= 1
  return matrix

def      a__	(						UpperCAmelCase				:     Any							,					UpperCAmelCase				:     Dict							,					UpperCAmelCase				:     Dict     )    ->		str:
  # Trivial Cases
  if n == 1:
    return fa
  elif n == 2:
    return fa
  UpperCAmelCase			:				List[str]										=					Matrix([[1, 1], [1, 0]]     )
  UpperCAmelCase			:				Union[str, Any]										=					modular_exponentiation(UpperCAmelCase							,					n - 2     )
  return fa * matrix.t[0][0] + fa * matrix.t[0][1]

def      a__	(						UpperCAmelCase				:     Any							,					UpperCAmelCase				:     int							,					UpperCAmelCase				:     List[Any]     )    ->		Optional[Any]:
  # Trivial Cases
  if n == 1:
    return fa
  elif n == 2:
    return fa

  UpperCAmelCase			:				str										=					fa
  UpperCAmelCase			:				List[str]										=					fa
  n -= 2

  while n > 0:
    UpperCAmelCase					,		UpperCAmelCase			:				str										=					fn_a + fn_a, fn_a
    n -= 1

  return fn_a

def      a__	(						)    ->		Any:
  UpperCAmelCase			:				int										=					'''
from random import randint
from __main__ import fibonacci_with_matrix_exponentiation
'''
  UpperCAmelCase			:				Dict										=					'''fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)'''
  UpperCAmelCase			:				Tuple										=					timeit.timeit(setup=UpperCAmelCase							,					stmt=UpperCAmelCase							,					number=100     )
  print('''With matrix exponentiation the average execution time is '''							,					exec_time / 100     )
  return exec_time

def      a__	(						)    ->		int:
  UpperCAmelCase			:				str										=					'''
from random import randint
from __main__ import simple_fibonacci
'''
  UpperCAmelCase			:				List[str]										=					'''simple_fibonacci(randint(1,70000), 1, 1)'''
  UpperCAmelCase			:				List[str]										=					timeit.timeit(setup=UpperCAmelCase							,					stmt=UpperCAmelCase							,					number=100     )
  print(
      '''Without matrix exponentiation the average execution time is '''							,					exec_time / 100     )
  return exec_time

def      a__	(						)    ->		List[str]:
  matrix_exponentiation_time()
  simple_fibonacci_time()


if __name__ == "__main__":
     main()





