





from __future__ import annotations



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )					->  int:


      '''simple docstring'''


      assert n > 1 and a > 0 and greatest_common_divisor(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ) == 1
      ((SCREAMING_SNAKE_CASE),   (SCREAMING_SNAKE_CASE),   (SCREAMING_SNAKE_CASE))						=		extended_gcd(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )  # Implemented below
      SCREAMING_SNAKE_CASE						=		(b * s) % n
      return x



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )					->  int:


      '''simple docstring'''


      ((SCREAMING_SNAKE_CASE),   (SCREAMING_SNAKE_CASE))						=		extended_euclid(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )  # Implemented below
      if b < 0:
            SCREAMING_SNAKE_CASE						=		(b % n + n) % n
      return b



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )					->  int:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		invert_modulo(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )
      SCREAMING_SNAKE_CASE						=		(b * s) % n
      return x



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )					->  tuple[int, int, int]:


      '''simple docstring'''


      assert a >= 0 and b >= 0

      if b == 0:
            SCREAMING_SNAKE_CASE,   SCREAMING_SNAKE_CASE,   SCREAMING_SNAKE_CASE						=		a, 1, 0
      else:
            ((SCREAMING_SNAKE_CASE),   (SCREAMING_SNAKE_CASE),   (SCREAMING_SNAKE_CASE))						=		extended_gcd(_SCREAMING_SNAKE_CASE					,		a % b      )
            SCREAMING_SNAKE_CASE						=		q
            SCREAMING_SNAKE_CASE						=		p - q * (a // b)

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

      return (d, x, y)



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )					->  tuple[int, int]:


      '''simple docstring'''


      if b == 0:
            return (1, 0)
      ((SCREAMING_SNAKE_CASE),   (SCREAMING_SNAKE_CASE))						=		extended_euclid(_SCREAMING_SNAKE_CASE					,		a % b      )
      SCREAMING_SNAKE_CASE						=		a // b
      return (y, x - k * y)



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )					->  int:


      '''simple docstring'''


      if a < b:
            SCREAMING_SNAKE_CASE,   SCREAMING_SNAKE_CASE						=		b, a

      while a % b != 0:
            SCREAMING_SNAKE_CASE,   SCREAMING_SNAKE_CASE						=		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)









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   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE=10					,		_SCREAMING_SNAKE_CASE=10					,		_SCREAMING_SNAKE_CASE=False					,		_SCREAMING_SNAKE_CASE=False      )					->  Optional[Any]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE						=		[]
      for i in range(image_processor_tester.num_frames      ):
            video.append(np.random.randint(2_55					,		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(_SCREAMING_SNAKE_CASE					,		0					,		-1      )      ) for frame in video]

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

      return video



def   __lowercase  ( _SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE=False					,		_SCREAMING_SNAKE_CASE=False					,		_SCREAMING_SNAKE_CASE=False      )					->  Tuple:


      '''simple docstring'''


      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=_SCREAMING_SNAKE_CASE					,		width=_SCREAMING_SNAKE_CASE					,		height=_SCREAMING_SNAKE_CASE					,		numpify=_SCREAMING_SNAKE_CASE					,		torchify=_SCREAMING_SNAKE_CASE					,		)
            video_inputs.append(_SCREAMING_SNAKE_CASE      )

      return video_inputs

class 						UpperCamelCase__					( unittest.TestCase  ):



     '''simple docstring'''

     def __init__(				self		:       Union[str, Any]		,lowerCamelCase__		:       Tuple		,lowerCamelCase__		:       Any=7		,lowerCamelCase__		:       str=3		,lowerCamelCase__		:       Union[str, Any]=4		,lowerCamelCase__		:       Union[str, Any]=18		,lowerCamelCase__		:       Any=30		,lowerCamelCase__		:       Union[str, Any]=400		,lowerCamelCase__		:       Any=True		,lowerCamelCase__		:       Any=None		,lowerCamelCase__		:       Tuple=True		,lowerCamelCase__		:       int=[0.5, 0.5, 0.5]		,lowerCamelCase__		:       Any=[0.5, 0.5, 0.5]		,lowerCamelCase__		:       Dict=True		,lowerCamelCase__		:       Tuple=None		,)	->						Tuple:




           '''simple docstring'''


           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 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						int:




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

@require_torch
@require_vision
class 						UpperCamelCase__					( lowerCAmelCase_				, unittest.TestCase  ):



     '''simple docstring'''

     __snake_case       :      int						      =     TvltImageProcessor if is_vision_available() else None






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Dict			)	->						Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		TvltImageProcessorTester(self			)






     @property
     def 							SCREAMING_SNAKE_CASE__    (				self		:       Optional[int]			)	->						Dict:




           '''simple docstring'''


           return self.image_processor_tester.prepare_image_processor_dict()






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Any			)	->						List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE						=		self.image_processing_class(**self.image_processor_dict			)
           self.assertTrue(hasattr(lowerCamelCase__		,"""image_mean"""			)			)
           self.assertTrue(hasattr(lowerCamelCase__		,"""image_std"""			)			)
           self.assertTrue(hasattr(lowerCamelCase__		,"""do_normalize"""			)			)
           self.assertTrue(hasattr(lowerCamelCase__		,"""do_resize"""			)			)
           self.assertTrue(hasattr(lowerCamelCase__		,"""do_center_crop"""			)			)
           self.assertTrue(hasattr(lowerCamelCase__		,"""size"""			)			)






     def 							SCREAMING_SNAKE_CASE__    (				self		:       List[Any]			)	->						List[str]:




           '''simple docstring'''


           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=lowerCamelCase__			)
           for video in video_inputs:
                 self.assertIsInstance(lowerCamelCase__		,lowerCamelCase__			)
                 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(lowerCamelCase__		,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 							SCREAMING_SNAKE_CASE__    (				self		:       int			)	->						int:




           '''simple docstring'''


           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=lowerCamelCase__		,numpify=lowerCamelCase__			)
           for video in video_inputs:
                 self.assertIsInstance(lowerCamelCase__		,lowerCamelCase__			)
                 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(lowerCamelCase__		,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 							SCREAMING_SNAKE_CASE__    (				self		:       str			)	->						Optional[Any]:




           '''simple docstring'''


           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=lowerCamelCase__		,torchify=lowerCamelCase__			)
           for video in video_inputs:
                 self.assertIsInstance(lowerCamelCase__		,lowerCamelCase__			)
                 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(lowerCamelCase__		,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"""],
               )		,)


