





'''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 TvltImageProcessor





def lowerCamelCase		(  __lowerCamelCase				:					str      , __lowerCamelCase				:					str=10      , __lowerCamelCase				:					Optional[Any]=10      , __lowerCamelCase				:					Tuple=False      , __lowerCamelCase				:					Dict=False					)		->Union[str, Any]:
    _SCREAMING_SNAKE_CASE             =    []
    for i in range(image_processor_tester.num_frames					):
        video.append(np.random.randint(255      , size=(image_processor_tester.num_channels, width, height)      , dtype=np.uinta					)					)

    if not numpify and not torchify:
        # PIL expects the channel dimension as last dimension
        _SCREAMING_SNAKE_CASE             =    [Image.fromarray(np.moveaxis(__lowerCamelCase      , 0      , -1					)					) for frame in video]

    if torchify:
        _SCREAMING_SNAKE_CASE             =    [torch.from_numpy(__lowerCamelCase					) for frame in video]

    return video





def lowerCamelCase		(  __lowerCamelCase				:					int      , __lowerCamelCase				:					List[Any]=False      , __lowerCamelCase				:					Tuple=False      , __lowerCamelCase				:					str=False					)		->List[Any]:
    assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

    _SCREAMING_SNAKE_CASE             =    []
    for i in range(image_processor_tester.batch_size					):
        if equal_resolution:
            _SCREAMING_SNAKE_CASE             =    _SCREAMING_SNAKE_CASE             =    image_processor_tester.max_resolution
        else:
            _SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE             =    np.random.choice(
                np.arange(image_processor_tester.min_resolution      , image_processor_tester.max_resolution					)      , 2					)
            _SCREAMING_SNAKE_CASE             =    prepare_video(
                image_processor_tester=__lowerCamelCase      , width=__lowerCamelCase      , height=__lowerCamelCase      , numpify=__lowerCamelCase      , torchify=__lowerCamelCase      , )
        video_inputs.append(__lowerCamelCase					)

    return video_inputs


class       a_  (       unittest.TestCase ):

    '''simple docstring'''


    def __init__(	self				,	A				,	A=7				,	A=3				,	A=4				,	A=18				,	A=30				,	A=400				,	A=True				,	A=None				,	A=True				,	A=[0.5, 0.5, 0.5]				,	A=[0.5, 0.5, 0.5]				,	A=True				,	A=None				,	)	->     List[str]:
        _SCREAMING_SNAKE_CASE             =    size if size is not None else {"""shortest_edge""": 18}
        _SCREAMING_SNAKE_CASE             =    crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

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





    def     snake_case_(	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,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
        }




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

    '''simple docstring'''
    UpperCamelCase									=				TvltImageProcessor if is_vision_available() else None


    def     snake_case_(	self )	->     Any:
        _SCREAMING_SNAKE_CASE             =    TvltImageProcessorTester(self )


    @property
    def     snake_case_(	self )	->     Any:
        return self.image_processor_tester.prepare_image_processor_dict()


    def     snake_case_(	self )	->     Tuple:
        _SCREAMING_SNAKE_CASE             =    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     snake_case_(	self )	->     List[str]:
        # Initialize image_processor
        _SCREAMING_SNAKE_CASE             =    self.image_processing_class(**self.image_processor_dict )
        # create random PIL videos
        _SCREAMING_SNAKE_CASE             =    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
        _SCREAMING_SNAKE_CASE             =    image_processor(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
        _SCREAMING_SNAKE_CASE             =    image_processor(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     snake_case_(	self )	->     Optional[int]:
        # Initialize image_processor
        _SCREAMING_SNAKE_CASE             =    self.image_processing_class(**self.image_processor_dict )
        # create random numpy tensors
        _SCREAMING_SNAKE_CASE             =    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
        _SCREAMING_SNAKE_CASE             =    image_processor(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
        _SCREAMING_SNAKE_CASE             =    image_processor(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     snake_case_(	self )	->     int:
        # Initialize image_processor
        _SCREAMING_SNAKE_CASE             =    self.image_processing_class(**self.image_processor_dict )
        # create random PyTorch tensors
        _SCREAMING_SNAKE_CASE             =    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
        _SCREAMING_SNAKE_CASE             =    image_processor(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
        _SCREAMING_SNAKE_CASE             =    image_processor(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"""],
            )				,	)










'''simple docstring'''

import math
from timeit import timeit





def lowerCamelCase		(  __lowerCamelCase				:					int					)		->int:
    _SCREAMING_SNAKE_CASE             =    0
    _SCREAMING_SNAKE_CASE             =    abs(__lowerCamelCase					)
    while True:
        _SCREAMING_SNAKE_CASE             =    n // 10
        digits += 1
        if n == 0:
            break
    return digits





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





def lowerCamelCase		(  __lowerCamelCase				:					int					)		->int:
    return len(str(abs(__lowerCamelCase					)					)					)





def lowerCamelCase		(  )		->None:
    from collections.abc import Callable

    def benchmark_a_function(__lowerCamelCase				:					Callable      , __lowerCamelCase				:					int					) -> None:
        _SCREAMING_SNAKE_CASE             =    F'{func.__name__}({value})'
        _SCREAMING_SNAKE_CASE             =    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()



