'''simple docstring'''




import json
import os
import tempfile

from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available


if is_torch_available():
  import numpy as np
  import torch

if is_vision_available():
  from PIL import Image

def      UpperCamelCase_							( _UpperCAmelCase  :				Optional[Any]      ,     _UpperCAmelCase  :				str=False      ,     _UpperCAmelCase  :				Union[str, Any]=False      ,     _UpperCAmelCase  :				Union[str, Any]=False   )   ->   Any:


       """simple docstring"""







       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       _UpperCAmelCase					:  Union[str, Any]											=   []
       for i in range(image_processor_tester.batch_size   ):
              if equal_resolution:
                     _UpperCAmelCase					:  Union[str, Any]											=   image_processor_tester.max_resolution
              else:
                     # To avoid getting image width/height 0
                     _UpperCAmelCase					:  Tuple											=   image_processor_tester.min_resolution
                     if getattr(_UpperCAmelCase      ,     "size_divisor"      ,     _UpperCAmelCase   ):
                            # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
                            _UpperCAmelCase					:  Optional[int]											=   max(image_processor_tester.size_divisor      ,     _UpperCAmelCase   )
                     _UpperCAmelCase				,  _UpperCAmelCase					:  Optional[Any]											=   np.random.choice(np.arange(_UpperCAmelCase      ,     image_processor_tester.max_resolution   )      ,     2   )
              image_inputs.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
              _UpperCAmelCase					:  Any											=   [Image.fromarray(np.moveaxis(_UpperCAmelCase      ,     0      ,     -1   )   ) for image in image_inputs]

       if torchify:
              _UpperCAmelCase					:  List[Any]											=   [torch.from_numpy(_UpperCAmelCase   ) for image in image_inputs]

       return image_inputs

def      UpperCamelCase_							( _UpperCAmelCase  :				Tuple      ,     _UpperCAmelCase  :				List[str]=10      ,     _UpperCAmelCase  :				Optional[Any]=10      ,     _UpperCAmelCase  :				Optional[int]=False      ,     _UpperCAmelCase  :				Optional[Any]=False   )   ->   List[Any]:


       """simple docstring"""







       _UpperCAmelCase					:  Any											=   []
       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
              _UpperCAmelCase					:  Optional[Any]											=   [Image.fromarray(np.moveaxis(_UpperCAmelCase      ,     0      ,     -1   )   ) for frame in video]

       if torchify:
              _UpperCAmelCase					:  int											=   [torch.from_numpy(_UpperCAmelCase   ) for frame in video]

       return video

def      UpperCamelCase_							( _UpperCAmelCase  :				int      ,     _UpperCAmelCase  :				Any=False      ,     _UpperCAmelCase  :				Union[str, Any]=False      ,     _UpperCAmelCase  :				Tuple=False   )   ->   Any:


       """simple docstring"""







       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       _UpperCAmelCase					:  Optional[int]											=   []
       for i in range(image_processor_tester.batch_size   ):
              if equal_resolution:
                     _UpperCAmelCase					:  Tuple											=   image_processor_tester.max_resolution
              else:
                     _UpperCAmelCase				,  _UpperCAmelCase					:  str											=   np.random.choice(
                         np.arange(image_processor_tester.min_resolution      ,     image_processor_tester.max_resolution   )      ,     2   )
                     _UpperCAmelCase					:  Optional[Any]											=   prepare_video(
                         image_processor_tester=_UpperCAmelCase      ,     width=_UpperCAmelCase      ,     height=_UpperCAmelCase      ,     numpify=_UpperCAmelCase      ,     torchify=_UpperCAmelCase      ,     )
              video_inputs.append(_UpperCAmelCase   )

       return video_inputs


