
'''simple docstring'''







import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
					import torch

if is_tf_available():
					import tensorflow as tf


@require_vision
@require_torchvision
class 						__UpperCAmelCase      (					unittest.TestCase						):


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		tempfile.mkdtemp()
								_snake_case   						=		SamImageProcessor()
								_snake_case   						=		SamProcessor(lowerCAmelCase_  )
								processor.save_pretrained(self.tmpdirname  )


							def  lowerCamelCase				(       self    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								return AutoProcessor.from_pretrained(self.tmpdirname    ,   **lowerCAmelCase_  ).image_processor


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								shutil.rmtree(self.tmpdirname  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		[np.random.randint(2_55    ,   size=(3, 30, 4_00)    ,   dtype=np.uinta  )]

								_snake_case   						=		[Image.fromarray(np.moveaxis(lowerCAmelCase_    ,   0    ,   -1  )  ) for x in image_inputs]

								return image_inputs


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		SamProcessor(image_processor=self.get_image_processor()  )
								processor.save_pretrained(self.tmpdirname  )

								_snake_case   						=		self.get_image_processor(do_normalize=lowerCAmelCase_    ,   padding_value=1.0  )

								_snake_case   						=		SamProcessor.from_pretrained(self.tmpdirname    ,   do_normalize=lowerCAmelCase_    ,   padding_value=1.0  )

								self.assertEqual(processor.image_processor.to_json_string()    ,   image_processor_add_kwargs.to_json_string()  )
								self.assertIsInstance(processor.image_processor    ,   lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_image_processor()

								_snake_case   						=		SamProcessor(image_processor=lowerCAmelCase_  )

								_snake_case   						=		self.prepare_image_inputs()

								_snake_case   						=		image_processor(lowerCAmelCase_    ,   return_tensors='np'  )
								_snake_case   						=		processor(images=lowerCAmelCase_    ,   return_tensors='np'  )

								input_feat_extract.pop('original_sizes'  )  # pop original_sizes as it is popped in the processor
								input_feat_extract.pop('reshaped_input_sizes'  )  # pop original_sizes as it is popped in the processor

								for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()    ,   input_processor[key].sum()    ,   delta=1E-2  )




							@require_torch
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_image_processor()

								_snake_case   						=		SamProcessor(image_processor=lowerCAmelCase_  )
								_snake_case   						=		[torch.ones((1, 3, 5, 5)  )]

								_snake_case   						=		[[17_64, 26_46]]

								_snake_case   						=		[[6_83, 10_24]]
								_snake_case   						=		processor.post_process_masks(lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  )
								self.assertEqual(masks[0].shape    ,   (1, 3, 17_64, 26_46)  )

								_snake_case   						=		processor.post_process_masks(
								    lowerCAmelCase_    ,   torch.tensor(lowerCAmelCase_  )    ,   torch.tensor(lowerCAmelCase_  )  )
								self.assertEqual(masks[0].shape    ,   (1, 3, 17_64, 26_46)  )

								# should also work with np
								_snake_case   						=		[np.ones((1, 3, 5, 5)  )]
								_snake_case   						=		processor.post_process_masks(lowerCAmelCase_    ,   np.array(lowerCAmelCase_  )    ,   np.array(lowerCAmelCase_  )  )

								self.assertEqual(masks[0].shape    ,   (1, 3, 17_64, 26_46)  )

								_snake_case   						=		[[1, 0], [0, 1]]
								with self.assertRaises(lowerCAmelCase_  ):
									_snake_case   						=		processor.post_process_masks(lowerCAmelCase_    ,   np.array(lowerCAmelCase_  )    ,   np.array(lowerCAmelCase_  )  )


@require_vision
@require_tf
class 						__UpperCAmelCase      (					unittest.TestCase						):


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		tempfile.mkdtemp()
								_snake_case   						=		SamImageProcessor()
								_snake_case   						=		SamProcessor(lowerCAmelCase_  )
								processor.save_pretrained(self.tmpdirname  )


							def  lowerCamelCase				(       self    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								return AutoProcessor.from_pretrained(self.tmpdirname    ,   **lowerCAmelCase_  ).image_processor


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								shutil.rmtree(self.tmpdirname  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		[np.random.randint(2_55    ,   size=(3, 30, 4_00)    ,   dtype=np.uinta  )]

								_snake_case   						=		[Image.fromarray(np.moveaxis(lowerCAmelCase_    ,   0    ,   -1  )  ) for x in image_inputs]

								return image_inputs


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		SamProcessor(image_processor=self.get_image_processor()  )
								processor.save_pretrained(self.tmpdirname  )

								_snake_case   						=		self.get_image_processor(do_normalize=lowerCAmelCase_    ,   padding_value=1.0  )

								_snake_case   						=		SamProcessor.from_pretrained(self.tmpdirname    ,   do_normalize=lowerCAmelCase_    ,   padding_value=1.0  )

								self.assertEqual(processor.image_processor.to_json_string()    ,   image_processor_add_kwargs.to_json_string()  )
								self.assertIsInstance(processor.image_processor    ,   lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_image_processor()

								_snake_case   						=		SamProcessor(image_processor=lowerCAmelCase_  )

								_snake_case   						=		self.prepare_image_inputs()

								_snake_case   						=		image_processor(lowerCAmelCase_    ,   return_tensors='np'  )
								_snake_case   						=		processor(images=lowerCAmelCase_    ,   return_tensors='np'  )

								input_feat_extract.pop('original_sizes'  )  # pop original_sizes as it is popped in the processor
								input_feat_extract.pop('reshaped_input_sizes'  )  # pop reshaped_input_sizes as it is popped in the processor

								for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()    ,   input_processor[key].sum()    ,   delta=1E-2  )




							@require_tf
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_image_processor()

								_snake_case   						=		SamProcessor(image_processor=lowerCAmelCase_  )
								_snake_case   						=		[tf.ones((1, 3, 5, 5)  )]

								_snake_case   						=		[[17_64, 26_46]]

								_snake_case   						=		[[6_83, 10_24]]
								_snake_case   						=		processor.post_process_masks(lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   return_tensors='tf'  )
								self.assertEqual(masks[0].shape    ,   (1, 3, 17_64, 26_46)  )

								_snake_case   						=		processor.post_process_masks(
								    lowerCAmelCase_    ,   tf.convert_to_tensor(lowerCAmelCase_  )    ,   tf.convert_to_tensor(lowerCAmelCase_  )    ,   return_tensors='tf'    ,   )
								self.assertEqual(masks[0].shape    ,   (1, 3, 17_64, 26_46)  )

								# should also work with np
								_snake_case   						=		[np.ones((1, 3, 5, 5)  )]
								_snake_case   						=		processor.post_process_masks(
								    lowerCAmelCase_    ,   np.array(lowerCAmelCase_  )    ,   np.array(lowerCAmelCase_  )    ,   return_tensors='tf'  )

								self.assertEqual(masks[0].shape    ,   (1, 3, 17_64, 26_46)  )

								_snake_case   						=		[[1, 0], [0, 1]]
								with self.assertRaises(tf.errors.InvalidArgumentError  ):
									_snake_case   						=		processor.post_process_masks(
									    lowerCAmelCase_    ,   np.array(lowerCAmelCase_  )    ,   np.array(lowerCAmelCase_  )    ,   return_tensors='tf'  )


@require_vision
@require_torchvision
class 						__UpperCAmelCase      (					unittest.TestCase						):


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		tempfile.mkdtemp()
								_snake_case   						=		SamImageProcessor()
								_snake_case   						=		SamProcessor(lowerCAmelCase_  )
								processor.save_pretrained(self.tmpdirname  )


							def  lowerCamelCase				(       self    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								return AutoProcessor.from_pretrained(self.tmpdirname    ,   **lowerCAmelCase_  ).image_processor


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								shutil.rmtree(self.tmpdirname  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		[np.random.randint(2_55    ,   size=(3, 30, 4_00)    ,   dtype=np.uinta  )]

								_snake_case   						=		[Image.fromarray(np.moveaxis(lowerCAmelCase_    ,   0    ,   -1  )  ) for x in image_inputs]

								return image_inputs


							@is_pt_tf_cross_test
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_image_processor()

								_snake_case   						=		SamProcessor(image_processor=lowerCAmelCase_  )
								_snake_case   						=		np.random.randint(0    ,   2    ,   size=(1, 3, 5, 5)  ).astype(np.floataa  )
								_snake_case   						=		[tf.convert_to_tensor(lowerCAmelCase_  )]
								_snake_case   						=		[torch.tensor(lowerCAmelCase_  )]

								_snake_case   						=		[[17_64, 26_46]]

								_snake_case   						=		[[6_83, 10_24]]
								_snake_case   						=		processor.post_process_masks(
								    lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   return_tensors='tf'  )
								_snake_case   						=		processor.post_process_masks(
								    lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   return_tensors='pt'  )

								self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()  )  )




							@is_pt_tf_cross_test
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.get_image_processor()

								_snake_case   						=		SamProcessor(image_processor=lowerCAmelCase_  )

								_snake_case   						=		self.prepare_image_inputs()

								_snake_case   						=		image_processor(lowerCAmelCase_    ,   return_tensors='pt'  )['pixel_values'].numpy()
								_snake_case   						=		processor(images=lowerCAmelCase_    ,   return_tensors='pt'  )['pixel_values'].numpy()

								_snake_case   						=		image_processor(lowerCAmelCase_    ,   return_tensors='tf'  )['pixel_values'].numpy()
								_snake_case   						=		processor(images=lowerCAmelCase_    ,   return_tensors='tf'  )['pixel_values'].numpy()

								self.assertTrue(np.allclose(lowerCAmelCase_    ,   lowerCAmelCase_  )  )
								self.assertTrue(np.allclose(lowerCAmelCase_    ,   lowerCAmelCase_  )  )
								self.assertTrue(np.allclose(lowerCAmelCase_    ,   lowerCAmelCase_  )  )





def 		lowercase_(  SCREAMING_SNAKE_CASE_	):


  '''simple docstring'''

  for i in range(len(SCREAMING_SNAKE_CASE_	) - 1      ,						0      ,						-1	):
    lowerCamelCase :					Tuple    =				False

    for j in range(SCREAMING_SNAKE_CASE_      ,						0      ,						-1	):
      if unsorted[j] < unsorted[j - 1]:
        lowerCamelCase    ,	lowerCamelCase :					int    =				unsorted[j - 1], unsorted[j]
        lowerCamelCase :					Optional[int]    =				True

    for j in range(SCREAMING_SNAKE_CASE_	):
      if unsorted[j] > unsorted[j + 1]:
        lowerCamelCase    ,	lowerCamelCase :					Union[str, Any]    =				unsorted[j + 1], unsorted[j]
        lowerCamelCase :					Optional[Any]    =				True

    if not swapped:
      break
  return unsorted


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   _snake_case             =     input('''Enter numbers separated by a comma:\n''').strip()
   _snake_case             =     [int(item) for item in user_input.split(''',''')]
   print(f'''{cocktail_shaker_sort(unsorted) = }''')



