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


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import VivitImageProcessor
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				def __init__(					self	:			Tuple				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			str=7				,     UpperCAmelCase_	:			str=3				,     UpperCAmelCase_	:			Optional[Any]=10				,     UpperCAmelCase_	:			Tuple=18				,     UpperCAmelCase_	:			int=30				,     UpperCAmelCase_	:			Dict=400				,     UpperCAmelCase_	:			List[str]=True				,     UpperCAmelCase_	:			Optional[Any]=None				,     UpperCAmelCase_	:			Optional[int]=True				,     UpperCAmelCase_	:			Optional[int]=[0.5, 0.5, 0.5]				,     UpperCAmelCase_	:			Any=[0.5, 0.5, 0.5]				,     UpperCAmelCase_	:			Tuple=None				,     )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			size if size is not None else {'shortest_edge': 18}
											_lowerCAmelCase  	=			crop_size if crop_size is not None else {'height': 18, 'width': 18}

											_lowerCAmelCase  	=			parent
											_lowerCAmelCase  	=			batch_size
											_lowerCAmelCase  	=			num_channels
											_lowerCAmelCase  	=			num_frames
											_lowerCAmelCase  	=			image_size
											_lowerCAmelCase  	=			min_resolution
											_lowerCAmelCase  	=			max_resolution
											_lowerCAmelCase  	=			do_resize
											_lowerCAmelCase  	=			size
											_lowerCAmelCase  	=			do_normalize
											_lowerCAmelCase  	=			image_mean
											_lowerCAmelCase  	=			image_std
											_lowerCAmelCase  	=			crop_size







				def 				__lowerCamelCase	(					self	:			Dict  )		->  List[Any]:







											"""simple docstring"""




											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        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Optional[int]							=					VivitImageProcessor if is_vision_available() else None


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Tuple:







											"""simple docstring"""




											_lowerCAmelCase  	=			VivitImageProcessingTester(self  )


				@property
				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Tuple:







											"""simple docstring"""




											return self.image_processor_tester.prepare_image_processor_dict()


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.image_processing_class(**self.image_processor_dict  )
											self.assertTrue(hasattr(UpperCAmelCase_				,     'image_mean'  )  )
											self.assertTrue(hasattr(UpperCAmelCase_				,     'image_std'  )  )
											self.assertTrue(hasattr(UpperCAmelCase_				,     'do_normalize'  )  )
											self.assertTrue(hasattr(UpperCAmelCase_				,     'do_resize'  )  )
											self.assertTrue(hasattr(UpperCAmelCase_				,     'do_center_crop'  )  )
											self.assertTrue(hasattr(UpperCAmelCase_				,     'size'  )  )


				def 				__lowerCamelCase	(					self	:			List[str]  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.image_processing_class.from_dict(self.image_processor_dict  )
											self.assertEqual(image_processor.size				,     {'shortest_edge': 18}  )
											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 				__lowerCamelCase	(					self	:			Tuple  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.image_processing_class(**self.image_processor_dict  )
											# create random PIL videos
											_lowerCAmelCase  	=			prepare_video_inputs(self.image_processor_tester				,     equal_resolution=UpperCAmelCase_  )
											for video in video_inputs:
																		self.assertIsInstance(UpperCAmelCase_				,     UpperCAmelCase_  )
																		self.assertIsInstance(video[0]				,     Image.Image  )

											# Test not batched input
											_lowerCAmelCase  	=			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
											_lowerCAmelCase  	=			image_processing(UpperCAmelCase_				,     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 				__lowerCamelCase	(					self	:			Optional[int]  )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.image_processing_class(**self.image_processor_dict  )
											# create random numpy tensors
											_lowerCAmelCase  	=			prepare_video_inputs(self.image_processor_tester				,     equal_resolution=UpperCAmelCase_				,     numpify=UpperCAmelCase_  )
											for video in video_inputs:
																		self.assertIsInstance(UpperCAmelCase_				,     UpperCAmelCase_  )
																		self.assertIsInstance(video[0]				,     np.ndarray  )

											# Test not batched input
											_lowerCAmelCase  	=			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
											_lowerCAmelCase  	=			image_processing(UpperCAmelCase_				,     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 				__lowerCamelCase	(					self	:			Tuple  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.image_processing_class(**self.image_processor_dict  )
											# create random PyTorch tensors
											_lowerCAmelCase  	=			prepare_video_inputs(self.image_processor_tester				,     equal_resolution=UpperCAmelCase_				,     torchify=UpperCAmelCase_  )
											for video in video_inputs:
																		self.assertIsInstance(UpperCAmelCase_				,     UpperCAmelCase_  )
																		self.assertIsInstance(video[0]				,     torch.Tensor  )

											# Test not batched input
											_lowerCAmelCase  	=			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
											_lowerCAmelCase  	=			image_processing(UpperCAmelCase_				,     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'],
											    )				,     )








"""simple docstring"""



def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							_lowerCAmelCase  	=			generate_pascal_triangle(SCREAMING_SNAKE_CASE						)
							for row_idx in range(SCREAMING_SNAKE_CASE						):
														# Print left spaces
														for _ in range(num_rows - row_idx - 1						):
																					print(end=' '						)
														# Print row values
														for col_idx in range(row_idx + 1						):
																					if col_idx != row_idx:
																												print(triangle[row_idx][col_idx]     ,  end=' '						)
																					else:
																												print(triangle[row_idx][col_idx]     ,  end=''						)
														print()






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							if not isinstance(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						):
														raise TypeError('The input value of \'num_rows\' should be \'int\''						)

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    'The input value of \'num_rows\' should be greater than or equal to 0'						)

							_lowerCAmelCase  	=			[]
							for current_row_idx in range(SCREAMING_SNAKE_CASE						):
														_lowerCAmelCase  	=			populate_current_row(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
														triangle.append(SCREAMING_SNAKE_CASE						)
							return triangle






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					list[list[int]]     ,  SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							_lowerCAmelCase  	=			[-1] * (current_row_idx + 1)
							# first and last elements of current row are equal to 1
							_lowerCAmelCase	,		_lowerCAmelCase  	=			1, 1
							for current_col_idx in range(1     ,  SCREAMING_SNAKE_CASE						):
														calculate_current_element(
														    SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
							return current_row






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					list[list[int]]     ,  SCREAMING_SNAKE_CASE:					list[int]     ,  SCREAMING_SNAKE_CASE:					int     ,  SCREAMING_SNAKE_CASE:					int     ,  ):






							"""simple docstring"""



							_lowerCAmelCase  	=			triangle[current_row_idx - 1][current_col_idx - 1]
							_lowerCAmelCase  	=			triangle[current_row_idx - 1][current_col_idx]
							_lowerCAmelCase  	=			above_to_left_elt + above_to_right_elt






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int						):






							"""simple docstring"""



							if not isinstance(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						):
														raise TypeError('The input value of \'num_rows\' should be \'int\''						)

							if num_rows == 0:
														return []
							elif num_rows < 0:
														raise ValueError(
														    'The input value of \'num_rows\' should be greater than or equal to 0'						)

							_lowerCAmelCase  	=			[[1]]

							for row_index in range(1     ,  SCREAMING_SNAKE_CASE						):
														_lowerCAmelCase  	=			[0] + result[-1] + [0]
														_lowerCAmelCase  	=			row_index + 1
														# Calculate the number of distinct elements in a row
														_lowerCAmelCase  	=			sum(divmod(SCREAMING_SNAKE_CASE     ,  2						)						)
														_lowerCAmelCase  	=			[
														    temp_row[i - 1] + temp_row[i] for i in range(1     ,  distinct_elements + 1						)
														]
														_lowerCAmelCase  	=			row_first_half[: (row_index + 1) // 2]
														row_second_half.reverse()
														_lowerCAmelCase  	=			row_first_half + row_second_half
														result.append(SCREAMING_SNAKE_CASE						)

							return result






def 	__snake_case  (    ):






							"""simple docstring"""



							from collections.abc import Callable
							from timeit import timeit

							def benchmark_a_function(SCREAMING_SNAKE_CASE:					Callable     ,  SCREAMING_SNAKE_CASE:					int						) -> None:
														_lowerCAmelCase  	=			f"""{func.__name__}({value})"""
														_lowerCAmelCase  	=			timeit(f"""__main__.{call}"""     ,  setup='import __main__'						)
														# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
														print(f"""{call:38} -- {timing:.4f} seconds"""						)

							for value in range(15						):  # (1, 7, 14):
														for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
																					benchmark_a_function(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						)
														print()


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	benchmark()