class      lowerCamelCase_     :
      '''simple docstring'''

      __UpperCamelCase:  Any      =						None




      def 					_A     (	self						:    Union[str, Any]	):
             _UpperCAmelCase					:  Dict											=   self.image_processing_class(**self.image_processor_dict	)
             _UpperCAmelCase					:  List[Any]											=   json.loads(image_processor.to_json_string()	)
             for key, value in self.image_processor_dict.items():
                    self.assertEqual(obj[key]						, A	)




      def 					_A     (	self						:    int	):
             _UpperCAmelCase					:  Optional[Any]											=   self.image_processing_class(**self.image_processor_dict	)

             with tempfile.TemporaryDirectory() as tmpdirname:
                    _UpperCAmelCase					:  Dict											=   os.path.join(A						, "image_processor.json"	)
                    image_processor_first.to_json_file(A	)
                    _UpperCAmelCase					:  str											=   self.image_processing_class.from_json_file(A	)

             self.assertEqual(image_processor_second.to_dict()						, image_processor_first.to_dict()	)




      def 					_A     (	self						:    Optional[int]	):
             _UpperCAmelCase					:  Union[str, Any]											=   self.image_processing_class(**self.image_processor_dict	)

             with tempfile.TemporaryDirectory() as tmpdirname:
                    _UpperCAmelCase					:  List[str]											=   image_processor_first.save_pretrained(A	)[0]
                    check_json_file_has_correct_format(A	)
                    _UpperCAmelCase					:  str											=   self.image_processing_class.from_pretrained(A	)

             self.assertEqual(image_processor_second.to_dict()						, image_processor_first.to_dict()	)




      def 					_A     (	self						:    Optional[Any]	):
             _UpperCAmelCase					:  Optional[int]											=   self.image_processing_class()
             self.assertIsNotNone(A	)





      @require_torch
      @require_vision
      def 					_A     (	self						:    Any	):
             if self.test_cast_dtype is not None:
                    # Initialize image_processor
                    _UpperCAmelCase					:  str											=   self.image_processing_class(**self.image_processor_dict	)

                    # create random PyTorch tensors
                    _UpperCAmelCase					:  Optional[int]											=   prepare_image_inputs(self.image_processor_tester						, equal_resolution=A						, torchify=A	)

                    _UpperCAmelCase					:  Optional[Any]											=   image_processor(A						, return_tensors="pt"	)
                    # for layoutLM compatiblity
                    self.assertEqual(encoding.pixel_values.device						, torch.device("cpu"	)	)
                    self.assertEqual(encoding.pixel_values.dtype						, torch.floataa	)

                    _UpperCAmelCase					:  Dict											=   image_processor(A						, return_tensors="pt"	).to(torch.floataa	)
                    self.assertEqual(encoding.pixel_values.device						, torch.device("cpu"	)	)
                    self.assertEqual(encoding.pixel_values.dtype						, torch.floataa	)

                    _UpperCAmelCase					:  int											=   image_processor(A						, return_tensors="pt"	).to("cpu"						, torch.bfloataa	)
                    self.assertEqual(encoding.pixel_values.device						, torch.device("cpu"	)	)
                    self.assertEqual(encoding.pixel_values.dtype						, torch.bfloataa	)

                    with self.assertRaises(A	):
                           _UpperCAmelCase					:  Dict											=   image_processor(A						, return_tensors="pt"	).to(torch.bfloataa						, "cpu"	)

                    # Try with text + image feature
                    _UpperCAmelCase					:  Union[str, Any]											=   image_processor(A						, return_tensors="pt"	)
                    encoding.update({"input_ids": torch.LongTensor([[1, 2, 3], [4, 5, 6]]	)}	)
                    _UpperCAmelCase					:  List[str]											=   encoding.to(torch.floataa	)

                    self.assertEqual(encoding.pixel_values.device						, torch.device("cpu"	)	)
                    self.assertEqual(encoding.pixel_values.dtype						, torch.floataa	)
                    self.assertEqual(encoding.input_ids.dtype						, torch.long	)


'''simple docstring'''




from __future__ import annotations

from collections.abc import Generator

def      UpperCamelCase_							( _UpperCAmelCase  :				int   )   ->   Generator[int, None, None]:


       """simple docstring"""







       if not isinstance(_UpperCAmelCase      ,     _UpperCAmelCase   ) or n < 1:
              raise Exception("Sequence only defined for positive integers"   )

       yield n
       while n != 1:
              if n % 2 == 0:
                     n //= 2
              else:
                     _UpperCAmelCase					:  Optional[Any]											=   3 * n + 1
              yield n

def      UpperCamelCase_							( )   ->   Any:


       """simple docstring"""







       _UpperCAmelCase					:  str											=   43
       _UpperCAmelCase					:  Optional[int]											=   tuple(collatz_sequence(_UpperCAmelCase   )   )
       print(_UpperCAmelCase   )
       print(F"""Collatz sequence from {n} took {len(_UpperCAmelCase   )} steps."""   )


if __name__ == "__main__":
  main()

